-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
105 lines (87 loc) · 3.16 KB
/
app.py
File metadata and controls
105 lines (87 loc) · 3.16 KB
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import RootModel
import joblib
import pandas as pd
import shap
from services.preprocessing import preprocess_single_row
from services.chatgpt_service import generate_qos_insight
from services.faq_search import find_best_faq_answer
# ✅ Init
app = FastAPI()
# ✅ Load model
model = joblib.load("Models/best_xgb_model.pkl")
explainer = shap.Explainer(model)
# ✅ CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # change to your domain in prod
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ✅ Mount static folders
app.mount("/css", StaticFiles(directory="frontend/css"), name="css")
app.mount("/js", StaticFiles(directory="frontend/js"), name="js")
app.mount("/images", StaticFiles(directory="frontend/images"), name="images")
app.mount("/webfonts", StaticFiles(directory="frontend/webfonts"), name="webfonts")
app.mount("/components", StaticFiles(directory="frontend/components"), name="components")
# ✅ Serve pages
@app.get("/")
async def serve_index():
return FileResponse("frontend/index.html")
@app.get("/csv")
async def serve_index():
return FileResponse("frontend/csv.html")
@app.get("/xai")
async def serve_index():
return FileResponse("frontend/explain.html")
@app.get("/predict-page")
async def serve_predict_page():
return FileResponse("frontend/predict.html")
@app.get("/about")
async def serve_predict_page():
return FileResponse("frontend/aboutus.html")
@app.get("/chatbot")
async def serve_index():
return FileResponse("frontend/components/chatbot.html")
# ✅ Prediction input model
class RawQoSInput(RootModel[dict]):
pass
# ✅ Predict
@app.post("/predict")
async def predict(payload: RawQoSInput):
data = payload.root
input_df = preprocess_single_row(data)
prediction = model.predict(input_df)
return {"prediction_mbps": float(prediction[0]) / 1e6}
@app.post("/explain")
async def explain(data: dict):
input_df = preprocess_single_row(data)
shap_values = explainer(input_df)
explanation_mbps = [float(val) / 1e6 for val in shap_values[0].values.tolist()]
base_value_mbps = float(shap_values.base_values[0]) / 1e6
predicted_throughput_mbps = float(base_value_mbps + sum(explanation_mbps))
return {
"throughput_mbps": predicted_throughput_mbps,
"explanation": explanation_mbps,
"features": input_df.columns.tolist(),
"base_value": base_value_mbps,
}
# ✅ SHAP → GPT Insight
@app.post("/chat")
async def chat_explanation(data: dict):
insight = generate_qos_insight(
data["throughput_mbps"], data["explanation"], data["features"]
)
print("✅ Insight generated:\n", insight)
return {"insight": insight}
@app.post("/faq-chat")
async def faq_chat(request: dict):
user_question = request.get("question", "")
result = find_best_faq_answer(user_question)
if result["answer"] is None:
return {"response": "❌ Sorry, I couldn't find an answer. Try rephrasing?"}
return {"response": result["answer"]}