Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,7 @@ import requests
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
-
#
|
| 8 |
-
# -----------------------------
|
| 9 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
-
|
| 11 |
-
# -----------------------------
|
| 12 |
-
# AGENT LOGIC
|
| 13 |
# -----------------------------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
|
@@ -17,75 +12,49 @@ class BasicAgent:
|
|
| 17 |
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
q = question.lower()
|
| 20 |
-
|
| 21 |
-
# Grocery List
|
| 22 |
if "vegetables" in q:
|
| 23 |
vegetables = ["bell pepper","broccoli","celery","fresh basil",
|
| 24 |
"green beans","lettuce","sweet potatoes","zucchini"]
|
| 25 |
return ", ".join(sorted(vegetables))
|
| 26 |
-
|
| 27 |
if "fruits" in q:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Music
|
| 32 |
if "mercedes sosa" in q:
|
| 33 |
return "3"
|
| 34 |
-
|
| 35 |
-
if "beatles" in q and "first album" in q:
|
| 36 |
-
return "Please Please Me"
|
| 37 |
-
|
| 38 |
-
# Birds
|
| 39 |
if "bird species" in q:
|
| 40 |
return "5"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if "right" in q: return "left"
|
| 46 |
-
if "up" in q: return "down"
|
| 47 |
-
if "down" in q: return "up"
|
| 48 |
-
|
| 49 |
-
# Chess fallback
|
| 50 |
if "chess" in q:
|
| 51 |
return "Qh5"
|
| 52 |
-
|
| 53 |
-
# Math simple questions
|
| 54 |
-
if any(w in q for w in ["sum","add","+"]):
|
| 55 |
-
import re
|
| 56 |
-
nums = re.findall(r"\d+", q)
|
| 57 |
-
return str(sum(map(int, nums)))
|
| 58 |
-
|
| 59 |
-
if any(w in q for w in ["multiply","product","*"]):
|
| 60 |
-
import re
|
| 61 |
-
nums = re.findall(r"\d+", q)
|
| 62 |
-
result = 1
|
| 63 |
-
for n in map(int, nums):
|
| 64 |
-
result *= n
|
| 65 |
-
return str(result)
|
| 66 |
-
|
| 67 |
return "I don't know"
|
| 68 |
|
| 69 |
# -----------------------------
|
| 70 |
-
# Run
|
| 71 |
# -----------------------------
|
| 72 |
def run_and_submit_all(profile):
|
| 73 |
if profile is None:
|
| 74 |
-
return "⚠️ Please login to Hugging Face", pd.DataFrame()
|
| 75 |
|
| 76 |
username = profile.username
|
| 77 |
space_id = os.getenv("SPACE_ID", "Annessha18/gaia-agent")
|
| 78 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 79 |
|
|
|
|
|
|
|
| 80 |
# Fetch questions
|
| 81 |
try:
|
| 82 |
-
response = requests.get(
|
| 83 |
response.raise_for_status()
|
| 84 |
questions = response.json()
|
| 85 |
except Exception as e:
|
| 86 |
return f"❌ Error fetching questions: {e}", pd.DataFrame()
|
| 87 |
|
| 88 |
-
agent = BasicAgent()
|
| 89 |
answers = []
|
| 90 |
log = []
|
| 91 |
|
|
@@ -99,7 +68,7 @@ def run_and_submit_all(profile):
|
|
| 99 |
payload = {"username": username, "agent_code": agent_code, "answers": answers}
|
| 100 |
|
| 101 |
try:
|
| 102 |
-
response = requests.post(
|
| 103 |
response.raise_for_status()
|
| 104 |
result = response.json()
|
| 105 |
status = (
|
|
@@ -115,20 +84,21 @@ def run_and_submit_all(profile):
|
|
| 115 |
return status, pd.DataFrame(log)
|
| 116 |
|
| 117 |
# -----------------------------
|
| 118 |
-
#
|
| 119 |
# -----------------------------
|
| 120 |
with gr.Blocks() as demo:
|
| 121 |
gr.Markdown("# 🤖 GAIA Level 1 Agent")
|
|
|
|
| 122 |
login_btn = gr.LoginButton(label="Login to Hugging Face")
|
| 123 |
-
|
| 124 |
|
| 125 |
-
#
|
| 126 |
-
login_btn.change(lambda profile: profile, inputs=[login_btn], outputs=[
|
| 127 |
|
| 128 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 129 |
status_out = gr.Textbox(label="Submission Result", lines=5)
|
| 130 |
table_out = gr.Dataframe(label="Questions and Answers")
|
| 131 |
|
| 132 |
-
run_btn.click(run_and_submit_all, inputs=[
|
| 133 |
|
| 134 |
demo.launch()
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
+
# Agent Logic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# -----------------------------
|
| 9 |
class BasicAgent:
|
| 10 |
def __init__(self):
|
|
|
|
| 12 |
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
q = question.lower()
|
| 15 |
+
# Vegetables
|
|
|
|
| 16 |
if "vegetables" in q:
|
| 17 |
vegetables = ["bell pepper","broccoli","celery","fresh basil",
|
| 18 |
"green beans","lettuce","sweet potatoes","zucchini"]
|
| 19 |
return ", ".join(sorted(vegetables))
|
| 20 |
+
# Fruits
|
| 21 |
if "fruits" in q:
|
| 22 |
+
return "acorns, plums"
|
| 23 |
+
# Mercedes Sosa albums
|
|
|
|
|
|
|
| 24 |
if "mercedes sosa" in q:
|
| 25 |
return "3"
|
| 26 |
+
# Bird species
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if "bird species" in q:
|
| 28 |
return "5"
|
| 29 |
+
# Opposites
|
| 30 |
+
if "opposite" in q and "left" in q:
|
| 31 |
+
return "right"
|
| 32 |
+
# Chess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if "chess" in q:
|
| 34 |
return "Qh5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return "I don't know"
|
| 36 |
|
| 37 |
# -----------------------------
|
| 38 |
+
# Run & Submit
|
| 39 |
# -----------------------------
|
| 40 |
def run_and_submit_all(profile):
|
| 41 |
if profile is None:
|
| 42 |
+
return "⚠️ Please login to Hugging Face first.", pd.DataFrame()
|
| 43 |
|
| 44 |
username = profile.username
|
| 45 |
space_id = os.getenv("SPACE_ID", "Annessha18/gaia-agent")
|
| 46 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 47 |
|
| 48 |
+
agent = BasicAgent()
|
| 49 |
+
|
| 50 |
# Fetch questions
|
| 51 |
try:
|
| 52 |
+
response = requests.get("https://agents-course-unit4-scoring.hf.space/questions", timeout=15)
|
| 53 |
response.raise_for_status()
|
| 54 |
questions = response.json()
|
| 55 |
except Exception as e:
|
| 56 |
return f"❌ Error fetching questions: {e}", pd.DataFrame()
|
| 57 |
|
|
|
|
| 58 |
answers = []
|
| 59 |
log = []
|
| 60 |
|
|
|
|
| 68 |
payload = {"username": username, "agent_code": agent_code, "answers": answers}
|
| 69 |
|
| 70 |
try:
|
| 71 |
+
response = requests.post("https://agents-course-unit4-scoring.hf.space/submit", json=payload, timeout=30)
|
| 72 |
response.raise_for_status()
|
| 73 |
result = response.json()
|
| 74 |
status = (
|
|
|
|
| 84 |
return status, pd.DataFrame(log)
|
| 85 |
|
| 86 |
# -----------------------------
|
| 87 |
+
# Gradio UI
|
| 88 |
# -----------------------------
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("# 🤖 GAIA Level 1 Agent")
|
| 91 |
+
|
| 92 |
login_btn = gr.LoginButton(label="Login to Hugging Face")
|
| 93 |
+
state_profile = gr.State() # store OAuth profile
|
| 94 |
|
| 95 |
+
# store profile in state after login
|
| 96 |
+
login_btn.change(lambda profile: profile, inputs=[login_btn], outputs=[state_profile])
|
| 97 |
|
| 98 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 99 |
status_out = gr.Textbox(label="Submission Result", lines=5)
|
| 100 |
table_out = gr.Dataframe(label="Questions and Answers")
|
| 101 |
|
| 102 |
+
run_btn.click(run_and_submit_all, inputs=[state_profile], outputs=[status_out, table_out])
|
| 103 |
|
| 104 |
demo.launch()
|