wahibtim's picture
Update app.py
e875e25 verified
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class BasicAgent:
def __init__(self):
print("Initializing agent via OpenRouter...")
# Uses OpenRouter β€” free $1 credit, no HF credit consumption
self.agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=OpenAIServerModel(
model_id="llama-3.3-70b-versatile",
api_base="https://api.groq.com/openai/v1",
api_key=os.environ["GROQ_API_KEY"],
),
max_steps=4,
verbosity_level=0,
)
print("Agent ready.")
def __call__(self, question: str) -> str:
print(f"Q: {question[:80]}...")
try:
result = self.agent.run(question)
answer = str(result).strip()
print(f"A: {answer[:100]}")
return answer
except Exception as e:
print(f"Error: {e}")
return "I don't know."
def run_and_submit_all(profile: gr.OAuthProfile | None):
if not profile:
return "Please login with your Hugging Face account first.", None
username = profile.username
space_id = os.getenv("SPACE_ID", "unknown/space")
api_url = DEFAULT_API_URL
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
try:
agent = BasicAgent()
except Exception as e:
return f"Agent init failed: {e}", None
try:
resp = requests.get(f"{api_url}/questions", timeout=15)
resp.raise_for_status()
questions_data = resp.json()
print(f"Fetched {len(questions_data)} questions.")
except Exception as e:
return f"Failed to fetch 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 question_text is None:
continue
try:
answer = agent(question_text)
except Exception as e:
answer = "I don't know."
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
results_log.append({"Task ID": task_id, "Question": question_text, "Answer": answer})
if not answers_payload:
return "No answers produced.", pd.DataFrame(results_log)
submission = {
"username": username.strip(),
"agent_code": agent_code,
"answers": answers_payload
}
try:
resp = requests.post(f"{api_url}/submit", json=submission, timeout=120)
resp.raise_for_status()
r = resp.json()
status = (
f"βœ… Submitted!\n"
f"User: {r.get('username')}\n"
f"Score: {r.get('score', 'N/A')}% "
f"({r.get('correct_count', '?')}/{r.get('total_attempted', '?')} correct)\n"
f"Message: {r.get('message', '')}"
)
return status, pd.DataFrame(results_log)
except Exception as e:
return f"Submission failed: {e}", pd.DataFrame(results_log)
with gr.Blocks() as demo:
gr.Markdown("# πŸ€– HF Agents Course β€” Final Assignment")
gr.Markdown("""
1. Login with your HF account below
2. Click **Run Evaluation** and wait ~3–5 min
3. Results and score appear below
""")
gr.LoginButton()
run_btn = gr.Button("πŸš€ Run Evaluation & Submit All Answers")
status_out = gr.Textbox(label="Result", lines=6, interactive=False)
results_table = gr.DataFrame(label="Answers", wrap=True)
run_btn.click(fn=run_and_submit_all, outputs=[status_out, results_table])
if __name__ == "__main__":
demo.launch(debug=True, share=False)