Joash commited on
Commit
090ffc1
·
1 Parent(s): faf71f6

Add refresh functionality and auto-updates for history and metrics

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -281,7 +281,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
281
  value="python",
282
  label="Language"
283
  )
284
- submit_btn = gr.Button("Submit for Review")
285
  with gr.Column():
286
  output = gr.Textbox(
287
  label="Review Results",
@@ -289,39 +289,32 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
289
  )
290
 
291
  with gr.Tab("History"):
 
 
292
  history_output = gr.Textbox(
293
  label="Review History",
294
  lines=20,
295
- value="No reviews yet."
296
  )
297
 
298
  with gr.Tab("Metrics"):
 
 
299
  metrics_output = gr.JSON(
300
- label="Performance Metrics",
301
- value={
302
- 'Total Reviews': 0,
303
- 'Average Response Time': '0.00s',
304
- 'Reviews Today': 0,
305
- 'Device': 'Not initialized'
306
- }
307
  )
308
 
309
  @spaces.GPU
310
- def review_code_interface(code: str, language: str) -> tuple:
311
  if not code.strip():
312
- return "Please enter some code to review.", None, None
313
  try:
314
  reviewer.ensure_initialized()
315
  result = reviewer.review_code(code, language)
316
-
317
- # Update history and metrics immediately
318
- history = get_history_interface()
319
- metrics = get_metrics_interface()
320
-
321
- return result, history, metrics
322
  except Exception as e:
323
  logger.error(f"Interface error: {e}")
324
- return f"Error: {str(e)}", None, None
325
 
326
  def get_history_interface() -> str:
327
  try:
@@ -343,16 +336,41 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
343
 
344
  def get_metrics_interface() -> Dict:
345
  try:
346
- return reviewer.get_metrics()
 
 
 
 
 
 
 
 
347
  except Exception as e:
348
  logger.error(f"Metrics error: {e}")
349
  return {"error": str(e)}
350
 
351
- # Update all outputs when submitting code
352
  submit_btn.click(
353
  review_code_interface,
354
  inputs=[code_input, language_input],
355
- outputs=[output, history_output, metrics_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  )
357
 
358
  # Add example inputs
 
281
  value="python",
282
  label="Language"
283
  )
284
+ submit_btn = gr.Button("Submit for Review", variant="primary")
285
  with gr.Column():
286
  output = gr.Textbox(
287
  label="Review Results",
 
289
  )
290
 
291
  with gr.Tab("History"):
292
+ with gr.Row():
293
+ refresh_history = gr.Button("Refresh History", variant="secondary")
294
  history_output = gr.Textbox(
295
  label="Review History",
296
  lines=20,
297
+ value="Click 'Refresh History' to view review history"
298
  )
299
 
300
  with gr.Tab("Metrics"):
301
+ with gr.Row():
302
+ refresh_metrics = gr.Button("Refresh Metrics", variant="secondary")
303
  metrics_output = gr.JSON(
304
+ label="Performance Metrics"
 
 
 
 
 
 
305
  )
306
 
307
  @spaces.GPU
308
+ def review_code_interface(code: str, language: str) -> str:
309
  if not code.strip():
310
+ return "Please enter some code to review."
311
  try:
312
  reviewer.ensure_initialized()
313
  result = reviewer.review_code(code, language)
314
+ return result
 
 
 
 
 
315
  except Exception as e:
316
  logger.error(f"Interface error: {e}")
317
+ return f"Error: {str(e)}"
318
 
319
  def get_history_interface() -> str:
320
  try:
 
336
 
337
  def get_metrics_interface() -> Dict:
338
  try:
339
+ metrics = reviewer.get_metrics()
340
+ if not metrics:
341
+ return {
342
+ 'Total Reviews': 0,
343
+ 'Average Response Time': '0.00s',
344
+ 'Reviews Today': 0,
345
+ 'Device': str(reviewer.device) if reviewer.device else "Not initialized"
346
+ }
347
+ return metrics
348
  except Exception as e:
349
  logger.error(f"Metrics error: {e}")
350
  return {"error": str(e)}
351
 
352
+ # Set up event handlers
353
  submit_btn.click(
354
  review_code_interface,
355
  inputs=[code_input, language_input],
356
+ outputs=output
357
+ ).then(
358
+ get_history_interface,
359
+ outputs=history_output
360
+ ).then(
361
+ get_metrics_interface,
362
+ outputs=metrics_output
363
+ )
364
+
365
+ # Add refresh buttons
366
+ refresh_history.click(
367
+ get_history_interface,
368
+ outputs=history_output
369
+ )
370
+
371
+ refresh_metrics.click(
372
+ get_metrics_interface,
373
+ outputs=metrics_output
374
  )
375
 
376
  # Add example inputs