Spaces:
Sleeping
Sleeping
File size: 3,417 Bytes
10e9b7d eccf8e4 3c4371f fa566b6 0172cc8 0711fc0 3db6293 e80aab9 0172cc8 0711fc0 0172cc8 31243f4 0711fc0 0172cc8 0711fc0 0172cc8 0711fc0 0172cc8 0711fc0 5fd9d92 caa4b5d 31243f4 0711fc0 0172cc8 4021bf3 fa566b6 0711fc0 fa566b6 0172cc8 3c4371f 0711fc0 3c4371f caa4b5d 0711fc0 e80aab9 0711fc0 5fd9d92 0711fc0 0172cc8 fa566b6 0711fc0 e80aab9 0711fc0 0172cc8 fa566b6 0711fc0 0172cc8 0711fc0 fa566b6 0711fc0 fa566b6 0711fc0 fa566b6 0711fc0 fa566b6 0711fc0 0172cc8 e80aab9 0711fc0 5fd9d92 0711fc0 0172cc8 0711fc0 e80aab9 fa566b6 0711fc0 fa566b6 e80aab9 0711fc0 fa566b6 caa4b5d 0711fc0 caa4b5d e80aab9 7e4a06b fa566b6 5fd9d92 e80aab9 0711fc0 e80aab9 5fd9d92 31243f4 0711fc0 e80aab9 caa4b5d 0711fc0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | import os
import gradio as gr
import requests
import pandas as pd
from smolagents import (
CodeAgent,
DuckDuckGoSearchTool,
VisitWebpageTool,
InferenceClientModel
)
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# -----------------------------
# REAL AGENT (FIXED VERSION)
# -----------------------------
class BasicAgent:
def __init__(self):
print("Initializing real agent...")
# Model (Hugging Face hosted inference)
self.model = InferenceClientModel()
# Tools
tools = [
DuckDuckGoSearchTool(),
VisitWebpageTool()
]
# CodeAgent (actual reasoning agent)
self.agent = CodeAgent(
tools=tools,
model=self.model
)
def __call__(self, question: str) -> str:
print(f"Question: {question}")
try:
result = self.agent.run(question)
return str(result)
except Exception as e:
print(f"Agent error: {e}")
return f"ERROR: {e}"
# -----------------------------
# RUN + SUBMIT FUNCTION
# -----------------------------
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if profile:
username = profile.username
print(f"User logged in: {username}")
else:
return "Please login first using Hugging Face button.", None
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
# Create agent
agent = BasicAgent()
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
# Fetch questions
response = requests.get(questions_url, timeout=30)
response.raise_for_status()
questions_data = response.json()
results_log = []
answers_payload = []
# Run agent
for item in questions_data:
task_id = item["task_id"]
question = item["question"]
answer = agent(question)
answers_payload.append({
"task_id": task_id,
"submitted_answer": answer
})
results_log.append({
"Task ID": task_id,
"Question": question,
"Answer": answer
})
# Submit results
submission_data = {
"username": username,
"agent_code": agent_code,
"answers": answers_payload
}
submit_response = requests.post(submit_url, json=submission_data, timeout=60)
submit_response.raise_for_status()
result = submit_response.json()
status = (
f"Submission Successful!\n"
f"Score: {result.get('score')}%\n"
f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
f"Message: {result.get('message')}"
)
return status, pd.DataFrame(results_log)
# -----------------------------
# GRADIO UI
# -----------------------------
with gr.Blocks() as demo:
gr.Markdown("# Unit 4 Agent Evaluation (Fixed Version)")
gr.Markdown(
"1. Login with Hugging Face\n"
"2. Click Run Evaluation\n"
"3. Wait for scoring"
)
gr.LoginButton()
btn = gr.Button("Run Evaluation")
status = gr.Textbox(label="Status")
table = gr.DataFrame(label="Results")
btn.click(
fn=run_and_submit_all,
outputs=[status, table]
)
if __name__ == "__main__":
demo.launch() |