Spaces:
Sleeping
Sleeping
Joash
commited on
Commit
·
24ce75d
1
Parent(s):
090ffc1
Fix history and metrics updates with proper state management
Browse files
app.py
CHANGED
|
@@ -205,13 +205,30 @@ Code:
|
|
| 205 |
logger.error(f"Decoding error: {decode_error}")
|
| 206 |
return "Error: Failed to decode model output. Please try again."
|
| 207 |
|
|
|
|
| 208 |
end_time = datetime.now()
|
| 209 |
review = Review(code, language, suggestions)
|
| 210 |
review.response_time = (end_time - start_time).total_seconds()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
self.review_history.append(review)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
if self.device and self.device.type == "cuda":
|
| 217 |
del inputs, outputs
|
|
@@ -349,20 +366,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 349 |
logger.error(f"Metrics error: {e}")
|
| 350 |
return {"error": str(e)}
|
| 351 |
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
submit_btn.click(
|
| 354 |
-
|
| 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
|
|
|
|
| 205 |
logger.error(f"Decoding error: {decode_error}")
|
| 206 |
return "Error: Failed to decode model output. Please try again."
|
| 207 |
|
| 208 |
+
# Create and save review
|
| 209 |
end_time = datetime.now()
|
| 210 |
review = Review(code, language, suggestions)
|
| 211 |
review.response_time = (end_time - start_time).total_seconds()
|
| 212 |
+
|
| 213 |
+
# Update metrics first
|
| 214 |
+
self.metrics['total_reviews'] += 1
|
| 215 |
+
total_time = self.metrics['avg_response_time'] * (self.metrics['total_reviews'] - 1)
|
| 216 |
+
total_time += review.response_time
|
| 217 |
+
self.metrics['avg_response_time'] = total_time / self.metrics['total_reviews']
|
| 218 |
+
|
| 219 |
+
today = datetime.now().date()
|
| 220 |
+
|
| 221 |
+
# Add review to history
|
| 222 |
self.review_history.append(review)
|
| 223 |
|
| 224 |
+
# Update today's reviews count
|
| 225 |
+
self.metrics['reviews_today'] = sum(
|
| 226 |
+
1 for r in self.review_history
|
| 227 |
+
if datetime.fromisoformat(r.timestamp).date() == today
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Save to file
|
| 231 |
+
self.save_history()
|
| 232 |
|
| 233 |
if self.device and self.device.type == "cuda":
|
| 234 |
del inputs, outputs
|
|
|
|
| 366 |
logger.error(f"Metrics error: {e}")
|
| 367 |
return {"error": str(e)}
|
| 368 |
|
| 369 |
+
def update_all_outputs(code: str, language: str) -> tuple:
|
| 370 |
+
"""Update all outputs after code review."""
|
| 371 |
+
result = review_code_interface(code, language)
|
| 372 |
+
history = get_history_interface()
|
| 373 |
+
metrics = get_metrics_interface()
|
| 374 |
+
return result, history, metrics
|
| 375 |
+
|
| 376 |
+
# Connect the interface
|
| 377 |
submit_btn.click(
|
| 378 |
+
update_all_outputs,
|
| 379 |
inputs=[code_input, language_input],
|
| 380 |
+
outputs=[output, history_output, metrics_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
)
|
| 382 |
|
|
|
|
| 383 |
refresh_history.click(
|
| 384 |
get_history_interface,
|
| 385 |
outputs=history_output
|