Spaces:
Sleeping
Sleeping
David Kovář
commited on
Commit
·
a08fec1
1
Parent(s):
81917a3
add some tools
Browse files- GaiaAgent.py +66 -0
- app.py +5 -14
- requirements.txt +3 -1
- tool/SupadataTool.py +36 -0
- tool/UnknownTextTool.py +57 -0
GaiaAgent.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import InferenceClientModel, CodeAgent, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool
|
| 2 |
+
|
| 3 |
+
from tool.SupadataTool import SupadataTool
|
| 4 |
+
from tool.UnknownTextTool import UnknownTextTool, BasicMathTool
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class GaiaAgent:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
manager_model = InferenceClientModel(
|
| 10 |
+
model_id='deepseek-ai/DeepSeek-R1',
|
| 11 |
+
provider='nebius',
|
| 12 |
+
max_tokens=8096,
|
| 13 |
+
)
|
| 14 |
+
agent_model = InferenceClientModel(
|
| 15 |
+
"Qwen/Qwen3-Coder-30B-A3B-Instruct", provider="together", max_tokens=8096
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
self.web_agent = CodeAgent(
|
| 19 |
+
model=agent_model,
|
| 20 |
+
tools=[
|
| 21 |
+
GoogleSearchTool(provider="serper"),
|
| 22 |
+
VisitWebpageTool()
|
| 23 |
+
],
|
| 24 |
+
name="web_agent",
|
| 25 |
+
description="Browses the web to find information",
|
| 26 |
+
verbosity_level=0,
|
| 27 |
+
max_steps=10,
|
| 28 |
+
)
|
| 29 |
+
self.wikipedia_agent = CodeAgent(
|
| 30 |
+
model=agent_model,
|
| 31 |
+
tools=[WikipediaSearchTool()],
|
| 32 |
+
name="wikipedia_agent",
|
| 33 |
+
description="Searches Wikipedia and returns a summary or full text of the given topic, along with the page URL. If you need to get any encyclopedia information, use this tool.",
|
| 34 |
+
max_steps=5,
|
| 35 |
+
)
|
| 36 |
+
self.text_agent = CodeAgent(
|
| 37 |
+
model=agent_model,
|
| 38 |
+
tools=[UnknownTextTool(), BasicMathTool()],
|
| 39 |
+
name="text_agent",
|
| 40 |
+
description="This agent is perfect for solving specific tasks: * decoding unknown text (if you have unknown text, use this tool); * math and algorithmic problems add, subtract, multiply, divide, etc.",
|
| 41 |
+
)
|
| 42 |
+
self.youtube_transcript_agent = CodeAgent(
|
| 43 |
+
model=agent_model,
|
| 44 |
+
tools=[SupadataTool()],
|
| 45 |
+
name="youtube_transcript_agent",
|
| 46 |
+
description="This agent is perfect for generating transcripts of YouTube videos. If you need to know what the video is about, use this tool.",
|
| 47 |
+
)
|
| 48 |
+
self.manager_agent = CodeAgent(
|
| 49 |
+
tools=[],
|
| 50 |
+
model=manager_model,
|
| 51 |
+
managed_agents=[self.web_agent, self.wikipedia_agent, self.text_agent, self.youtube_transcript_agent],
|
| 52 |
+
additional_authorized_imports=[
|
| 53 |
+
],
|
| 54 |
+
planning_interval=5,
|
| 55 |
+
verbosity_level=2,
|
| 56 |
+
max_steps=15,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
print("GaiaAgent initialized.")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def __call__(self, question: str) -> str:
|
| 63 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 64 |
+
answer = self.manager_agent.run(question)
|
| 65 |
+
print(f"Agent returning fixed answer: {answer}")
|
| 66 |
+
return answer
|
app.py
CHANGED
|
@@ -4,24 +4,15 @@ 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 ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
| 21 |
-
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -35,12 +26,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 35 |
return "Please Login to Hugging Face with the button.", None
|
| 36 |
|
| 37 |
api_url = DEFAULT_API_URL
|
| 38 |
-
questions_url = f"{api_url}/
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from GaiaAgent import GaiaAgent
|
| 8 |
+
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 14 |
"""
|
| 15 |
+
Fetches all questions, runs the GaiaAgent on them, submits all answers,
|
| 16 |
and displays the results.
|
| 17 |
"""
|
| 18 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 26 |
return "Please Login to Hugging Face with the button.", None
|
| 27 |
|
| 28 |
api_url = DEFAULT_API_URL
|
| 29 |
+
questions_url = f"{api_url}/random-question"
|
| 30 |
submit_url = f"{api_url}/submit"
|
| 31 |
|
| 32 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 33 |
try:
|
| 34 |
+
agent = GaiaAgent()
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error instantiating agent: {e}")
|
| 37 |
return f"Error initializing agent: {e}", None
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
supadata
|
| 4 |
+
wikipedia-api
|
tool/SupadataTool.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from smolagents import Tool
|
| 4 |
+
from supadata import Supadata
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class SupadataTool(Tool):
|
| 8 |
+
name = "supadata_tool"
|
| 9 |
+
description = "Get the YouTube video URL and generate transcription describing the video content. If you need to know what the video is about, use this tool."
|
| 10 |
+
inputs = {
|
| 11 |
+
"url": {
|
| 12 |
+
"type": "string",
|
| 13 |
+
"description": "Full YouTube video URL",
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
output_type = "string"
|
| 17 |
+
|
| 18 |
+
def __init__(self, *args, **kwargs):
|
| 19 |
+
super().__init__(*args, **kwargs)
|
| 20 |
+
self.supadata = Supadata(api_key=os.getenv("SUPADATA_API_KEY"))
|
| 21 |
+
|
| 22 |
+
def forward(self, url):
|
| 23 |
+
video_id = self._extract_video_id(url)
|
| 24 |
+
transcript = self.supadata.youtube.transcript(video_id=video_id, lang="en")
|
| 25 |
+
return transcript.content
|
| 26 |
+
|
| 27 |
+
@staticmethod
|
| 28 |
+
def _extract_video_id(url):
|
| 29 |
+
if not url.startswith('https://'):
|
| 30 |
+
return url
|
| 31 |
+
|
| 32 |
+
if 'youtube.com/watch?v=' in url:
|
| 33 |
+
return url.split('watch?v=')[1].split('&')[0]
|
| 34 |
+
elif 'youtu.be/' in url:
|
| 35 |
+
return url.split('youtu.be/')[1].split('?')[0]
|
| 36 |
+
return url
|
tool/UnknownTextTool.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class UnknownTextTool(Tool):
|
| 5 |
+
name = "unknown_text_tool"
|
| 6 |
+
description = "Returns the reversed version of the text. If you receive some unknown text, that can't be recognized and analyzed, then you need to use this tool to make it clear."
|
| 7 |
+
inputs = {
|
| 8 |
+
"query": {
|
| 9 |
+
"type": "string",
|
| 10 |
+
"description": "Unknown text to be decoded"
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
output_type = "string"
|
| 14 |
+
|
| 15 |
+
def forward(self, query):
|
| 16 |
+
try:
|
| 17 |
+
print(f"Reversed text: {query[::-1]}")
|
| 18 |
+
return query[::-1]
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Can't reverse text: {e}"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class BasicMathTool(Tool):
|
| 24 |
+
name = "basic_math_tool"
|
| 25 |
+
description = "Performs basic arithmetic operations (addition, subtraction, multiplication, division)"
|
| 26 |
+
inputs = {
|
| 27 |
+
"operation": {
|
| 28 |
+
"type": "string",
|
| 29 |
+
"description": "Mathematical operation to perform (+, -, *, /)"
|
| 30 |
+
},
|
| 31 |
+
"x": {
|
| 32 |
+
"type": "float",
|
| 33 |
+
"description": "First number"
|
| 34 |
+
},
|
| 35 |
+
"y": {
|
| 36 |
+
"type": "float",
|
| 37 |
+
"description": "Second number"
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
output_type = "float"
|
| 41 |
+
|
| 42 |
+
def forward(self, operation, x, y):
|
| 43 |
+
try:
|
| 44 |
+
if operation == "+":
|
| 45 |
+
return x + y
|
| 46 |
+
elif operation == "-":
|
| 47 |
+
return x - y
|
| 48 |
+
elif operation == "*":
|
| 49 |
+
return x * y
|
| 50 |
+
elif operation == "/":
|
| 51 |
+
if y == 0:
|
| 52 |
+
raise ValueError("Division by zero")
|
| 53 |
+
return x / y
|
| 54 |
+
else:
|
| 55 |
+
raise ValueError(f"Unknown operation: {operation}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Math error: {e}"
|