Spaces:
Sleeping
Sleeping
update app.py to create a celebrity finder agent
#1
by
hapchen
- opened
code from talking to chatgpt:
import requests
from bs4 import BeautifulSoup
from smolagents.tools.duckduckgo import DuckDuckGoSearchTool
class CelebritySummaryAgent:
def init(self):
self.search_tool = DuckDuckGoSearchTool()
def run(self, celebrity_name: str) -> str:
query = f"{celebrity_name} site:en.wikipedia.org"
results = self.search_tool.query(query)
if not results:
return f"Sorry, no Wikipedia results found for {celebrity_name}."
# Step 1: Get first search result (hopefully Wikipedia)
wiki_url = results[0].get("href")
if not wiki_url or "wikipedia.org" not in wiki_url:
return f"No Wikipedia link found in top results for {celebrity_name}."
# Step 2: Fetch the Wikipedia page
try:
response = requests.get(wiki_url)
response.raise_for_status()
except Exception as e:
return f"Error fetching Wikipedia page: {e}"
# Step 3: Parse with BeautifulSoup
soup = BeautifulSoup(response.text, "html.parser")
paragraphs = soup.select("p")
summary_sentences = []
for p in paragraphs:
text = p.get_text(strip=True)
if text:
summary_sentences.append(text)
if len(" ".join(summary_sentences).split()) > 50:
break
summary = " ".join(summary_sentences)
return summary or "Could not extract summary from Wikipedia."
Example usage
agent = CelebritySummaryAgent()
print(agent.run("Zendaya"))
hapchen
changed pull request status to
merged