Arafath10 commited on
Commit
838216d
1 Parent(s): 9ccf15e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -27
main.py CHANGED
@@ -1,11 +1,6 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from fastapi.responses import HTMLResponse
3
- from fastapi.responses import StreamingResponse
4
- from fastapi.responses import FileResponse
5
  from fastapi.middleware.cors import CORSMiddleware
6
- from io import StringIO
7
- import os
8
- import uuid,requests
9
  import data_collector as dc
10
  import pandas as pd
11
 
@@ -18,25 +13,41 @@ app.add_middleware(
18
  allow_headers=["*"],
19
  )
20
 
21
-
22
-
23
  @app.post("/get_product_count_prediction")
24
- async def get_product_count_prediction(b_id:int,product_name:str):
25
- # main
26
- data,message = dc.get_data(b_id = b_id , product_name = product_name)
27
-
28
- if message=="done":
29
- # Summarize the sales count per month
30
- data['transaction_date'] = pd.to_datetime(data['transaction_date'])
31
- data.set_index('transaction_date', inplace=True)
32
- monthly_sales = data['sell_qty'].resample('M').sum().reset_index()
33
-
34
- full_trend,forecasted_value,rounded_value = dc.forecast(monthly_sales)
35
- print(full_trend,forecasted_value,rounded_value)
36
-
37
- rounded_value.columns = ["next_month", "y", "predicted_count"]
38
-
39
- # Convert to dictionary
40
- result_dict = rounded_value.to_dict(orient="records")[0]
41
 
42
- return {"next_month":str(result_dict["next_month"]) , "predicted_count":result_dict["predicted_count"]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import JSONResponse
 
 
3
  from fastapi.middleware.cors import CORSMiddleware
 
 
 
4
  import data_collector as dc
5
  import pandas as pd
6
 
 
13
  allow_headers=["*"],
14
  )
15
 
 
 
16
  @app.post("/get_product_count_prediction")
17
+ async def get_product_count_prediction(b_id: int, product_name: str):
18
+ try:
19
+ # main
20
+ data, message = dc.get_data(b_id=b_id, product_name=product_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ if message == "done":
23
+ # Summarize the sales count per month
24
+ data['transaction_date'] = pd.to_datetime(data['transaction_date'])
25
+ data.set_index('transaction_date', inplace=True)
26
+ monthly_sales = data['sell_qty'].resample('M').sum().reset_index()
27
+
28
+ full_trend, forecasted_value, rounded_value = dc.forecast(monthly_sales)
29
+ print(full_trend, forecasted_value, rounded_value)
30
+
31
+ rounded_value.columns = ["next_month", "y", "predicted_count"]
32
+
33
+ # Convert to dictionary
34
+ result_dict = rounded_value.to_dict(orient="records")[0]
35
+
36
+ response_content = {
37
+ "status": "success",
38
+ "message": "Prediction successful",
39
+ "data": {
40
+ "next_month": str(result_dict["next_month"]),
41
+ "predicted_count": result_dict["predicted_count"]
42
+ }
43
+ }
44
+ return JSONResponse(content=response_content, status_code=200)
45
+ else:
46
+ raise HTTPException(status_code=400, detail=message)
47
+ except Exception as e:
48
+ response_content = {
49
+ "status": "error",
50
+ "message": str(e),
51
+ "data": None
52
+ }
53
+ return JSONResponse(content=response_content, status_code=500)