Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
# --- Constants ---
|
| 7 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf. space
|
| 8 |
|
| 9 |
# --- Basic Agent Definition ---
|
| 10 |
class BasicAgent:
|
|
@@ -25,7 +25,7 @@ class BasicAgent:
|
|
| 25 |
return "Berlin"
|
| 26 |
elif "italy" in question_lower:
|
| 27 |
return "Rome"
|
| 28 |
-
elif "spain" in question_lower:
|
| 29 |
return "Madrid"
|
| 30 |
elif "japan" in question_lower:
|
| 31 |
return "Tokyo"
|
|
@@ -76,29 +76,29 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 76 |
# 1. Instantiate Agent
|
| 77 |
try:
|
| 78 |
agent = BasicAgent()
|
| 79 |
-
except Exception as e:
|
| 80 |
print(f"Error instantiating agent: {e}")
|
| 81 |
return f"Error initializing agent: {e}", None
|
| 82 |
|
| 83 |
# Build agent code URL
|
| 84 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 85 |
-
print(f"Agent code:
|
| 86 |
|
| 87 |
# 2. Fetch Questions
|
| 88 |
-
print(f"Fetching questions from:
|
| 89 |
try:
|
| 90 |
response = requests.get(questions_url, timeout=15)
|
| 91 |
response.raise_for_status()
|
| 92 |
questions_data = response.json()
|
| 93 |
-
if not questions_data:
|
| 94 |
print("Fetched questions list is empty.")
|
| 95 |
return "Fetched questions list is empty or invalid format.", None
|
| 96 |
print(f"Fetched {len(questions_data)} questions.")
|
| 97 |
-
except requests.exceptions.RequestException as e:
|
| 98 |
print(f"Error fetching questions: {e}")
|
| 99 |
return f"Error fetching questions: {e}", None
|
| 100 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 101 |
-
print(f"Error decoding JSON response from questions endpoint:
|
| 102 |
print(f"Response text: {response.text[: 500]}")
|
| 103 |
return f"Error decoding server response for questions: {e}", None
|
| 104 |
except Exception as e:
|
|
@@ -113,24 +113,24 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 113 |
task_id = item.get("task_id")
|
| 114 |
question_text = item.get("question")
|
| 115 |
if not task_id or question_text is None:
|
| 116 |
-
print(f"Skipping item with missing task_id or question:
|
| 117 |
continue
|
| 118 |
|
| 119 |
try:
|
| 120 |
answer = agent(question_text)
|
| 121 |
-
#
|
| 122 |
-
answers_payload.append({
|
| 123 |
"task_id": task_id,
|
| 124 |
-
"submitted_answer": answer
|
| 125 |
})
|
| 126 |
-
results_log.append((task_id, question_text[:50], answer[: 100]))
|
| 127 |
except Exception as e:
|
| 128 |
print(f"Error running agent on question {task_id}: {e}")
|
| 129 |
answers_payload.append({
|
| 130 |
"task_id": task_id,
|
| 131 |
-
"submitted_answer": f"Error: {e}"
|
| 132 |
})
|
| 133 |
-
results_log.append((task_id, question_text[:50], f"Error: {e}"))
|
| 134 |
|
| 135 |
# 4. Submit answers with CORRECT payload format
|
| 136 |
print(f"Submitting {len(answers_payload)} answers to {submit_url}...")
|
|
@@ -138,35 +138,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 138 |
# Correct payload format
|
| 139 |
payload = {
|
| 140 |
"username": username,
|
| 141 |
-
"answers":
|
| 142 |
"agent_code": agent_code
|
| 143 |
}
|
| 144 |
|
| 145 |
-
print(f"Payload structure:
|
| 146 |
|
| 147 |
-
submit_response = requests.post(
|
| 148 |
submit_url,
|
| 149 |
json=payload,
|
| 150 |
timeout=30
|
| 151 |
)
|
| 152 |
submit_response.raise_for_status()
|
| 153 |
submission_result = submit_response.json()
|
| 154 |
-
print(f"Submission successful:
|
| 155 |
except requests.exceptions.HTTPError as e:
|
| 156 |
print(f"HTTP Error submitting answers: {e}")
|
| 157 |
-
print(f"Response text: {submit_response.text[:500]}")
|
| 158 |
return f"Error submitting answers: {e}\n\nResponse: {submit_response.text[:200]}", None
|
| 159 |
-
except Exception as e:
|
| 160 |
print(f"Error submitting answers: {e}")
|
| 161 |
return f"Error submitting answers: {e}", None
|
| 162 |
|
| 163 |
# Display results
|
| 164 |
-
results_df = pd.DataFrame(results_log, columns=["task_id", "question", "answer"])
|
| 165 |
|
| 166 |
# Extract score from submission result
|
| 167 |
score = submission_result.get('score', 'N/A')
|
| 168 |
result_message = f"""
|
| 169 |
-
## ✅ Submission Successful!
|
| 170 |
|
| 171 |
**Score:** {score}
|
| 172 |
|
|
@@ -196,10 +196,10 @@ with gr.Blocks() as demo:
|
|
| 196 |
with gr.Row():
|
| 197 |
submit_button = gr.Button("🚀 Run Evaluation & Submit All Answers", variant="primary", size="lg")
|
| 198 |
|
| 199 |
-
output_text = gr.
|
| 200 |
output_table = gr.Dataframe(label="Results")
|
| 201 |
|
| 202 |
-
# The profile is automatically injected when using gr.
|
| 203 |
submit_button.click(
|
| 204 |
run_and_submit_all,
|
| 205 |
inputs=None,
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
# --- Constants ---
|
| 7 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # ← FIXED: No space!
|
| 8 |
|
| 9 |
# --- Basic Agent Definition ---
|
| 10 |
class BasicAgent:
|
|
|
|
| 25 |
return "Berlin"
|
| 26 |
elif "italy" in question_lower:
|
| 27 |
return "Rome"
|
| 28 |
+
elif "spain" in question_lower:
|
| 29 |
return "Madrid"
|
| 30 |
elif "japan" in question_lower:
|
| 31 |
return "Tokyo"
|
|
|
|
| 76 |
# 1. Instantiate Agent
|
| 77 |
try:
|
| 78 |
agent = BasicAgent()
|
| 79 |
+
except Exception as e:
|
| 80 |
print(f"Error instantiating agent: {e}")
|
| 81 |
return f"Error initializing agent: {e}", None
|
| 82 |
|
| 83 |
# Build agent code URL
|
| 84 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 85 |
+
print(f"Agent code: {agent_code}")
|
| 86 |
|
| 87 |
# 2. Fetch Questions
|
| 88 |
+
print(f"Fetching questions from: {questions_url}")
|
| 89 |
try:
|
| 90 |
response = requests.get(questions_url, timeout=15)
|
| 91 |
response.raise_for_status()
|
| 92 |
questions_data = response.json()
|
| 93 |
+
if not questions_data:
|
| 94 |
print("Fetched questions list is empty.")
|
| 95 |
return "Fetched questions list is empty or invalid format.", None
|
| 96 |
print(f"Fetched {len(questions_data)} questions.")
|
| 97 |
+
except requests. exceptions.RequestException as e:
|
| 98 |
print(f"Error fetching questions: {e}")
|
| 99 |
return f"Error fetching questions: {e}", None
|
| 100 |
+
except requests. exceptions.JSONDecodeError as e:
|
| 101 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 102 |
print(f"Response text: {response.text[: 500]}")
|
| 103 |
return f"Error decoding server response for questions: {e}", None
|
| 104 |
except Exception as e:
|
|
|
|
| 113 |
task_id = item.get("task_id")
|
| 114 |
question_text = item.get("question")
|
| 115 |
if not task_id or question_text is None:
|
| 116 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 117 |
continue
|
| 118 |
|
| 119 |
try:
|
| 120 |
answer = agent(question_text)
|
| 121 |
+
# Use "submitted_answer" as required by API
|
| 122 |
+
answers_payload. append({
|
| 123 |
"task_id": task_id,
|
| 124 |
+
"submitted_answer": answer
|
| 125 |
})
|
| 126 |
+
results_log.append((task_id, question_text[: 50], answer[: 100]))
|
| 127 |
except Exception as e:
|
| 128 |
print(f"Error running agent on question {task_id}: {e}")
|
| 129 |
answers_payload.append({
|
| 130 |
"task_id": task_id,
|
| 131 |
+
"submitted_answer": f"Error: {e}"
|
| 132 |
})
|
| 133 |
+
results_log.append((task_id, question_text[: 50], f"Error: {e}"))
|
| 134 |
|
| 135 |
# 4. Submit answers with CORRECT payload format
|
| 136 |
print(f"Submitting {len(answers_payload)} answers to {submit_url}...")
|
|
|
|
| 138 |
# Correct payload format
|
| 139 |
payload = {
|
| 140 |
"username": username,
|
| 141 |
+
"answers": answers_payload,
|
| 142 |
"agent_code": agent_code
|
| 143 |
}
|
| 144 |
|
| 145 |
+
print(f"Payload structure: username={username}, answers count={len(answers_payload)}, agent_code={agent_code}")
|
| 146 |
|
| 147 |
+
submit_response = requests. post(
|
| 148 |
submit_url,
|
| 149 |
json=payload,
|
| 150 |
timeout=30
|
| 151 |
)
|
| 152 |
submit_response.raise_for_status()
|
| 153 |
submission_result = submit_response.json()
|
| 154 |
+
print(f"Submission successful: {submission_result}")
|
| 155 |
except requests.exceptions.HTTPError as e:
|
| 156 |
print(f"HTTP Error submitting answers: {e}")
|
| 157 |
+
print(f"Response text: {submit_response.text[: 500]}")
|
| 158 |
return f"Error submitting answers: {e}\n\nResponse: {submit_response.text[:200]}", None
|
| 159 |
+
except Exception as e:
|
| 160 |
print(f"Error submitting answers: {e}")
|
| 161 |
return f"Error submitting answers: {e}", None
|
| 162 |
|
| 163 |
# Display results
|
| 164 |
+
results_df = pd. DataFrame(results_log, columns=["task_id", "question", "answer"])
|
| 165 |
|
| 166 |
# Extract score from submission result
|
| 167 |
score = submission_result.get('score', 'N/A')
|
| 168 |
result_message = f"""
|
| 169 |
+
## ✅ Submission Successful!
|
| 170 |
|
| 171 |
**Score:** {score}
|
| 172 |
|
|
|
|
| 196 |
with gr.Row():
|
| 197 |
submit_button = gr.Button("🚀 Run Evaluation & Submit All Answers", variant="primary", size="lg")
|
| 198 |
|
| 199 |
+
output_text = gr.Markdown()
|
| 200 |
output_table = gr.Dataframe(label="Results")
|
| 201 |
|
| 202 |
+
# The profile is automatically injected when using gr.LoginButton()
|
| 203 |
submit_button.click(
|
| 204 |
run_and_submit_all,
|
| 205 |
inputs=None,
|