yazoniak commited on
Commit
82d8d08
·
verified ·
1 Parent(s): b6ce507

Updated app.py to include optional reviewers

Browse files
Files changed (1) hide show
  1. app.py +51 -6
app.py CHANGED
@@ -45,7 +45,10 @@ print(f"Model loaded successfully with {len(id2label)} reviewers")
45
 
46
 
47
  def predict_reviewers(
48
- pr_title: str, files_input: str, threshold: float = DEFAULT_THRESHOLD
 
 
 
49
  ) -> tuple[str, str]:
50
  """
51
  Predict reviewers for a PR based on title and modified files.
@@ -54,6 +57,7 @@ def predict_reviewers(
54
  pr_title: The PR title/description
55
  files_input: Comma or semicolon separated list of modified files
56
  threshold: Prediction threshold (0-1)
 
57
 
58
  Returns:
59
  tuple: (formatted_predictions, all_scores_json)
@@ -78,6 +82,18 @@ def predict_reviewers(
78
  if threshold < 0 or threshold > 1:
79
  return "⚠️ Threshold must be between 0 and 1", ""
80
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # Format input for the model
82
  files_text = f"files: {', '.join(files_list)}"
83
 
@@ -102,7 +118,7 @@ def predict_reviewers(
102
  all_scores = {}
103
 
104
  for idx, prob in enumerate(probabilities):
105
- reviewer_name = id2label[idx]
106
  all_scores[reviewer_name] = float(prob)
107
 
108
  if prob > threshold:
@@ -153,11 +169,17 @@ def predict_reviewers(
153
 
154
  # Example inputs
155
  examples = [
156
- ["Fix authentication bug in user service", "auth.py, user.py, test_auth.py", 0.5],
 
 
 
 
 
157
  [
158
  "Add new payment gateway integration",
159
  "gateway.py; payment_routes.py; config.py",
160
  0.5,
 
161
  ],
162
  ]
163
 
@@ -197,18 +219,41 @@ with gr.Blocks(title="PR Reviewer Assignment", theme=gr.themes.Soft()) as demo:
197
  info="Only show predictions above this confidence score",
198
  )
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  predict_btn = gr.Button("Predict Reviewers", variant="primary", size="lg")
201
 
202
  with gr.Column(scale=3):
203
  prediction_output = gr.Markdown(label="Predictions")
204
 
205
- with gr.Accordion("Detailed JSON Output", open=False):
206
  json_output = gr.JSON(label="Full Prediction Details")
207
 
208
  # Connect the button
209
  predict_btn.click(
210
  fn=predict_reviewers,
211
- inputs=[pr_title_input, files_input, threshold_input],
212
  outputs=[prediction_output, json_output],
213
  )
214
 
@@ -216,7 +261,7 @@ with gr.Blocks(title="PR Reviewer Assignment", theme=gr.themes.Soft()) as demo:
216
  gr.Markdown("### Example Inputs")
217
  gr.Examples(
218
  examples=examples,
219
- inputs=[pr_title_input, files_input, threshold_input],
220
  outputs=[prediction_output, json_output],
221
  fn=predict_reviewers,
222
  cache_examples=False,
 
45
 
46
 
47
  def predict_reviewers(
48
+ pr_title: str,
49
+ files_input: str,
50
+ threshold: float = DEFAULT_THRESHOLD,
51
+ custom_mapping: str = "",
52
  ) -> tuple[str, str]:
53
  """
54
  Predict reviewers for a PR based on title and modified files.
 
57
  pr_title: The PR title/description
58
  files_input: Comma or semicolon separated list of modified files
59
  threshold: Prediction threshold (0-1)
60
+ custom_mapping: Optional JSON mapping of label IDs to names
61
 
62
  Returns:
63
  tuple: (formatted_predictions, all_scores_json)
 
82
  if threshold < 0 or threshold > 1:
83
  return "⚠️ Threshold must be between 0 and 1", ""
84
 
85
+ # Parse custom mapping if provided
86
+ label_mapping = id2label # Default to model's labels
87
+ if custom_mapping and custom_mapping.strip():
88
+ try:
89
+ parsed_mapping = json.loads(custom_mapping)
90
+ # Convert string keys to integers
91
+ label_mapping = {int(k): v for k, v in parsed_mapping.items()}
92
+ except json.JSONDecodeError:
93
+ return "⚠️ Invalid JSON format for custom mapping", ""
94
+ except (ValueError, TypeError):
95
+ return "⚠️ Custom mapping must have numeric keys", ""
96
+
97
  # Format input for the model
98
  files_text = f"files: {', '.join(files_list)}"
99
 
 
118
  all_scores = {}
119
 
120
  for idx, prob in enumerate(probabilities):
121
+ reviewer_name = label_mapping.get(idx, f"label_{idx}")
122
  all_scores[reviewer_name] = float(prob)
123
 
124
  if prob > threshold:
 
169
 
170
  # Example inputs
171
  examples = [
172
+ [
173
+ "Fix authentication bug in user service",
174
+ "auth.py, user.py, test_auth.py",
175
+ 0.5,
176
+ "",
177
+ ],
178
  [
179
  "Add new payment gateway integration",
180
  "gateway.py; payment_routes.py; config.py",
181
  0.5,
182
+ "",
183
  ],
184
  ]
185
 
 
219
  info="Only show predictions above this confidence score",
220
  )
221
 
222
+ with gr.Accordion("Custom Label Mapping (Optional)", open=False):
223
+ gr.Markdown(
224
+ """
225
+ If your deployed model has generic labels (e.g., `label_0`, `label_1`),
226
+ you can paste your own ID to name mapping here in JSON format.
227
+
228
+ **Example format:**
229
+ ```json
230
+ {
231
+ "0": "John Doe",
232
+ "1": "Jane Smith",
233
+ "2": "Bob Johnson"
234
+ }
235
+ ```
236
+ """
237
+ )
238
+ custom_mapping_input = gr.Code(
239
+ label="Custom ID to Label Mapping (JSON)",
240
+ language="json",
241
+ lines=10,
242
+ value="",
243
+ )
244
+
245
  predict_btn = gr.Button("Predict Reviewers", variant="primary", size="lg")
246
 
247
  with gr.Column(scale=3):
248
  prediction_output = gr.Markdown(label="Predictions")
249
 
250
+ with gr.Accordion("📋 Detailed JSON Output", open=False):
251
  json_output = gr.JSON(label="Full Prediction Details")
252
 
253
  # Connect the button
254
  predict_btn.click(
255
  fn=predict_reviewers,
256
+ inputs=[pr_title_input, files_input, threshold_input, custom_mapping_input],
257
  outputs=[prediction_output, json_output],
258
  )
259
 
 
261
  gr.Markdown("### Example Inputs")
262
  gr.Examples(
263
  examples=examples,
264
+ inputs=[pr_title_input, files_input, threshold_input, custom_mapping_input],
265
  outputs=[prediction_output, json_output],
266
  fn=predict_reviewers,
267
  cache_examples=False,