Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,58 +13,7 @@ from statsmodels.tsa.arima.model import ARIMA
|
|
| 13 |
import tensorflow as tf
|
| 14 |
import utils
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def predict_medicine_demand(location: str, year: int, month: int):
|
| 18 |
-
results = []
|
| 19 |
-
products=('Medicine_4', 'Medicine_10', 'Medicine_5', 'Medicine_7','Medicine_3', 'Medicine_2', 'Medicine_8', 'Medicine_1','Medicine_6', 'Medicine_9')
|
| 20 |
-
for product in products:
|
| 21 |
-
try:
|
| 22 |
-
monthly_data=pd.read_csv('monthly_data.csv')
|
| 23 |
-
|
| 24 |
-
# Get last 3 values from the data
|
| 25 |
-
ts_df = monthly_data[(monthly_data['product'] == product) & (monthly_data['location'] == location)].copy()
|
| 26 |
-
ts_df.set_index('date', inplace=True)
|
| 27 |
-
ts_df = ts_df.sort_index()
|
| 28 |
-
ts = ts_df['quantity']
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Load ARIMA model
|
| 32 |
-
arima_model = joblib.load(f"models/arima_{product}_{location}.pkl")
|
| 33 |
-
arima_forecast = arima_model.forecast(steps=1)[0]
|
| 34 |
-
|
| 35 |
-
# Load ANN
|
| 36 |
-
ann_model = tf.keras.models.load_model(f"models/ann_{product}_{location}.h5")
|
| 37 |
-
scaler_X = joblib.load(f"models/ann_scalerX_{product}_{location}.pkl")
|
| 38 |
-
scaler_y = joblib.load(f"models/ann_scalerY_{product}_{location}.pkl")
|
| 39 |
-
|
| 40 |
-
last_3 = ts[-3:].values.reshape(1, -1)
|
| 41 |
-
last_scaled = scaler_X.transform(last_3)
|
| 42 |
-
ann_pred_scaled = ann_model.predict(last_scaled, verbose=0)
|
| 43 |
-
ann_forecast = scaler_y.inverse_transform(ann_pred_scaled)[0][0]
|
| 44 |
-
|
| 45 |
-
# Load LSTM
|
| 46 |
-
lstm_model = tf.keras.models.load_model(f"models/lstm_{product}_{location}.h5")
|
| 47 |
-
lstm_scaler = joblib.load(f"models/lstm_scaler_{product}_{location}.pkl")
|
| 48 |
-
|
| 49 |
-
series_scaled = lstm_scaler.transform(ts.values.reshape(-1, 1)).flatten()
|
| 50 |
-
last_seq = series_scaled[-3:].reshape(1, 3, 1)
|
| 51 |
-
lstm_pred_scaled = lstm_model.predict(last_seq, verbose=0)[0][0]
|
| 52 |
-
lstm_forecast = lstm_scaler.inverse_transform([[lstm_pred_scaled]])[0][0]
|
| 53 |
-
|
| 54 |
-
results.append({
|
| 55 |
-
"Product": product,
|
| 56 |
-
"ARIMA Pred": round(arima_forecast, 2),
|
| 57 |
-
"ANN Pred": round(ann_forecast, 2),
|
| 58 |
-
"LSTM Pred": round(lstm_forecast, 2),
|
| 59 |
-
"Average Requirement":round((arima_forecast+ann_forecast+lstm_forecast)/3,2)
|
| 60 |
-
})
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"Failed to predict for {product} - {location}: {e}")
|
| 64 |
-
return pd.DataFrame(results)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
def load_locations():
|
| 70 |
df = pd.read_csv("medicinal_sales_data_large.csv")
|
|
@@ -90,10 +39,6 @@ if st.button("📈 Predict Demand"):
|
|
| 90 |
st.info("Running prediction, please wait...")
|
| 91 |
try:
|
| 92 |
result_df = predict_medicine_demand(selected_location, selected_year, selected_month)
|
| 93 |
-
st.write(selected_year)
|
| 94 |
-
st.write(selected_month)
|
| 95 |
-
|
| 96 |
-
st.write(result_df.head())
|
| 97 |
if result_df.empty:
|
| 98 |
st.warning("No data available for this location.")
|
| 99 |
else:
|
|
|
|
| 13 |
import tensorflow as tf
|
| 14 |
import utils
|
| 15 |
|
| 16 |
+
from utils import predict_medicine_demand
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def load_locations():
|
| 19 |
df = pd.read_csv("medicinal_sales_data_large.csv")
|
|
|
|
| 39 |
st.info("Running prediction, please wait...")
|
| 40 |
try:
|
| 41 |
result_df = predict_medicine_demand(selected_location, selected_year, selected_month)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if result_df.empty:
|
| 43 |
st.warning("No data available for this location.")
|
| 44 |
else:
|