Spaces:
Sleeping
Sleeping
add "get_todays_stock" tool
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -17,21 +18,13 @@ def calculator(a: int, b: int, oper: str)-> str: #it's import to specify the ret
|
|
| 17 |
b: the second argument
|
| 18 |
oper: operetion to be performed. Either "add", "sub", "mult" or "div".
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
if oper == "add":
|
| 22 |
result = a + b
|
| 23 |
elif oper == "sub":
|
| 24 |
result = a - b
|
| 25 |
elif oper == "mult":
|
| 26 |
result = a * b
|
| 27 |
-
elif oper == "div":
|
| 28 |
-
|
| 29 |
-
#try:
|
| 30 |
-
# result = a / b
|
| 31 |
-
#except ZeroDivisionError:
|
| 32 |
-
# result = "inf"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
if b == 0:
|
| 36 |
return "Error: Division by zero."
|
| 37 |
result = a / b
|
|
@@ -40,6 +33,35 @@ def calculator(a: int, b: int, oper: str)-> str: #it's import to specify the ret
|
|
| 40 |
|
| 41 |
return str(result)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@tool
|
| 45 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -74,7 +96,12 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 74 |
|
| 75 |
agent = CodeAgent(
|
| 76 |
model=model,
|
| 77 |
-
tools=[final_answer,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
max_steps=6,
|
| 79 |
verbosity_level=1,
|
| 80 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import yfinance as yf
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
| 18 |
b: the second argument
|
| 19 |
oper: operetion to be performed. Either "add", "sub", "mult" or "div".
|
| 20 |
"""
|
|
|
|
| 21 |
if oper == "add":
|
| 22 |
result = a + b
|
| 23 |
elif oper == "sub":
|
| 24 |
result = a - b
|
| 25 |
elif oper == "mult":
|
| 26 |
result = a * b
|
| 27 |
+
elif oper == "div":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if b == 0:
|
| 29 |
return "Error: Division by zero."
|
| 30 |
result = a / b
|
|
|
|
| 33 |
|
| 34 |
return str(result)
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def get_todays_stock(ticker: str) -> tuple:
|
| 38 |
+
"""A tool that that fetches today's stock data for a selected company.
|
| 39 |
+
Args:
|
| 40 |
+
ticker: The stock's ticker symbol, which is a unique abbreviation assigned to publicly traded companies.
|
| 41 |
+
"""
|
| 42 |
+
try:
|
| 43 |
+
stock = yf.Ticker(ticker)
|
| 44 |
+
data = stock.history(period="1d")
|
| 45 |
+
|
| 46 |
+
if data.empty:
|
| 47 |
+
raise ValueError(f"No data available for {ticker}. Check if the ticker is correct.")
|
| 48 |
+
|
| 49 |
+
latest = data.iloc[-1]
|
| 50 |
+
return {
|
| 51 |
+
"Date": latest.name.date(),
|
| 52 |
+
"Open": latest["Open"],
|
| 53 |
+
"High": latest["High"],
|
| 54 |
+
"Low": latest["Low"],
|
| 55 |
+
"Close": latest["Close"],
|
| 56 |
+
"Volume": latest["Volume"]
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
except ValueError as ve:
|
| 60 |
+
return {"Error": str(ve)}
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return {"Error": f"An unexpected error occurred: {str(e)}"}
|
| 64 |
+
|
| 65 |
|
| 66 |
@tool
|
| 67 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 96 |
|
| 97 |
agent = CodeAgent(
|
| 98 |
model=model,
|
| 99 |
+
tools=[final_answer,
|
| 100 |
+
DuckDuckGoSearchTool(),
|
| 101 |
+
calculator,
|
| 102 |
+
get_current_time_in_timezone,
|
| 103 |
+
image_generation_tool,
|
| 104 |
+
], ## add your tools here (don't remove final answer)
|
| 105 |
max_steps=6,
|
| 106 |
verbosity_level=1,
|
| 107 |
grammar=None,
|