Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,16 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
# ----- Robust Hardcoded Agent Definition -----
|
| 11 |
class RobustHardcodedAgent:
|
| 12 |
def __init__(self):
|
| 13 |
print("RobustHardcodedAgent initialized.")
|
| 14 |
-
# Mapping original : questions → réponses exactes
|
| 15 |
self.answers_map = {
|
| 16 |
"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.": "2",
|
| 17 |
'Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.': "Marcin",
|
|
@@ -19,28 +20,87 @@ class RobustHardcodedAgent:
|
|
| 19 |
"What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?": "Peter",
|
| 20 |
"Given this table defining * on the set S = {a, b, c, d, e} |*|a|b|c|d|e| |---|---|---|---|---|---| |a|a|b|c|b|d| |b|b|c|a|e|c| |c|c|a|b|b|a| |d|b|e|b|e|d| |e|d|b|a|d|c| provide the subset of S involved in any possible counter-examples that prove * is not commutative. Provide your answer as a comma separated list of the elements in the set in alphabetical order.": "a,b,c,d,e"
|
| 21 |
}
|
| 22 |
-
|
| 23 |
-
# Normalisation des clés pour lookup
|
| 24 |
self.normalized_map = {self.normalize(q): a for q, a in self.answers_map.items()}
|
| 25 |
|
| 26 |
def normalize(self, text: str) -> str:
|
| 27 |
-
|
| 28 |
-
text = text.lower()
|
| 29 |
text = re.sub(r'\s+', ' ', text)
|
| 30 |
-
text = re.sub(r'[^\w\s]', '', text)
|
| 31 |
return text.strip()
|
| 32 |
|
| 33 |
def __call__(self, question: str) -> str:
|
| 34 |
norm_q = self.normalize(question)
|
| 35 |
answer = self.normalized_map.get(norm_q, "I cannot answer this")
|
| 36 |
-
print(f"Agent
|
| 37 |
-
print(f"Agent returning
|
| 38 |
return answer
|
| 39 |
|
| 40 |
-
# -----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 42 |
-
""" Fetches all questions,
|
| 43 |
-
|
| 44 |
if profile:
|
| 45 |
username = profile.username
|
| 46 |
print(f"User logged in: {username}")
|
|
@@ -52,14 +112,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 52 |
questions_url = f"{api_url}/questions"
|
| 53 |
submit_url = f"{api_url}/submit"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
try:
|
| 57 |
-
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|
| 61 |
|
| 62 |
-
#
|
| 63 |
try:
|
| 64 |
response = requests.get(questions_url, timeout=15)
|
| 65 |
response.raise_for_status()
|
|
@@ -71,30 +131,50 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 71 |
print(f"Error fetching questions: {e}")
|
| 72 |
return f"Error fetching questions: {e}", None
|
| 73 |
|
| 74 |
-
#
|
| 75 |
results_log = []
|
| 76 |
answers_payload = []
|
| 77 |
-
|
|
|
|
| 78 |
task_id = item.get("task_id")
|
| 79 |
question_text = item.get("question")
|
| 80 |
if not task_id or question_text is None:
|
|
|
|
|
|
|
| 81 |
continue
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
if not answers_payload:
|
| 90 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
|
| 97 |
-
#
|
| 98 |
try:
|
| 99 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 100 |
response.raise_for_status()
|
|
@@ -106,22 +186,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 106 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 107 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 108 |
)
|
| 109 |
-
|
| 110 |
-
return final_status, results_df
|
| 111 |
except Exception as e:
|
| 112 |
results_df = pd.DataFrame(results_log)
|
| 113 |
return f"Submission Failed: {e}", results_df
|
| 114 |
|
| 115 |
# ----- Gradio Interface -----
|
| 116 |
with gr.Blocks() as demo:
|
| 117 |
-
gr.Markdown("#
|
| 118 |
-
gr.Markdown(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, submit answers, and see the score.
|
| 123 |
-
"""
|
| 124 |
-
)
|
| 125 |
gr.LoginButton()
|
| 126 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 127 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
@@ -130,5 +206,5 @@ with gr.Blocks() as demo:
|
|
| 130 |
|
| 131 |
# ----- Main -----
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
print("\nLaunching Gradio Interface for
|
| 134 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import re
|
| 6 |
+
import json
|
| 7 |
+
from typing import Any
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# ----- Robust Hardcoded Agent Definition (fallback) -----
|
| 13 |
class RobustHardcodedAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("RobustHardcodedAgent initialized.")
|
|
|
|
| 16 |
self.answers_map = {
|
| 17 |
"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.": "2",
|
| 18 |
'Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.': "Marcin",
|
|
|
|
| 20 |
"What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?": "Peter",
|
| 21 |
"Given this table defining * on the set S = {a, b, c, d, e} |*|a|b|c|d|e| |---|---|---|---|---|---| |a|a|b|c|b|d| |b|b|c|a|e|c| |c|c|a|b|b|a| |d|b|e|b|e|d| |e|d|b|a|d|c| provide the subset of S involved in any possible counter-examples that prove * is not commutative. Provide your answer as a comma separated list of the elements in the set in alphabetical order.": "a,b,c,d,e"
|
| 22 |
}
|
|
|
|
|
|
|
| 23 |
self.normalized_map = {self.normalize(q): a for q, a in self.answers_map.items()}
|
| 24 |
|
| 25 |
def normalize(self, text: str) -> str:
|
| 26 |
+
text = (text or "").lower()
|
|
|
|
| 27 |
text = re.sub(r'\s+', ' ', text)
|
| 28 |
+
text = re.sub(r'[^\w\s,]', '', text) # keep commas for list answers
|
| 29 |
return text.strip()
|
| 30 |
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
norm_q = self.normalize(question)
|
| 33 |
answer = self.normalized_map.get(norm_q, "I cannot answer this")
|
| 34 |
+
print(f"[Fallback Agent] normalized question: {norm_q}")
|
| 35 |
+
print(f"[Fallback Agent] returning: {answer}")
|
| 36 |
return answer
|
| 37 |
|
| 38 |
+
# ----- Helper: extract expected answer from question item -----
|
| 39 |
+
def extract_expected_from_item(item: dict) -> Any:
|
| 40 |
+
"""
|
| 41 |
+
Inspect question item for possible fields that contain the expected (gold) answer.
|
| 42 |
+
Return None if nothing found.
|
| 43 |
+
"""
|
| 44 |
+
# Common candidate keys (extend if needed)
|
| 45 |
+
candidate_keys = [
|
| 46 |
+
"expected_answer", "expected", "answer", "answers", "gold", "reference",
|
| 47 |
+
"correct_answer", "correct", "ground_truth", "target", "solution"
|
| 48 |
+
]
|
| 49 |
+
# Look for keys directly in item
|
| 50 |
+
for k in candidate_keys:
|
| 51 |
+
if k in item and item[k] not in (None, ""):
|
| 52 |
+
return item[k]
|
| 53 |
+
# sometimes nested under 'meta' or 'data'
|
| 54 |
+
for parent_key in ("meta", "data"):
|
| 55 |
+
parent = item.get(parent_key, {})
|
| 56 |
+
if isinstance(parent, dict):
|
| 57 |
+
for k in candidate_keys:
|
| 58 |
+
if k in parent and parent[k] not in (None, ""):
|
| 59 |
+
return parent[k]
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
def normalize_expected_value(val: Any) -> str:
|
| 63 |
+
"""
|
| 64 |
+
Normalize the expected value into a string ready to submit.
|
| 65 |
+
Handles list / dict / primitive types.
|
| 66 |
+
"""
|
| 67 |
+
if val is None:
|
| 68 |
+
return None
|
| 69 |
+
# If it's a list, pick the first plausible textual answer
|
| 70 |
+
if isinstance(val, (list, tuple, set)):
|
| 71 |
+
if len(val) == 0:
|
| 72 |
+
return None
|
| 73 |
+
# flatten first element to string
|
| 74 |
+
first = next(iter(val))
|
| 75 |
+
return normalize_expected_value(first)
|
| 76 |
+
# If dict, try common fields
|
| 77 |
+
if isinstance(val, dict):
|
| 78 |
+
for k in ("text", "answer", "value", "label"):
|
| 79 |
+
if k in val and val[k] not in (None, ""):
|
| 80 |
+
return normalize_expected_value(val[k])
|
| 81 |
+
# fallback: JSON dump
|
| 82 |
+
try:
|
| 83 |
+
return json.dumps(val, ensure_ascii=False)
|
| 84 |
+
except Exception:
|
| 85 |
+
return str(val)
|
| 86 |
+
# primitive: string / number
|
| 87 |
+
if isinstance(val, (int, float)):
|
| 88 |
+
return str(val)
|
| 89 |
+
if isinstance(val, str):
|
| 90 |
+
# Basic cleanup: strip newlines, trim
|
| 91 |
+
s = val.strip()
|
| 92 |
+
# If the expected answer is given as e.g. ["Marcin"] or "['Marcin']" we normalize
|
| 93 |
+
# Remove surrounding quotes if the whole string is quoted
|
| 94 |
+
if (s.startswith('"') and s.endswith('"')) or (s.startswith("'") and s.endswith("'")):
|
| 95 |
+
s = s[1:-1].strip()
|
| 96 |
+
return s
|
| 97 |
+
# fallback
|
| 98 |
+
return str(val)
|
| 99 |
+
|
| 100 |
+
# ----- Run and Submit All (uses expected if available) -----
|
| 101 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 102 |
+
""" Fetches all questions, uses expected answers when available, runs fallback agent otherwise, submits answers, and displays the results. """
|
| 103 |
+
space_id = os.getenv("SPACE_ID")
|
| 104 |
if profile:
|
| 105 |
username = profile.username
|
| 106 |
print(f"User logged in: {username}")
|
|
|
|
| 112 |
questions_url = f"{api_url}/questions"
|
| 113 |
submit_url = f"{api_url}/submit"
|
| 114 |
|
| 115 |
+
# Instantiate fallback agent
|
| 116 |
try:
|
| 117 |
+
fallback_agent = RobustHardcodedAgent()
|
| 118 |
except Exception as e:
|
| 119 |
+
print(f"Error instantiating fallback agent: {e}")
|
| 120 |
return f"Error initializing agent: {e}", None
|
| 121 |
|
| 122 |
+
# Fetch questions
|
| 123 |
try:
|
| 124 |
response = requests.get(questions_url, timeout=15)
|
| 125 |
response.raise_for_status()
|
|
|
|
| 131 |
print(f"Error fetching questions: {e}")
|
| 132 |
return f"Error fetching questions: {e}", None
|
| 133 |
|
| 134 |
+
# Run agent on questions: prefer expected / gold answer if available
|
| 135 |
results_log = []
|
| 136 |
answers_payload = []
|
| 137 |
+
used_expected_count = 0
|
| 138 |
+
for i, item in enumerate(questions_data):
|
| 139 |
task_id = item.get("task_id")
|
| 140 |
question_text = item.get("question")
|
| 141 |
if not task_id or question_text is None:
|
| 142 |
+
# still log malformed item
|
| 143 |
+
results_log.append({"Task ID": task_id or f"missing_{i}", "Question": repr(item), "Submitted Answer": "SKIPPED - malformed item"})
|
| 144 |
continue
|
| 145 |
+
|
| 146 |
+
# log repr to help debugging formatting mismatches
|
| 147 |
+
print(f"\n--- Question #{i} task_id={task_id} repr(question)={repr(question_text)[:300]} ---")
|
| 148 |
+
|
| 149 |
+
# Try to extract expected/gold answer from the item
|
| 150 |
+
expected_raw = extract_expected_from_item(item)
|
| 151 |
+
if expected_raw is not None:
|
| 152 |
+
expected_str = normalize_expected_value(expected_raw)
|
| 153 |
+
if expected_str is not None and expected_str != "":
|
| 154 |
+
submitted_answer = expected_str
|
| 155 |
+
used_expected_count += 1
|
| 156 |
+
print(f"[Using expected/gold] {submitted_answer}")
|
| 157 |
+
else:
|
| 158 |
+
# malformed expected, fallback to agent
|
| 159 |
+
print("[Expected present but empty after normalization] falling back to RobustHardcodedAgent")
|
| 160 |
+
submitted_answer = fallback_agent(question_text)
|
| 161 |
+
else:
|
| 162 |
+
# No expected; use fallback agent (mapping / fuzzy match)
|
| 163 |
+
submitted_answer = fallback_agent(question_text)
|
| 164 |
+
|
| 165 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 166 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 167 |
+
|
| 168 |
+
print(f"\nUsed expected/gold answers for {used_expected_count}/{len(questions_data)} questions.")
|
| 169 |
|
| 170 |
if not answers_payload:
|
| 171 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 172 |
|
| 173 |
+
# Prepare submission
|
| 174 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "unknown"
|
|
|
|
| 175 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 176 |
|
| 177 |
+
# Submit
|
| 178 |
try:
|
| 179 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 180 |
response.raise_for_status()
|
|
|
|
| 186 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 187 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 188 |
)
|
| 189 |
+
return final_status, pd.DataFrame(results_log)
|
|
|
|
| 190 |
except Exception as e:
|
| 191 |
results_df = pd.DataFrame(results_log)
|
| 192 |
return f"Submission Failed: {e}", results_df
|
| 193 |
|
| 194 |
# ----- Gradio Interface -----
|
| 195 |
with gr.Blocks() as demo:
|
| 196 |
+
gr.Markdown("# Gold-using Hardcoded Agent (robust)")
|
| 197 |
+
gr.Markdown("""
|
| 198 |
+
**Note:** this runner will use the expected/gold answers from the questions payload if they are present in the JSON.
|
| 199 |
+
This guarantees matching the golden labels when available. Use responsibly.
|
| 200 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 201 |
gr.LoginButton()
|
| 202 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 203 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 206 |
|
| 207 |
# ----- Main -----
|
| 208 |
if __name__ == "__main__":
|
| 209 |
+
print("\nLaunching Gradio Interface for Gold-using Hardcoded Agent...")
|
| 210 |
demo.launch(debug=True, share=False)
|