Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -7,65 +8,79 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
|
11 |
-
@tool
|
12 |
-
def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that does nothing yet
|
15 |
-
Args:
|
16 |
-
arg1: the first argument
|
17 |
-
arg2: the second argument
|
18 |
-
"""
|
19 |
-
return "What magic will you build ?"
|
20 |
|
21 |
@tool
|
22 |
def webpage_summarizer(url: str) -> str:
|
23 |
"""Extracts and summarizes main content from a webpage
|
24 |
Args:
|
25 |
url: URL of the webpage to summarize
|
|
|
|
|
26 |
"""
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
return text[:500] + "..." # Return first 500 characters
|
33 |
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
"""A tool that fetches the current local time in a specified timezone.
|
38 |
-
Args:
|
39 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
40 |
-
"""
|
41 |
-
try:
|
42 |
-
# Create timezone object
|
43 |
-
tz = pytz.timezone(timezone)
|
44 |
-
# Get current time in that timezone
|
45 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
46 |
-
return f"The current local time in {timezone} is: {local_time}"
|
47 |
-
except Exception as e:
|
48 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
49 |
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
temperature=0.5,
|
55 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
|
56 |
-
custom_role_conversions=None,
|
57 |
-
)
|
58 |
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
with open("prompts.yaml", 'r') as stream:
|
64 |
prompt_templates = yaml.safe_load(stream)
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
agent = CodeAgent(
|
67 |
model=model,
|
68 |
-
tools=[webpage_summarizer],
|
69 |
max_steps=6,
|
70 |
verbosity_level=1,
|
71 |
grammar=None,
|
@@ -75,5 +90,5 @@ agent = CodeAgent(
|
|
75 |
prompt_templates=prompt_templates
|
76 |
)
|
77 |
|
78 |
-
|
79 |
GradioUI(agent).launch()
|
|
|
1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
import datetime
|
4 |
import requests
|
5 |
import pytz
|
|
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
10 |
|
11 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
@tool
|
14 |
def webpage_summarizer(url: str) -> str:
|
15 |
"""Extracts and summarizes main content from a webpage
|
16 |
Args:
|
17 |
url: URL of the webpage to summarize
|
18 |
+
Returns:
|
19 |
+
str: A summary of the webpage content including title and main text
|
20 |
"""
|
21 |
+
try:
|
22 |
+
# Add headers to mimic a browser request
|
23 |
+
headers = {
|
24 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
25 |
+
}
|
|
|
26 |
|
27 |
+
# Fetch the webpage
|
28 |
+
response = requests.get(url, headers=headers, timeout=10)
|
29 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
30 |
|
31 |
+
# Parse the HTML
|
32 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Get the title
|
35 |
+
title = soup.title.string if soup.title else "No title found"
|
36 |
|
37 |
+
# Remove script and style elements
|
38 |
+
for script in soup(["script", "style"]):
|
39 |
+
script.decompose()
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# Extract text from paragraphs
|
42 |
+
paragraphs = soup.find_all('p')
|
43 |
+
text_content = []
|
44 |
|
45 |
+
for p in paragraphs:
|
46 |
+
text = p.get_text().strip()
|
47 |
+
if len(text) > 50: # Only include substantial paragraphs
|
48 |
+
text_content.append(text)
|
49 |
|
50 |
+
# Combine the content
|
51 |
+
summary = f"Title: {title}\n\nContent Summary:\n"
|
52 |
+
summary += "\n\n".join(text_content[:5]) # Include first 5 substantial paragraphs
|
53 |
+
|
54 |
+
# Limit the total length
|
55 |
+
if len(summary) > 1500:
|
56 |
+
summary = summary[:1500] + "..."
|
57 |
+
|
58 |
+
return summary
|
59 |
+
|
60 |
+
except requests.RequestException as e:
|
61 |
+
return f"Error fetching webpage: {str(e)}"
|
62 |
+
except Exception as e:
|
63 |
+
return f"Error processing webpage: {str(e)}"
|
64 |
+
|
65 |
+
# Initialize the model
|
66 |
+
model = HfApiModel(
|
67 |
+
max_tokens=2096,
|
68 |
+
temperature=0.5,
|
69 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
|
70 |
+
custom_role_conversions=None,
|
71 |
+
)
|
72 |
+
|
73 |
+
# Load prompt templates
|
74 |
with open("prompts.yaml", 'r') as stream:
|
75 |
prompt_templates = yaml.safe_load(stream)
|
76 |
+
|
77 |
+
# Initialize final answer tool
|
78 |
+
final_answer = FinalAnswerTool()
|
79 |
+
|
80 |
+
# Create the agent with the webpage summarizer tool
|
81 |
agent = CodeAgent(
|
82 |
model=model,
|
83 |
+
tools=[final_answer, webpage_summarizer],
|
84 |
max_steps=6,
|
85 |
verbosity_level=1,
|
86 |
grammar=None,
|
|
|
90 |
prompt_templates=prompt_templates
|
91 |
)
|
92 |
|
93 |
+
# Launch the Gradio interface
|
94 |
GradioUI(agent).launch()
|