Spaces:
Sleeping
Sleeping
File size: 2,486 Bytes
c816c96 9b5b26a c19d193 6b094ec 6aae614 9b5b26a 6b094ec 59bb92a 4e9ff34 59bb92a 4239c5a 59bb92a cb963bb 59bb92a b04f7b6 59bb92a cb963bb 59bb92a cb963bb 59bb92a 6bd7ded 4b44d1e 9b5b26a 4b44d1e 4239c5a 4b44d1e c816c96 6aae614 ae7a494 c816c96 e121372 4b44d1e 13d500a 8c01ffb 6bd7ded 861422e 4b44d1e 6bd7ded c816c96 8c01ffb 4b44d1e 8fe992b 6bd7ded 2105cae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | from smolagents import CodeAgent, HfApiModel, tool
import datetime
import requests
import pytz
import yaml
from bs4 import BeautifulSoup
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def web_scraper(url: str, element: str = "p") -> str:
"""
Extracts content from a webpage.
Args:
url: The URL of the webpage to scrape. Must be a valid HTTP or HTTPS URL.
element: The HTML element to extract. Defaults to "p" (paragraphs).
Other valid values include "h1" (headings), "div" (divisions), etc.
"""
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
elements = soup.find_all(element)
if not elements:
return f"No '{element}' elements found on the page."
content = "\n".join([elem.get_text().strip() for elem in elements])
return f"Scraped content from {url}:\n{content}"
except requests.exceptions.RequestException as e:
return f"Error fetching the webpage: {str(e)}"
except Exception as e:
return f"An error occurred while scraping: {str(e)}"
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""
Fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
# Load Final Answer Tool
final_answer = FinalAnswerTool()
# Initialize the LLaMA-3 Model
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='meta-llama/Meta-Llama-3-8B-Instruct',
custom_role_conversions=None,
)
# Load Prompts
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Initialize Code Agent with Meta-Llama-3 System Prompt
agent = CodeAgent(
model=model,
tools=[final_answer, get_current_time_in_timezone, web_scraper],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="First Agent",
description="A code execution agent with web scraping capabilities",
prompt_templates=prompt_templates
)
# Launch UI
GradioUI(agent).launch() |