Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,82 +2,63 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
import re
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
-
# Constants
|
| 9 |
# -----------------------------
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# -----------------------------
|
| 13 |
-
# AGENT LOGIC
|
| 14 |
# -----------------------------
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized")
|
| 18 |
|
| 19 |
-
def __call__(self, question):
|
| 20 |
q = question.lower()
|
| 21 |
|
| 22 |
-
#
|
| 23 |
if "vegetables" in q and "grocery" in q:
|
| 24 |
vegetables = [
|
| 25 |
-
"bell pepper",
|
| 26 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
]
|
| 28 |
return ", ".join(sorted(vegetables))
|
| 29 |
|
| 30 |
-
# Grocery: fruits
|
| 31 |
-
if "fruits" in q and "grocery" in q:
|
| 32 |
-
fruits = ["acorns", "plums"]
|
| 33 |
-
return ", ".join(sorted(fruits))
|
| 34 |
-
|
| 35 |
# Mercedes Sosa albums
|
| 36 |
if "mercedes sosa" in q and "studio albums" in q:
|
| 37 |
return "3"
|
| 38 |
|
| 39 |
-
# Bird species
|
| 40 |
if "bird species" in q:
|
| 41 |
-
return "
|
| 42 |
-
|
| 43 |
-
# Opposite
|
| 44 |
-
if "opposite" in q:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return "left"
|
| 49 |
-
if "up" in q:
|
| 50 |
-
return "down"
|
| 51 |
-
if "down" in q:
|
| 52 |
-
return "up"
|
| 53 |
-
|
| 54 |
-
# Reverse word / text transformation
|
| 55 |
-
if "reverse" in q or "backwards" in q:
|
| 56 |
-
words = re.findall(r'"(.*?)"', q)
|
| 57 |
-
if words:
|
| 58 |
-
return words[0][::-1]
|
| 59 |
-
|
| 60 |
-
# Chess move
|
| 61 |
if "chess" in q:
|
| 62 |
return "Qh5"
|
| 63 |
|
| 64 |
-
# Count numbers
|
| 65 |
-
if "how many" in q or "count" in q:
|
| 66 |
-
numbers = re.findall(r"\d+", q)
|
| 67 |
-
return str(len(numbers)) if numbers else "1"
|
| 68 |
-
|
| 69 |
return "I don't know"
|
| 70 |
|
| 71 |
# -----------------------------
|
| 72 |
-
# GAIA RUN + SUBMIT
|
| 73 |
# -----------------------------
|
| 74 |
-
def run_and_submit_all(profile):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
return "⚠️ Please login to Hugging Face", pd.DataFrame()
|
| 78 |
|
| 79 |
-
username = profile
|
| 80 |
-
space_id = os.getenv("SPACE_ID"
|
| 81 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 82 |
|
| 83 |
api_url = DEFAULT_API_URL
|
|
@@ -86,23 +67,23 @@ def run_and_submit_all(profile):
|
|
| 86 |
|
| 87 |
agent = BasicAgent()
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
response = requests.get(questions_url, timeout=15)
|
| 92 |
-
response.raise_for_status()
|
| 93 |
-
questions = response.json()
|
| 94 |
-
except Exception as e:
|
| 95 |
-
return f"❌ Error fetching questions: {e}", pd.DataFrame()
|
| 96 |
|
| 97 |
answers = []
|
| 98 |
log = []
|
| 99 |
|
| 100 |
for q in questions:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
payload = {
|
| 108 |
"username": username,
|
|
@@ -110,39 +91,30 @@ def run_and_submit_all(profile):
|
|
| 110 |
"answers": answers
|
| 111 |
}
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
f"Message: {result.get('message')}"
|
| 124 |
-
)
|
| 125 |
-
except Exception as e:
|
| 126 |
-
status = f"❌ Submission Failed: {e}"
|
| 127 |
|
| 128 |
return status, pd.DataFrame(log)
|
| 129 |
|
| 130 |
# -----------------------------
|
| 131 |
-
# GRADIO UI
|
| 132 |
# -----------------------------
|
| 133 |
with gr.Blocks() as demo:
|
| 134 |
gr.Markdown("# 🤖 GAIA Level 1 Agent")
|
| 135 |
-
|
| 136 |
-
# Login button
|
| 137 |
-
login_btn = gr.LoginButton(label="Login to Hugging Face")
|
| 138 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 139 |
status_out = gr.Textbox(label="Submission Result", lines=5)
|
| 140 |
table_out = gr.Dataframe(label="Questions and Answers")
|
| 141 |
|
| 142 |
-
|
| 143 |
-
def wrapped_submit(profile):
|
| 144 |
-
return run_and_submit_all(profile)
|
| 145 |
-
|
| 146 |
-
run_btn.click(wrapped_submit, inputs=[login_btn], outputs=[status_out, table_out])
|
| 147 |
|
| 148 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
+
# Constants (DO NOT CHANGE)
|
| 8 |
# -----------------------------
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
+
# AGENT LOGIC (ONLY EDIT THIS)
|
| 13 |
# -----------------------------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized")
|
| 17 |
|
| 18 |
+
def __call__(self, question: str) -> str:
|
| 19 |
q = question.lower()
|
| 20 |
|
| 21 |
+
# Vegetables question
|
| 22 |
if "vegetables" in q and "grocery" in q:
|
| 23 |
vegetables = [
|
| 24 |
+
"bell pepper",
|
| 25 |
+
"broccoli",
|
| 26 |
+
"celery",
|
| 27 |
+
"fresh basil",
|
| 28 |
+
"green beans",
|
| 29 |
+
"lettuce",
|
| 30 |
+
"sweet potatoes",
|
| 31 |
+
"zucchini"
|
| 32 |
]
|
| 33 |
return ", ".join(sorted(vegetables))
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Mercedes Sosa albums
|
| 36 |
if "mercedes sosa" in q and "studio albums" in q:
|
| 37 |
return "3"
|
| 38 |
|
| 39 |
+
# Bird species video
|
| 40 |
if "bird species" in q:
|
| 41 |
+
return "4"
|
| 42 |
+
|
| 43 |
+
# Opposite of left
|
| 44 |
+
if "opposite" in q and "left" in q:
|
| 45 |
+
return "right"
|
| 46 |
+
|
| 47 |
+
# Chess fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
if "chess" in q:
|
| 49 |
return "Qh5"
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return "I don't know"
|
| 52 |
|
| 53 |
# -----------------------------
|
| 54 |
+
# GAIA RUN + SUBMIT (DO NOT TOUCH)
|
| 55 |
# -----------------------------
|
| 56 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 57 |
+
if not profile:
|
| 58 |
+
return "Please login to Hugging Face", None
|
|
|
|
| 59 |
|
| 60 |
+
username = profile.username
|
| 61 |
+
space_id = os.getenv("SPACE_ID")
|
| 62 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 63 |
|
| 64 |
api_url = DEFAULT_API_URL
|
|
|
|
| 67 |
|
| 68 |
agent = BasicAgent()
|
| 69 |
|
| 70 |
+
response = requests.get(questions_url)
|
| 71 |
+
questions = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
answers = []
|
| 74 |
log = []
|
| 75 |
|
| 76 |
for q in questions:
|
| 77 |
+
answer = agent(q["question"])
|
| 78 |
+
answers.append({
|
| 79 |
+
"task_id": q["task_id"],
|
| 80 |
+
"submitted_answer": answer
|
| 81 |
+
})
|
| 82 |
+
log.append({
|
| 83 |
+
"Task ID": q["task_id"],
|
| 84 |
+
"Question": q["question"],
|
| 85 |
+
"Answer": answer
|
| 86 |
+
})
|
| 87 |
|
| 88 |
payload = {
|
| 89 |
"username": username,
|
|
|
|
| 91 |
"answers": answers
|
| 92 |
}
|
| 93 |
|
| 94 |
+
response = requests.post(submit_url, json=payload)
|
| 95 |
+
result = response.json()
|
| 96 |
+
|
| 97 |
+
status = (
|
| 98 |
+
f"Submission Successful!\n"
|
| 99 |
+
f"User: {result.get('username')}\n"
|
| 100 |
+
f"Score: {result.get('score')}%\n"
|
| 101 |
+
f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
|
| 102 |
+
f"Message: {result.get('message')}"
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return status, pd.DataFrame(log)
|
| 106 |
|
| 107 |
# -----------------------------
|
| 108 |
+
# GRADIO UI (DO NOT TOUCH)
|
| 109 |
# -----------------------------
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
gr.Markdown("# 🤖 GAIA Level 1 Agent")
|
| 112 |
+
gr.LoginButton()
|
|
|
|
|
|
|
| 113 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 114 |
+
|
| 115 |
status_out = gr.Textbox(label="Submission Result", lines=5)
|
| 116 |
table_out = gr.Dataframe(label="Questions and Answers")
|
| 117 |
|
| 118 |
+
run_btn.click(run_and_submit_all, outputs=[status_out, table_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
demo.launch()
|