Update app.py
Browse files
app.py
CHANGED
@@ -501,7 +501,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
501 |
# 1. Instantiate Agent
|
502 |
try:
|
503 |
print("Initializing agent...")
|
504 |
-
agent = BasicAgent(api_url=api_url)
|
505 |
print("Agent initialized successfully.")
|
506 |
except Exception as e:
|
507 |
print(f"Error instantiating agent: {e}")
|
@@ -528,6 +528,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
528 |
for item in questions_data:
|
529 |
task_id = item.get("task_id")
|
530 |
if not task_id:
|
|
|
531 |
continue
|
532 |
|
533 |
try:
|
@@ -536,15 +537,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
536 |
# Handle file URL with conditional fallback to generic attachment endpoint
|
537 |
raw_url = item.get("file_url") or ""
|
538 |
if not raw_url: # field missing
|
539 |
-
raw_url =
|
540 |
file_url = raw_url # already absolute
|
541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
542 |
answer = agent(
|
543 |
-
question=
|
544 |
task_id=task_id,
|
545 |
file_url=file_url
|
546 |
)
|
547 |
|
|
|
|
|
|
|
|
|
548 |
# Add to results
|
549 |
answers_payload.append({
|
550 |
"task_id": task_id,
|
@@ -552,10 +567,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
552 |
})
|
553 |
results_log.append({
|
554 |
"Task ID": task_id,
|
555 |
-
"Question":
|
556 |
"Submitted Answer": answer
|
557 |
})
|
558 |
|
|
|
|
|
559 |
except Exception as e:
|
560 |
print(f"Error processing task {task_id}: {e}")
|
561 |
results_log.append({
|
@@ -565,9 +582,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
565 |
})
|
566 |
|
567 |
if not answers_payload:
|
568 |
-
|
|
|
569 |
|
570 |
# 4. Submit Answers
|
|
|
571 |
submission_data = {
|
572 |
"username": username.strip(),
|
573 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
@@ -575,9 +594,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
575 |
}
|
576 |
|
577 |
try:
|
|
|
|
|
|
|
578 |
response = sess.post(submit_url, json=submission_data, timeout=60)
|
579 |
response.raise_for_status()
|
580 |
result_data = response.json()
|
|
|
581 |
final_status = (
|
582 |
f"Submission Successful!\n"
|
583 |
f"User: {result_data.get('username')}\n"
|
@@ -585,9 +608,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
585 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
586 |
f"Message: {result_data.get('message', 'No message received.')}"
|
587 |
)
|
|
|
588 |
return final_status, pd.DataFrame(results_log)
|
589 |
except Exception as e:
|
590 |
-
|
|
|
|
|
591 |
|
592 |
def attachment_url(task_id: str, api_url: str, sess: requests.Session) -> str | None:
|
593 |
"""Probe if a task has an attachment, return URL if it exists."""
|
|
|
501 |
# 1. Instantiate Agent
|
502 |
try:
|
503 |
print("Initializing agent...")
|
504 |
+
agent = BasicAgent(api_url=api_url)
|
505 |
print("Agent initialized successfully.")
|
506 |
except Exception as e:
|
507 |
print(f"Error instantiating agent: {e}")
|
|
|
528 |
for item in questions_data:
|
529 |
task_id = item.get("task_id")
|
530 |
if not task_id:
|
531 |
+
print("Skipping item without task_id")
|
532 |
continue
|
533 |
|
534 |
try:
|
|
|
537 |
# Handle file URL with conditional fallback to generic attachment endpoint
|
538 |
raw_url = item.get("file_url") or ""
|
539 |
if not raw_url: # field missing
|
540 |
+
raw_url = discover_attachment(task_id, api_url, sess) or ""
|
541 |
file_url = raw_url # already absolute
|
542 |
|
543 |
+
# Get the question text
|
544 |
+
question = item.get("question", "")
|
545 |
+
if not question:
|
546 |
+
print(f"Skipping task {task_id} - no question text")
|
547 |
+
continue
|
548 |
+
|
549 |
+
print(f"Question: {question}")
|
550 |
+
print(f"File URL: {file_url}")
|
551 |
+
|
552 |
+
# Get answer from agent
|
553 |
answer = agent(
|
554 |
+
question=question,
|
555 |
task_id=task_id,
|
556 |
file_url=file_url
|
557 |
)
|
558 |
|
559 |
+
if not answer:
|
560 |
+
print(f"Warning: Empty answer for task {task_id}")
|
561 |
+
answer = "No answer generated"
|
562 |
+
|
563 |
# Add to results
|
564 |
answers_payload.append({
|
565 |
"task_id": task_id,
|
|
|
567 |
})
|
568 |
results_log.append({
|
569 |
"Task ID": task_id,
|
570 |
+
"Question": question,
|
571 |
"Submitted Answer": answer
|
572 |
})
|
573 |
|
574 |
+
print(f"Successfully processed task {task_id}")
|
575 |
+
|
576 |
except Exception as e:
|
577 |
print(f"Error processing task {task_id}: {e}")
|
578 |
results_log.append({
|
|
|
582 |
})
|
583 |
|
584 |
if not answers_payload:
|
585 |
+
print("No answers were generated.")
|
586 |
+
return "No answers were generated. Please check the logs for details.", pd.DataFrame(results_log)
|
587 |
|
588 |
# 4. Submit Answers
|
589 |
+
print(f"\nSubmitting {len(answers_payload)} answers...")
|
590 |
submission_data = {
|
591 |
"username": username.strip(),
|
592 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
|
|
594 |
}
|
595 |
|
596 |
try:
|
597 |
+
print(f"Submitting to: {submit_url}")
|
598 |
+
print(f"Submission data: {json.dumps(submission_data, indent=2)}")
|
599 |
+
|
600 |
response = sess.post(submit_url, json=submission_data, timeout=60)
|
601 |
response.raise_for_status()
|
602 |
result_data = response.json()
|
603 |
+
|
604 |
final_status = (
|
605 |
f"Submission Successful!\n"
|
606 |
f"User: {result_data.get('username')}\n"
|
|
|
608 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
609 |
f"Message: {result_data.get('message', 'No message received.')}"
|
610 |
)
|
611 |
+
print(final_status)
|
612 |
return final_status, pd.DataFrame(results_log)
|
613 |
except Exception as e:
|
614 |
+
error_msg = f"Submission Failed: {str(e)}"
|
615 |
+
print(error_msg)
|
616 |
+
return error_msg, pd.DataFrame(results_log)
|
617 |
|
618 |
def attachment_url(task_id: str, api_url: str, sess: requests.Session) -> str | None:
|
619 |
"""Probe if a task has an attachment, return URL if it exists."""
|