-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
125 lines (104 loc) · 4.24 KB
/
run.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import uvicorn
from pathlib import Path
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import JSONResponse
from llm.llm import LLM
from db.sql_db import DB
db = DB()
llm = LLM(db, load_model_data_on_start=True)
server_host = "127.0.0.1"
server_port = 8000
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": f"Try ot APIs: http://{server_host}:{server_port}/docs"}
@app.get("/get_customer_profile/{user_id}")
async def get_customer_profile(user_id: int):
return llm.get_user_profile(user_id)
@app.get("/recommend_product/{user_id}")
async def recommend_product(user_id: int):
"""
Returns recommendations id for a given user ID.
Args:
user_id (int): ID of the user.
Returns:
dict: dictionary containing given user id, generated recommended id, and recommended product data.
"""
if db.get_data(table="User", id=user_id):
recommendation_data = llm.make_recommendation_for_customer(user_id=user_id)
recommendation_id = recommendation_data['recommendation_id']
product_id = recommendation_data['product_id']
product_dict = db.get_data(table="Product", id=product_id, return_as_dict=True)
return {
'user_id': user_id,
'recommendation_id': recommendation_id,
'product_data': product_dict
}
else:
raise HTTPException(status_code=404, detail="User ID not found")
@app.get("/search_products")
async def search_products(
user_id: int,
query: str = Query(..., min_length=1, max_length=100),
min_price: float = Query(None, ge=0),
max_price: float = Query(None, ge=0),
min_stock: int = Query(None, ge=1),
):
"""
Search for products by a query string with optional price filters.
Args:
query (str): The search term.
min_price (float): Minimum price for filtering (optional).
max_price (float): Maximum price for filtering (optional).
min_stock (float): Minimum stock for filtering (optional).
Returns:
dict: A list of matching products.
"""
if db.get_data(table="User", id=user_id):
return db.search_product(
user_id=user_id,
search_keyword=query,
min_price=min_price,
max_price=max_price,
min_stock=min_stock,
return_as_dict=True
)
else:
raise HTTPException(status_code=404, detail="User ID not found")
@app.post("/set_recommendation_feedback")
async def set_recommendation_feedback(
user_id: int,
recommendation_id: int,
rating_score: int = Query(..., ge=0, le=5),
):
"""
Sets feedback for a recommendation. Only accepts feedback from the same user as the user who was recommended.
Args:
recommendation_id (int): ID of the recommendation.
rating_score (int): Rating score (0 to 5).
"""
if user_id == db.get_data(table="Recommendation", id=recommendation_id, return_as_dict=False).user_id:
recommendation_feedback_id = db.record_recommendation_feedback(
recommendation_id=recommendation_id,
user_id=user_id,
rating=rating_score
)
if recommendation_feedback_id:
return JSONResponse(
content={"status": "success", "message": "Feedback recorded successfully"},
status_code=200,
)
else:
raise HTTPException(status_code=409, detail="Feedback for this recommendation has already been submitted.")
else:
raise HTTPException(status_code=403, detail="Given user is not authorized to access or interact with this recommendation.")
@app.get("/get_recommendation_performance")
async def get_recommendation_performance():
"""
Returns a dictionary of the number of recommendations per rating score (0 to 5). Can be used for visualization like barplot for real time monitoring of recommendation performance from user feedbacks.
Returns:
dict: Key-value pairs where keys are rating scores and values are the count of recommendations for each score.
"""
return db.summarize_recommendation_feedback_rating()
if __name__ == "__main__":
uvicorn.run(f"{Path(__file__).stem}:app", host=server_host, port=server_port, reload=True)