Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,17 +3,14 @@ import datetime
|
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from transformers import pipeline
|
6 |
-
from
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
# Real summarization model
|
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.
|
@@ -22,8 +19,14 @@ def search_and_summarize(query: str, max_sentences: int) -> str:
|
|
22 |
max_sentences: Maximum number of sentences in the summary.
|
23 |
"""
|
24 |
try:
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
summary = summarizer(
|
28 |
combined_text,
|
29 |
max_length=60 * max_sentences,
|
@@ -34,7 +37,7 @@ def search_and_summarize(query: str, max_sentences: int) -> str:
|
|
34 |
except Exception as e:
|
35 |
return f"Error during search or summarization: {str(e)}"
|
36 |
|
37 |
-
#
|
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.
|
@@ -48,10 +51,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
48 |
except Exception as e:
|
49 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
50 |
|
51 |
-
# Final answer tool
|
52 |
final_answer = FinalAnswerTool()
|
53 |
|
54 |
-
# Model
|
55 |
model = HfApiModel(
|
56 |
max_tokens=2096,
|
57 |
temperature=0.5,
|
@@ -59,14 +62,14 @@ model = HfApiModel(
|
|
59 |
custom_role_conversions=None,
|
60 |
)
|
61 |
|
62 |
-
# Load
|
63 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
64 |
|
65 |
-
# Load
|
66 |
with open("prompts.yaml", "r") as stream:
|
67 |
prompt_templates = yaml.safe_load(stream)
|
68 |
|
69 |
-
#
|
70 |
agent = CodeAgent(
|
71 |
model=model,
|
72 |
tools=[search_and_summarize, get_current_time_in_timezone, final_answer],
|
@@ -79,9 +82,9 @@ agent = CodeAgent(
|
|
79 |
prompt_templates=prompt_templates,
|
80 |
)
|
81 |
|
82 |
-
#
|
83 |
globals()["search_and_summarize"] = search_and_summarize
|
84 |
globals()["get_current_time_in_timezone"] = get_current_time_in_timezone
|
85 |
|
86 |
-
# Launch the
|
87 |
GradioUI(agent).launch()
|
|
|
3 |
import pytz
|
4 |
import yaml
|
5 |
from transformers import pipeline
|
6 |
+
from duckduckgo_search import DDGS
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
# Real summarization model
|
11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
12 |
|
13 |
+
# Real DuckDuckGo search + summarization tool
|
|
|
|
|
|
|
14 |
@tool
|
15 |
def search_and_summarize(query: str, max_sentences: int) -> str:
|
16 |
"""A tool that performs a web search and returns a summary.
|
|
|
19 |
max_sentences: Maximum number of sentences in the summary.
|
20 |
"""
|
21 |
try:
|
22 |
+
with DDGS() as ddgs:
|
23 |
+
results = ddgs.text(query, max_results=5)
|
24 |
+
search_texts = [r["body"] for r in results if "body" in r]
|
25 |
+
combined_text = "\n".join(search_texts)
|
26 |
+
|
27 |
+
if not combined_text.strip():
|
28 |
+
return "No content found to summarize."
|
29 |
+
|
30 |
summary = summarizer(
|
31 |
combined_text,
|
32 |
max_length=60 * max_sentences,
|
|
|
37 |
except Exception as e:
|
38 |
return f"Error during search or summarization: {str(e)}"
|
39 |
|
40 |
+
# Timezone tool
|
41 |
@tool
|
42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
43 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
51 |
except Exception as e:
|
52 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
53 |
|
54 |
+
# Final answer tool
|
55 |
final_answer = FinalAnswerTool()
|
56 |
|
57 |
+
# Model configuration
|
58 |
model = HfApiModel(
|
59 |
max_tokens=2096,
|
60 |
temperature=0.5,
|
|
|
62 |
custom_role_conversions=None,
|
63 |
)
|
64 |
|
65 |
+
# Load image tool (optional)
|
66 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
67 |
|
68 |
+
# Load system prompts
|
69 |
with open("prompts.yaml", "r") as stream:
|
70 |
prompt_templates = yaml.safe_load(stream)
|
71 |
|
72 |
+
# Agent setup
|
73 |
agent = CodeAgent(
|
74 |
model=model,
|
75 |
tools=[search_and_summarize, get_current_time_in_timezone, final_answer],
|
|
|
82 |
prompt_templates=prompt_templates,
|
83 |
)
|
84 |
|
85 |
+
# Register tools for use in python_interpreter
|
86 |
globals()["search_and_summarize"] = search_and_summarize
|
87 |
globals()["get_current_time_in_timezone"] = get_current_time_in_timezone
|
88 |
|
89 |
+
# Launch the UI
|
90 |
GradioUI(agent).launch()
|