Update app.py
Browse files
app.py
CHANGED
@@ -3,36 +3,21 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
-
|
|
|
7 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
duck_result = ""
|
21 |
-
try:
|
22 |
-
wiki_result = self.wiki.run(question)
|
23 |
-
except Exception as e:
|
24 |
-
wiki_result = f"(Wiki error: {e})"
|
25 |
-
# If Wikipedia yields nothing or too short, use DuckDuckGo as fallback
|
26 |
-
if wiki_result and len(str(wiki_result)) > 40:
|
27 |
-
return str(wiki_result)
|
28 |
-
try:
|
29 |
-
duck_result = self.duck.run(question)
|
30 |
-
except Exception as e:
|
31 |
-
duck_result = f"(DuckDuckGo error: {e})"
|
32 |
-
if duck_result:
|
33 |
-
return str(duck_result)
|
34 |
-
# If both fail, return error info
|
35 |
-
return f"Wikipedia: {wiki_result}\nDuckDuckGo: {duck_result}\n(No answer found.)"
|
36 |
|
37 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
38 |
space_id = os.getenv("SPACE_ID")
|
@@ -47,23 +32,31 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
47 |
questions_url = f"{api_url}/questions"
|
48 |
submit_url = f"{api_url}/submit"
|
49 |
|
|
|
50 |
try:
|
51 |
-
agent =
|
52 |
except Exception as e:
|
53 |
return f"Error initializing agent: {e}", None
|
|
|
54 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
55 |
print(agent_code)
|
56 |
|
|
|
57 |
print(f"Fetching questions from: {questions_url}")
|
58 |
try:
|
59 |
response = requests.get(questions_url, timeout=15)
|
60 |
response.raise_for_status()
|
61 |
questions_data = response.json()
|
62 |
if not questions_data:
|
|
|
63 |
return "Fetched questions list is empty or invalid format.", None
|
64 |
-
|
|
|
65 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
66 |
|
|
|
67 |
results_log = []
|
68 |
answers_payload = []
|
69 |
print(f"Running agent on {len(questions_data)} questions...")
|
@@ -71,21 +64,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
71 |
task_id = item.get("task_id")
|
72 |
question_text = item.get("question")
|
73 |
if not task_id or question_text is None:
|
|
|
74 |
continue
|
75 |
try:
|
76 |
submitted_answer = agent(question_text)
|
77 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
78 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
79 |
except Exception as e:
|
80 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"
|
81 |
|
82 |
if not answers_payload:
|
83 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
84 |
|
85 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
86 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
87 |
print(status_update)
|
88 |
|
|
|
|
|
89 |
try:
|
90 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
91 |
response.raise_for_status()
|
@@ -100,45 +100,52 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
100 |
results_df = pd.DataFrame(results_log)
|
101 |
return final_status, results_df
|
102 |
except Exception as e:
|
|
|
103 |
results_df = pd.DataFrame(results_log)
|
104 |
-
return
|
105 |
|
106 |
-
# ---
|
107 |
with gr.Blocks() as demo:
|
108 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
109 |
gr.Markdown(
|
110 |
"""
|
111 |
**Instructions:**
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
117 |
"""
|
118 |
)
|
119 |
-
|
120 |
gr.LoginButton()
|
121 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
122 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
123 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
124 |
run_button.click(
|
125 |
fn=run_and_submit_all,
|
126 |
outputs=[status_output, results_table]
|
127 |
)
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
-
print("\n
|
131 |
space_host_startup = os.getenv("SPACE_HOST")
|
132 |
space_id_startup = os.getenv("SPACE_ID")
|
|
|
133 |
if space_host_startup:
|
134 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
135 |
-
print(f" Runtime URL: https://{space_host_startup}.hf.space")
|
136 |
else:
|
137 |
print("ℹ️ SPACE_HOST not found (running locally?)")
|
|
|
138 |
if space_id_startup:
|
139 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
140 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}
|
|
|
141 |
else:
|
142 |
print("ℹ️ SPACE_ID not found")
|
143 |
-
|
|
|
|
|
144 |
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
+
from smolagents.agents import ToolCallingAgent
|
7 |
+
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
10 |
+
def create_agent():
|
11 |
+
"""
|
12 |
+
Create a multi-tool agent with SmolAgents.
|
13 |
+
"""
|
14 |
+
# You specify the tool names as strings!
|
15 |
+
agent = ToolCallingAgent(
|
16 |
+
tools=["wikipedia", "duckduckgo", "web_search"],
|
17 |
+
model="huggingface",
|
18 |
+
model_kwargs={"repo_id": "HuggingFaceH4/zephyr-7b-beta"},
|
19 |
+
)
|
20 |
+
return agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
23 |
space_id = os.getenv("SPACE_ID")
|
|
|
32 |
questions_url = f"{api_url}/questions"
|
33 |
submit_url = f"{api_url}/submit"
|
34 |
|
35 |
+
# Create agent with all relevant tools
|
36 |
try:
|
37 |
+
agent = create_agent()
|
38 |
except Exception as e:
|
39 |
return f"Error initializing agent: {e}", None
|
40 |
+
|
41 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
42 |
print(agent_code)
|
43 |
|
44 |
+
# Fetch Questions
|
45 |
print(f"Fetching questions from: {questions_url}")
|
46 |
try:
|
47 |
response = requests.get(questions_url, timeout=15)
|
48 |
response.raise_for_status()
|
49 |
questions_data = response.json()
|
50 |
if not questions_data:
|
51 |
+
print("Fetched questions list is empty.")
|
52 |
return "Fetched questions list is empty or invalid format.", None
|
53 |
+
print(f"Fetched {len(questions_data)} questions.")
|
54 |
+
except requests.exceptions.RequestException as e:
|
55 |
return f"Error fetching questions: {e}", None
|
56 |
+
except Exception as e:
|
57 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
58 |
|
59 |
+
# Run the agent on all questions
|
60 |
results_log = []
|
61 |
answers_payload = []
|
62 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
64 |
task_id = item.get("task_id")
|
65 |
question_text = item.get("question")
|
66 |
if not task_id or question_text is None:
|
67 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
68 |
continue
|
69 |
try:
|
70 |
submitted_answer = agent(question_text)
|
71 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
72 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
73 |
except Exception as e:
|
74 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"(Agent error: {e})"})
|
75 |
|
76 |
if not answers_payload:
|
77 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
78 |
|
79 |
+
submission_data = {
|
80 |
+
"username": username.strip(),
|
81 |
+
"agent_code": agent_code,
|
82 |
+
"answers": answers_payload
|
83 |
+
}
|
84 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
85 |
print(status_update)
|
86 |
|
87 |
+
# Submit answers
|
88 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
89 |
try:
|
90 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
91 |
response.raise_for_status()
|
|
|
100 |
results_df = pd.DataFrame(results_log)
|
101 |
return final_status, results_df
|
102 |
except Exception as e:
|
103 |
+
status_message = f"Submission Failed: {e}"
|
104 |
results_df = pd.DataFrame(results_log)
|
105 |
+
return status_message, results_df
|
106 |
|
107 |
+
# --- Gradio UI ---
|
108 |
with gr.Blocks() as demo:
|
109 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
110 |
gr.Markdown(
|
111 |
"""
|
112 |
**Instructions:**
|
113 |
+
|
114 |
+
- Clone and modify this space to improve your agent logic as you see fit.
|
115 |
+
- Log in to your Hugging Face account with the button below.
|
116 |
+
- Click 'Run Evaluation & Submit All Answers' to begin.
|
117 |
+
|
118 |
+
Disclaimer: Submission may take a while depending on the number of questions and agent speed.
|
119 |
"""
|
120 |
)
|
|
|
121 |
gr.LoginButton()
|
122 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
123 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
124 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
125 |
+
|
126 |
run_button.click(
|
127 |
fn=run_and_submit_all,
|
128 |
outputs=[status_output, results_table]
|
129 |
)
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
133 |
space_host_startup = os.getenv("SPACE_HOST")
|
134 |
space_id_startup = os.getenv("SPACE_ID")
|
135 |
+
|
136 |
if space_host_startup:
|
137 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
138 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
139 |
else:
|
140 |
print("ℹ️ SPACE_HOST not found (running locally?)")
|
141 |
+
|
142 |
if space_id_startup:
|
143 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
144 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
145 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
146 |
else:
|
147 |
print("ℹ️ SPACE_ID not found")
|
148 |
+
|
149 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
150 |
+
print("Launching Gradio Interface for SmolAgent Evaluation...")
|
151 |
demo.launch(debug=True, share=False)
|