Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,146 +1,52 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
import pandas as pd
|
| 7 |
-
import os
|
| 8 |
-
import json
|
| 9 |
-
from typing import Dict, Union, List
|
| 10 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@tool
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
api_key: str,
|
| 21 |
-
growth_rate: float = 0.15,
|
| 22 |
-
terminal_growth: float = 0.03,
|
| 23 |
-
discount_rate: float = 0.10,
|
| 24 |
-
years_to_project: int = 5
|
| 25 |
-
) -> Dict[str, Union[float, str, List[float]]]:
|
| 26 |
-
"""
|
| 27 |
-
Calculates the DCF valuation for a given stock symbol using Alpha Vantage data.
|
| 28 |
-
|
| 29 |
Args:
|
| 30 |
-
|
| 31 |
-
api_key: Alpha Vantage API key
|
| 32 |
-
growth_rate: Expected annual growth rate (default: 15%)
|
| 33 |
-
terminal_growth: Terminal growth rate (default: 3%)
|
| 34 |
-
discount_rate: Discount rate (default: 10%)
|
| 35 |
-
years_to_project: Number of years to project (default: 5)
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
Dictionary containing:
|
| 39 |
-
- intrinsic_value: Calculated intrinsic value per share
|
| 40 |
-
- current_price: Current market price
|
| 41 |
-
- recommendation: Buy/Sell/Hold recommendation
|
| 42 |
-
- projected_cash_flows: List of projected cash flows
|
| 43 |
-
- error: Error message if any
|
| 44 |
"""
|
| 45 |
try:
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if "annualReports" not in cash_flow_data:
|
| 52 |
-
return {"error": "Unable to fetch cash flow data"}
|
| 53 |
-
|
| 54 |
-
# Get operating cash flow from most recent year
|
| 55 |
-
latest_cash_flow = float(cash_flow_data["annualReports"][0]["operatingCashflow"])
|
| 56 |
-
|
| 57 |
-
# Get shares outstanding
|
| 58 |
-
overview_url = f"https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}&apikey=NR9AFISYRYH2B5U3"
|
| 59 |
-
response = requests.get(overview_url)
|
| 60 |
-
overview_data = response.json()
|
| 61 |
-
shares_outstanding = float(overview_data.get("SharesOutstanding", 0))
|
| 62 |
-
|
| 63 |
-
# Project future cash flows
|
| 64 |
-
projected_cash_flows = []
|
| 65 |
-
current_cash_flow = latest_cash_flow
|
| 66 |
-
|
| 67 |
-
for year in range(years_to_project):
|
| 68 |
-
current_cash_flow *= (1 + growth_rate)
|
| 69 |
-
projected_cash_flows.append(current_cash_flow)
|
| 70 |
-
|
| 71 |
-
# Calculate terminal value
|
| 72 |
-
terminal_value = (current_cash_flow * (1 + terminal_growth)) / (discount_rate - terminal_growth)
|
| 73 |
-
projected_cash_flows.append(terminal_value)
|
| 74 |
-
|
| 75 |
-
# Calculate present value of all cash flows
|
| 76 |
-
present_value = 0
|
| 77 |
-
for i, cash_flow in enumerate(projected_cash_flows):
|
| 78 |
-
present_value += cash_flow / ((1 + discount_rate) ** (i + 1))
|
| 79 |
-
|
| 80 |
-
# Calculate per share value
|
| 81 |
-
intrinsic_value = present_value / shares_outstanding
|
| 82 |
-
|
| 83 |
-
# Get current price
|
| 84 |
-
price_url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey=NR9AFISYRYH2B5U3"
|
| 85 |
-
response = requests.get(price_url)
|
| 86 |
-
price_data = response.json()
|
| 87 |
-
current_price = float(price_data["Global Quote"]["05. price"])
|
| 88 |
-
|
| 89 |
-
# Generate recommendation
|
| 90 |
-
margin_of_safety = 0.2 # 20% margin of safety
|
| 91 |
-
if intrinsic_value * (1 - margin_of_safety) > current_price:
|
| 92 |
-
recommendation = "Buy"
|
| 93 |
-
elif intrinsic_value * (1 + margin_of_safety) < current_price:
|
| 94 |
-
recommendation = "Sell"
|
| 95 |
-
else:
|
| 96 |
-
recommendation = "Hold"
|
| 97 |
-
|
| 98 |
-
return {
|
| 99 |
-
"intrinsic_value": round(intrinsic_value, 2),
|
| 100 |
-
"current_price": round(current_price, 2),
|
| 101 |
-
"recommendation": recommendation,
|
| 102 |
-
"projected_cash_flows": [round(cf, 2) for cf in projected_cash_flows],
|
| 103 |
-
"error": None
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
except Exception as e:
|
| 107 |
-
return
|
| 108 |
|
| 109 |
-
# Example of how to format it as an AI agent tool
|
| 110 |
-
@tool
|
| 111 |
-
def get_stock_dcf_valuation(symbol: str, api_key: str) -> str:
|
| 112 |
-
"""
|
| 113 |
-
A tool that calculates the DCF valuation for a given stock symbol.
|
| 114 |
-
|
| 115 |
-
Args:
|
| 116 |
-
symbol: Stock symbol (e.g., 'AAPL')
|
| 117 |
-
api_key: Alpha Vantage API key
|
| 118 |
-
|
| 119 |
-
Returns:
|
| 120 |
-
A string containing the DCF analysis results or error message
|
| 121 |
-
"""
|
| 122 |
-
result = calculate_dcf_valuation(symbol, api_key)
|
| 123 |
-
|
| 124 |
-
if result.get("error"):
|
| 125 |
-
return result["error"]
|
| 126 |
-
|
| 127 |
-
return (
|
| 128 |
-
f"DCF Analysis for {symbol}:\n"
|
| 129 |
-
f"Intrinsic Value: ${result['intrinsic_value']}\n"
|
| 130 |
-
f"Current Price: ${result['current_price']}\n"
|
| 131 |
-
f"Recommendation: {result['recommendation']}\n"
|
| 132 |
-
f"Projected Cash Flows: {result['projected_cash_flows']}"
|
| 133 |
-
)
|
| 134 |
|
| 135 |
final_answer = FinalAnswerTool()
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
model = HfApiModel(
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
)
|
| 143 |
|
|
|
|
| 144 |
# Import tool from Hub
|
| 145 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 146 |
|
|
@@ -149,11 +55,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 149 |
|
| 150 |
agent = CodeAgent(
|
| 151 |
model=model,
|
| 152 |
-
tools=[
|
| 153 |
-
|
| 154 |
-
calculate_dcf_valuation,
|
| 155 |
-
get_stock_dcf_valuation # Added the DCF tool here
|
| 156 |
-
],
|
| 157 |
max_steps=6,
|
| 158 |
verbosity_level=1,
|
| 159 |
grammar=None,
|
|
@@ -163,4 +66,5 @@ agent = CodeAgent(
|
|
| 163 |
prompt_templates=prompt_templates
|
| 164 |
)
|
| 165 |
|
|
|
|
| 166 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
+
# @tool
|
| 12 |
+
# def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
+
# #Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
+
# """A tool that does nothing yet
|
| 15 |
+
# Args:
|
| 16 |
+
# arg1: the first argument
|
| 17 |
+
# arg2: the second argument
|
| 18 |
+
# """
|
| 19 |
+
# return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
+
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
Args:
|
| 25 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
+
# Create timezone object
|
| 29 |
+
tz = pytz.timezone(timezone)
|
| 30 |
+
# Get current time in that timezone
|
| 31 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
+
return f"The current local time in {timezone} is: {local_time}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
+
# 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:
|
| 40 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
+
|
| 42 |
model = HfApiModel(
|
| 43 |
+
max_tokens=2096,
|
| 44 |
+
temperature=0.5,
|
| 45 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
+
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
|
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
+
tools=[final_answer,
|
| 59 |
+
get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
|
|
|
|
|
|
|
|
|
| 60 |
max_steps=6,
|
| 61 |
verbosity_level=1,
|
| 62 |
grammar=None,
|
|
|
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
| 69 |
+
|
| 70 |
GradioUI(agent).launch()
|