Amii2410 commited on
Commit
f400493
·
verified ·
1 Parent(s): b808597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -38
app.py CHANGED
@@ -1,22 +1,13 @@
1
  # app.py
2
  """
3
- Complaint Prioritization API (Gradio-based)
4
- Drop this file into a Hugging Face Space (SDK: Gradio) or run locally.
 
5
  """
6
 
7
- from typing import List, Dict, Any
8
  import gradio as gr
9
 
10
- # ================================
11
- # Default priority values mapped 0–1
12
- DEFAULT_PRIORITY_SCORE = {
13
- "water": 0.9,
14
- "electricity": 0.9,
15
- "gas": 0.6,
16
- "road": 0.5,
17
- "garbage": 0.2
18
- }
19
-
20
  # Helper to convert 0–1 score into labels
21
  def get_priority_label(score: float) -> str:
22
  if score >= 0.75:
@@ -37,37 +28,33 @@ def calculate_weighted_score(upvotes: int, complaints: int, alpha: float = 0.6,
37
  return weighted
38
 
39
  # Handle a single complaint
40
- def handle_complaint(text: str, category: str, complaints: int, upvotes: int) -> Dict[str, Any]:
41
- default_score = DEFAULT_PRIORITY_SCORE.get(category.lower(), 0.3)
42
  weighted_score = calculate_weighted_score(upvotes, complaints)
43
- final_score = 0.5 * default_score + 0.5 * weighted_score
44
-
45
  return {
46
  "text": text,
47
- "category": category,
48
- "default_score": round(default_score, 2),
49
- "weighted_score": round(weighted_score, 2),
50
- "final_score": round(final_score, 2),
51
- "final_label": get_priority_label(final_score)
52
  }
53
 
54
- # API entrypoint for a single complaint (for UI)
55
- def predict_single(text: str, category: str, complaints: int, upvotes: int):
56
  try:
57
  complaints = int(complaints)
58
  upvotes = int(upvotes)
59
  except Exception:
60
  return {"error": "complaints and upvotes must be integers"}
61
- return handle_complaint(text, category, complaints, upvotes)
62
 
63
  # API entrypoint for batch complaints (JSON text input)
64
  def predict_batch(json_string: str):
65
  """
66
- Accepts a JSON array string (list of dicts with keys: text, category, complaints, upvotes)
67
  Example:
68
  [
69
- {"text":"Pothole","category":"road","complaints":2,"upvotes":5},
70
- {"text":"Leak","category":"water","complaints":15,"upvotes":8}
71
  ]
72
  """
73
  import json
@@ -81,32 +68,31 @@ def predict_batch(json_string: str):
81
  results = []
82
  for it in items:
83
  t = it.get("text", "")
84
- cat = it.get("category", "")
85
  complaints = int(it.get("complaints", 0))
86
  upvotes = int(it.get("upvotes", 0))
87
- results.append(handle_complaint(t, cat, complaints, upvotes))
88
  return {"results": results}
89
 
90
- # Small UI using Gradio; Spaces will host this and expose a programmatic endpoint automatically
91
  with gr.Blocks() as demo:
92
- gr.Markdown("## Complaint Prioritization API\nProvide a single complaint or a JSON list for batch processing.")
93
  with gr.Tab("Single complaint"):
94
  txt = gr.Textbox(label="Complaint text", value="Huge pothole on main road, damaging cars daily.")
95
- cat = gr.Dropdown(label="Category", choices=["water","electricity","gas","road","garbage","other"], value="road")
96
  comp = gr.Number(label="Number of complaints", value=2, precision=0)
97
  upv = gr.Number(label="Number of upvotes", value=5, precision=0)
98
  out = gr.JSON(label="Result")
99
  btn = gr.Button("Predict")
100
- btn.click(fn=predict_single, inputs=[txt, cat, comp, upv], outputs=out)
101
 
102
  with gr.Tab("Batch (JSON array)"):
103
- batch_in = gr.Textbox(label="JSON array of complaints", lines=10, value='[{"text":"Leak near market","category":"water","complaints":15,"upvotes":8}]')
 
 
 
 
104
  batch_out = gr.JSON(label="Batch results")
105
  batch_btn = gr.Button("Predict batch")
106
  batch_btn.click(fn=predict_batch, inputs=batch_in, outputs=batch_out)
107
 
108
- gr.Markdown("### Programmatic usage\nUse the Spaces' `gradio` API endpoint `/api/predict/` (or use the `predict_batch` JSON route). See README for examples.")
109
-
110
- # When running locally in debug mode, this will start a Gradio server.
111
  if __name__ == "__main__":
112
  demo.launch()
 
1
  # app.py
2
  """
3
+ Complaint Prioritization API (no category input).
4
+ Inputs: text, complaints, upvotes
5
+ Outputs: score + label
6
  """
7
 
8
+ from typing import Dict, Any
9
  import gradio as gr
10
 
 
 
 
 
 
 
 
 
 
 
11
  # Helper to convert 0–1 score into labels
12
  def get_priority_label(score: float) -> str:
13
  if score >= 0.75:
 
28
  return weighted
29
 
30
  # Handle a single complaint
31
+ def handle_complaint(text: str, complaints: int, upvotes: int) -> Dict[str, Any]:
 
32
  weighted_score = calculate_weighted_score(upvotes, complaints)
 
 
33
  return {
34
  "text": text,
35
+ "complaints": complaints,
36
+ "upvotes": upvotes,
37
+ "final_score": round(weighted_score, 2),
38
+ "final_label": get_priority_label(weighted_score)
 
39
  }
40
 
41
+ # API entrypoint for single complaint (for UI)
42
+ def predict_single(text: str, complaints: int, upvotes: int):
43
  try:
44
  complaints = int(complaints)
45
  upvotes = int(upvotes)
46
  except Exception:
47
  return {"error": "complaints and upvotes must be integers"}
48
+ return handle_complaint(text, complaints, upvotes)
49
 
50
  # API entrypoint for batch complaints (JSON text input)
51
  def predict_batch(json_string: str):
52
  """
53
+ Accepts a JSON array string (list of dicts with keys: text, complaints, upvotes)
54
  Example:
55
  [
56
+ {"text":"Pothole on main road","complaints":2,"upvotes":5},
57
+ {"text":"Water leakage","complaints":15,"upvotes":8}
58
  ]
59
  """
60
  import json
 
68
  results = []
69
  for it in items:
70
  t = it.get("text", "")
 
71
  complaints = int(it.get("complaints", 0))
72
  upvotes = int(it.get("upvotes", 0))
73
+ results.append(handle_complaint(t, complaints, upvotes))
74
  return {"results": results}
75
 
76
+ # Small UI using Gradio
77
  with gr.Blocks() as demo:
78
+ gr.Markdown("## Complaint Prioritization API (No Category Input)")
79
  with gr.Tab("Single complaint"):
80
  txt = gr.Textbox(label="Complaint text", value="Huge pothole on main road, damaging cars daily.")
 
81
  comp = gr.Number(label="Number of complaints", value=2, precision=0)
82
  upv = gr.Number(label="Number of upvotes", value=5, precision=0)
83
  out = gr.JSON(label="Result")
84
  btn = gr.Button("Predict")
85
+ btn.click(fn=predict_single, inputs=[txt, comp, upv], outputs=out)
86
 
87
  with gr.Tab("Batch (JSON array)"):
88
+ batch_in = gr.Textbox(
89
+ label="JSON array of complaints",
90
+ lines=10,
91
+ value='[{"text":"Leak near market","complaints":15,"upvotes":8}]'
92
+ )
93
  batch_out = gr.JSON(label="Batch results")
94
  batch_btn = gr.Button("Predict batch")
95
  batch_btn.click(fn=predict_batch, inputs=batch_in, outputs=batch_out)
96
 
 
 
 
97
  if __name__ == "__main__":
98
  demo.launch()