CoolFace
Apppublic

ohayoga/First_agent_template

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py98 linesDownload Raw Back to root
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import yfinance as yf7from pypdf import PdfReader8import re9from tools.final_answer import FinalAnswerTool10 11from Gradio_UI import GradioUI12 13# Below is an example of a tool that does nothing. Amaze us with your creativity !14@tool15def get_latest_earnings(stock_ticker: str) -> str:16    """A tool that displays the information of the latest Earnings for a particular stock17    Args:18        stock_ticker: the stock ticker, informed by the user19    """20    try:21        df = yf.Ticker(stock_ticker).earnings_dates.dropna(subset='EPS Estimate')22        df = df.reset_index().iloc[0]23        ed = df['Earnings Date'].strftime("%c")24        ee = df['EPS Estimate']25        re = df['Reported EPS']26        if df.empty:27            return f"Error: No data found for '{stock_ticker}'. It might be a wrong ticker."28 29        return f"The last Earnings Date: {ed}, estimated EPS: {ee}, reported EPS: {re}" 30    except Exception as e:31        return f"Error fetching price for stock '{stock_ticker}': {str(e)}"32 33@tool34def parse_PDF(file_path: str) -> str:35    """A tool that parses the email from a PDF file36    Args:37        file_path: path to the file38    """39    try:40        with open(file_path, "rb") as f:41            reader = PdfReader(f)42            for page in reader.pages:43                line = page.extract_text()44                email = re.search(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", line)45                if email:46                    return f"the extracted email is: {email.group(0)}"47 48        return "No email found"49        50    except Exception as e:51        return f"Error parsing PDF: {str(e)}"52        53@tool54def get_current_time_in_timezone(timezone: str) -> str:55    """A tool that fetches the current local time in a specified timezone.56    Args:57        timezone: A string representing a valid timezone (e.g., 'America/New_York').58    """59    try:60        # Create timezone object61        tz = pytz.timezone(timezone)62        # Get current time in that timezone63        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")64        return f"The current local time in {timezone} is: {local_time}"65    except Exception as e:66        return f"Error fetching time for timezone '{timezone}': {str(e)}"67 68 69final_answer = FinalAnswerTool()70model = HfApiModel(71max_tokens=2096,72temperature=0.5,73model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded74# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',75custom_role_conversions=None,76)77 78 79# Import tool from Hub80# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)81 82with open("prompts.yaml", 'r') as stream:83    prompt_templates = yaml.safe_load(stream)84    85agent = CodeAgent(86    model=model,87    tools=[final_answer, parse_PDF, get_latest_earnings], ## add your tools here (don't remove final answer)88    max_steps=6,89    verbosity_level=1,90    grammar=None,91    planning_interval=None,92    name=None,93    description=None,94    prompt_templates=prompt_templates95)96 97 98GradioUI(agent, file_upload_folder="data/").launch()