Amii2410 commited on
Commit
66bfa33
·
verified ·
1 Parent(s): 3d2bcdb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ # Complaint Prioritization System wrapped in a Gradio app (and API)
3
+
4
+ from typing import Dict, Any
5
+ import gradio as gr
6
+
7
+ # ================================
8
+ # Configuration (default values)
9
+ # ================================
10
+ DEFAULT_PRIORITY_SCORE = {
11
+ "water": 0.9,
12
+ "electricity": 0.9,
13
+ "gas": 0.6,
14
+ "road": 0.5,
15
+ "garbage": 0.2
16
+ }
17
+
18
+ DEPARTMENT_WEIGHT = {
19
+ "water": 1.2,
20
+ "electricity": 1.2,
21
+ "gas": 1.0,
22
+ "road": 0.9,
23
+ "garbage": 0.8
24
+ }
25
+
26
+ # Helper to convert 0–1 score into labels
27
+ def get_priority_label(score: float) -> str:
28
+ if score >= 0.75:
29
+ return "Critical"
30
+ elif score >= 0.55:
31
+ return "High"
32
+ elif score >= 0.35:
33
+ return "Medium"
34
+ else:
35
+ return "Low"
36
+
37
+ # Calculate weighted score (0–1)
38
+ def calculate_weighted_score(upvotes: int, complaints: int, alpha: float=0.6, beta: float=0.4) -> float:
39
+ # Normalize to 0–1 scale (cap at 1)
40
+ upvote_score = min(upvotes / 50.0, 1.0) # assume 50 upvotes = max
41
+ complaint_score = min(complaints / 20.0, 1.0) # assume 20 complaints = max
42
+ weighted = (alpha * complaint_score) + (beta * upvote_score)
43
+ return weighted
44
+
45
+ # Core handler
46
+ def handle_complaint(text: str, category: str, complaints: int, upvotes: int) -> Dict[str, Any]:
47
+ if category is None:
48
+ category = ""
49
+ category_key = category.strip().lower()
50
+ default_score = DEFAULT_PRIORITY_SCORE.get(category_key, 0.3)
51
+ weighted_score = calculate_weighted_score(upvotes=upvotes, complaints=complaints)
52
+ department_bias = DEPARTMENT_WEIGHT.get(category_key, 1.0)
53
+
54
+ # Final score = (default + weighted)/2 * department bias
55
+ raw_final = 0.5 * default_score + 0.5 * weighted_score
56
+ final_score = min(raw_final * department_bias, 1.0) # cap at 1.0
57
+
58
+ result = {
59
+ "text": text,
60
+ "category": category_key if category_key else "unknown",
61
+ "default_score": round(default_score, 2),
62
+ "weighted_score": round(weighted_score, 2),
63
+ "department_bias": department_bias,
64
+ "final_score": round(final_score, 2),
65
+ "final_label": get_priority_label(final_score)
66
+ }
67
+ return result
68
+
69
+ # Gradio UI & API
70
+ with gr.Blocks() as demo:
71
+ gr.Markdown("# Complaint Prioritization API")
72
+ gr.Markdown("Provide complaint text, category, number of complaints and upvotes. The endpoint `/api/predict` will return the same JSON.")
73
+ with gr.Row():
74
+ txt = gr.Textbox(lines=3, label="Complaint text", value="Pothole on main road causing damage.")
75
+ cat = gr.Textbox(lines=1, label="Category (water, electricity, gas, road, garbage)", value="road")
76
+ with gr.Row():
77
+ complaints_in = gr.Number(value=1, label="Number of distinct complaints (integer)")
78
+ upvotes_in = gr.Number(value=0, label="Number of upvotes/flags (integer)")
79
+ run_btn = gr.Button("Get Priority")
80
+ output = gr.JSON(label="Result")
81
+
82
+ run_btn.click(fn=handle_complaint,
83
+ inputs=[txt, cat, complaints_in, upvotes_in],
84
+ outputs=output)
85
+
86
+ # Provide a simple example on load
87
+ demo.load(lambda: {
88
+ "text": "Example: Water pipeline leakage near market area.",
89
+ "category": "water",
90
+ "default_score": DEFAULT_PRIORITY_SCORE["water"],
91
+ "weighted_score": 0.0,
92
+ "department_bias": DEPARTMENT_WEIGHT["water"],
93
+ "final_score": None,
94
+ "final_label": None
95
+ }, outputs=output)
96
+
97
+ if __name__ == "__main__":
98
+ demo.launch()