File size: 2,625 Bytes
838216d
 
d92c861
be531b6
6e48fd5
54db18f
d92c861
 
 
 
 
 
 
 
 
318071e
8345257
838216d
 
8345257
318071e
838216d
8345257
622420b
8345257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d728e0
8345257
 
 
 
 
 
838216d
 
 
94224b8
838216d
 
8345257
838216d
 
 
6d728e0
838216d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import data_collector as dc
import pandas as pd

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/get_product_count_prediction")
async def get_product_count_prediction(b_id: int):
    try:
        # main
        data, message = dc.get_data(b_id=b_id, product_name="sample")
        
        if message == "done":

            grouped_df = data.groupby('product_name')

            results = []
            for product_name, data in grouped_df:
                # Summarize the sales count per month
                data['transaction_date'] = pd.to_datetime(data['transaction_date'])
                data.set_index('transaction_date', inplace=True)
                monthly_sales = data['sell_qty'].resample('M').sum().reset_index()
                
                try:
                    full_trend, forecasted_value, rounded_value = forecast(monthly_sales)
                    rounded_value.columns = ["next_month", "y", "predicted_count"]
                    # Convert to dictionary
                    result_dict = rounded_value.to_dict(orient="records")[0]
                    #print(full_trend, forecasted_value, rounded_value)
                    results.append({
                                "Product Name" : product_name,
                                "next_month": str(result_dict["next_month"]),
                                "predicted_count": result_dict["predicted_count"]
                            })
                except Exception as e:
                    print(str(e))
                    results.append({
                                "Product Name" : product_name,
                                "next_month": str(e),
                                "predicted_count": "not predicted"
                            })
                    break      
            response_content = {
                "status": "success",
                "message": "Prediction successful",
                "data": results
            }
            return JSONResponse(content=response_content, status_code=200)
            
        else:
            raise HTTPException(status_code=400, detail=message)
    except Exception as e:
        print(str(e))
        response_content = {
            "status": "error",
            "message": str(e),
            "data": None
        }
        return JSONResponse(content=response_content, status_code=500)