Spaces:
Sleeping
Sleeping
update app.py to create a celebrity finder agent
#1
by
hapchen
- opened
app.py
CHANGED
|
@@ -4,19 +4,48 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -55,7 +84,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from bs4 import BeautifulSoup
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
@tool
|
| 13 |
+
def celebrity_finder(name:str)-> str: #it's import to specify the return type
|
| 14 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
+
"""A tool that provides a summary for a celebrity's Wikipedia page
|
| 16 |
Args:
|
| 17 |
+
name: A name of a celebrity (e.g., 'Sam Altman').
|
|
|
|
| 18 |
"""
|
| 19 |
+
search_tool = DuckDuckGoSearchTool()
|
| 20 |
+
query = f"{celebrity_name} site:en.wikipedia.org"
|
| 21 |
+
results = self.search_tool.query(query)
|
| 22 |
+
|
| 23 |
+
# Step 1: Get first search result (hopefully Wikipedia)
|
| 24 |
+
wiki_url = results[0].get("href")
|
| 25 |
+
if not wiki_url or "wikipedia.org" not in wiki_url:
|
| 26 |
+
return f"No Wikipedia link found in top results for {celebrity_name}."
|
| 27 |
+
|
| 28 |
+
# Step 2: Fetch the Wikipedia page
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(wiki_url)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error fetching Wikipedia page: {e}"
|
| 34 |
+
|
| 35 |
+
# Step 3: Parse with BeautifulSoup
|
| 36 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 37 |
+
paragraphs = soup.select("p")
|
| 38 |
+
summary_sentences = []
|
| 39 |
+
|
| 40 |
+
for p in paragraphs:
|
| 41 |
+
text = p.get_text(strip=True)
|
| 42 |
+
if text:
|
| 43 |
+
summary_sentences.append(text)
|
| 44 |
+
if len(" ".join(summary_sentences).split()) > 50:
|
| 45 |
+
break
|
| 46 |
+
|
| 47 |
+
summary = " ".join(summary_sentences)
|
| 48 |
+
return summary or "Could not extract summary from Wikipedia."
|
| 49 |
|
| 50 |
@tool
|
| 51 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 84 |
|
| 85 |
agent = CodeAgent(
|
| 86 |
model=model,
|
| 87 |
+
tools=[final_answer,DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
|
| 88 |
max_steps=6,
|
| 89 |
verbosity_level=1,
|
| 90 |
grammar=None,
|