obocloud's picture
Update app.py
3a3d959 verified
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import requests
from bs4 import BeautifulSoup
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def get_richest_people(_: str) -> str:
"""Fetches a list of the richest people in the world from Bloomberg Billionaires Index."""
url = "https://www.bloomberg.com/billionaires/"
headers = {
"User-Agent": "Mozilla/5.0"
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an error for bad responses
soup = BeautifulSoup(response.text, "html.parser")
people = []
for item in soup.select("div.profile-card__info")[:5]: # Get top 5
name = item.select_one("a.profile-card__name").text.strip()
net_worth = item.select_one("div.profile-card__worth").text.strip()
people.append(f"{name} - {net_worth}")
if not people:
return "Unable to fetch data from Bloomberg at this time."
return "Top richest people in the world (Bloomberg):\n" + "\n".join(people)
except requests.RequestException as req_err:
return f"Network error: {str(req_err)}"
except Exception as e:
return f"Failed to fetch data: {str(e)}"
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
)
# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, get_richest_people],
max_steps=6,
verbosity_level=1,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()