Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +47 -0
- general_recommendation.py +10 -0
- recommendation_explainer.py +33 -0
- recommended_plan.csv +38 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
import pandas as pd
|
5 |
+
from general_recommendation import get_recommendations
|
6 |
+
from recommendation_explainer import explain_recommendation
|
7 |
+
import uvicorn
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
df = pd.read_csv('recommended_plan.csv')
|
13 |
+
|
14 |
+
# CORS middleware to allow requests from any origin
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=["*"],
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["*"],
|
20 |
+
allow_headers=["*"],
|
21 |
+
)
|
22 |
+
|
23 |
+
@app.get("/api/v1/recommendations")
|
24 |
+
def get_recommendations():
|
25 |
+
try:
|
26 |
+
sampled_data = df.sample(10)
|
27 |
+
results = sampled_data.to_dict(orient="records")
|
28 |
+
return JSONResponse(content={"recommendations": results})
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
32 |
+
|
33 |
+
|
34 |
+
@app.get("/api/v1/explainrecommendation/{userid}")
|
35 |
+
def get_explanations(userid):
|
36 |
+
try:
|
37 |
+
|
38 |
+
explanation = explain_recommendation(userid, df)
|
39 |
+
return JSONResponse(content={"explanation": explanation})
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
47 |
+
|
general_recommendation.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
|
3 |
+
|
4 |
+
df = pd.read_csv('recommended_plan.csv')
|
5 |
+
|
6 |
+
|
7 |
+
def get_recommendations():
|
8 |
+
top_10_rows = df.sample(10)
|
9 |
+
recommendations = top_10_rows.to_dict(orient='records')
|
10 |
+
return recommendations
|
recommendation_explainer.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_groq import ChatGroq
|
2 |
+
from langchain_core.prompts import PromptTemplate
|
3 |
+
from langchain_core.output_parsers import StrOutputParser
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
10 |
+
|
11 |
+
model = ChatGroq(model="qwen/qwen3-32b")
|
12 |
+
|
13 |
+
prompt_template = PromptTemplate(template = '''You are a Mobile Plan Analyzer tool that evaluates a user's current mobile plan against their usage patterns and a recommended plan, and generates a clear, one-sentence justification highlighting improved value, efficiency, or suitability—tailored for quick stakeholder insights.
|
14 |
+
The user info is : {user_data}''', input_variables=["user_data"])
|
15 |
+
|
16 |
+
parser = StrOutputParser()
|
17 |
+
|
18 |
+
|
19 |
+
def explain_recommendation(user_id, df):
|
20 |
+
if user_id and user_id in df['user_id'].values:
|
21 |
+
user_data = df[df['user_id'] == user_id].iloc[0]
|
22 |
+
|
23 |
+
chain = prompt_template | model | parser
|
24 |
+
response = chain.invoke({"user_data": user_data})
|
25 |
+
if isinstance(response, str) and "</think>" in response:
|
26 |
+
return response.split("</think>")[1].replace("\n","").strip()
|
27 |
+
else:
|
28 |
+
return response
|
29 |
+
else:
|
30 |
+
raise ValueError("User ID not found in the dataset.")
|
31 |
+
|
32 |
+
|
33 |
+
|
recommended_plan.csv
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
user_id,name,profession,recommended_plan,score,price,monthly_data,monthly_calls_mins
|
2 |
+
U006,Nicholas Gibbs,Business,Celcom Mega 98,0.73,98,90,442
|
3 |
+
U017,Janice Stark,Employee,Celcom Mega 98,0.88,98,99,804
|
4 |
+
U018,Jeremy Garcia,Employee,Celcom Mega 98,0.8,98,52,284
|
5 |
+
U015,Dawn Nielsen,Business,Celcom Mega 98,0.81,98,69,756
|
6 |
+
U010,Kathy Wilson,Employee,Celcom Mega 98,0.91,98,81,643
|
7 |
+
U019,Lisa Hayes,Employee,Celcom Mega 98,0.71,98,28,681
|
8 |
+
U037,Gregory Castro,Employee,Celcom Mega 98,0.82,98,52,697
|
9 |
+
U049,Rebecca Krause,Employee,Celcom Mega 98,0.71,98,60,265
|
10 |
+
U042,Dominique Crawford,Business,Celcom Traveller Pack,0.76,78,36,695
|
11 |
+
U047,Dawn Richards,Traveller,Celcom Traveller Pack,0.84,78,94,357
|
12 |
+
U035,Laura Garcia,Traveller,Celcom Traveller Pack,0.76,78,71,179
|
13 |
+
U045,Johnny Chandler,Traveller,Celcom Traveller Pack,0.79,78,46,443
|
14 |
+
U016,Ernest Wagner,Traveller,Celcom Traveller Pack,0.78,78,35,957
|
15 |
+
U025,Tony Mendoza,Traveller,Celcom Traveller Pack,0.84,78,77,489
|
16 |
+
U044,Lisa Bauer,Traveller,Celcom Traveller Pack,0.86,78,53,204
|
17 |
+
U020,Cynthia Cain,Traveller,Celcom Traveller Pack,0.75,78,19,281
|
18 |
+
U022,Roger Miller,Traveller,Celcom Traveller Pack,0.78,78,47,234
|
19 |
+
U036,Anthony Wright,Traveller,Celcom Traveller Pack,0.85,78,28,961
|
20 |
+
U034,Laura Erickson,Traveller,Celcom Traveller Pack,0.74,78,9,788
|
21 |
+
U007,John Simmons,Business,Celcom XP Lite,0.76,38,98,354
|
22 |
+
U008,Shawn Cain,Student,Celcom XP Lite,0.78,38,46,877
|
23 |
+
U046,Robert Richardson,Student,Celcom XP Lite,0.85,38,87,679
|
24 |
+
U009,Michael Ramirez,Traveller,Celcom XP Lite,0.78,38,69,137
|
25 |
+
U026,Larry Alexander,Student,Celcom XP Lite,0.75,38,88,748
|
26 |
+
U028,Teresa Burch,Student,Celcom XP Lite,0.75,38,93,906
|
27 |
+
U023,Thomas Bird,Student,Celcom XP Lite,0.73,38,97,58
|
28 |
+
U024,Amber Kerr,Business,Celcom XP Lite,0.81,38,88,442
|
29 |
+
U014,Christian Adams,Employee,Celcom XP Lite,0.79,38,90,969
|
30 |
+
U027,Michael Fowler,Employee,Celcom XP Lite,0.71,38,15,310
|
31 |
+
U041,William Thomas,Student,Celcom XP Lite,0.77,38,5,890
|
32 |
+
U005,Victoria Sutton,Senior Citizen,Celcom Senior Plan,0.76,28,54,235
|
33 |
+
U021,Michael Bonilla,Senior Citizen,Celcom Senior Plan,0.75,28,69,279
|
34 |
+
U033,Mark Crawford,Business,Celcom Senior Plan,0.74,28,8,256
|
35 |
+
U029,George Santiago,Senior Citizen,Celcom Senior Plan,0.76,28,14,901
|
36 |
+
U030,Amy Hopkins,Senior Citizen,Celcom Senior Plan,0.75,28,78,213
|
37 |
+
U031,Thomas Parker,Senior Citizen,Celcom Senior Plan,0.72,28,100,954
|
38 |
+
U038,Jennifer Lynch,Senior Citizen,Celcom Senior Plan,0.75,28,58,133
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
numpy
|
3 |
+
fastapi
|
4 |
+
uvicorn
|
5 |
+
langchain
|
6 |
+
langchain-community
|
7 |
+
langchain-groq
|
8 |
+
langchain-core
|
9 |
+
python-dotenv
|