Spaces:
Sleeping
Sleeping
Joash
commited on
Commit
·
faf71f6
1
Parent(s):
defa041
Update interface to handle history and metrics updates
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ CACHE_DIR = "/home/user/.cache/huggingface"
|
|
| 26 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 27 |
|
| 28 |
# History file
|
| 29 |
-
HISTORY_FILE = "review_history.json"
|
| 30 |
|
| 31 |
class Review:
|
| 32 |
def __init__(self, code: str, language: str, suggestions: str):
|
|
@@ -289,28 +289,39 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 289 |
)
|
| 290 |
|
| 291 |
with gr.Tab("History"):
|
| 292 |
-
refresh_history = gr.Button("Refresh History")
|
| 293 |
history_output = gr.Textbox(
|
| 294 |
label="Review History",
|
| 295 |
-
lines=20
|
|
|
|
| 296 |
)
|
| 297 |
|
| 298 |
with gr.Tab("Metrics"):
|
| 299 |
-
refresh_metrics = gr.Button("Refresh Metrics")
|
| 300 |
metrics_output = gr.JSON(
|
| 301 |
-
label="Performance Metrics"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
@spaces.GPU
|
| 305 |
-
def review_code_interface(code: str, language: str) ->
|
| 306 |
if not code.strip():
|
| 307 |
-
return "Please enter some code to review."
|
| 308 |
try:
|
| 309 |
reviewer.ensure_initialized()
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
except Exception as e:
|
| 312 |
logger.error(f"Interface error: {e}")
|
| 313 |
-
return f"Error: {str(e)}"
|
| 314 |
|
| 315 |
def get_history_interface() -> str:
|
| 316 |
try:
|
|
@@ -337,20 +348,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 337 |
logger.error(f"Metrics error: {e}")
|
| 338 |
return {"error": str(e)}
|
| 339 |
|
|
|
|
| 340 |
submit_btn.click(
|
| 341 |
review_code_interface,
|
| 342 |
inputs=[code_input, language_input],
|
| 343 |
-
outputs=output
|
| 344 |
-
)
|
| 345 |
-
refresh_history.click(
|
| 346 |
-
get_history_interface,
|
| 347 |
-
outputs=history_output
|
| 348 |
-
)
|
| 349 |
-
refresh_metrics.click(
|
| 350 |
-
get_metrics_interface,
|
| 351 |
-
outputs=metrics_output
|
| 352 |
)
|
| 353 |
|
|
|
|
| 354 |
gr.Examples(
|
| 355 |
examples=[
|
| 356 |
["""def add_numbers(a, b):
|
|
@@ -366,6 +371,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 366 |
inputs=[code_input, language_input]
|
| 367 |
)
|
| 368 |
|
|
|
|
| 369 |
if __name__ == "__main__":
|
| 370 |
iface.launch(
|
| 371 |
server_name="0.0.0.0",
|
|
|
|
| 26 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 27 |
|
| 28 |
# History file
|
| 29 |
+
HISTORY_FILE = "/home/user/review_history.json"
|
| 30 |
|
| 31 |
class Review:
|
| 32 |
def __init__(self, code: str, language: str, suggestions: str):
|
|
|
|
| 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:
|
|
|
|
| 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
|
| 359 |
gr.Examples(
|
| 360 |
examples=[
|
| 361 |
["""def add_numbers(a, b):
|
|
|
|
| 371 |
inputs=[code_input, language_input]
|
| 372 |
)
|
| 373 |
|
| 374 |
+
# Launch the app
|
| 375 |
if __name__ == "__main__":
|
| 376 |
iface.launch(
|
| 377 |
server_name="0.0.0.0",
|