Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,30 +7,6 @@ from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
# A dummy tool that does nothing (for demonstration)
|
| 11 |
-
@tool
|
| 12 |
-
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 13 |
-
"""A tool that does nothing yet.
|
| 14 |
-
Args:
|
| 15 |
-
arg1: the first argument
|
| 16 |
-
arg2: the second argument
|
| 17 |
-
"""
|
| 18 |
-
return "What magic will you build ?"
|
| 19 |
-
|
| 20 |
-
# Tool to get the current time in a given timezone.
|
| 21 |
-
@tool
|
| 22 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""Fetches the current local time in the specified timezone.
|
| 24 |
-
Args:
|
| 25 |
-
timezone: A valid timezone string (e.g., 'America/New_York').
|
| 26 |
-
"""
|
| 27 |
-
try:
|
| 28 |
-
tz = pytz.timezone(timezone)
|
| 29 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 30 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 33 |
-
|
| 34 |
# Tool to fetch the current stock price using yfinance.
|
| 35 |
@tool
|
| 36 |
def get_current_stock_price(symbol: str) -> str:
|
|
@@ -51,13 +27,13 @@ def get_current_stock_price(symbol: str) -> str:
|
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error fetching current price for {symbol}: {str(e)}"
|
| 53 |
|
| 54 |
-
# Tool to fetch
|
| 55 |
@tool
|
| 56 |
def get_stock_historical_data(symbol: str, timeline: str) -> str:
|
| 57 |
"""Fetches historical stock data for a given symbol and timeline.
|
| 58 |
|
| 59 |
Args:
|
| 60 |
-
symbol: The stock ticker symbol (e.g., '
|
| 61 |
timeline: The timeframe for data. Acceptable values: '1h', '1day', '1week', '1year'.
|
| 62 |
|
| 63 |
Returns:
|
|
@@ -95,34 +71,35 @@ def get_stock_historical_data(symbol: str, timeline: str) -> str:
|
|
| 95 |
except Exception as e:
|
| 96 |
return f"Error fetching historical data for {symbol} over {timeline}: {str(e)}"
|
| 97 |
|
| 98 |
-
# Final answer tool
|
| 99 |
final_answer = FinalAnswerTool()
|
| 100 |
|
| 101 |
-
# Define the model
|
|
|
|
| 102 |
model = HfApiModel(
|
| 103 |
-
max_tokens=2096
|
| 104 |
temperature=0.5,
|
| 105 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 106 |
custom_role_conversions=None,
|
| 107 |
)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 111 |
|
| 112 |
-
# Load prompt templates from
|
| 113 |
with open("prompts.yaml", 'r') as stream:
|
| 114 |
prompt_templates = yaml.safe_load(stream)
|
| 115 |
|
| 116 |
-
# Initialize the CodeAgent with the new yfinance tools
|
| 117 |
agent = CodeAgent(
|
| 118 |
model=model,
|
| 119 |
-
tools=[final_answer,
|
| 120 |
-
max_steps=
|
| 121 |
verbosity_level=1,
|
| 122 |
grammar=None,
|
| 123 |
planning_interval=None,
|
| 124 |
name="TradingAgent",
|
| 125 |
-
description="An AI agent that analyzes stock data using yfinance and advises on long-term trading decisions
|
| 126 |
prompt_templates=prompt_templates
|
| 127 |
)
|
| 128 |
|
|
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Tool to fetch the current stock price using yfinance.
|
| 11 |
@tool
|
| 12 |
def get_current_stock_price(symbol: str) -> str:
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"Error fetching current price for {symbol}: {str(e)}"
|
| 29 |
|
| 30 |
+
# Tool to fetch historical stock data for a given timeline using yfinance.
|
| 31 |
@tool
|
| 32 |
def get_stock_historical_data(symbol: str, timeline: str) -> str:
|
| 33 |
"""Fetches historical stock data for a given symbol and timeline.
|
| 34 |
|
| 35 |
Args:
|
| 36 |
+
symbol: The stock ticker symbol (e.g., 'TSLA').
|
| 37 |
timeline: The timeframe for data. Acceptable values: '1h', '1day', '1week', '1year'.
|
| 38 |
|
| 39 |
Returns:
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"Error fetching historical data for {symbol} over {timeline}: {str(e)}"
|
| 73 |
|
| 74 |
+
# Final answer tool (must be included)
|
| 75 |
final_answer = FinalAnswerTool()
|
| 76 |
|
| 77 |
+
# Define the model configuration.
|
| 78 |
+
# Here we reduce max_tokens to help keep the total token count below the limit.
|
| 79 |
model = HfApiModel(
|
| 80 |
+
max_tokens=1000, # Reduced from 2096 to reduce total token count
|
| 81 |
temperature=0.5,
|
| 82 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 83 |
custom_role_conversions=None,
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Optionally, load an additional tool from the Hub (e.g., a text-to-image tool)
|
| 87 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 88 |
|
| 89 |
+
# Load prompt templates from YAML.
|
| 90 |
with open("prompts.yaml", 'r') as stream:
|
| 91 |
prompt_templates = yaml.safe_load(stream)
|
| 92 |
|
| 93 |
+
# Initialize the CodeAgent with the new yfinance tools.
|
| 94 |
agent = CodeAgent(
|
| 95 |
model=model,
|
| 96 |
+
tools=[final_answer, get_current_stock_price, get_stock_historical_data],
|
| 97 |
+
max_steps=3, # Reduced steps to limit the chain-of-thought length
|
| 98 |
verbosity_level=1,
|
| 99 |
grammar=None,
|
| 100 |
planning_interval=None,
|
| 101 |
name="TradingAgent",
|
| 102 |
+
description="An AI agent that analyzes stock data using yfinance and advises on long-term trading decisions.",
|
| 103 |
prompt_templates=prompt_templates
|
| 104 |
)
|
| 105 |
|