Spaces:
Running
Running
Update app.py
Browse filesAdded a function provided by the agent as a tool. Let's see if it can summarize information from a website, and incorporate that as an answer to a question, well :)
app.py
CHANGED
@@ -89,6 +89,50 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
89 |
return f"The current local time in {timezone} is: {local_time}"
|
90 |
except Exception as e:
|
91 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
|
94 |
final_answer = FinalAnswerTool()
|
@@ -117,7 +161,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
117 |
agent = CodeAgent(
|
118 |
model=model,
|
119 |
tools=[final_answer, image_generation_tool,webpage_contents_get,
|
120 |
-
webpage_header_get, webpage_contents_soup_links, webpage_contents_soup_paragraphs
|
|
|
121 |
max_steps=6,
|
122 |
verbosity_level=1,
|
123 |
grammar=None,
|
|
|
89 |
return f"The current local time in {timezone} is: {local_time}"
|
90 |
except Exception as e:
|
91 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
92 |
+
@tool
|
93 |
+
def summarize_and_answer_from_web(url, question):
|
94 |
+
'''
|
95 |
+
A tool which takes a pair of inputs, a website and a question, and finds all relevant information
|
96 |
+
about that question from the website, and return both the summary and the answer
|
97 |
+
Args:
|
98 |
+
url: a string which says which website to get information from
|
99 |
+
question: a string of text asking for information from that website
|
100 |
+
'''
|
101 |
+
# Get the webpage content
|
102 |
+
webpage_content = webpage_contents_get(url=url)
|
103 |
+
|
104 |
+
# Get links from the webpage content
|
105 |
+
links = webpage_contents_soup_links(response_content=webpage_content)
|
106 |
+
|
107 |
+
# Get paragraphs from the webpage content
|
108 |
+
paragraphs = webpage_contents_soup_paragraphs(response_content=webpage_content)
|
109 |
+
|
110 |
+
# Combine the extracted information into a summary
|
111 |
+
summary = f"Title: {paragraphs[0] if paragraphs else 'No title found'}\\n\\nHeadings:\\n"
|
112 |
+
headings = [para for para in paragraphs if len(para.split()) < 15] # Heuristic for headings
|
113 |
+
if headings:
|
114 |
+
for heading in headings:
|
115 |
+
summary += f"- {heading}\\n"
|
116 |
+
else:
|
117 |
+
summary += "No headings found.\\n"
|
118 |
+
|
119 |
+
summary += "\\nParagraphs:\\n"
|
120 |
+
for para in paragraphs:
|
121 |
+
summary += f"- {para}\\n"
|
122 |
+
|
123 |
+
summary += "\\nLinks:\\n"
|
124 |
+
for link in links:
|
125 |
+
summary += f"- {link}\\n"
|
126 |
+
|
127 |
+
# Answer the question based on the summary
|
128 |
+
# Simple heuristic: check if the question is in any of the paragraphs
|
129 |
+
answer = "No specific answer found."
|
130 |
+
for para in paragraphs:
|
131 |
+
if question.lower() in para.lower():
|
132 |
+
answer = para
|
133 |
+
break
|
134 |
+
|
135 |
+
return summary, answer
|
136 |
|
137 |
|
138 |
final_answer = FinalAnswerTool()
|
|
|
161 |
agent = CodeAgent(
|
162 |
model=model,
|
163 |
tools=[final_answer, image_generation_tool,webpage_contents_get,
|
164 |
+
webpage_header_get, webpage_contents_soup_links, webpage_contents_soup_paragraphs,
|
165 |
+
summarize_and_answer_from_web], ## add your tools here (don't remove final answer)
|
166 |
max_steps=6,
|
167 |
verbosity_level=1,
|
168 |
grammar=None,
|