Spaces:
Runtime error
Runtime error
File size: 4,103 Bytes
ee3a112 9b5b26a 7906fa3 c19d193 f0f828c 6aae614 ee3a112 9b5b26a 7ffd8e1 082daaf d2a188e ee2f758 44ca4f6 d2a188e 9b5b26a 288c481 d2a188e 445d3bb f950ece d2a188e f950ece d2a188e 7ffd8e1 f950ece d2a188e 7ffd8e1 d2a188e f950ece d2a188e f950ece 7ffd8e1 f950ece 7ffd8e1 f950ece 7ffd8e1 f950ece 445d3bb 7ffd8e1 445d3bb 93c5a0a 8c01ffb f950ece f8ded7c 13e664e ee3a112 e121372 ee3a112 a88ce85 ee3a112 13d500a 8c01ffb ee3a112 f8ded7c 9b5b26a 8c01ffb 861422e f8ded7c 8c01ffb 8fe992b ee3a112 7ffd8e1 8c01ffb 861422e 8fe992b ee3a112 8c01ffb |
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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
# Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
@tool
def get_dcf_of_company(symbol: str) -> str:
"""Retrieve the Discounted Cash Flow (DCF) of a company based on its stock symbol.
Args:
symbol: The stock symbol of the company (e.g., 'AAPL' for Apple, 'IBM' for IBM).
Returns:
str: A message with the DCF value or an error message if the calculation fails.
"""
try:
api_key = 'NR9AFISYRYH2B5U3'
# Retrieve the company's financial statements
financials = {}
for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey={api_key}'
response = requests.get(url)
data = response.json()
if 'annualReports' in data:
financials[statement] = data['annualReports'][0] # Use the most recent annual report
else:
return f"Error: Unable to retrieve {statement} for symbol '{symbol}'."
# Extract necessary financial data
income_statement = financials['INCOME_STATEMENT']
cash_flow_statement = financials['CASH_FLOW']
balance_sheet = financials['BALANCE_SHEET']
net_income = float(income_statement.get('netIncome', 0))
depreciation = float(cash_flow_statement.get('depreciationDepletionAndAmortization', 0)) # Corrected key
capex = float(cash_flow_statement.get('capitalExpenditures', 0))
change_in_working_capital = (
float(balance_sheet.get('totalCurrentAssets', 0)) - float(balance_sheet.get('totalCurrentLiabilities', 0))
)
# Calculate Free Cash Flow (FCF)
fcf = net_income + depreciation - capex - change_in_working_capital
# Assume a growth rate and discount rate (WACC)
growth_rate = 0.03 # 3% growth rate
discount_rate = 0.10 # 10% discount rate
years = 5 # Number of years to project
# Calculate DCF
dcf_value = 0
for year in range(1, years + 1):
future_cash_flow = fcf * ((1 + growth_rate) ** year)
discounted_cash_flow = future_cash_flow / ((1 + discount_rate) ** year)
dcf_value += discounted_cash_flow
# Calculate Terminal Value
terminal_value = (fcf * (1 + growth_rate)) / (discount_rate - growth_rate)
discounted_terminal_value = terminal_value / ((1 + discount_rate) ** years)
# Total DCF value
total_value = dcf_value + discounted_terminal_value
return f"The Discounted Cash Flow (DCF) for company '{symbol}' is: ${total_value:,.2f}"
except Exception as e:
return f"Error calculating DCF for company '{symbol}': {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() |