Spaces:
Running
on
Zero
Running
on
Zero
Add manual clean up of the resources
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import pandas as pd
|
|
| 4 |
import os
|
| 5 |
import threading
|
| 6 |
import time
|
| 7 |
-
from pathlib import Path
|
| 8 |
from utils.data_loader import get_random_example
|
| 9 |
from utils.models import generate_summaries, model_names
|
| 10 |
from utils.ui_helpers import toggle_context_display, update_feedback, get_context_html
|
|
@@ -97,14 +96,13 @@ def process_generation_result(result):
|
|
| 97 |
"", "", "", "", None, [], False, load_leaderboard_data(),
|
| 98 |
gr.update(value="Generation was interrupted or failed. Please try again."),
|
| 99 |
gr.update(value="Generation was interrupted or failed. Please try again."),
|
| 100 |
-
|
| 101 |
-
gr.update(interactive=
|
| 102 |
-
gr.update(interactive=
|
| 103 |
-
gr.update(interactive=
|
| 104 |
-
gr.update(interactive=False, elem_classes=["vote-button", "vote-button-neither"]),
|
| 105 |
gr.update(choices=[], value=[], interactive=False, visible=False),
|
| 106 |
gr.update(visible=False),
|
| 107 |
-
gr.update(interactive=
|
| 108 |
gr.update(visible=False),
|
| 109 |
gr.update(interactive=True),
|
| 110 |
gr.update(elem_classes=[])
|
|
@@ -118,7 +116,6 @@ def process_generation_result(result):
|
|
| 118 |
None, [], False, agg_results,
|
| 119 |
gr.update(value=result["summary_a"]),
|
| 120 |
gr.update(value=result["summary_b"]),
|
| 121 |
-
# Enable voting buttons only when both summaries are ready
|
| 122 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 123 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 124 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
|
@@ -181,7 +178,6 @@ def show_loading_state():
|
|
| 181 |
return [
|
| 182 |
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 183 |
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 184 |
-
# Disable voting buttons during loading
|
| 185 |
gr.update(interactive=False),
|
| 186 |
gr.update(interactive=False),
|
| 187 |
gr.update(interactive=False),
|
|
@@ -203,6 +199,12 @@ def update_ui_for_new_context(example):
|
|
| 203 |
False
|
| 204 |
]
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
# Create Gradio interface
|
| 207 |
with gr.Blocks(theme=gr.themes.Default(
|
| 208 |
primary_hue=gr.themes.colors.orange,
|
|
@@ -210,10 +212,25 @@ with gr.Blocks(theme=gr.themes.Default(
|
|
| 210 |
)) as demo:
|
| 211 |
# Load CSS
|
| 212 |
css_path = os.path.join(os.getcwd(), 'static', 'styles.css')
|
|
|
|
|
|
|
| 213 |
with open(css_path, 'r') as f:
|
| 214 |
css_content = f.read()
|
| 215 |
|
|
|
|
| 216 |
gr.HTML(f"<style>{css_content}</style>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
# State Variables
|
| 219 |
current_example = gr.State({})
|
|
@@ -292,11 +309,10 @@ with gr.Blocks(theme=gr.themes.Default(
|
|
| 292 |
# Voting section
|
| 293 |
gr.Markdown("### 🏅 Cast Your Vote", elem_classes="section-heading")
|
| 294 |
with gr.Row():
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
vote_button_neither = gr.Button("❌ Neither is Good", elem_classes=["vote-button", "vote-button-neither"], interactive=False)
|
| 300 |
|
| 301 |
# Feedback and Submit sections
|
| 302 |
with gr.Group(elem_classes=["feedback-section"], visible=False) as feedback_section:
|
|
@@ -340,6 +356,7 @@ The Elo rating system provides a more accurate ranking than simple win rates:
|
|
| 340 |
|
| 341 |
results_table_display = gr.HTML(label="Model Performance")
|
| 342 |
|
|
|
|
| 343 |
# Toggle context display
|
| 344 |
context_toggle_btn.click(
|
| 345 |
fn=toggle_context_display,
|
|
@@ -434,6 +451,9 @@ The Elo rating system provides a more accurate ranking than simple win rates:
|
|
| 434 |
outputs=[results_table_display],
|
| 435 |
api_name="refresh_leaderboard"
|
| 436 |
)
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
if __name__ == "__main__":
|
| 439 |
demo.launch(debug=True)
|
|
|
|
| 4 |
import os
|
| 5 |
import threading
|
| 6 |
import time
|
|
|
|
| 7 |
from utils.data_loader import get_random_example
|
| 8 |
from utils.models import generate_summaries, model_names
|
| 9 |
from utils.ui_helpers import toggle_context_display, update_feedback, get_context_html
|
|
|
|
| 96 |
"", "", "", "", None, [], False, load_leaderboard_data(),
|
| 97 |
gr.update(value="Generation was interrupted or failed. Please try again."),
|
| 98 |
gr.update(value="Generation was interrupted or failed. Please try again."),
|
| 99 |
+
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 100 |
+
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 101 |
+
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 102 |
+
gr.update(interactive=True, elem_classes=["vote-button", "vote-button-neither"]),
|
|
|
|
| 103 |
gr.update(choices=[], value=[], interactive=False, visible=False),
|
| 104 |
gr.update(visible=False),
|
| 105 |
+
gr.update(interactive=False, visible=True),
|
| 106 |
gr.update(visible=False),
|
| 107 |
gr.update(interactive=True),
|
| 108 |
gr.update(elem_classes=[])
|
|
|
|
| 116 |
None, [], False, agg_results,
|
| 117 |
gr.update(value=result["summary_a"]),
|
| 118 |
gr.update(value=result["summary_b"]),
|
|
|
|
| 119 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 120 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
| 121 |
gr.update(interactive=True, elem_classes=["vote-button"]),
|
|
|
|
| 178 |
return [
|
| 179 |
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 180 |
gr.update(value="Loading new question and summaries...", interactive=False),
|
|
|
|
| 181 |
gr.update(interactive=False),
|
| 182 |
gr.update(interactive=False),
|
| 183 |
gr.update(interactive=False),
|
|
|
|
| 199 |
False
|
| 200 |
]
|
| 201 |
|
| 202 |
+
# Resource cleanup function for unload event
|
| 203 |
+
def cleanup_on_disconnect():
|
| 204 |
+
"""Clean up resources when browser disconnects"""
|
| 205 |
+
print(f"Browser disconnected. Cleaning up resources...")
|
| 206 |
+
generation_interrupt.set()
|
| 207 |
+
|
| 208 |
# Create Gradio interface
|
| 209 |
with gr.Blocks(theme=gr.themes.Default(
|
| 210 |
primary_hue=gr.themes.colors.orange,
|
|
|
|
| 212 |
)) as demo:
|
| 213 |
# Load CSS
|
| 214 |
css_path = os.path.join(os.getcwd(), 'static', 'styles.css')
|
| 215 |
+
|
| 216 |
+
# Load the files
|
| 217 |
with open(css_path, 'r') as f:
|
| 218 |
css_content = f.read()
|
| 219 |
|
| 220 |
+
# Create HTML components with CSS and JavaScript links
|
| 221 |
gr.HTML(f"<style>{css_content}</style>")
|
| 222 |
+
|
| 223 |
+
# Add JavaScript to handle browser unload events
|
| 224 |
+
unload_js = """
|
| 225 |
+
<script>
|
| 226 |
+
// This runs when the page is about to be closed or refreshed
|
| 227 |
+
window.addEventListener('beforeunload', function(e) {
|
| 228 |
+
// Send a synchronous request to the server
|
| 229 |
+
navigator.sendBeacon('/cleanup?session_id=' + window.gradioClientState.session_hash);
|
| 230 |
+
});
|
| 231 |
+
</script>
|
| 232 |
+
"""
|
| 233 |
+
gr.HTML(unload_js)
|
| 234 |
|
| 235 |
# State Variables
|
| 236 |
current_example = gr.State({})
|
|
|
|
| 309 |
# Voting section
|
| 310 |
gr.Markdown("### 🏅 Cast Your Vote", elem_classes="section-heading")
|
| 311 |
with gr.Row():
|
| 312 |
+
vote_button_a = gr.Button("⬅️ Summary A is Better", elem_classes=["vote-button"])
|
| 313 |
+
vote_button_tie = gr.Button("🤝 Tie / Equally Good", elem_classes=["vote-button"])
|
| 314 |
+
vote_button_b = gr.Button("➡️ Summary B is Better", elem_classes=["vote-button"])
|
| 315 |
+
vote_button_neither = gr.Button("❌ Neither is Good", elem_classes=["vote-button", "vote-button-neither"])
|
|
|
|
| 316 |
|
| 317 |
# Feedback and Submit sections
|
| 318 |
with gr.Group(elem_classes=["feedback-section"], visible=False) as feedback_section:
|
|
|
|
| 356 |
|
| 357 |
results_table_display = gr.HTML(label="Model Performance")
|
| 358 |
|
| 359 |
+
# Event handling
|
| 360 |
# Toggle context display
|
| 361 |
context_toggle_btn.click(
|
| 362 |
fn=toggle_context_display,
|
|
|
|
| 451 |
outputs=[results_table_display],
|
| 452 |
api_name="refresh_leaderboard"
|
| 453 |
)
|
| 454 |
+
|
| 455 |
+
# Register unload event for browser disconnections
|
| 456 |
+
demo.unload(cleanup_on_disconnect)
|
| 457 |
|
| 458 |
if __name__ == "__main__":
|
| 459 |
demo.launch(debug=True)
|