Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,96 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from utils.models import predict_umkm
|
4 |
import matplotlib.pyplot as plt
|
|
|
|
|
5 |
|
6 |
-
#
|
|
|
7 |
st.title("🚀 AI Supply Chain UMKM")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Upload data
|
10 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
11 |
if uploaded_file:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
|
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
from utils.models import predict_umkm
|
5 |
+
from utils.preprocessing import clean_data
|
6 |
|
7 |
+
# Konfigurasi tampilan
|
8 |
+
st.set_page_config(page_title="AI Supply Chain UMKM", layout="wide")
|
9 |
st.title("🚀 AI Supply Chain UMKM")
|
10 |
|
11 |
+
# Sidebar untuk parameter
|
12 |
+
with st.sidebar:
|
13 |
+
st.header("Pengaturan Model")
|
14 |
+
prediction_days = st.slider("Jumlah Hari Prediksi", 1, 30, 7)
|
15 |
+
confidence_level = st.slider("Tingkat Kepercayaan (%)", 70, 95, 85)
|
16 |
+
|
17 |
# Upload data
|
18 |
+
uploaded_file = st.file_uploader(
|
19 |
+
"Upload Data CSV (Format: tanggal, demand, supply)",
|
20 |
+
type="csv"
|
21 |
+
)
|
22 |
+
|
23 |
if uploaded_file:
|
24 |
+
try:
|
25 |
+
# Preprocessing data
|
26 |
+
df = clean_data(uploaded_file)
|
27 |
+
|
28 |
+
# Pastikan kolom ada
|
29 |
+
if not all(col in df.columns for col in ['tanggal', 'demand', 'supply']):
|
30 |
+
raise ValueError("Format kolom harus: tanggal, demand, supply")
|
31 |
+
|
32 |
+
# ===== 1. Tampilkan Grafik =====
|
33 |
+
st.subheader("Grafik Supply vs Demand")
|
34 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
35 |
+
|
36 |
+
ax.plot(df['tanggal'], df['demand'], label='Demand', color='blue', marker='o')
|
37 |
+
ax.plot(df['tanggal'], df['supply'], label='Supply', color='red', marker='s')
|
38 |
+
|
39 |
+
ax.fill_between(
|
40 |
+
df['tanggal'],
|
41 |
+
df['demand'],
|
42 |
+
df['supply'],
|
43 |
+
where=(df['supply'] > df['demand']),
|
44 |
+
color='green', alpha=0.3, label='Surplus'
|
45 |
+
)
|
46 |
+
ax.fill_between(
|
47 |
+
df['tanggal'],
|
48 |
+
df['demand'],
|
49 |
+
df['supply'],
|
50 |
+
where=(df['supply'] < df['demand']),
|
51 |
+
color='red', alpha=0.3, label='Defisit'
|
52 |
+
)
|
53 |
+
|
54 |
+
ax.set_xlabel("Tanggal")
|
55 |
+
ax.set_ylabel("Jumlah")
|
56 |
+
ax.legend()
|
57 |
+
ax.grid(True)
|
58 |
+
st.pyplot(fig)
|
59 |
+
|
60 |
+
# ===== 2. Prediksi dengan AI =====
|
61 |
+
st.subheader("Rekomendasi AI")
|
62 |
+
|
63 |
+
with st.spinner("Menganalisis data..."):
|
64 |
+
# Prediksi menggunakan GRANITE-TTM + Chronos-T5
|
65 |
+
recommendation = predict_umkm(
|
66 |
+
df,
|
67 |
+
prediction_length=prediction_days,
|
68 |
+
confidence=confidence_level/100
|
69 |
+
)
|
70 |
+
|
71 |
+
# Tampilkan hasil
|
72 |
+
st.success(recommendation['text'])
|
73 |
+
|
74 |
+
# Detail teknis
|
75 |
+
with st.expander("Detail Prediksi"):
|
76 |
+
st.json({
|
77 |
+
"prediksi_demand": recommendation['predictions'],
|
78 |
+
"roi_estimasi": f"{recommendation['roi']*100:.1f}%",
|
79 |
+
"confidence_score": recommendation['confidence']
|
80 |
+
})
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
st.error(f"⚠️ Error: {str(e)}")
|
84 |
+
st.info("Pastikan data berformat:\n- Kolom: tanggal, demand, supply\n- Minimal 30 data historis")
|
85 |
+
|
86 |
+
# Contoh data default jika tidak ada upload
|
87 |
+
else:
|
88 |
+
st.info("""
|
89 |
+
**Contoh Format CSV:**
|
90 |
+
```
|
91 |
+
tanggal,demand,supply
|
92 |
+
2024-01-01,100,120
|
93 |
+
2024-01-02,150,130
|
94 |
+
2024-01-03,200,180
|
95 |
+
```
|
96 |
+
""")
|