Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,57 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
import math
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
-
from Gradio_UI import GradioUI
|
| 9 |
-
|
| 10 |
-
# Disclaimer: This agent is for educational purposes only.
|
| 11 |
-
# It uses a very simple model (based on the Kelly Criterion) to recommend an investment amount.
|
| 12 |
-
# It does NOT guarantee profitability and should not be used as a sole basis for any real trading decision.
|
| 13 |
|
| 14 |
-
|
| 15 |
-
"""
|
| 16 |
-
Fetches the current BTC price (USD) from CoinGecko.
|
| 17 |
-
"""
|
| 18 |
-
url = "https://api.coingecko.com/api/v3/simple/price"
|
| 19 |
-
params = {"ids": "bitcoin", "vs_currencies": "usd"}
|
| 20 |
-
response = requests.get(url, params=params)
|
| 21 |
-
data = response.json()
|
| 22 |
-
return data["bitcoin"]["usd"]
|
| 23 |
-
|
| 24 |
-
def calculate_kelly(prob: float, reward_ratio: float) -> float:
|
| 25 |
-
"""
|
| 26 |
-
Calculate the Kelly fraction.
|
| 27 |
-
Formula: (prob * (reward_ratio + 1) - 1) / reward_ratio
|
| 28 |
-
"""
|
| 29 |
-
return (prob * (reward_ratio + 1) - 1) / reward_ratio
|
| 30 |
|
|
|
|
| 31 |
@tool
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
Args:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
target_profit_ratio: Your target reward ratio (e.g., 2 means you aim for 2:1 reward-to-risk).
|
| 40 |
-
probability_edge: Your estimated probability of a favorable outcome (0 to 1).
|
| 41 |
-
|
| 42 |
-
Returns:
|
| 43 |
-
A string with:
|
| 44 |
-
- Current BTC price,
|
| 45 |
-
- Recommended investment amount (USD),
|
| 46 |
-
- Calculated Kelly fraction,
|
| 47 |
-
- Risk/Profit target ratio,
|
| 48 |
-
- A final recommendation (Invest / Hold / Caution).
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
-
current_price = get_btc_price()
|
| 52 |
-
# Calculate the optimal fraction using Kelly's formula.
|
| 53 |
-
kelly_fraction = calculate_kelly(probability_edge, target_profit_ratio)
|
| 54 |
-
# Limit the fraction by your risk tolerance.
|
| 55 |
-
recommended_fraction = min(kelly_fraction, risk_tolerance)
|
| 56 |
-
recommended_investment = recommended_fraction * total_capital
|
| 57 |
-
|
| 58 |
-
# Generate a simple decision based on your probability edge.
|
| 59 |
-
if probability_edge < 0.5:
|
| 60 |
-
decision = "Caution: Your edge is below 50%; consider holding off."
|
| 61 |
-
else:
|
| 62 |
-
decision = "Invest" if recommended_investment > 0 else "Hold"
|
| 63 |
-
|
| 64 |
-
result = (
|
| 65 |
-
f"Current BTC Price: ${current_price:.2f}\n"
|
| 66 |
-
f"Total Capital: ${total_capital:.2f}\n"
|
| 67 |
-
f"Risk Tolerance: {risk_tolerance*100:.1f}%\n"
|
| 68 |
-
f"Target Profit Ratio (Reward-to-Risk): {target_profit_ratio}:1\n"
|
| 69 |
-
f"Estimated Probability Edge: {probability_edge*100:.1f}%\n"
|
| 70 |
-
f"Calculated Kelly Fraction: {kelly_fraction*100:.1f}%\n"
|
| 71 |
-
f"Recommended Investment (limited by risk tolerance): ${recommended_investment:.2f}\n"
|
| 72 |
-
f"Decision: {decision}\n\n"
|
| 73 |
-
"Note: This is a simplified simulation and not financial advice. "
|
| 74 |
-
"Always do your own research and consider multiple factors before investing."
|
| 75 |
-
)
|
| 76 |
-
return result
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"Error: {str(e)}"
|
| 79 |
|
| 80 |
@tool
|
| 81 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 82 |
-
"""
|
| 83 |
-
Returns the current local time in the specified timezone.
|
| 84 |
-
|
| 85 |
Args:
|
| 86 |
-
timezone: A valid timezone
|
| 87 |
"""
|
| 88 |
try:
|
|
|
|
| 89 |
tz = pytz.timezone(timezone)
|
|
|
|
| 90 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 91 |
return f"The current local time in {timezone} is: {local_time}"
|
| 92 |
except Exception as e:
|
| 93 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 94 |
|
| 95 |
-
@tool
|
| 96 |
-
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 97 |
-
"""
|
| 98 |
-
A placeholder tool.
|
| 99 |
-
|
| 100 |
-
Args:
|
| 101 |
-
arg1: The first argument.
|
| 102 |
-
arg2: The second argument.
|
| 103 |
-
"""
|
| 104 |
-
return "What magic will you build?"
|
| 105 |
|
| 106 |
final_answer = FinalAnswerTool()
|
| 107 |
-
|
| 108 |
model = HfApiModel(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
with open("prompts.yaml", 'r') as stream:
|
| 116 |
prompt_templates = yaml.safe_load(stream)
|
| 117 |
|
| 118 |
agent = CodeAgent(
|
| 119 |
model=model,
|
| 120 |
-
tools=[final_answer,
|
| 121 |
max_steps=6,
|
| 122 |
verbosity_level=1,
|
| 123 |
grammar=None,
|
|
@@ -127,4 +61,5 @@ agent = CodeAgent(
|
|
| 127 |
prompt_templates=prompt_templates
|
| 128 |
)
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
| 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 |
model = HfApiModel(
|
| 39 |
+
max_tokens=2096,
|
| 40 |
+
temperature=0.5,
|
| 41 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 42 |
+
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
+
|
| 46 |
+
# Import tool from Hub
|
| 47 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
+
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
+
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 61 |
prompt_templates=prompt_templates
|
| 62 |
)
|
| 63 |
|
| 64 |
+
|
| 65 |
+
GradioUI(agent).launch()
|