|
import os |
|
import time |
|
import gradio as gr |
|
import requests |
|
import pandas as pd |
|
import tiktoken |
|
|
|
from smolagents import CodeAgent, OpenAIServerModel |
|
from smolagents.tools import code_tools |
|
|
|
|
|
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
MAX_TOKENS = 3000 |
|
|
|
|
|
def token_length(text: str, model: str = "gpt-4") -> int: |
|
try: |
|
encoding = tiktoken.encoding_for_model(model) |
|
return len(encoding.encode(text)) |
|
except Exception: |
|
return len(text) // 4 |
|
|
|
|
|
class SmartGAIAAgent: |
|
def __init__(self): |
|
key = os.getenv("OPENAI_API_KEY") |
|
if not key: |
|
raise ValueError("Missing OPENAI_API_KEY") |
|
model = OpenAIServerModel(model_id="gpt-4", api_key=key) |
|
self.agent = CodeAgent( |
|
tools=code_tools(), |
|
model=model |
|
) |
|
|
|
def __call__(self, question: str) -> str: |
|
try: |
|
return self.agent.run(question).strip() |
|
except Exception as e: |
|
print("Agent error:", e) |
|
return "error" |
|
|
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
username = profile.username if profile else None |
|
if not username: |
|
return "Please login to Hugging Face", None |
|
|
|
try: |
|
agent = SmartGAIAAgent() |
|
except Exception as e: |
|
return f"Error initializing agent: {e}", None |
|
|
|
|
|
try: |
|
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15) |
|
resp.raise_for_status() |
|
questions = resp.json() |
|
except Exception as e: |
|
return f"Failed to fetch questions: {e}", None |
|
|
|
skip_kw = ['.mp3', '.wav', '.png', '.jpg', 'youtube', 'video', 'watch', 'listen'] |
|
payload, logs = [], [] |
|
|
|
for item in questions: |
|
tid = item.get("task_id") |
|
q = item.get("question", "") |
|
if not tid or not q: |
|
continue |
|
if token_length(q) > MAX_TOKENS or any(k in q.lower() for k in skip_kw): |
|
continue |
|
|
|
try: |
|
ans = agent(q) |
|
except Exception as e: |
|
print(f"[Error Task {tid}] {e}") |
|
ans = "error" |
|
|
|
payload.append({"task_id": tid, "submitted_answer": ans}) |
|
logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans}) |
|
|
|
if not payload: |
|
return "No valid questions to submit.", pd.DataFrame(logs) |
|
|
|
sub = { |
|
"username": username, |
|
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main", |
|
"answers": payload |
|
} |
|
|
|
try: |
|
resp = requests.post(f"{DEFAULT_API_URL}/submit", json=sub, timeout=60) |
|
resp.raise_for_status() |
|
result = resp.json() |
|
score = result.get("score") |
|
correct = result.get("correct_count") |
|
attempted = result.get("total_attempted") |
|
status = f"Score: {score}% ({correct}/{attempted})" |
|
except Exception as e: |
|
status = f"Submission failed: {e}" |
|
|
|
return status, pd.DataFrame(logs) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# GAIA Agent") |
|
gr.LoginButton() |
|
run_btn = gr.Button("Run & Submit") |
|
status = gr.Textbox(lines=4, label="Result") |
|
table = gr.DataFrame() |
|
run_btn.click(run_and_submit_all, outputs=[status, table]) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(debug=True, share=False) |