Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,54 @@
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from prophet import Prophet
|
3 |
import pandas as pd
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
df =
|
19 |
-
|
20 |
-
#
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
@app.post("/run")
|
27 |
async def forecast(request: Request):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from prophet import Prophet
|
3 |
import pandas as pd
|
4 |
+
import logging
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Setup logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
|
12 |
+
@app.on_event("startup")
|
13 |
+
async def startup_event():
|
14 |
+
logger.info("Starting the application")
|
15 |
|
16 |
+
@app.on_event("shutdown")
|
17 |
+
async def shutdown_event():
|
18 |
+
logger.info("Shutting down the application")
|
19 |
|
20 |
+
try:
|
21 |
+
# Load the historical data CSV file
|
22 |
+
df = pd.read_csv("Queue_Historical_Data_Jan_to_May_2025.csv")
|
23 |
+
|
24 |
+
# Ensure the 'ds' column is in datetime format
|
25 |
+
df['ds'] = pd.to_datetime(df['ds'], errors='coerce', dayfirst=False)
|
26 |
+
df = df.dropna(subset=['ds'])
|
27 |
+
|
28 |
+
# Ensure the 'y' column exists and is numerical
|
29 |
+
df['y'] = pd.to_numeric(df['y'], errors='coerce')
|
30 |
+
df = df.dropna(subset=['y'])
|
31 |
+
|
32 |
+
# Initialize the Prophet model
|
33 |
+
model = Prophet()
|
34 |
+
model.fit(df)
|
35 |
+
logger.info("Model successfully fitted")
|
36 |
|
37 |
+
except Exception as e:
|
38 |
+
logger.error(f"Error during initialization: {e}")
|
39 |
|
40 |
@app.post("/run")
|
41 |
async def forecast(request: Request):
|
42 |
+
try:
|
43 |
+
# Create a future dataframe for the next 7 days
|
44 |
+
future = model.make_future_dataframe(df, periods=7)
|
45 |
+
|
46 |
+
# Predict the future values
|
47 |
+
forecast = model.predict(future)
|
48 |
+
|
49 |
+
# Return the last 7 predicted values
|
50 |
+
return forecast[['ds', 'yhat']].tail(7).to_dict(orient="records")
|
51 |
|
52 |
+
except Exception as e:
|
53 |
+
logger.error(f"Error during forecast: {e}")
|
54 |
+
return {"error": "Error occurred while making predictions"}
|