Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -14,34 +14,49 @@ app.add_middleware(
|
|
14 |
)
|
15 |
|
16 |
@app.post("/get_product_count_prediction")
|
17 |
-
async def get_product_count_prediction(b_id: int
|
18 |
try:
|
19 |
# main
|
20 |
-
data, message = dc.get_data(b_id=b_id, product_name=
|
21 |
|
22 |
if message == "done":
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
response_content = {
|
37 |
"status": "success",
|
38 |
"message": "Prediction successful",
|
39 |
"data": {
|
40 |
-
|
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:
|
|
|
14 |
)
|
15 |
|
16 |
@app.post("/get_product_count_prediction")
|
17 |
+
async def get_product_count_prediction(b_id: int):
|
18 |
try:
|
19 |
# main
|
20 |
+
data, message = dc.get_data(b_id=b_id, product_name="sample")
|
21 |
|
22 |
if message == "done":
|
23 |
+
|
24 |
+
grouped_df = df.groupby('product_name')
|
25 |
+
|
26 |
+
results = []
|
27 |
+
for product_name, data in grouped_df:
|
28 |
+
# Summarize the sales count per month
|
29 |
+
data['transaction_date'] = pd.to_datetime(data['transaction_date'])
|
30 |
+
data.set_index('transaction_date', inplace=True)
|
31 |
+
monthly_sales = data['sell_qty'].resample('M').sum().reset_index()
|
32 |
+
|
33 |
+
try:
|
34 |
+
full_trend, forecasted_value, rounded_value = forecast(monthly_sales)
|
35 |
+
rounded_value.columns = ["next_month", "y", "predicted_count"]
|
36 |
+
# Convert to dictionary
|
37 |
+
result_dict = rounded_value.to_dict(orient="records")[0]
|
38 |
+
#print(full_trend, forecasted_value, rounded_value)
|
39 |
+
results.append({
|
40 |
+
"Product Name" : product_name,
|
41 |
+
"next_month": str(result_dict["next_month"]),
|
42 |
+
"predicted_count": result_dict["predicted_count"]
|
43 |
+
})
|
44 |
+
except Exception as e:
|
45 |
+
results.append({
|
46 |
+
"Product Name" : product_name,
|
47 |
+
"next_month": str(e),
|
48 |
+
"predicted_count": "not predicted"
|
49 |
+
})
|
50 |
+
break
|
51 |
response_content = {
|
52 |
"status": "success",
|
53 |
"message": "Prediction successful",
|
54 |
"data": {
|
55 |
+
results
|
|
|
56 |
}
|
57 |
}
|
58 |
return JSONResponse(content=response_content, status_code=200)
|
59 |
+
|
60 |
else:
|
61 |
raise HTTPException(status_code=400, detail=message)
|
62 |
except Exception as e:
|