Spaces:
Running
on
Zero
Running
on
Zero
updated interruption handling for the "try a new question" + user experience improvement (#16)
Browse files- updated interruption handling for the "try a new question" + user experience improvement (0bcf8d6e41b22949a90b8c80327678ce5e5345f8)
Co-authored-by: Kai <kai-aizip@users.noreply.huggingface.co>
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import random
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import threading
|
|
|
|
| 6 |
from utils.data_loader import get_random_example
|
| 7 |
from utils.models import generate_summaries, model_names
|
| 8 |
from utils.ui_helpers import toggle_context_display, update_feedback, get_context_html
|
|
@@ -161,6 +162,33 @@ def handle_vote_submission(example, m_a, m_b, winner, feedback, summary_a, summa
|
|
| 161 |
# Update Elo ratings and get UI updates
|
| 162 |
return submit_vote_with_elo(m_a, m_b, winner, feedback, current_results)
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# Create Gradio interface
|
| 165 |
with gr.Blocks(theme=gr.themes.Default(
|
| 166 |
primary_hue=gr.themes.colors.orange,
|
|
@@ -168,9 +196,22 @@ with gr.Blocks(theme=gr.themes.Default(
|
|
| 168 |
)) as demo:
|
| 169 |
# Load CSS
|
| 170 |
css_path = os.path.join(os.getcwd(), 'static', 'styles.css')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
with open(css_path, 'r') as f:
|
| 172 |
css_content = f.read()
|
|
|
|
|
|
|
| 173 |
gr.HTML(f"<style>{css_content}</style>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
# State Variables
|
| 176 |
current_example = gr.State({})
|
|
@@ -204,10 +245,9 @@ with gr.Blocks(theme=gr.themes.Default(
|
|
| 204 |
|
| 205 |
with gr.Row(elem_id="query-container"):
|
| 206 |
with gr.Row(elem_classes="query-box-row"):
|
| 207 |
-
query_display = gr.Markdown(value="Loading question...", elem_classes="query-text")
|
| 208 |
random_question_btn = gr.Button("🔄 Try a New Question", elem_classes="query-button")
|
| 209 |
|
| 210 |
-
|
| 211 |
# Context description and display
|
| 212 |
context_description = gr.Markdown("", elem_classes="context-description")
|
| 213 |
|
|
@@ -222,14 +262,28 @@ with gr.Blocks(theme=gr.themes.Default(
|
|
| 222 |
gr.Markdown("---")
|
| 223 |
gr.Markdown("### 🔍 Compare Answers from Models", elem_classes="section-heading")
|
| 224 |
|
| 225 |
-
# Model summaries
|
| 226 |
-
with gr.Row():
|
| 227 |
with gr.Column(scale=1):
|
| 228 |
with gr.Group(elem_classes=["summary-card", "summary-card-a"]):
|
| 229 |
-
summary_a_display = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
with gr.Column(scale=1):
|
| 231 |
with gr.Group(elem_classes=["summary-card", "summary-card-b"]):
|
| 232 |
-
summary_b_display = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
gr.HTML("<hr>")
|
| 235 |
|
|
@@ -283,31 +337,6 @@ The Elo rating system provides a more accurate ranking than simple win rates:
|
|
| 283 |
|
| 284 |
results_table_display = gr.HTML(label="Model Performance")
|
| 285 |
|
| 286 |
-
# Generic function to handle starting a new example
|
| 287 |
-
def handle_new_example_click():
|
| 288 |
-
generation_interrupt.set() # Interrupt any ongoing generation
|
| 289 |
-
return load_context()[0]
|
| 290 |
-
|
| 291 |
-
def update_ui_for_new_context(example):
|
| 292 |
-
return [
|
| 293 |
-
gr.update(value=example['question']),
|
| 294 |
-
gr.update(value=example.get('processed_context_desc', ''), visible=bool(example.get('processed_context_desc', ''))),
|
| 295 |
-
gr.update(value=get_context_html(example, False)),
|
| 296 |
-
gr.update(value="Show Full Context", elem_classes=["context-toggle-button"]),
|
| 297 |
-
False
|
| 298 |
-
]
|
| 299 |
-
|
| 300 |
-
# Add this new function to show a loading state
|
| 301 |
-
def show_loading_state():
|
| 302 |
-
return [
|
| 303 |
-
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 304 |
-
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 305 |
-
gr.update(interactive=False),
|
| 306 |
-
gr.update(interactive=False),
|
| 307 |
-
gr.update(interactive=False),
|
| 308 |
-
gr.update(interactive=False)
|
| 309 |
-
]
|
| 310 |
-
|
| 311 |
# Event handling
|
| 312 |
# Toggle context display
|
| 313 |
context_toggle_btn.click(
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import threading
|
| 6 |
+
import time # Added for sleep
|
| 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
|
|
|
|
| 162 |
# Update Elo ratings and get UI updates
|
| 163 |
return submit_vote_with_elo(m_a, m_b, winner, feedback, current_results)
|
| 164 |
|
| 165 |
+
def show_loading_state():
|
| 166 |
+
"""Show loading state while fetching new content"""
|
| 167 |
+
return [
|
| 168 |
+
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 169 |
+
gr.update(value="Loading new question and summaries...", interactive=False),
|
| 170 |
+
gr.update(interactive=False),
|
| 171 |
+
gr.update(interactive=False),
|
| 172 |
+
gr.update(interactive=False),
|
| 173 |
+
gr.update(interactive=False)
|
| 174 |
+
]
|
| 175 |
+
|
| 176 |
+
def handle_new_example_click():
|
| 177 |
+
"""Handle clicking 'Get new example' button"""
|
| 178 |
+
generation_interrupt.set() # Interrupt any ongoing generation
|
| 179 |
+
time.sleep(0.2) # Added delay to allow threads to detect the interrupt
|
| 180 |
+
return load_context()[0]
|
| 181 |
+
|
| 182 |
+
def update_ui_for_new_context(example):
|
| 183 |
+
"""Update UI with new context information"""
|
| 184 |
+
return [
|
| 185 |
+
gr.update(value=example['question']),
|
| 186 |
+
gr.update(value=example.get('processed_context_desc', ''), visible=bool(example.get('processed_context_desc', ''))),
|
| 187 |
+
gr.update(value=get_context_html(example, False)),
|
| 188 |
+
gr.update(value="Show Full Context", elem_classes=["context-toggle-button"]),
|
| 189 |
+
False
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
# Create Gradio interface
|
| 193 |
with gr.Blocks(theme=gr.themes.Default(
|
| 194 |
primary_hue=gr.themes.colors.orange,
|
|
|
|
| 196 |
)) as demo:
|
| 197 |
# Load CSS
|
| 198 |
css_path = os.path.join(os.getcwd(), 'static', 'styles.css')
|
| 199 |
+
|
| 200 |
+
# Make sure the JavaScript directory exists
|
| 201 |
+
os.makedirs(os.path.join(os.getcwd(), 'static', 'js'), exist_ok=True)
|
| 202 |
+
|
| 203 |
+
# Load the files
|
| 204 |
with open(css_path, 'r') as f:
|
| 205 |
css_content = f.read()
|
| 206 |
+
|
| 207 |
+
# Create HTML components with CSS and JavaScript links
|
| 208 |
gr.HTML(f"<style>{css_content}</style>")
|
| 209 |
+
|
| 210 |
+
# Load JavaScript file via script tag
|
| 211 |
+
js_path = os.path.join(os.getcwd(), 'static', 'js', 'scroll_helpers.js')
|
| 212 |
+
# Use relative path for the script source
|
| 213 |
+
js_path_relative = 'static/js/scroll_helpers.js'
|
| 214 |
+
gr.HTML(f'<script src="{js_path_relative}"></script>')
|
| 215 |
|
| 216 |
# State Variables
|
| 217 |
current_example = gr.State({})
|
|
|
|
| 245 |
|
| 246 |
with gr.Row(elem_id="query-container"):
|
| 247 |
with gr.Row(elem_classes="query-box-row"):
|
| 248 |
+
query_display = gr.Markdown(value="Loading question...", elem_classes="query-text", elem_id="query-section")
|
| 249 |
random_question_btn = gr.Button("🔄 Try a New Question", elem_classes="query-button")
|
| 250 |
|
|
|
|
| 251 |
# Context description and display
|
| 252 |
context_description = gr.Markdown("", elem_classes="context-description")
|
| 253 |
|
|
|
|
| 262 |
gr.Markdown("---")
|
| 263 |
gr.Markdown("### 🔍 Compare Answers from Models", elem_classes="section-heading")
|
| 264 |
|
| 265 |
+
# Model summaries - Add ID for JavaScript to target and disable autoscroll
|
| 266 |
+
with gr.Row(elem_id="summary-containers"):
|
| 267 |
with gr.Column(scale=1):
|
| 268 |
with gr.Group(elem_classes=["summary-card", "summary-card-a"]):
|
| 269 |
+
summary_a_display = gr.Textbox(
|
| 270 |
+
label="Model A",
|
| 271 |
+
lines=10,
|
| 272 |
+
interactive=False,
|
| 273 |
+
show_copy_button=True,
|
| 274 |
+
autoscroll=False, # Disable auto-scrolling
|
| 275 |
+
elem_id="summary-a-display"
|
| 276 |
+
)
|
| 277 |
with gr.Column(scale=1):
|
| 278 |
with gr.Group(elem_classes=["summary-card", "summary-card-b"]):
|
| 279 |
+
summary_b_display = gr.Textbox(
|
| 280 |
+
label="Model B",
|
| 281 |
+
lines=10,
|
| 282 |
+
interactive=False,
|
| 283 |
+
show_copy_button=True,
|
| 284 |
+
autoscroll=False, # Disable auto-scrolling
|
| 285 |
+
elem_id="summary-b-display"
|
| 286 |
+
)
|
| 287 |
|
| 288 |
gr.HTML("<hr>")
|
| 289 |
|
|
|
|
| 337 |
|
| 338 |
results_table_display = gr.HTML(label="Model Performance")
|
| 339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
# Event handling
|
| 341 |
# Toggle context display
|
| 342 |
context_toggle_btn.click(
|