Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
from prophet import Prophet
|
| 4 |
from datetime import datetime, timedelta
|
|
|
|
| 5 |
|
| 6 |
# Prepare data for Prophet
|
| 7 |
def prepare_prophet_data(usage_series):
|
|
@@ -12,35 +13,33 @@ def prepare_prophet_data(usage_series):
|
|
| 12 |
'ds': dates,
|
| 13 |
'y': usage_series
|
| 14 |
})
|
| 15 |
-
prophet_df['cap'] =
|
| 16 |
prophet_df['floor'] = 0
|
| 17 |
return prophet_df
|
| 18 |
|
| 19 |
-
# Train
|
| 20 |
def train_model_with_usage(usage_series):
|
| 21 |
-
print("Training with changepoint_prior_scale=0.001, usage:", usage_series) # Debug to logs
|
| 22 |
prophet_df = prepare_prophet_data(usage_series)
|
| 23 |
model = Prophet(
|
| 24 |
yearly_seasonality=False,
|
| 25 |
weekly_seasonality=True,
|
| 26 |
daily_seasonality=True,
|
| 27 |
-
changepoint_prior_scale=0.
|
| 28 |
growth='logistic'
|
| 29 |
)
|
| 30 |
model.fit(prophet_df)
|
| 31 |
return model
|
| 32 |
|
| 33 |
-
#
|
| 34 |
def make_forecast(model, periods):
|
| 35 |
future = model.make_future_dataframe(periods=periods)
|
| 36 |
-
future['cap'] =
|
| 37 |
future['floor'] = 0
|
| 38 |
forecast = model.predict(future)
|
| 39 |
daily_forecasts = forecast['yhat'].tail(periods).tolist()
|
| 40 |
-
|
| 41 |
-
return round(sum(max(0, y) for y in daily_forecasts)) # Clip negative values
|
| 42 |
|
| 43 |
-
#
|
| 44 |
def validate_usage_series(usage_str):
|
| 45 |
try:
|
| 46 |
usage_list = [float(x) for x in usage_str.split(',')]
|
|
@@ -55,10 +54,7 @@ def validate_usage_series(usage_str):
|
|
| 55 |
# Main Streamlit app
|
| 56 |
def main():
|
| 57 |
st.title("SmartLab Consumables Forecast")
|
| 58 |
-
|
| 59 |
-
st.cache_resource.clear()
|
| 60 |
-
st.write("Cache cleared!")
|
| 61 |
-
|
| 62 |
st.header("Input Parameters")
|
| 63 |
consumable_type = st.selectbox("Consumable Type", ['Filters', 'Reagents', 'Vials'])
|
| 64 |
usage_series = st.text_input("Last 60 Days Usage (comma-separated)", "")
|
|
@@ -69,7 +65,6 @@ def main():
|
|
| 69 |
if error:
|
| 70 |
st.error(error)
|
| 71 |
return
|
| 72 |
-
print("Input usage series:", usage_list) # Debug to logs
|
| 73 |
|
| 74 |
try:
|
| 75 |
model = train_model_with_usage(usage_list)
|
|
@@ -110,4 +105,4 @@ def main():
|
|
| 110 |
st.write(f"**For 30 Days**: Order {order_30} additional units.")
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
-
main()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from prophet import Prophet
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Prepare data for Prophet
|
| 8 |
def prepare_prophet_data(usage_series):
|
|
|
|
| 13 |
'ds': dates,
|
| 14 |
'y': usage_series
|
| 15 |
})
|
| 16 |
+
prophet_df['cap'] = 60 # Max observed usage
|
| 17 |
prophet_df['floor'] = 0
|
| 18 |
return prophet_df
|
| 19 |
|
| 20 |
+
# Train Prophet model
|
| 21 |
def train_model_with_usage(usage_series):
|
|
|
|
| 22 |
prophet_df = prepare_prophet_data(usage_series)
|
| 23 |
model = Prophet(
|
| 24 |
yearly_seasonality=False,
|
| 25 |
weekly_seasonality=True,
|
| 26 |
daily_seasonality=True,
|
| 27 |
+
changepoint_prior_scale=0.002,
|
| 28 |
growth='logistic'
|
| 29 |
)
|
| 30 |
model.fit(prophet_df)
|
| 31 |
return model
|
| 32 |
|
| 33 |
+
# Forecast function
|
| 34 |
def make_forecast(model, periods):
|
| 35 |
future = model.make_future_dataframe(periods=periods)
|
| 36 |
+
future['cap'] = 60
|
| 37 |
future['floor'] = 0
|
| 38 |
forecast = model.predict(future)
|
| 39 |
daily_forecasts = forecast['yhat'].tail(periods).tolist()
|
| 40 |
+
return round(sum(max(0, y) for y in daily_forecasts)) # Clip negatives
|
|
|
|
| 41 |
|
| 42 |
+
# Input validation
|
| 43 |
def validate_usage_series(usage_str):
|
| 44 |
try:
|
| 45 |
usage_list = [float(x) for x in usage_str.split(',')]
|
|
|
|
| 54 |
# Main Streamlit app
|
| 55 |
def main():
|
| 56 |
st.title("SmartLab Consumables Forecast")
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
| 58 |
st.header("Input Parameters")
|
| 59 |
consumable_type = st.selectbox("Consumable Type", ['Filters', 'Reagents', 'Vials'])
|
| 60 |
usage_series = st.text_input("Last 60 Days Usage (comma-separated)", "")
|
|
|
|
| 65 |
if error:
|
| 66 |
st.error(error)
|
| 67 |
return
|
|
|
|
| 68 |
|
| 69 |
try:
|
| 70 |
model = train_model_with_usage(usage_list)
|
|
|
|
| 105 |
st.write(f"**For 30 Days**: Order {order_30} additional units.")
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
+
main()
|