Spaces:
Sleeping
Sleeping
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
|
|
@@ -7,46 +7,113 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"""
|
|
|
|
| 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',
|
| 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,7 +122,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +132,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# Tool: Calculates the RSI for Bitcoin using historical price data.
|
| 11 |
@tool
|
| 12 |
+
def calculateRSI(period: int = 14) -> int:
|
| 13 |
+
"""
|
| 14 |
+
A tool that calculates the current RSI value for Bitcoin.
|
| 15 |
+
Args:
|
| 16 |
+
period: Number of periods to use for RSI calculation (default is 14).
|
| 17 |
+
Returns:
|
| 18 |
+
The current RSI value as an integer.
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
# Fetch historical BTC price data for the past 30 days from CoinGecko
|
| 22 |
+
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart"
|
| 23 |
+
params = {"vs_currency": "usd", "days": "30"}
|
| 24 |
+
response = requests.get(url, params=params)
|
| 25 |
+
data = response.json()
|
| 26 |
+
prices = [price_point[1] for price_point in data["prices"]]
|
| 27 |
+
|
| 28 |
+
if len(prices) < period + 1:
|
| 29 |
+
raise ValueError("Not enough data to calculate RSI.")
|
| 30 |
+
|
| 31 |
+
# Calculate price changes (deltas)
|
| 32 |
+
deltas = [prices[i+1] - prices[i] for i in range(len(prices) - 1)]
|
| 33 |
+
gains = [delta if delta > 0 else 0 for delta in deltas]
|
| 34 |
+
losses = [-delta if delta < 0 else 0 for delta in deltas]
|
| 35 |
+
|
| 36 |
+
# Initial average gain and loss
|
| 37 |
+
avg_gain = sum(gains[:period]) / period
|
| 38 |
+
avg_loss = sum(losses[:period]) / period
|
| 39 |
+
|
| 40 |
+
# Smooth averages over the remaining data
|
| 41 |
+
for i in range(period, len(gains)):
|
| 42 |
+
gain = gains[i]
|
| 43 |
+
loss = losses[i]
|
| 44 |
+
avg_gain = (avg_gain * (period - 1) + gain) / period
|
| 45 |
+
avg_loss = (avg_loss * (period - 1) + loss) / period
|
| 46 |
+
|
| 47 |
+
if avg_loss == 0:
|
| 48 |
+
rsi = 100
|
| 49 |
+
else:
|
| 50 |
+
rs = avg_gain / avg_loss
|
| 51 |
+
rsi = 100 - (100 / (1 + rs))
|
| 52 |
+
|
| 53 |
+
return int(rsi)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
# Returning a negative value signals an error in this example.
|
| 56 |
+
return -1
|
| 57 |
+
|
| 58 |
+
# Tool: Provides a trading signal based on the calculated RSI.
|
| 59 |
+
@tool
|
| 60 |
+
def rsi_signal(period: int = 14) -> str:
|
| 61 |
+
"""
|
| 62 |
+
A tool that calculates the current RSI for Bitcoin and returns a trading recommendation.
|
| 63 |
+
Args:
|
| 64 |
+
period: The RSI period to use (default 14).
|
| 65 |
+
Returns:
|
| 66 |
+
A string with the RSI value and a recommendation:
|
| 67 |
+
- "Buy" if RSI < 30 (oversold)
|
| 68 |
+
- "Sell" if RSI > 70 (overbought)
|
| 69 |
+
- "Hold" otherwise.
|
| 70 |
+
"""
|
| 71 |
+
rsi_value = calculateRSI(period)
|
| 72 |
+
if rsi_value == -1:
|
| 73 |
+
return "Error calculating RSI. Please try again later."
|
| 74 |
+
|
| 75 |
+
if rsi_value < 30:
|
| 76 |
+
signal = "Buy"
|
| 77 |
+
elif rsi_value > 70:
|
| 78 |
+
signal = "Sell"
|
| 79 |
+
else:
|
| 80 |
+
signal = "Hold"
|
| 81 |
+
|
| 82 |
+
return f"Current RSI: {rsi_value}. Recommendation: {signal}."
|
| 83 |
+
|
| 84 |
+
@tool
|
| 85 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 86 |
+
"""
|
| 87 |
+
A tool that does nothing yet.
|
| 88 |
Args:
|
| 89 |
+
arg1: the first argument.
|
| 90 |
+
arg2: the second argument.
|
| 91 |
"""
|
| 92 |
return "What magic will you build ?"
|
| 93 |
|
| 94 |
@tool
|
| 95 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 96 |
+
"""
|
| 97 |
+
A tool that fetches the current local time in a specified timezone.
|
| 98 |
Args:
|
| 99 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 100 |
"""
|
| 101 |
try:
|
|
|
|
| 102 |
tz = pytz.timezone(timezone)
|
|
|
|
| 103 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 104 |
return f"The current local time in {timezone} is: {local_time}"
|
| 105 |
except Exception as e:
|
| 106 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 107 |
|
|
|
|
| 108 |
final_answer = FinalAnswerTool()
|
| 109 |
|
|
|
|
|
|
|
|
|
|
| 110 |
model = HfApiModel(
|
| 111 |
+
max_tokens=2096,
|
| 112 |
+
temperature=0.5,
|
| 113 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 114 |
+
custom_role_conversions=None,
|
| 115 |
)
|
| 116 |
|
|
|
|
| 117 |
# Import tool from Hub
|
| 118 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 119 |
|
|
|
|
| 122 |
|
| 123 |
agent = CodeAgent(
|
| 124 |
model=model,
|
| 125 |
+
tools=[final_answer, calculateRSI, rsi_signal, my_custom_tool, get_current_time_in_timezone],
|
| 126 |
max_steps=6,
|
| 127 |
verbosity_level=1,
|
| 128 |
grammar=None,
|
|
|
|
| 132 |
prompt_templates=prompt_templates
|
| 133 |
)
|
| 134 |
|
| 135 |
+
GradioUI(agent).launch()
|
|
|