Update app.py
Browse files
app.py
CHANGED
|
@@ -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"
|
|
@@ -33,26 +33,26 @@ class BasicAgent:
|
|
| 33 |
if "who" in question_lower and "ceo" in question_lower:
|
| 34 |
if "tesla" in question_lower or "spacex" in question_lower:
|
| 35 |
return "Elon Musk"
|
| 36 |
-
elif "apple" in question_lower:
|
| 37 |
return "Tim Cook"
|
| 38 |
-
elif "microsoft" in question_lower:
|
| 39 |
return "Satya Nadella"
|
| 40 |
|
| 41 |
-
if "what year" in question_lower or "when" in question_lower:
|
| 42 |
-
if "world war 2" in question_lower or "wwii" in question_lower:
|
| 43 |
return "1939-1945"
|
| 44 |
-
elif "world war 1" in question_lower or "wwi" in question_lower:
|
| 45 |
return "1914-1918"
|
| 46 |
|
| 47 |
-
if "how many" in question_lower:
|
| 48 |
-
if "planets" in question_lower:
|
| 49 |
return "8"
|
| 50 |
elif "continents" in question_lower:
|
| 51 |
return "7"
|
| 52 |
|
| 53 |
# Default response
|
| 54 |
answer = "I don't have enough information to answer this question accurately."
|
| 55 |
-
print(f"Agent returning answer:
|
| 56 |
return answer
|
| 57 |
|
| 58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
@@ -80,8 +80,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"Error instantiating agent: {e}")
|
| 81 |
return f"Error initializing agent: {e}", None
|
| 82 |
|
|
|
|
| 83 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 84 |
-
print(f"Agent code:
|
| 85 |
|
| 86 |
# 2. Fetch Questions
|
| 87 |
print(f"Fetching questions from: {questions_url}")
|
|
@@ -98,7 +99,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 98 |
return f"Error fetching questions: {e}", None
|
| 99 |
except requests.exceptions.JSONDecodeError as e:
|
| 100 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 101 |
-
print(f"Response text: {response.text[:500]}")
|
| 102 |
return f"Error decoding server response for questions: {e}", None
|
| 103 |
except Exception as e:
|
| 104 |
print(f"An unexpected error occurred fetching questions: {e}")
|
|
@@ -108,8 +109,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 108 |
results_log = []
|
| 109 |
answers_payload = []
|
| 110 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 111 |
-
for item in questions_data:
|
| 112 |
-
task_id = item.
|
| 113 |
question_text = item.get("question")
|
| 114 |
if not task_id or question_text is None:
|
| 115 |
print(f"Skipping item with missing task_id or question: {item}")
|
|
@@ -124,30 +125,56 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 124 |
answers_payload.append({"task_id": task_id, "answer": f"Error: {e}"})
|
| 125 |
results_log.append((task_id, question_text[:50], f"Error: {e}"))
|
| 126 |
|
| 127 |
-
# 4. Submit answers
|
| 128 |
print(f"Submitting {len(answers_payload)} answers to {submit_url}...")
|
| 129 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
submit_response = requests.post(
|
| 131 |
submit_url,
|
| 132 |
-
json=
|
| 133 |
timeout=30
|
| 134 |
)
|
| 135 |
submit_response.raise_for_status()
|
| 136 |
submission_result = submit_response.json()
|
| 137 |
-
print(f"Submission successful:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
print(f"Error submitting answers: {e}")
|
| 140 |
return f"Error submitting answers: {e}", None
|
| 141 |
|
| 142 |
# Display results
|
| 143 |
results_df = pd.DataFrame(results_log, columns=["task_id", "question", "answer"])
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
return result_message, results_df
|
| 147 |
|
| 148 |
# --- Gradio UI Setup ---
|
| 149 |
with gr.Blocks() as demo:
|
| 150 |
-
gr.
|
| 151 |
# π€ Agent Final Assignment - Unit 4
|
| 152 |
|
| 153 |
**Instructions:**
|
|
@@ -163,7 +190,7 @@ with gr.Blocks() as demo:
|
|
| 163 |
submit_button = gr.Button("π Run Evaluation & Submit All Answers", variant="primary", size="lg")
|
| 164 |
|
| 165 |
output_text = gr. Markdown()
|
| 166 |
-
output_table = gr.
|
| 167 |
|
| 168 |
# The profile is automatically injected when using gr.LoginButton()
|
| 169 |
submit_button.click(
|
|
|
|
| 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"
|
|
|
|
| 33 |
if "who" in question_lower and "ceo" in question_lower:
|
| 34 |
if "tesla" in question_lower or "spacex" in question_lower:
|
| 35 |
return "Elon Musk"
|
| 36 |
+
elif "apple" in question_lower:
|
| 37 |
return "Tim Cook"
|
| 38 |
+
elif "microsoft" in question_lower:
|
| 39 |
return "Satya Nadella"
|
| 40 |
|
| 41 |
+
if "what year" in question_lower or "when" in question_lower:
|
| 42 |
+
if "world war 2" in question_lower or "wwii" in question_lower:
|
| 43 |
return "1939-1945"
|
| 44 |
+
elif "world war 1" in question_lower or "wwi" in question_lower:
|
| 45 |
return "1914-1918"
|
| 46 |
|
| 47 |
+
if "how many" in question_lower:
|
| 48 |
+
if "planets" in question_lower:
|
| 49 |
return "8"
|
| 50 |
elif "continents" in question_lower:
|
| 51 |
return "7"
|
| 52 |
|
| 53 |
# Default response
|
| 54 |
answer = "I don't have enough information to answer this question accurately."
|
| 55 |
+
print(f"Agent returning answer: {answer}")
|
| 56 |
return answer
|
| 57 |
|
| 58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 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}")
|
|
|
|
| 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:
|
| 105 |
print(f"An unexpected error occurred fetching questions: {e}")
|
|
|
|
| 109 |
results_log = []
|
| 110 |
answers_payload = []
|
| 111 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 112 |
+
for item in questions_data:
|
| 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}")
|
|
|
|
| 125 |
answers_payload.append({"task_id": task_id, "answer": f"Error: {e}"})
|
| 126 |
results_log.append((task_id, question_text[:50], f"Error: {e}"))
|
| 127 |
|
| 128 |
+
# 4. Submit answers with CORRECT payload format
|
| 129 |
print(f"Submitting {len(answers_payload)} answers to {submit_url}...")
|
| 130 |
try:
|
| 131 |
+
# THIS IS THE CORRECT PAYLOAD FORMAT!
|
| 132 |
+
payload = {
|
| 133 |
+
"username": username,
|
| 134 |
+
"answers": answers_payload,
|
| 135 |
+
"agent_code": agent_code
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
print(f"Payload structure: username={username}, answers count={len(answers_payload)}, agent_code={agent_code}")
|
| 139 |
+
|
| 140 |
submit_response = requests.post(
|
| 141 |
submit_url,
|
| 142 |
+
json=payload,
|
| 143 |
timeout=30
|
| 144 |
)
|
| 145 |
submit_response.raise_for_status()
|
| 146 |
submission_result = submit_response.json()
|
| 147 |
+
print(f"Submission successful: {submission_result}")
|
| 148 |
+
except requests.exceptions.HTTPError as e:
|
| 149 |
+
print(f"HTTP Error submitting answers: {e}")
|
| 150 |
+
print(f"Response text: {submit_response.text[: 500]}")
|
| 151 |
+
return f"Error submitting answers: {e}\n\nResponse: {submit_response.text[: 200]}", None
|
| 152 |
except Exception as e:
|
| 153 |
print(f"Error submitting answers: {e}")
|
| 154 |
return f"Error submitting answers: {e}", None
|
| 155 |
|
| 156 |
# Display results
|
| 157 |
results_df = pd.DataFrame(results_log, columns=["task_id", "question", "answer"])
|
| 158 |
+
|
| 159 |
+
# Extract score from submission result
|
| 160 |
+
score = submission_result.get('score', 'N/A')
|
| 161 |
+
result_message = f"""
|
| 162 |
+
## β
Submission Successful!
|
| 163 |
+
|
| 164 |
+
**Score:** {score}
|
| 165 |
+
|
| 166 |
+
**Username:** {username}
|
| 167 |
+
|
| 168 |
+
**Questions Answered:** {len(answers_payload)}
|
| 169 |
+
|
| 170 |
+
π [View Your Agent Code]({agent_code})
|
| 171 |
+
"""
|
| 172 |
|
| 173 |
return result_message, results_df
|
| 174 |
|
| 175 |
# --- Gradio UI Setup ---
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
+
gr.Markdown("""
|
| 178 |
# π€ Agent Final Assignment - Unit 4
|
| 179 |
|
| 180 |
**Instructions:**
|
|
|
|
| 190 |
submit_button = gr.Button("π Run Evaluation & Submit All Answers", variant="primary", size="lg")
|
| 191 |
|
| 192 |
output_text = gr. Markdown()
|
| 193 |
+
output_table = gr.Dataframe(label="Results")
|
| 194 |
|
| 195 |
# The profile is automatically injected when using gr.LoginButton()
|
| 196 |
submit_button.click(
|