Abhilash7 commited on
Commit
f32cc30
·
verified ·
1 Parent(s): 8b995b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -21
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
- # Load the historical data CSV file
8
- df = pd.read_csv("Queue_Historical_Data_Jan_to_May_2025.csv")
 
9
 
10
- # Ensure the 'ds' column is in datetime format
11
- df['ds'] = pd.to_datetime(df['ds'], errors='coerce', dayfirst=False)
 
12
 
13
- # Drop rows where 'ds' could not be parsed
14
- df = df.dropna(subset=['ds'])
 
15
 
16
- # Ensure the 'y' column exists and is numerical
17
- df['y'] = pd.to_numeric(df['y'], errors='coerce')
18
- df = df.dropna(subset=['y'])
19
-
20
- # Initialize the Prophet model
21
- model = Prophet()
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Fit the model with historical data
24
- model.fit(df)
25
 
26
  @app.post("/run")
27
  async def forecast(request: Request):
28
- # Create a future dataframe for the next 7 days
29
- future = model.make_future_dataframe(df, periods=7)
30
-
31
- # Predict the future values
32
- forecast = model.predict(future)
 
 
 
 
33
 
34
- # Return the last 7 predicted values
35
- return forecast[['ds', 'yhat']].tail(7).to_dict(orient="records")
 
 
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"}