Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,98 +2,85 @@ import os
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
-
from
|
6 |
-
|
7 |
-
from smolagents import GradioUI, CodeAgent, HfApiModel
|
8 |
from smolagents.tools import Tool
|
9 |
-
from
|
10 |
-
|
11 |
|
12 |
# --- Constants ---
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
14 |
|
15 |
-
|
16 |
-
# --- Custom Tool using DuckDuckGo ---
|
17 |
class CustomSearchTool(Tool):
|
18 |
name = "duckduckgo_search"
|
19 |
-
description = "
|
20 |
-
inputs =
|
21 |
-
outputs =
|
22 |
-
|
23 |
-
def __init__(self):
|
24 |
-
self.search = DuckDuckGoSearchRun()
|
25 |
-
super().__init__()
|
26 |
|
27 |
def run(self, query: str) -> str:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
model=model,
|
38 |
-
add_base_tools=True,
|
39 |
-
planning_interval=3,
|
40 |
-
)
|
41 |
-
|
42 |
|
43 |
-
# ---
|
44 |
-
class BasicAgent:
|
45 |
def __init__(self):
|
46 |
-
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
-
print(f"Received question: {question}")
|
51 |
-
messages = [HumanMessage(content=question)]
|
52 |
-
result = self.graph.invoke({"messages": messages})
|
53 |
-
answer = result['messages'][-1].content
|
54 |
-
print(f"Returning answer: {answer}")
|
55 |
-
return answer[14:] # optional slicing depending on model behavior
|
56 |
-
|
57 |
-
|
58 |
-
# --- Evaluation Logic ---
|
59 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
60 |
space_id = os.getenv("SPACE_ID")
|
61 |
|
62 |
if profile:
|
63 |
-
username = profile.username
|
64 |
-
print(f"
|
65 |
else:
|
66 |
-
|
|
|
67 |
|
68 |
api_url = DEFAULT_API_URL
|
69 |
questions_url = f"{api_url}/questions"
|
70 |
submit_url = f"{api_url}/submit"
|
71 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
72 |
|
73 |
try:
|
74 |
-
|
75 |
except Exception as e:
|
76 |
-
|
|
|
77 |
|
78 |
-
|
|
|
|
|
|
|
|
|
79 |
try:
|
80 |
response = requests.get(questions_url, timeout=15)
|
81 |
response.raise_for_status()
|
82 |
questions_data = response.json()
|
|
|
|
|
83 |
except Exception as e:
|
84 |
return f"Error fetching questions: {e}", None
|
85 |
|
86 |
-
# 2. Run Agent
|
87 |
results_log = []
|
88 |
answers_payload = []
|
89 |
|
|
|
90 |
for item in questions_data:
|
91 |
task_id = item.get("task_id")
|
92 |
question_text = item.get("question")
|
93 |
if not task_id or question_text is None:
|
94 |
continue
|
95 |
try:
|
96 |
-
submitted_answer =
|
97 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
98 |
results_log.append({
|
99 |
"Task ID": task_id,
|
@@ -108,55 +95,64 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
108 |
})
|
109 |
|
110 |
if not answers_payload:
|
111 |
-
return "Agent did not produce answers.", pd.DataFrame(results_log)
|
112 |
|
113 |
-
#
|
114 |
submission_data = {
|
115 |
"username": username.strip(),
|
116 |
"agent_code": agent_code,
|
117 |
"answers": answers_payload
|
118 |
}
|
119 |
-
|
120 |
try:
|
121 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
122 |
response.raise_for_status()
|
123 |
result_data = response.json()
|
124 |
-
|
125 |
final_status = (
|
126 |
-
f"
|
127 |
f"User: {result_data.get('username')}\n"
|
128 |
-
f"Score: {result_data.get('score', 'N/A')}
|
129 |
-
f"
|
130 |
f"Message: {result_data.get('message', 'No message received.')}"
|
131 |
)
|
132 |
return final_status, pd.DataFrame(results_log)
|
133 |
except Exception as e:
|
134 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
135 |
|
136 |
-
|
137 |
-
# --- Gradio Interface ---
|
138 |
with gr.Blocks() as demo:
|
139 |
-
gr.Markdown("
|
140 |
gr.Markdown(
|
141 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
)
|
143 |
|
144 |
gr.LoginButton()
|
145 |
-
run_button = gr.Button("▶️ Run Evaluation & Submit All Answers")
|
146 |
-
status_output = gr.Textbox(label="Submission Status", lines=5, interactive=False)
|
147 |
-
results_table = gr.DataFrame(label="Agent Answers")
|
148 |
|
149 |
-
run_button.
|
150 |
-
|
151 |
-
|
152 |
-
)
|
153 |
|
|
|
154 |
|
155 |
-
# ---
|
156 |
if __name__ == "__main__":
|
157 |
-
print("
|
|
|
158 |
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
159 |
if space_id:
|
160 |
-
print(f"
|
161 |
-
print(f"Repo: https://huggingface.co/spaces/{space_id}
|
162 |
-
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
+
from smolagents import SmolAgent
|
6 |
+
from smolagents.models import InferenceClientModel
|
|
|
7 |
from smolagents.tools import Tool
|
8 |
+
from duckduckgo_search import DDGS
|
|
|
9 |
|
10 |
# --- Constants ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
+
# --- Custom Search Tool ---
|
|
|
14 |
class CustomSearchTool(Tool):
|
15 |
name = "duckduckgo_search"
|
16 |
+
description = "A simple tool that performs web search using DuckDuckGo."
|
17 |
+
inputs = {"query": str}
|
18 |
+
outputs = str
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def run(self, query: str) -> str:
|
21 |
+
print(f"[SearchTool] Searching DuckDuckGo for: {query}")
|
22 |
+
try:
|
23 |
+
with DDGS() as ddgs:
|
24 |
+
results = ddgs.text(query, max_results=3)
|
25 |
+
output = "\n".join([r["body"] for r in results if "body" in r])
|
26 |
+
return output.strip() or "No results found."
|
27 |
+
except Exception as e:
|
28 |
+
print(f"[SearchTool] Search failed: {e}")
|
29 |
+
return f"Search error: {e}"
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# --- BasicAgent Class ---
|
32 |
+
class BasicAgent(SmolAgent):
|
33 |
def __init__(self):
|
34 |
+
model = InferenceClientModel("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
35 |
+
tools = [CustomSearchTool()]
|
36 |
+
super().__init__(model=model, tools=tools)
|
37 |
|
38 |
+
# --- Submission + Evaluation ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
40 |
space_id = os.getenv("SPACE_ID")
|
41 |
|
42 |
if profile:
|
43 |
+
username = f"{profile.username}"
|
44 |
+
print(f"User logged in: {username}")
|
45 |
else:
|
46 |
+
print("User not logged in.")
|
47 |
+
return "Please Login to Hugging Face with the button.", None
|
48 |
|
49 |
api_url = DEFAULT_API_URL
|
50 |
questions_url = f"{api_url}/questions"
|
51 |
submit_url = f"{api_url}/submit"
|
|
|
52 |
|
53 |
try:
|
54 |
+
agent = BasicAgent()
|
55 |
except Exception as e:
|
56 |
+
print(f"Error instantiating agent: {e}")
|
57 |
+
return f"Error initializing agent: {e}", None
|
58 |
|
59 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
60 |
+
print(f"Agent code: {agent_code}")
|
61 |
+
|
62 |
+
# Fetch questions
|
63 |
+
print(f"Fetching questions from: {questions_url}")
|
64 |
try:
|
65 |
response = requests.get(questions_url, timeout=15)
|
66 |
response.raise_for_status()
|
67 |
questions_data = response.json()
|
68 |
+
if not questions_data:
|
69 |
+
return "Fetched questions list is empty or invalid format.", None
|
70 |
except Exception as e:
|
71 |
return f"Error fetching questions: {e}", None
|
72 |
|
|
|
73 |
results_log = []
|
74 |
answers_payload = []
|
75 |
|
76 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
77 |
for item in questions_data:
|
78 |
task_id = item.get("task_id")
|
79 |
question_text = item.get("question")
|
80 |
if not task_id or question_text is None:
|
81 |
continue
|
82 |
try:
|
83 |
+
submitted_answer = agent(question_text)
|
84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
85 |
results_log.append({
|
86 |
"Task ID": task_id,
|
|
|
95 |
})
|
96 |
|
97 |
if not answers_payload:
|
98 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
99 |
|
100 |
+
# Submit
|
101 |
submission_data = {
|
102 |
"username": username.strip(),
|
103 |
"agent_code": agent_code,
|
104 |
"answers": answers_payload
|
105 |
}
|
|
|
106 |
try:
|
107 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
108 |
response.raise_for_status()
|
109 |
result_data = response.json()
|
|
|
110 |
final_status = (
|
111 |
+
f"Submission Successful!\n"
|
112 |
f"User: {result_data.get('username')}\n"
|
113 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
114 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
115 |
f"Message: {result_data.get('message', 'No message received.')}"
|
116 |
)
|
117 |
return final_status, pd.DataFrame(results_log)
|
118 |
except Exception as e:
|
119 |
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
120 |
|
121 |
+
# --- Gradio UI ---
|
|
|
122 |
with gr.Blocks() as demo:
|
123 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
124 |
gr.Markdown(
|
125 |
+
"""
|
126 |
+
**Instructions:**
|
127 |
+
1. Clone this space, then modify your agent logic, tools, and setup.
|
128 |
+
2. Log in to your Hugging Face account below.
|
129 |
+
3. Click "Run Evaluation & Submit All Answers" to start.
|
130 |
+
---
|
131 |
+
**Note:** This is intentionally basic. Improve and optimize your agent for better scores.
|
132 |
+
"""
|
133 |
)
|
134 |
|
135 |
gr.LoginButton()
|
|
|
|
|
|
|
136 |
|
137 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
138 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
139 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
140 |
|
141 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
142 |
|
143 |
+
# --- Launch ---
|
144 |
if __name__ == "__main__":
|
145 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
146 |
+
space_host = os.getenv("SPACE_HOST")
|
147 |
space_id = os.getenv("SPACE_ID")
|
148 |
+
|
149 |
+
if space_host:
|
150 |
+
print(f"✅ SPACE_HOST found: {space_host}")
|
151 |
+
print(f" Runtime URL: https://{space_host}.hf.space")
|
152 |
if space_id:
|
153 |
+
print(f"✅ SPACE_ID found: {space_id}")
|
154 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
155 |
+
print("-" * 70 + "\n")
|
156 |
+
|
157 |
+
print("Launching Gradio Interface...")
|
158 |
+
demo.launch(debug=True, share=False)
|