tournas's picture
Update app.py
facce6c verified
raw
history blame
3.41 kB
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import os
import yaml
import pandas as pd
from typing import Dict, Union, List
import json
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
api_key = os.getenv("ALPHAVANTAGE")
# Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
@tool
def get_dcf_of_company(company_name: str) -> str:
"""A tool that retrieves the DCF of a company based on its name.
Args:
company_name: The name of the company.
"""
try:
# Step 1: Retrieve the stock ticker symbol
global api_key
search_url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={company_name}&apikey=NR9AFISYRYH2B5U3'
response = requests.get(search_url)
data = response.json()
if 'bestMatches' in data and len(data['bestMatches']) > 0:
symbol = data['bestMatches'][0]['1. symbol']
else:
return f"No stock symbol found for company '{company_name}'."
# Step 2: Retrieve the company's financial data
overview_url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey=NR9AFISYRYH2B5U3'
response = requests.get(overview_url)
overview = response.json()
if not overview:
return f"No financial data found for company with symbol '{symbol}'."
# Extract necessary data
free_cash_flow = float(overview.get('FreeCashFlow', 0))
wacc = float(overview.get('WeightedAverageCostOfCapital', 0)) / 100
growth_rate = float(overview.get('GrowthRate', 0)) / 100
if free_cash_flow == 0 or wacc == 0:
return f"Insufficient data to calculate DCF for company '{company_name}'."
# Step 3: Calculate DCF
dcf_value = 0
years = 5 # Number of years for forecasting
for year in range(1, years + 1):
future_cash_flow = free_cash_flow * ((1 + growth_rate) ** year)
discounted_cash_flow = future_cash_flow / ((1 + wacc) ** year)
dcf_value += discounted_cash_flow
return f"The Discounted Cash Flow (DCF) for company '{company_name}' is: ${dcf_value:,.2f}"
except Exception as e:
return f"Error calculating DCF for company '{company_name}': {str(e)}"
final_answer = FinalAnswerTool()
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
custom_role_conversions=None,
)
# 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_dcf_of_company], ## add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()