Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,38 +11,41 @@ from tools.final_answer import FinalAnswerTool
|
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
-
api_key = os.getenv("ALPHAVANTAGE")
|
| 15 |
-
|
| 16 |
# Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
|
| 17 |
|
| 18 |
@tool
|
| 19 |
def get_dcf_of_company(symbol: str) -> str:
|
| 20 |
-
"""
|
|
|
|
| 21 |
Args:
|
| 22 |
-
symbol: The symbol
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
# Retrieve the company's financial statements
|
| 28 |
financials = {}
|
| 29 |
for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
|
| 30 |
-
url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey=
|
| 31 |
response = requests.get(url)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Extract necessary financial data
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
depreciation = float(cash_flow_statement['depreciationDepletionAndAmortization'])
|
| 43 |
-
capex = float(cash_flow_statement['capitalExpenditures'])
|
| 44 |
change_in_working_capital = (
|
| 45 |
-
float(balance_sheet
|
| 46 |
)
|
| 47 |
|
| 48 |
# Calculate Free Cash Flow (FCF)
|
|
|
|
| 11 |
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# Define a tool to retrieve the Discounted Cash Flow (DCF) of a company
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def get_dcf_of_company(symbol: str) -> str:
|
| 18 |
+
"""Retrieve the Discounted Cash Flow (DCF) of a company based on its stock symbol.
|
| 19 |
+
|
| 20 |
Args:
|
| 21 |
+
symbol (str): The stock symbol of the company (e.g., 'AAPL' for Apple, 'IBM' for IBM).
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
str: A message with the DCF value or an error message if the calculation fails.
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
+
api_key = 'NR9AFISYRYH2B5U3'
|
| 28 |
# Retrieve the company's financial statements
|
| 29 |
financials = {}
|
| 30 |
for statement in ['INCOME_STATEMENT', 'BALANCE_SHEET', 'CASH_FLOW']:
|
| 31 |
+
url = f'https://www.alphavantage.co/query?function={statement}&symbol={symbol}&apikey={api_key}'
|
| 32 |
response = requests.get(url)
|
| 33 |
+
data = response.json()
|
| 34 |
+
if 'annualReports' in data:
|
| 35 |
+
financials[statement] = data['annualReports'][0] # Use the most recent annual report
|
| 36 |
+
else:
|
| 37 |
+
return f"Error: Unable to retrieve {statement} for symbol '{symbol}'."
|
| 38 |
|
| 39 |
# Extract necessary financial data
|
| 40 |
+
income_statement = financials['INCOME_STATEMENT']
|
| 41 |
+
cash_flow_statement = financials['CASH_FLOW']
|
| 42 |
+
balance_sheet = financials['BALANCE_SHEET']
|
| 43 |
+
|
| 44 |
+
net_income = float(income_statement.get('netIncome', 0))
|
| 45 |
+
depreciation = float(cash_flow_statement.get('depreciationDepletionAndAmortization', 0)) # Corrected key
|
| 46 |
+
capex = float(cash_flow_statement.get('capitalExpenditures', 0))
|
|
|
|
|
|
|
| 47 |
change_in_working_capital = (
|
| 48 |
+
float(balance_sheet.get('totalCurrentAssets', 0)) - float(balance_sheet.get('totalCurrentLiabilities', 0))
|
| 49 |
)
|
| 50 |
|
| 51 |
# Calculate Free Cash Flow (FCF)
|