Spaces:
Runtime error
Runtime error
# app.py | |
import os | |
import gradio as gr | |
import requests | |
import pandas as pd | |
from bs4 import BeautifulSoup | |
from smolagents import CodeAgent, tool, InferenceClientModel | |
# --- Constants --- | |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
# --- Custom Web Search Tool (avoiding proxies) --- | |
def simple_search(query: str) -> str: | |
""" | |
Search DuckDuckGo via HTML parsing. | |
Args: | |
query (str): search query string. | |
Returns: | |
str: top 3 titles and URLs. | |
""" | |
try: | |
resp = requests.get( | |
"https://html.duckduckgo.com/html/", | |
params={"q": query}, | |
timeout=10 | |
) | |
resp.raise_for_status() | |
soup = BeautifulSoup(resp.text, "html.parser") | |
results = [] | |
for a in soup.select("a.result__a")[:3]: | |
title = a.get_text() | |
url = a["href"] | |
results.append(f"- {title}\n {url}") | |
return "\n".join(results) if results else "No results." | |
except Exception as e: | |
return f"Search error: {e}" | |
# --- Enhanced Agent --- | |
class BasicAgent: | |
def __init__(self): | |
print("BasicAgent initialized.") | |
self.model = InferenceClientModel() | |
self.agent = CodeAgent( | |
model=self.model, | |
tools=[simple_search] | |
) | |
def __call__(self, question: str) -> str: | |
print("Received:", question[:50], "...") | |
try: | |
return self.agent.run(question) | |
except Exception as e: | |
return f"Agent run error: {e}" | |
def run_and_submit_all(profile: gr.OAuthProfile | None): | |
space_id = os.getenv("SPACE_ID") | |
if not profile: | |
return "Please login to Hugging Face", None | |
username = profile.username | |
questions_url = f"{DEFAULT_API_URL}/questions" | |
submit_url = f"{DEFAULT_API_URL}/submit" | |
try: agent = BasicAgent() | |
except Exception as e: | |
return f"Agent init error: {e}", None | |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" | |
try: | |
r = requests.get(questions_url, timeout=15) | |
r.raise_for_status() | |
qs = r.json() | |
except Exception as e: | |
return f"Fetch questions error: {e}", None | |
logs, payload = [], [] | |
for item in qs: | |
tid, q = item.get("task_id"), item.get("question") | |
if not tid or q is None: continue | |
ans = agent(q) | |
payload.append({"task_id": tid, "submitted_answer": ans}) | |
logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans}) | |
if not payload: | |
return "No answers generated.", pd.DataFrame(logs) | |
submit = {"username": username, "agent_code": agent_code, "answers": payload} | |
try: | |
resp = requests.post(submit_url, json=submit, timeout=60) | |
resp.raise_for_status() | |
data = resp.json() | |
status = ( | |
f"✅ Score: {data.get('score','N/A')}% " | |
f"({data.get('correct_count','?')}/{data.get('total_attempted','?')} correct)\n" | |
f"{data.get('message','No message')}" | |
) | |
return status, pd.DataFrame(logs) | |
except Exception as e: | |
return f"Submit error: {e}", pd.DataFrame(logs) | |
# --- Gradio UI --- | |
with gr.Blocks() as demo: | |
gr.Markdown("# GAIA Evaluation Runner") | |
gr.LoginButton() | |
run_btn = gr.Button("Run Evaluation & Submit") | |
status = gr.Textbox(lines=5, interactive=False) | |
results = gr.DataFrame(wrap=True) | |
run_btn.click(run_and_submit_all, outputs=[status, results]) | |
if __name__ == "__main__": | |
demo.launch(debug=True, share=False) | |