First_agent / app.py
Israelbliz's picture
changed
8098f7a verified
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
# Constants
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# Basic Agent
class BasicAgent:
def __init__(self):
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise ValueError(
"OPENAI_API_KEY not set. Add it in HF → Settings → Secrets."
)
model = OpenAIServerModel(
model_id="gpt-4o-mini",
api_key=OPENAI_API_KEY
)
search_tool = DuckDuckGoSearchTool()
self.agent = CodeAgent(
model=model,
tools=[search_tool],
max_steps=3 # prevents infinite loops
)
def __call__(self, question: str) -> str:
return self.agent.run(question)
# Run + Submit
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if not profile:
return "Please login first.", 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"Error initializing agent: {e}", None
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
except Exception as e:
return f"Error fetching questions: {e}", None
results_log = []
answers_payload = []
for item in questions_data:
task_id = item.get("task_id")
question_text = item.get("question")
if not task_id or not question_text:
continue
try:
answer = agent(question_text)
except Exception as e:
answer = f"AGENT ERROR: {e}"
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
results_log.append({
"Task ID": task_id,
"Question": question_text,
"Submitted Answer": answer
})
if not answers_payload:
return "No answers generated.", pd.DataFrame(results_log)
submission_data = {
"username": username.strip(),
"agent_code": agent_code,
"answers": answers_payload
}
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
response.raise_for_status()
result = response.json()
final_status = (
f"Submission Successful!\n"
f"User: {result.get('username')}\n"
f"Score: {result.get('score', 'N/A')}% "
f"({result.get('correct_count', '?')}/"
f"{result.get('total_attempted', '?')})\n"
f"Message: {result.get('message', '')}"
)
return final_status, pd.DataFrame(results_log)
except Exception as e:
return f"Submission failed: {e}", pd.DataFrame(results_log)
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Basic Agent Evaluation Runner")
gr.Markdown("""
**Instructions**
1. Login with your Hugging Face account.
2. Click **Run Evaluation & Submit All Answers**.
3. Wait for the agent to finish.
**Requirements**
- Uses **OpenAI (gpt-4o-mini)**
- Requires `OPENAI_API_KEY` in HF Space Secrets
- Agent is **step-limited** (max 3 steps)
""")
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
demo.launch(debug=True, share=False)