Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
# --- Constants ---
|
|
@@ -17,9 +18,9 @@ class ZeroShotAgent:
|
|
| 17 |
print(f"Received question: {question[:100]}")
|
| 18 |
labels = ["Yes", "No", "Not Enough Information"]
|
| 19 |
result = self.classifier(question, labels)
|
| 20 |
-
best_answer = result["labels"][0]
|
| 21 |
print(f"Answer: {best_answer}")
|
| 22 |
-
return best_answer
|
| 23 |
|
| 24 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 25 |
space_id = os.getenv("SPACE_ID")
|
|
@@ -49,35 +50,39 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 49 |
response.raise_for_status()
|
| 50 |
questions_data = response.json()
|
| 51 |
if not questions_data:
|
| 52 |
-
print("Fetched questions list is empty.")
|
| 53 |
return "Fetched questions list is empty or invalid format.", None
|
| 54 |
print(f"Fetched {len(questions_data)} questions.")
|
| 55 |
except Exception as e:
|
| 56 |
-
print(f"Error fetching questions: {e}")
|
| 57 |
return f"Error fetching questions: {e}", None
|
| 58 |
|
| 59 |
results_log = []
|
| 60 |
answers_payload = []
|
| 61 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 62 |
for item in questions_data:
|
| 63 |
task_id = item.get("task_id")
|
| 64 |
question_text = item.get("question")
|
| 65 |
if not task_id or question_text is None:
|
| 66 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 67 |
continue
|
| 68 |
try:
|
| 69 |
submitted_answer = agent(question_text)
|
|
|
|
|
|
|
| 70 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 71 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 72 |
except Exception as e:
|
| 73 |
-
print(f"Error running agent on task {task_id}: {e}")
|
| 74 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 75 |
|
| 76 |
if not answers_payload:
|
| 77 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 78 |
|
| 79 |
-
submission_data = {
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -90,10 +95,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 90 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 91 |
f"π Message: {result_data.get('message', 'No message received.')}"
|
| 92 |
)
|
| 93 |
-
print(final_status)
|
| 94 |
return final_status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
-
return f"β
|
| 97 |
|
| 98 |
# --- Gradio UI ---
|
| 99 |
with gr.Blocks() as demo:
|
|
@@ -103,7 +109,8 @@ with gr.Blocks() as demo:
|
|
| 103 |
β
**Instructions:**
|
| 104 |
1. Login to your Hugging Face account.
|
| 105 |
2. Click 'Run Evaluation & Submit All Answers'.
|
| 106 |
-
3. Wait
|
|
|
|
| 107 |
"""
|
| 108 |
)
|
| 109 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import json
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
# --- Constants ---
|
|
|
|
| 18 |
print(f"Received question: {question[:100]}")
|
| 19 |
labels = ["Yes", "No", "Not Enough Information"]
|
| 20 |
result = self.classifier(question, labels)
|
| 21 |
+
best_answer = result["labels"][0] if result and "labels" in result else "Not Enough Information"
|
| 22 |
print(f"Answer: {best_answer}")
|
| 23 |
+
return best_answer or "Not Enough Information"
|
| 24 |
|
| 25 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 26 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 50 |
response.raise_for_status()
|
| 51 |
questions_data = response.json()
|
| 52 |
if not questions_data:
|
|
|
|
| 53 |
return "Fetched questions list is empty or invalid format.", None
|
| 54 |
print(f"Fetched {len(questions_data)} questions.")
|
| 55 |
except Exception as e:
|
|
|
|
| 56 |
return f"Error fetching questions: {e}", None
|
| 57 |
|
| 58 |
results_log = []
|
| 59 |
answers_payload = []
|
|
|
|
| 60 |
for item in questions_data:
|
| 61 |
task_id = item.get("task_id")
|
| 62 |
question_text = item.get("question")
|
| 63 |
if not task_id or question_text is None:
|
|
|
|
| 64 |
continue
|
| 65 |
try:
|
| 66 |
submitted_answer = agent(question_text)
|
| 67 |
+
if not submitted_answer:
|
| 68 |
+
submitted_answer = "Not Enough Information"
|
| 69 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 70 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 71 |
except Exception as e:
|
|
|
|
| 72 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 73 |
|
| 74 |
if not answers_payload:
|
| 75 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 76 |
|
| 77 |
+
submission_data = {
|
| 78 |
+
"username": username.strip(),
|
| 79 |
+
"agent_code": agent_code,
|
| 80 |
+
"answers": answers_payload
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
# Debug payload
|
| 84 |
+
print("Submission Payload Preview:")
|
| 85 |
+
print(json.dumps(submission_data, indent=2)[:1000]) # Safe preview log
|
| 86 |
|
| 87 |
try:
|
| 88 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 95 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 96 |
f"π Message: {result_data.get('message', 'No message received.')}"
|
| 97 |
)
|
|
|
|
| 98 |
return final_status, pd.DataFrame(results_log)
|
| 99 |
+
except requests.exceptions.RequestException as e:
|
| 100 |
+
return f"β Submission Failed: {str(e)}", pd.DataFrame(results_log)
|
| 101 |
except Exception as e:
|
| 102 |
+
return f"β Unexpected Error: {e}", pd.DataFrame(results_log)
|
| 103 |
|
| 104 |
# --- Gradio UI ---
|
| 105 |
with gr.Blocks() as demo:
|
|
|
|
| 109 |
β
**Instructions:**
|
| 110 |
1. Login to your Hugging Face account.
|
| 111 |
2. Click 'Run Evaluation & Submit All Answers'.
|
| 112 |
+
3. Wait ~1β2 mins for results.
|
| 113 |
+
π You need at least **30% correct** on Level 1 to unlock the certificate.
|
| 114 |
"""
|
| 115 |
)
|
| 116 |
|