Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -18,6 +18,13 @@ if not api_key:
|
|
| 18 |
def get_company_ticker(api_key: str, company_name: str) -> str:
|
| 19 |
"""
|
| 20 |
Retrieves the ticker symbol for a given company name using Alpha Vantage's SYMBOL_SEARCH.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
search_url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={company_name}&apikey={api_key}'
|
| 23 |
response = requests.get(search_url)
|
|
@@ -26,18 +33,36 @@ def get_company_ticker(api_key: str, company_name: str) -> str:
|
|
| 26 |
return data['bestMatches'][0]['1. symbol']
|
| 27 |
else:
|
| 28 |
return None
|
|
|
|
| 29 |
@tool
|
| 30 |
def get_company_overview(api_key: str, symbol: str) -> dict:
|
| 31 |
"""
|
| 32 |
Retrieves the company overview (financial fundamentals) for a given ticker using Alpha Vantage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
overview_url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey={api_key}'
|
| 35 |
response = requests.get(overview_url)
|
| 36 |
return response.json()
|
|
|
|
| 37 |
@tool
|
| 38 |
def calculate_dcf(free_cash_flow: float, wacc: float, growth_rate: float, years: int = 5) -> float:
|
| 39 |
"""
|
| 40 |
Calculates the Discounted Cash Flow (DCF) given the free cash flow, WACC and growth rate.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
dcf_value = 0
|
| 43 |
for year in range(1, years + 1):
|
|
@@ -49,13 +74,17 @@ def calculate_dcf(free_cash_flow: float, wacc: float, growth_rate: float, years:
|
|
| 49 |
@spaces.GPU
|
| 50 |
@tool
|
| 51 |
def get_company_dcf(company_name: str) -> str:
|
| 52 |
-
"""
|
|
|
|
| 53 |
|
| 54 |
Args:
|
| 55 |
-
company_name:
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
try:
|
| 58 |
-
api_key
|
| 59 |
# Step 1: Retrieve the ticker symbol for the company
|
| 60 |
symbol = get_company_ticker(api_key, company_name)
|
| 61 |
if not symbol:
|
|
@@ -67,7 +96,6 @@ def get_company_dcf(company_name: str) -> str:
|
|
| 67 |
return f"Could not retrieve financial data for ticker '{symbol}'."
|
| 68 |
|
| 69 |
# Extract required financial metrics.
|
| 70 |
-
# Σημείωση: Τα ονόματα πεδίων μπορεί να διαφέρουν στο Alpha Vantage Overview.
|
| 71 |
free_cash_flow = float(overview.get('FreeCashFlow', 0))
|
| 72 |
wacc = float(overview.get('WeightedAverageCostOfCapital', 0)) / 100 # converting percentage to decimal
|
| 73 |
growth_rate = float(overview.get('GrowthRate', 0)) / 100 # converting percentage to decimal
|
|
@@ -85,17 +113,13 @@ def get_company_dcf(company_name: str) -> str:
|
|
| 85 |
|
| 86 |
final_answer = FinalAnswerTool()
|
| 87 |
|
| 88 |
-
# 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:
|
| 89 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 90 |
-
|
| 91 |
model = HfApiModel(
|
| 92 |
-
max_tokens=2096,
|
| 93 |
-
temperature=0.5,
|
| 94 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 95 |
-
custom_role_conversions=None,
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
| 99 |
# Import tool from Hub
|
| 100 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 101 |
|
|
@@ -104,7 +128,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 104 |
|
| 105 |
agent = CodeAgent(
|
| 106 |
model=model,
|
| 107 |
-
tools=[final_answer,get_company_ticker, get_company_overview, calculate_dcf, get_company_dcf],
|
| 108 |
max_steps=6,
|
| 109 |
verbosity_level=1,
|
| 110 |
grammar=None,
|
|
@@ -114,5 +138,4 @@ agent = CodeAgent(
|
|
| 114 |
prompt_templates=prompt_templates
|
| 115 |
)
|
| 116 |
|
| 117 |
-
|
| 118 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 18 |
def get_company_ticker(api_key: str, company_name: str) -> str:
|
| 19 |
"""
|
| 20 |
Retrieves the ticker symbol for a given company name using Alpha Vantage's SYMBOL_SEARCH.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
api_key (str): The API key required for authentication with the Alpha Vantage API.
|
| 24 |
+
company_name (str): The name of the company to search for.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
str: The ticker symbol of the company if found, otherwise None.
|
| 28 |
"""
|
| 29 |
search_url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords={company_name}&apikey={api_key}'
|
| 30 |
response = requests.get(search_url)
|
|
|
|
| 33 |
return data['bestMatches'][0]['1. symbol']
|
| 34 |
else:
|
| 35 |
return None
|
| 36 |
+
|
| 37 |
@tool
|
| 38 |
def get_company_overview(api_key: str, symbol: str) -> dict:
|
| 39 |
"""
|
| 40 |
Retrieves the company overview (financial fundamentals) for a given ticker using Alpha Vantage.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
api_key (str): The API key required for authentication with the Alpha Vantage API.
|
| 44 |
+
symbol (str): The stock ticker symbol of the company.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
dict: A dictionary containing financial overview data for the company.
|
| 48 |
"""
|
| 49 |
overview_url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey={api_key}'
|
| 50 |
response = requests.get(overview_url)
|
| 51 |
return response.json()
|
| 52 |
+
|
| 53 |
@tool
|
| 54 |
def calculate_dcf(free_cash_flow: float, wacc: float, growth_rate: float, years: int = 5) -> float:
|
| 55 |
"""
|
| 56 |
Calculates the Discounted Cash Flow (DCF) given the free cash flow, WACC and growth rate.
|
| 57 |
+
|
| 58 |
+
Args:
|
| 59 |
+
free_cash_flow (float): The company's free cash flow.
|
| 60 |
+
wacc (float): The Weighted Average Cost of Capital (WACC).
|
| 61 |
+
growth_rate (float): The estimated growth rate of free cash flow.
|
| 62 |
+
years (int, optional): The number of years to project cash flows. Defaults to 5.
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
float: The total DCF value.
|
| 66 |
"""
|
| 67 |
dcf_value = 0
|
| 68 |
for year in range(1, years + 1):
|
|
|
|
| 74 |
@spaces.GPU
|
| 75 |
@tool
|
| 76 |
def get_company_dcf(company_name: str) -> str:
|
| 77 |
+
"""
|
| 78 |
+
A tool that calculates the Discounted Cash Flow (DCF) of a company's stock.
|
| 79 |
|
| 80 |
Args:
|
| 81 |
+
company_name (str): The name of the company (e.g., 'Apple Inc').
|
| 82 |
+
|
| 83 |
+
Returns:
|
| 84 |
+
str: The calculated DCF value for the given company.
|
| 85 |
"""
|
| 86 |
try:
|
| 87 |
+
global api_key # Ensure we are using the correct API key variable
|
| 88 |
# Step 1: Retrieve the ticker symbol for the company
|
| 89 |
symbol = get_company_ticker(api_key, company_name)
|
| 90 |
if not symbol:
|
|
|
|
| 96 |
return f"Could not retrieve financial data for ticker '{symbol}'."
|
| 97 |
|
| 98 |
# Extract required financial metrics.
|
|
|
|
| 99 |
free_cash_flow = float(overview.get('FreeCashFlow', 0))
|
| 100 |
wacc = float(overview.get('WeightedAverageCostOfCapital', 0)) / 100 # converting percentage to decimal
|
| 101 |
growth_rate = float(overview.get('GrowthRate', 0)) / 100 # converting percentage to decimal
|
|
|
|
| 113 |
|
| 114 |
final_answer = FinalAnswerTool()
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
model = HfApiModel(
|
| 117 |
+
max_tokens=2096,
|
| 118 |
+
temperature=0.5,
|
| 119 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 120 |
+
custom_role_conversions=None,
|
| 121 |
)
|
| 122 |
|
|
|
|
| 123 |
# Import tool from Hub
|
| 124 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 125 |
|
|
|
|
| 128 |
|
| 129 |
agent = CodeAgent(
|
| 130 |
model=model,
|
| 131 |
+
tools=[final_answer, get_company_ticker, get_company_overview, calculate_dcf, get_company_dcf], # add tools here
|
| 132 |
max_steps=6,
|
| 133 |
verbosity_level=1,
|
| 134 |
grammar=None,
|
|
|
|
| 138 |
prompt_templates=prompt_templates
|
| 139 |
)
|
| 140 |
|
|
|
|
| 141 |
GradioUI(agent).launch()
|