Spaces:
Sleeping
Sleeping
Initial commit with full functionality extend app req tools
Browse files- app.py +9 -48
- tools/final_answer.py +10 -14
app.py
CHANGED
@@ -5,47 +5,11 @@ import requests
|
|
5 |
import pytz
|
6 |
import yaml
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
-
from Gradio_UI import GradioUI
|
9 |
-
|
10 |
-
@tool
|
11 |
-
def extract_text_tool(url: str) -> str:
|
12 |
-
"""A tool that extracts clean text content from a webpage
|
13 |
-
Args:
|
14 |
-
url: The URL of the webpage to analyze
|
15 |
-
"""
|
16 |
-
try:
|
17 |
-
response = requests.get(url, timeout=10)
|
18 |
-
response.raise_for_status()
|
19 |
-
return response.text
|
20 |
-
except Exception as e:
|
21 |
-
return f"Error extracting text: {str(e)}"
|
22 |
-
|
23 |
-
extract_text_tool.name = "extract_text"
|
24 |
-
extract_text_tool.description = "Extracts text content from a URL"
|
25 |
-
|
26 |
-
@tool
|
27 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
28 |
-
"""A tool that fetches the current local time in a specified timezone.
|
29 |
-
Args:
|
30 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
31 |
-
"""
|
32 |
-
try:
|
33 |
-
tz = pytz.timezone(timezone)
|
34 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
35 |
-
return f"The current local time in {timezone} is: {local_time}"
|
36 |
-
except Exception as e:
|
37 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
38 |
|
39 |
-
|
40 |
-
get_current_time_in_timezone.description = "Gets current time in a specific timezone"
|
41 |
-
|
42 |
-
# Initialize tools
|
43 |
final_answer = FinalAnswerTool()
|
44 |
-
search_tool = DuckDuckGoSearchTool()
|
45 |
-
search_tool.name = "web_search"
|
46 |
-
search_tool.description = "Searches the web for information"
|
47 |
|
48 |
-
#
|
49 |
model = HfApiModel(
|
50 |
max_tokens=2096,
|
51 |
temperature=0.5,
|
@@ -53,23 +17,17 @@ model = HfApiModel(
|
|
53 |
custom_role_conversions=None,
|
54 |
)
|
55 |
|
56 |
-
#
|
57 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
58 |
|
59 |
# Load prompt templates
|
60 |
with open("prompts.yaml", 'r') as stream:
|
61 |
prompt_templates = yaml.safe_load(stream)
|
62 |
|
63 |
-
# Create
|
64 |
agent = CodeAgent(
|
65 |
model=model,
|
66 |
-
tools=[
|
67 |
-
final_answer,
|
68 |
-
search_tool,
|
69 |
-
tool(extract_text_tool),
|
70 |
-
tool(get_current_time_in_timezone),
|
71 |
-
image_generation_tool
|
72 |
-
],
|
73 |
max_steps=6,
|
74 |
verbosity_level=1,
|
75 |
grammar=None,
|
@@ -79,8 +37,11 @@ agent = CodeAgent(
|
|
79 |
prompt_templates=prompt_templates
|
80 |
)
|
81 |
|
82 |
-
#
|
|
|
|
|
83 |
if __name__ == "__main__":
|
|
|
84 |
GradioUI(agent).launch(
|
85 |
server_name="0.0.0.0",
|
86 |
server_port=7860
|
|
|
5 |
import pytz
|
6 |
import yaml
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Initialize FinalAnswerTool
|
|
|
|
|
|
|
10 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
11 |
|
12 |
+
# Initialize model
|
13 |
model = HfApiModel(
|
14 |
max_tokens=2096,
|
15 |
temperature=0.5,
|
|
|
17 |
custom_role_conversions=None,
|
18 |
)
|
19 |
|
20 |
+
# Load tools
|
21 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
22 |
|
23 |
# Load prompt templates
|
24 |
with open("prompts.yaml", 'r') as stream:
|
25 |
prompt_templates = yaml.safe_load(stream)
|
26 |
|
27 |
+
# Create and configure agent
|
28 |
agent = CodeAgent(
|
29 |
model=model,
|
30 |
+
tools=[final_answer], # Start with just the final_answer tool
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
max_steps=6,
|
32 |
verbosity_level=1,
|
33 |
grammar=None,
|
|
|
37 |
prompt_templates=prompt_templates
|
38 |
)
|
39 |
|
40 |
+
# Import GradioUI after agent creation to avoid circular imports
|
41 |
+
from Gradio_UI import GradioUI
|
42 |
+
|
43 |
if __name__ == "__main__":
|
44 |
+
# Launch Gradio interface
|
45 |
GradioUI(agent).launch(
|
46 |
server_name="0.0.0.0",
|
47 |
server_port=7860
|
tools/final_answer.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
# tools/final_answer.py
|
2 |
-
from typing import Any
|
3 |
from smolagents import tool
|
4 |
|
5 |
@tool
|
@@ -9,30 +9,26 @@ class FinalAnswerTool:
|
|
9 |
This tool is used to format and return the final response from the agent.
|
10 |
It ensures that the response is properly formatted and ready for presentation
|
11 |
to the user.
|
12 |
-
|
13 |
-
Attributes:
|
14 |
-
name (str): The name identifier for the tool
|
15 |
-
description (str): A brief description of the tool's purpose
|
16 |
"""
|
17 |
|
18 |
-
name = "final_answer"
|
19 |
-
description = "
|
20 |
|
21 |
-
def __call__(self,
|
22 |
-
"""
|
23 |
|
24 |
Args:
|
25 |
-
|
26 |
|
27 |
Returns:
|
28 |
-
|
29 |
"""
|
30 |
-
return
|
31 |
|
32 |
def __str__(self) -> str:
|
33 |
-
"""
|
34 |
|
35 |
Returns:
|
36 |
-
|
37 |
"""
|
38 |
return self.name
|
|
|
1 |
# tools/final_answer.py
|
2 |
+
from typing import Callable, Any
|
3 |
from smolagents import tool
|
4 |
|
5 |
@tool
|
|
|
9 |
This tool is used to format and return the final response from the agent.
|
10 |
It ensures that the response is properly formatted and ready for presentation
|
11 |
to the user.
|
|
|
|
|
|
|
|
|
12 |
"""
|
13 |
|
14 |
+
name: str = "final_answer"
|
15 |
+
description: str = "Tool for providing the final response to the user"
|
16 |
|
17 |
+
def __call__(self, response: str) -> str:
|
18 |
+
"""Provide the final answer.
|
19 |
|
20 |
Args:
|
21 |
+
response: The response to be returned as the final answer
|
22 |
|
23 |
Returns:
|
24 |
+
The formatted final answer as a string
|
25 |
"""
|
26 |
+
return response
|
27 |
|
28 |
def __str__(self) -> str:
|
29 |
+
"""Get string representation.
|
30 |
|
31 |
Returns:
|
32 |
+
The tool's name
|
33 |
"""
|
34 |
return self.name
|