Amii2410 commited on
Commit
2f94f4c
·
verified ·
1 Parent(s): 5ee1870

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -36,7 +36,6 @@ def get_priority_label(score: float) -> str:
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)
@@ -51,7 +50,6 @@ def handle_complaint(text: str, category: str, complaints: int, upvotes: int) ->
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
 
@@ -66,16 +64,21 @@ def handle_complaint(text: str, category: str, complaints: int, upvotes: int) ->
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
 
@@ -83,16 +86,16 @@ with gr.Blocks() as demo:
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()
 
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
  upvote_score = min(upvotes / 50.0, 1.0) # assume 50 upvotes = max
40
  complaint_score = min(complaints / 20.0, 1.0) # assume 20 complaints = max
41
  weighted = (alpha * complaint_score) + (beta * upvote_score)
 
50
  weighted_score = calculate_weighted_score(upvotes=upvotes, complaints=complaints)
51
  department_bias = DEPARTMENT_WEIGHT.get(category_key, 1.0)
52
 
 
53
  raw_final = 0.5 * default_score + 0.5 * weighted_score
54
  final_score = min(raw_final * department_bias, 1.0) # cap at 1.0
55
 
 
64
  }
65
  return result
66
 
67
+ # ================================
68
  # Gradio UI & API
69
+ # ================================
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# Complaint Prioritization API")
72
+ gr.Markdown("Provide complaint details. You can also call `/api/predict` for API access.")
73
+
74
  with gr.Row():
75
  txt = gr.Textbox(lines=3, label="Complaint text", value="Pothole on main road causing damage.")
76
  cat = gr.Textbox(lines=1, label="Category (water, electricity, gas, road, garbage)", value="road")
77
+
78
  with gr.Row():
79
  complaints_in = gr.Number(value=1, label="Number of distinct complaints (integer)")
80
+ upvotes_in = gr.Number(value=0, label="Number of upvotes (integer)")
81
+
82
  run_btn = gr.Button("Get Priority")
83
  output = gr.JSON(label="Result")
84
 
 
86
  inputs=[txt, cat, complaints_in, upvotes_in],
87
  outputs=output)
88
 
89
+ # Move demo.load INSIDE the Blocks context
90
+ demo.load(
91
+ lambda: handle_complaint(
92
+ "Water pipeline leakage near market area.",
93
+ "water",
94
+ 15,
95
+ 8
96
+ ),
97
+ outputs=output
98
+ )
99
 
100
  if __name__ == "__main__":
101
  demo.launch()