Update app.py
Browse files
app.py
CHANGED
|
@@ -191,16 +191,40 @@ def run_and_submit_all(request: gr.Request):
|
|
| 191 |
Fetches all questions, runs the agent on them, submits all answers,
|
| 192 |
and displays the results.
|
| 193 |
"""
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
logging.info(f"User logged in: {username}")
|
| 204 |
|
| 205 |
api_url = DEFAULT_API_URL
|
| 206 |
|
|
@@ -234,7 +258,7 @@ def run_and_submit_all(request: gr.Request):
|
|
| 234 |
agent.setup()
|
| 235 |
|
| 236 |
# Initial yield for Gradio progress display
|
| 237 |
-
yield "π Initializing and fetching questions...", pd.DataFrame([])
|
| 238 |
|
| 239 |
# 2. Fetch Questions
|
| 240 |
questions_url = f"{api_url}/questions"
|
|
@@ -249,6 +273,15 @@ def run_and_submit_all(request: gr.Request):
|
|
| 249 |
return
|
| 250 |
|
| 251 |
logging.info(f"Fetched {len(questions_data)} questions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
# 3. Process each question
|
| 254 |
answers_payload = []
|
|
@@ -265,8 +298,10 @@ def run_and_submit_all(request: gr.Request):
|
|
| 265 |
logging.warning(f"Skipping item with missing task_id: {item}")
|
| 266 |
results_log.append({
|
| 267 |
"Task ID": "MISSING",
|
| 268 |
-
"Question": "ERROR
|
|
|
|
| 269 |
"Submitted Answer": "ERROR",
|
|
|
|
| 270 |
"Status": "ERROR"
|
| 271 |
})
|
| 272 |
continue
|
|
@@ -291,8 +326,10 @@ def run_and_submit_all(request: gr.Request):
|
|
| 291 |
|
| 292 |
results_log.append({
|
| 293 |
"Task ID": task_id,
|
| 294 |
-
"Question":
|
| 295 |
-
"
|
|
|
|
|
|
|
| 296 |
"Status": status
|
| 297 |
})
|
| 298 |
|
|
@@ -309,16 +346,25 @@ def run_and_submit_all(request: gr.Request):
|
|
| 309 |
|
| 310 |
results_log.append({
|
| 311 |
"Task ID": task_id,
|
| 312 |
-
"Question":
|
| 313 |
-
"
|
|
|
|
|
|
|
| 314 |
"Status": status
|
| 315 |
})
|
| 316 |
|
| 317 |
-
# Yield progressive updates to Gradio UI
|
| 318 |
-
|
| 319 |
-
f"Processing
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
)
|
|
|
|
|
|
|
| 322 |
|
| 323 |
if not answers_payload:
|
| 324 |
logging.info("Agent did not produce any answers to submit.")
|
|
@@ -326,6 +372,12 @@ def run_and_submit_all(request: gr.Request):
|
|
| 326 |
return
|
| 327 |
|
| 328 |
# 4. Submit answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
submission_data = {
|
| 330 |
"username": username.strip(),
|
| 331 |
"agent_code": agent_code,
|
|
@@ -340,11 +392,12 @@ def run_and_submit_all(request: gr.Request):
|
|
| 340 |
result_data = response.json()
|
| 341 |
|
| 342 |
final_status = (
|
| 343 |
-
f"Submission Successful!\n"
|
| 344 |
-
f"User: {result_data.get('username')}\n"
|
| 345 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 346 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 347 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
|
|
|
| 348 |
)
|
| 349 |
logging.info("Submission successful.")
|
| 350 |
|
|
@@ -396,9 +449,19 @@ with gr.Blocks() as demo:
|
|
| 396 |
)
|
| 397 |
|
| 398 |
gr.LoginButton() # Add the login button back
|
| 399 |
-
run_button = gr.Button("π Run Evaluation & Submit All Answers")
|
| 400 |
-
status_output = gr.Textbox(
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
run_button.click(
|
| 404 |
fn=run_and_submit_all,
|
|
|
|
| 191 |
Fetches all questions, runs the agent on them, submits all answers,
|
| 192 |
and displays the results.
|
| 193 |
"""
|
| 194 |
+
# Try multiple ways to get the user info
|
| 195 |
+
username = None
|
| 196 |
|
| 197 |
+
# Method 1: Check for oauth_profile
|
| 198 |
+
if hasattr(request, 'oauth_profile') and request.oauth_profile:
|
| 199 |
+
username = request.oauth_profile.username
|
| 200 |
+
logging.info(f"Found username via oauth_profile: {username}")
|
| 201 |
+
|
| 202 |
+
# Method 2: Check request headers for user info
|
| 203 |
+
elif hasattr(request, 'headers'):
|
| 204 |
+
# Look for common auth headers
|
| 205 |
+
for header_name in ['x-user', 'authorization', 'user']:
|
| 206 |
+
if header_name in request.headers:
|
| 207 |
+
logging.info(f"Found header {header_name}: {request.headers[header_name]}")
|
| 208 |
+
|
| 209 |
+
# Method 3: Try to extract from request attributes
|
| 210 |
+
if not username:
|
| 211 |
+
logging.info("Checking request attributes...")
|
| 212 |
+
for attr in dir(request):
|
| 213 |
+
if 'user' in attr.lower():
|
| 214 |
+
logging.info(f"Found request attribute: {attr} = {getattr(request, attr, 'N/A')}")
|
| 215 |
+
|
| 216 |
+
# For now, let's use a fallback username based on the space owner
|
| 217 |
+
if not username:
|
| 218 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 219 |
+
if "/" in space_id:
|
| 220 |
+
username = space_id.split("/")[0] # Extract username from space_id
|
| 221 |
+
logging.info(f"Using fallback username from SPACE_ID: {username}")
|
| 222 |
+
else:
|
| 223 |
+
logging.info("No authentication detected and no fallback available.")
|
| 224 |
+
yield "β Unable to detect logged-in user. Please make sure you're logged in to Hugging Face.", pd.DataFrame([])
|
| 225 |
+
return
|
| 226 |
|
| 227 |
+
logging.info(f"Processing with username: {username}")
|
|
|
|
| 228 |
|
| 229 |
api_url = DEFAULT_API_URL
|
| 230 |
|
|
|
|
| 258 |
agent.setup()
|
| 259 |
|
| 260 |
# Initial yield for Gradio progress display
|
| 261 |
+
yield "π Initializing agent and fetching questions...\nπ‘ Connecting to API...", pd.DataFrame([])
|
| 262 |
|
| 263 |
# 2. Fetch Questions
|
| 264 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 273 |
return
|
| 274 |
|
| 275 |
logging.info(f"Fetched {len(questions_data)} questions.")
|
| 276 |
+
|
| 277 |
+
# Show questions fetched
|
| 278 |
+
yield (
|
| 279 |
+
f"β
Successfully fetched {len(questions_data)} questions!\n"
|
| 280 |
+
f"π€ Starting agent evaluation...\n"
|
| 281 |
+
f"β³ This may take several minutes...\n"
|
| 282 |
+
f"{'='*50}",
|
| 283 |
+
pd.DataFrame([])
|
| 284 |
+
)
|
| 285 |
|
| 286 |
# 3. Process each question
|
| 287 |
answers_payload = []
|
|
|
|
| 298 |
logging.warning(f"Skipping item with missing task_id: {item}")
|
| 299 |
results_log.append({
|
| 300 |
"Task ID": "MISSING",
|
| 301 |
+
"Question Type": "ERROR",
|
| 302 |
+
"Question Preview": "ERROR: Missing task_id",
|
| 303 |
"Submitted Answer": "ERROR",
|
| 304 |
+
"Processing Time (s)": "N/A",
|
| 305 |
"Status": "ERROR"
|
| 306 |
})
|
| 307 |
continue
|
|
|
|
| 326 |
|
| 327 |
results_log.append({
|
| 328 |
"Task ID": task_id,
|
| 329 |
+
"Question Type": question_type,
|
| 330 |
+
"Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
|
| 331 |
+
"Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
|
| 332 |
+
"Processing Time (s)": f"{processing_time:.2f}",
|
| 333 |
"Status": status
|
| 334 |
})
|
| 335 |
|
|
|
|
| 346 |
|
| 347 |
results_log.append({
|
| 348 |
"Task ID": task_id,
|
| 349 |
+
"Question Type": question_type,
|
| 350 |
+
"Question Preview": question_preview[:80] + "..." if len(question_preview) > 80 else question_preview,
|
| 351 |
+
"Submitted Answer": submitted_answer[:100] + "..." if len(submitted_answer) > 100 else submitted_answer,
|
| 352 |
+
"Processing Time (s)": "ERROR",
|
| 353 |
"Status": status
|
| 354 |
})
|
| 355 |
|
| 356 |
+
# Yield progressive updates to Gradio UI with better formatting
|
| 357 |
+
progress_text = (
|
| 358 |
+
f"π Processing Question {i}/{len(questions_data)}\n"
|
| 359 |
+
f"π Question Type: {question_type}\n"
|
| 360 |
+
f"β Question Preview: {question_preview[:100]}{'...' if len(question_preview) > 100 else ''}\n"
|
| 361 |
+
f"π Agent Answer: {submitted_answer[:150]}{'...' if len(submitted_answer) > 150 else ''}\n"
|
| 362 |
+
f"β±οΈ Processing Time: {processing_time:.2f}s\n"
|
| 363 |
+
f"β
Status: {status}\n"
|
| 364 |
+
f"{'='*60}"
|
| 365 |
)
|
| 366 |
+
|
| 367 |
+
yield (progress_text, pd.DataFrame(results_log))
|
| 368 |
|
| 369 |
if not answers_payload:
|
| 370 |
logging.info("Agent did not produce any answers to submit.")
|
|
|
|
| 372 |
return
|
| 373 |
|
| 374 |
# 4. Submit answers
|
| 375 |
+
yield (
|
| 376 |
+
f"π€ Submitting {len(answers_payload)} answers to the evaluation server...\n"
|
| 377 |
+
f"β³ Please wait for scoring...",
|
| 378 |
+
pd.DataFrame(results_log)
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
submission_data = {
|
| 382 |
"username": username.strip(),
|
| 383 |
"agent_code": agent_code,
|
|
|
|
| 392 |
result_data = response.json()
|
| 393 |
|
| 394 |
final_status = (
|
| 395 |
+
f"π Submission Successful!\n"
|
| 396 |
+
f"π€ User: {result_data.get('username')}\n"
|
| 397 |
+
f"π Overall Score: {result_data.get('score', 'N/A')}% "
|
| 398 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 399 |
+
f"π¬ Message: {result_data.get('message', 'No message received.')}\n"
|
| 400 |
+
f"β° Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 401 |
)
|
| 402 |
logging.info("Submission successful.")
|
| 403 |
|
|
|
|
| 449 |
)
|
| 450 |
|
| 451 |
gr.LoginButton() # Add the login button back
|
| 452 |
+
run_button = gr.Button("π Run Evaluation & Submit All Answers", size="lg")
|
| 453 |
+
status_output = gr.Textbox(
|
| 454 |
+
label="π Real-time Progress & Status",
|
| 455 |
+
lines=8,
|
| 456 |
+
interactive=False,
|
| 457 |
+
show_copy_button=True
|
| 458 |
+
)
|
| 459 |
+
results_table = gr.DataFrame(
|
| 460 |
+
label="π Questions, Answers & Results",
|
| 461 |
+
wrap=True,
|
| 462 |
+
height=400,
|
| 463 |
+
interactive=False
|
| 464 |
+
)
|
| 465 |
|
| 466 |
run_button.click(
|
| 467 |
fn=run_and_submit_all,
|