Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
# ================================
|
| 5 |
+
# Complaint Prioritization Logic
|
| 6 |
+
# ================================
|
| 7 |
+
DEFAULT_PRIORITY_SCORE = {
|
| 8 |
+
"water": 0.9,
|
| 9 |
+
"electricity": 0.9,
|
| 10 |
+
"gas": 0.6,
|
| 11 |
+
"road": 0.5,
|
| 12 |
+
"garbage": 0.2
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def get_priority_label(score):
|
| 16 |
+
if score >= 0.75:
|
| 17 |
+
return "Critical"
|
| 18 |
+
elif score >= 0.55:
|
| 19 |
+
return "High"
|
| 20 |
+
elif score >= 0.35:
|
| 21 |
+
return "Medium"
|
| 22 |
+
else:
|
| 23 |
+
return "Low"
|
| 24 |
+
|
| 25 |
+
def calculate_weighted_score(upvotes, complaints, alpha=0.6, beta=0.4):
|
| 26 |
+
upvote_score = min(upvotes / 50, 1.0)
|
| 27 |
+
complaint_score = min(complaints / 20, 1.0)
|
| 28 |
+
weighted = (alpha * complaint_score) + (beta * upvote_score)
|
| 29 |
+
return weighted
|
| 30 |
+
|
| 31 |
+
def handle_complaint(text, category, complaints, upvotes):
|
| 32 |
+
default_score = DEFAULT_PRIORITY_SCORE.get(category.lower(), 0.3)
|
| 33 |
+
weighted_score = calculate_weighted_score(upvotes, complaints)
|
| 34 |
+
final_score = 0.5 * default_score + 0.5 * weighted_score
|
| 35 |
+
return {
|
| 36 |
+
"text": text,
|
| 37 |
+
"category": category,
|
| 38 |
+
"default_score": default_score,
|
| 39 |
+
"weighted_score": round(weighted_score, 2),
|
| 40 |
+
"final_score": round(final_score, 2),
|
| 41 |
+
"final_label": get_priority_label(final_score)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# ================================
|
| 45 |
+
# FastAPI Setup
|
| 46 |
+
# ================================
|
| 47 |
+
app = FastAPI(title="Complaint Prioritization API")
|
| 48 |
+
|
| 49 |
+
class ComplaintRequest(BaseModel):
|
| 50 |
+
text: str
|
| 51 |
+
category: str
|
| 52 |
+
complaints: int
|
| 53 |
+
upvotes: int
|
| 54 |
+
|
| 55 |
+
@app.get("/")
|
| 56 |
+
def root():
|
| 57 |
+
return {"message": "Complaint Prioritization API is live!"}
|
| 58 |
+
|
| 59 |
+
@app.post("/prioritize")
|
| 60 |
+
def prioritize_complaint(request: ComplaintRequest):
|
| 61 |
+
result = handle_complaint(request.text, request.category, request.complaints, request.upvotes)
|
| 62 |
+
return result
|