Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,40 @@
|
|
1 |
-
from smolagents import CodeAgent,
|
2 |
import datetime
|
3 |
-
import requests
|
4 |
import pytz
|
5 |
import yaml
|
|
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
@tool
|
12 |
def search_and_summarize(query: str, max_sentences: int) -> str:
|
13 |
-
"""A tool that
|
14 |
Args:
|
15 |
-
|
16 |
-
|
17 |
"""
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -25,52 +42,46 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
"""
|
27 |
try:
|
28 |
-
# Create timezone object
|
29 |
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
return f"The current local time in {timezone} is: {local_time}"
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
#
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
-
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id=
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
-
|
|
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
-
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer,
|
59 |
-
search_and_summarize,
|
60 |
-
get_current_time_in_timezone,
|
61 |
-
image_generation_tool
|
62 |
-
], ## add your tools here (don't remove final answer)
|
63 |
max_steps=6,
|
64 |
-
verbosity_level=
|
65 |
grammar=None,
|
66 |
planning_interval=None,
|
67 |
name=None,
|
68 |
description=None,
|
69 |
-
prompt_templates=prompt_templates
|
70 |
)
|
71 |
|
72 |
-
#
|
73 |
globals()["search_and_summarize"] = search_and_summarize
|
74 |
globals()["get_current_time_in_timezone"] = get_current_time_in_timezone
|
75 |
|
76 |
-
|
|
|
|
1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
2 |
import datetime
|
|
|
3 |
import pytz
|
4 |
import yaml
|
5 |
+
from transformers import pipeline
|
6 |
+
from smolagents.tools.search import DuckDuckGoSearchTool
|
7 |
from tools.final_answer import FinalAnswerTool
|
|
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
# Real summarization model (Facebook BART)
|
11 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
12 |
+
|
13 |
+
# Real DuckDuckGo search tool
|
14 |
+
search_tool = DuckDuckGoSearchTool()
|
15 |
+
|
16 |
+
# 🌍 Real functional web search + summarization tool
|
17 |
@tool
|
18 |
def search_and_summarize(query: str, max_sentences: int) -> str:
|
19 |
+
"""A tool that performs a web search and returns a summary.
|
20 |
Args:
|
21 |
+
query: The search query string.
|
22 |
+
max_sentences: Maximum number of sentences in the summary.
|
23 |
"""
|
24 |
+
try:
|
25 |
+
pages = search_tool(query=query)
|
26 |
+
combined_text = "\n".join(pages[:3])
|
27 |
+
summary = summarizer(
|
28 |
+
combined_text,
|
29 |
+
max_length=60 * max_sentences,
|
30 |
+
min_length=20 * max_sentences,
|
31 |
+
do_sample=False,
|
32 |
+
)[0]["summary_text"]
|
33 |
+
return summary
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error during search or summarization: {str(e)}"
|
36 |
|
37 |
+
# ⏰ Working timezone tool
|
38 |
@tool
|
39 |
def get_current_time_in_timezone(timezone: str) -> str:
|
40 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
42 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
43 |
"""
|
44 |
try:
|
|
|
45 |
tz = pytz.timezone(timezone)
|
|
|
46 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
47 |
return f"The current local time in {timezone} is: {local_time}"
|
48 |
except Exception as e:
|
49 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
50 |
|
51 |
+
# Final answer tool (required)
|
52 |
final_answer = FinalAnswerTool()
|
53 |
|
54 |
+
# Model setup (can change model here)
|
|
|
|
|
55 |
model = HfApiModel(
|
56 |
+
max_tokens=2096,
|
57 |
+
temperature=0.5,
|
58 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
59 |
+
custom_role_conversions=None,
|
60 |
)
|
61 |
|
62 |
+
# Load any extra tools if needed
|
|
|
63 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
64 |
|
65 |
+
# Load prompt templates
|
66 |
+
with open("prompts.yaml", "r") as stream:
|
67 |
prompt_templates = yaml.safe_load(stream)
|
68 |
+
|
69 |
+
# Create agent
|
70 |
agent = CodeAgent(
|
71 |
model=model,
|
72 |
+
tools=[search_and_summarize, get_current_time_in_timezone, final_answer],
|
|
|
|
|
|
|
|
|
73 |
max_steps=6,
|
74 |
+
verbosity_level=2,
|
75 |
grammar=None,
|
76 |
planning_interval=None,
|
77 |
name=None,
|
78 |
description=None,
|
79 |
+
prompt_templates=prompt_templates,
|
80 |
)
|
81 |
|
82 |
+
# Expose tools to REPL scope in case model tries to call them via python_interpreter
|
83 |
globals()["search_and_summarize"] = search_and_summarize
|
84 |
globals()["get_current_time_in_timezone"] = get_current_time_in_timezone
|
85 |
|
86 |
+
# Launch the agent UI
|
87 |
+
GradioUI(agent).launch()
|