Spaces:
Sleeping
Sleeping
final answer manager and web agents
Browse files- .env.example +4 -0
- .gitignore +1 -0
- __pycache__/agents.cpython-310.pyc +0 -0
- agents.py +182 -0
- app.py +33 -8
- data/gaia_validation.jsonl +0 -0
- requirements.txt +5 -1
.env.example
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
SPACE_ID=
|
2 |
+
HF_TOKEN=
|
3 |
+
OPENAI_API_KEY=
|
4 |
+
SERPAPI_API_KEY=
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
__pycache__/agents.cpython-310.pyc
ADDED
Binary file (6.08 kB). View file
|
|
agents.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool
|
5 |
+
from smolagents.tools import tool
|
6 |
+
import markdownify
|
7 |
+
|
8 |
+
MANAGER_MODEL = "deepseek-ai/DeepSeek-R1"
|
9 |
+
AGENT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
10 |
+
FINAL_ANSWER_MODEL = "deepseek-ai/DeepSeek-R1" # OpenAIServerModel
|
11 |
+
WEB_SEARCH_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
12 |
+
IMAGE_ANALYSIS_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
13 |
+
AUDIO_ANALYSIS_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
14 |
+
VIDEO_ANALYSIS_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
15 |
+
YOUTUBE_ANALYSIS_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
16 |
+
DOCUMENT_ANALYSIS_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
17 |
+
ARITHMETIC_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
18 |
+
CODE_GENERATION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
19 |
+
CODE_EXECUTION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
20 |
+
|
21 |
+
def orchestrate(message, file_path):
|
22 |
+
|
23 |
+
# Tools
|
24 |
+
|
25 |
+
simple_web_search_tool = DuckDuckGoSearchTool()
|
26 |
+
visit_web_page_tool = VisitWebpageTool()
|
27 |
+
|
28 |
+
@tool
|
29 |
+
def web_search_tool(query: str) -> str:
|
30 |
+
"""
|
31 |
+
Given a question, search the web and return a summary answer.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
query (str): The search query to look up.
|
35 |
+
|
36 |
+
Returns:
|
37 |
+
str: A relevant summary or result from DuckDuckGo.
|
38 |
+
"""
|
39 |
+
try:
|
40 |
+
url = "https://api.duckduckgo.com/"
|
41 |
+
params = {"q": query, "format": "json", "no_html": 1}
|
42 |
+
response = requests.get(url, params=params)
|
43 |
+
data = response.json()
|
44 |
+
|
45 |
+
if abstract := data.get("AbstractText"):
|
46 |
+
return abstract
|
47 |
+
elif related := data.get("RelatedTopics"):
|
48 |
+
return related[0]["Text"] if related else "No result found."
|
49 |
+
else:
|
50 |
+
return "No relevant information found via DuckDuckGo."
|
51 |
+
except Exception as e:
|
52 |
+
raise RuntimeError(f"DuckDuckGo search failed: {str(e)}")
|
53 |
+
|
54 |
+
# Promts
|
55 |
+
|
56 |
+
def get_manager_prompt(message, file_path=None):
|
57 |
+
prompt = f"""Your job is to answer the following question.
|
58 |
+
Answer the following question. If needed, delegate to one of your coworkers:\n
|
59 |
+
|
60 |
+
- Web Search Agent: Use when the question requires current information. Web Search Agent requires a question only.\n
|
61 |
+
Format the prompt like:
|
62 |
+
"You are an expert web search assistant. Your task is to search the web and provide accurate answers to the following question: [INSERT QUESTION]"
|
63 |
+
|
64 |
+
...
|
65 |
+
|
66 |
+
In case you cannot answer the question and there is not a good coworker, delegate to the Code Generation Agent.\n.
|
67 |
+
|
68 |
+
Question: {message}
|
69 |
+
"""
|
70 |
+
|
71 |
+
return prompt
|
72 |
+
|
73 |
+
def run_manager_workflow(message, file_path=None):
|
74 |
+
final_prompt = get_manager_prompt(message, file_path)
|
75 |
+
initial_answer = manager_agent.run(message)
|
76 |
+
|
77 |
+
final_answer = get_final_answer(final_answer_agent, message, str(initial_answer))
|
78 |
+
|
79 |
+
print(f"=> Initial question: {message}")
|
80 |
+
print(f"=> Final prompt: {final_prompt}")
|
81 |
+
print(f"=> Initial answer: {initial_answer}")
|
82 |
+
print(f"=> Final answer: {final_answer}")
|
83 |
+
|
84 |
+
return final_answer
|
85 |
+
|
86 |
+
def get_final_answer(agent, question: str, initial_answer: str) -> str:
|
87 |
+
prompt = f"""
|
88 |
+
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|
89 |
+
Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
90 |
+
If you are asked for a number, don't use comma to write your number neither use units such as USD, $, percent, or % unless specified otherwise.
|
91 |
+
If you are asked for a string, don't use articles, neither abbreviations (for example cities), and write the digits in plain text unless specified otherwise.
|
92 |
+
If you are asked for a comma-separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
93 |
+
If the final answer is a number, use a number not a word.
|
94 |
+
If the final answer is a string, start with an uppercase character.
|
95 |
+
If the final answer is a comma-separated list of numbers, use a space character after each comma.
|
96 |
+
If the final answer is a comma-separated list of strings, use a space character after each comma and start with a lowercase character.
|
97 |
+
Do not add any content to the final answer that is not in the initial answer.
|
98 |
+
|
99 |
+
**Question:** """ + question + """
|
100 |
+
|
101 |
+
**Initial answer:** """ + initial_answer + """
|
102 |
+
|
103 |
+
**Example 1:** What is the biggest city in California? Los Angeles
|
104 |
+
**Example 2:** How many 'r's are in strawberry? 3
|
105 |
+
**Example 3:** What is the opposite of black? White
|
106 |
+
**Example 4:** What are the first 5 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3
|
107 |
+
**Example 5:** What is the opposite of bad, worse, worst? good, better, best
|
108 |
+
|
109 |
+
**Final answer:**
|
110 |
+
"""
|
111 |
+
|
112 |
+
return agent.run(prompt)
|
113 |
+
|
114 |
+
# Agents
|
115 |
+
|
116 |
+
web_search_agent = CodeAgent(
|
117 |
+
name="web_search_agent",
|
118 |
+
description="As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}",
|
119 |
+
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
120 |
+
max_steps=2,
|
121 |
+
tools=[web_search_tool],
|
122 |
+
)
|
123 |
+
|
124 |
+
simple_web_search_agent = CodeAgent(
|
125 |
+
name="simple_web_search_agent",
|
126 |
+
description="As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}",
|
127 |
+
# system_message="As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}",
|
128 |
+
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
129 |
+
max_steps=2,
|
130 |
+
tools=[simple_web_search_tool, visit_web_page_tool],
|
131 |
+
)
|
132 |
+
|
133 |
+
manager_prompt = get_manager_prompt(message)
|
134 |
+
manager_agent = CodeAgent(
|
135 |
+
name="manager_agent",
|
136 |
+
model=InferenceClientModel(MANAGER_MODEL, provider="together", max_tokens=8096),
|
137 |
+
description=manager_prompt,
|
138 |
+
tools=[],
|
139 |
+
planning_interval=4,
|
140 |
+
verbosity_level=2,
|
141 |
+
managed_agents=[simple_web_search_agent],
|
142 |
+
max_steps=10,
|
143 |
+
additional_authorized_imports=[
|
144 |
+
"requests",
|
145 |
+
"zipfile",
|
146 |
+
"os",
|
147 |
+
"pandas",
|
148 |
+
"numpy",
|
149 |
+
"sympy",
|
150 |
+
"json",
|
151 |
+
"bs4",
|
152 |
+
"pubchempy",
|
153 |
+
"xml",
|
154 |
+
"yahoo_finance",
|
155 |
+
"Bio",
|
156 |
+
"sklearn",
|
157 |
+
"scipy",
|
158 |
+
"pydub",
|
159 |
+
"io",
|
160 |
+
"PIL",
|
161 |
+
"chess",
|
162 |
+
"PyPDF2",
|
163 |
+
"pptx",
|
164 |
+
"torch",
|
165 |
+
"datetime",
|
166 |
+
"csv",
|
167 |
+
"fractions",
|
168 |
+
],
|
169 |
+
)
|
170 |
+
|
171 |
+
final_answer_agent = CodeAgent(
|
172 |
+
name="final_answer_agent",
|
173 |
+
description="Given a question and an initial answer, return the final refined answer following strict formatting rules.",
|
174 |
+
model=InferenceClientModel(FINAL_ANSWER_MODEL),
|
175 |
+
max_steps=1,
|
176 |
+
tools=[],
|
177 |
+
)
|
178 |
+
|
179 |
+
final_answer = run_manager_workflow(message)
|
180 |
+
|
181 |
+
# final_answer = manager_agent.run(message)
|
182 |
+
return final_answer
|
app.py
CHANGED
@@ -3,10 +3,16 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
10 |
|
11 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
@@ -139,6 +145,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
139 |
results_df = pd.DataFrame(results_log)
|
140 |
return status_message, results_df
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
# --- Build Gradio Interface using Blocks ---
|
144 |
with gr.Blocks() as demo:
|
@@ -150,6 +166,7 @@ with gr.Blocks() as demo:
|
|
150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
153 |
|
154 |
---
|
155 |
**Disclaimers:**
|
@@ -159,19 +176,27 @@ with gr.Blocks() as demo:
|
|
159 |
)
|
160 |
|
161 |
gr.LoginButton()
|
|
|
162 |
|
163 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
|
165 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
166 |
-
# Removed max_rows=10 from DataFrame constructor
|
167 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
168 |
|
169 |
-
run_button.click(
|
170 |
-
|
171 |
-
|
172 |
-
)
|
173 |
|
174 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
176 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
177 |
space_host_startup = os.getenv("SPACE_HOST")
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from huggingface_hub import login
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
from agents import orchestrate
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
14 |
+
QUESTION_FILE_PATH = "data/gaia_validation.jsonl"
|
15 |
+
QUESTION_LEVEL = 1
|
16 |
|
17 |
# --- Basic Agent Definition ---
|
18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
145 |
results_df = pd.DataFrame(results_log)
|
146 |
return status_message, results_df
|
147 |
|
148 |
+
def test_init_agent_for_chat(text_input, history, file_name = ""):
|
149 |
+
|
150 |
+
if file_name:
|
151 |
+
file_name = f"data/{file_name}"
|
152 |
+
|
153 |
+
submitted_answer = orchestrate(text_input, file_name)
|
154 |
+
|
155 |
+
print(submitted_answer)
|
156 |
+
|
157 |
+
return submitted_answer
|
158 |
|
159 |
# --- Build Gradio Interface using Blocks ---
|
160 |
with gr.Blocks() as demo:
|
|
|
166 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
167 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
168 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
169 |
+
4. who is in the final of champions league this year?
|
170 |
|
171 |
---
|
172 |
**Disclaimers:**
|
|
|
176 |
)
|
177 |
|
178 |
gr.LoginButton()
|
179 |
+
gr.ChatInterface(test_init_agent_for_chat, type="messages")
|
180 |
|
181 |
+
# run_button = gr.Button("Run Evaluation & Submit All Answers")
|
182 |
|
183 |
+
# status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
184 |
+
# # Removed max_rows=10 from DataFrame constructor
|
185 |
+
# results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
186 |
|
187 |
+
# run_button.click(
|
188 |
+
# fn=run_and_submit_all,
|
189 |
+
# outputs=[status_output, results_table]
|
190 |
+
# )
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
+
load_dotenv()
|
194 |
+
hf_token = os.getenv("HF_TOKEN")
|
195 |
+
if hf_token:
|
196 |
+
login(hf_token)
|
197 |
+
else:
|
198 |
+
print("ℹ️ HF_TOKEN environment variable not found (running locally?).")
|
199 |
+
|
200 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
201 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
202 |
space_host_startup = os.getenv("SPACE_HOST")
|
data/gaia_validation.jsonl
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
CHANGED
@@ -1,2 +1,6 @@
|
|
1 |
gradio
|
2 |
-
requests
|
|
|
|
|
|
|
|
|
|
1 |
gradio
|
2 |
+
requests
|
3 |
+
smolagents
|
4 |
+
pandas
|
5 |
+
duckduckgo-search
|
6 |
+
markdownify
|