Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,9 +17,6 @@ model = HfApiModel(
|
|
| 17 |
custom_role_conversions=None,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# ✅ Import Tool from Hub
|
| 21 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 22 |
-
|
| 23 |
# ✅ Load Prompt Templates
|
| 24 |
with open("prompts.yaml", 'r') as stream:
|
| 25 |
prompt_templates = yaml.safe_load(stream)
|
|
@@ -28,15 +25,9 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 28 |
# --- Utility Functions ---
|
| 29 |
|
| 30 |
def fetch_data(url: str, params: Dict[str, Any] = None) -> Dict[str, Any]:
|
| 31 |
-
"""
|
| 32 |
-
Utility function to make GET requests and return the response as JSON.
|
| 33 |
-
|
| 34 |
-
:param url: The API endpoint URL.
|
| 35 |
-
:param params: Query parameters for the request.
|
| 36 |
-
:return: The JSON response from the API.
|
| 37 |
-
"""
|
| 38 |
try:
|
| 39 |
-
response = requests.get(url, params=params)
|
| 40 |
response.raise_for_status()
|
| 41 |
return response.json()
|
| 42 |
except requests.exceptions.RequestException as e:
|
|
@@ -44,142 +35,90 @@ def fetch_data(url: str, params: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
| 44 |
|
| 45 |
|
| 46 |
def compute_moving_average(prices: List[float], window: int) -> float:
|
| 47 |
-
"""
|
| 48 |
-
Computes the moving average for the given price data over a specified window.
|
| 49 |
-
|
| 50 |
-
:param prices: List of historical price values.
|
| 51 |
-
:param window: The number of data points to include in the average.
|
| 52 |
-
:return: The computed moving average.
|
| 53 |
-
"""
|
| 54 |
return np.mean(prices[-window:]) if len(prices) >= window else np.mean(prices)
|
| 55 |
|
| 56 |
|
| 57 |
# --- Tools ---
|
| 58 |
|
| 59 |
-
### 🔹 1. Fetch Latest BTC News ###
|
| 60 |
@tool
|
| 61 |
def fetch_btc_news() -> List[Dict[str, str]]:
|
| 62 |
-
"""
|
| 63 |
-
Fetches the latest Bitcoin news from the CryptoPanic API.
|
| 64 |
-
|
| 65 |
-
:return: A list of dictionaries, each containing the 'title' and 'url'
|
| 66 |
-
of a Bitcoin news article.
|
| 67 |
-
"""
|
| 68 |
url = "https://cryptopanic.com/api/v1/posts/?auth_token=ccbeafb47d266a78086f76c3f547546f6b747e88¤cies=BTC"
|
| 69 |
data = fetch_data(url)
|
| 70 |
return [{"title": post["title"], "url": post["url"]} for post in data.get("results", [])[:5]]
|
| 71 |
|
| 72 |
|
| 73 |
-
### 🔹 2. Get BTC Market Insights ###
|
| 74 |
@tool
|
| 75 |
def btc_market_insights() -> Dict[str, Any]:
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
""
|
| 91 |
-
|
| 92 |
-
price_url = "https://api.coingecko.com/api/v3/simple/price"
|
| 93 |
-
price_data = fetch_data(price_url, {"ids": "bitcoin", "vs_currencies": "usd", "include_24hr_change": "true"})
|
| 94 |
-
btc_price = price_data["bitcoin"]["usd"]
|
| 95 |
-
btc_change = price_data["bitcoin"]["usd_24h_change"]
|
| 96 |
-
|
| 97 |
-
# Fetch historical price data (1 year)
|
| 98 |
-
hist_url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart"
|
| 99 |
-
hist_data = fetch_data(hist_url, {"vs_currency": "usd", "days": "365", "interval": "minute"})
|
| 100 |
-
prices = [entry[1] for entry in hist_data.get("prices", [])]
|
| 101 |
-
|
| 102 |
-
# Compute moving averages for different timeframes
|
| 103 |
-
ma_5m = compute_moving_average(prices, 1) # 5-minute
|
| 104 |
-
ma_1h = compute_moving_average(prices, 12) # 1-hour
|
| 105 |
-
ma_1d = compute_moving_average(prices, 24) # 1-day
|
| 106 |
-
ma_7d = compute_moving_average(prices, 168) # 7-day
|
| 107 |
-
ma_1m = compute_moving_average(prices, 720) # 1-month
|
| 108 |
-
ma_1y = compute_moving_average(prices, 8760) # 1-year
|
| 109 |
-
|
| 110 |
-
# Fetch Fear & Greed Index
|
| 111 |
-
fgi_url = "https://api.alternative.me/fng/"
|
| 112 |
-
fgi_data = fetch_data(fgi_url)
|
| 113 |
-
fear_greed = fgi_data["data"][0]["value"]
|
| 114 |
-
fgi_text = fgi_data["data"][0]["value_classification"]
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
return {
|
| 117 |
-
"price":
|
| 118 |
-
"24h_change":
|
| 119 |
-
"
|
| 120 |
-
"
|
| 121 |
-
"
|
| 122 |
-
"ma_7d":
|
| 123 |
-
"
|
| 124 |
-
|
| 125 |
-
"fear_greed": fear_greed,
|
| 126 |
-
"fgi_text": fgi_text
|
| 127 |
}
|
| 128 |
|
| 129 |
|
| 130 |
-
### 🔹 3. Make Buy/Sell/HODL Decision ###
|
| 131 |
@tool
|
| 132 |
def btc_decision(market_insights: Dict[str, Any]) -> Dict[str, str]:
|
| 133 |
-
"""
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
- 'ma_1y': 1-year moving average.
|
| 145 |
-
- 'fear_greed': Current Fear & Greed index value.
|
| 146 |
-
- 'fgi_text': Classification text for the Fear & Greed index.
|
| 147 |
-
:return: A dictionary mapping timeframes (e.g., "5m-1H", "1D", "7D", "1M", "1Y") to a decision
|
| 148 |
-
("BUY 🚀", "SELL 📉", or "HODL 🤔").
|
| 149 |
-
"""
|
| 150 |
-
btc_price = market_insights["price"]
|
| 151 |
-
fgi = int(market_insights["fear_greed"])
|
| 152 |
-
|
| 153 |
-
# Moving averages for different timeframes
|
| 154 |
-
ma_5m = market_insights["ma_5m"]
|
| 155 |
-
ma_1h = market_insights["ma_1h"]
|
| 156 |
-
ma_1d = market_insights["ma_1d"]
|
| 157 |
-
ma_7d = market_insights["ma_7d"]
|
| 158 |
-
ma_1m = market_insights["ma_1m"]
|
| 159 |
-
ma_1y = market_insights["ma_1y"]
|
| 160 |
-
|
| 161 |
-
# Initialize decision template
|
| 162 |
-
decision = {
|
| 163 |
-
"5m-1H": "HODL 🤔",
|
| 164 |
-
"1D": "HODL 🤔",
|
| 165 |
-
"7D": "HODL 🤔",
|
| 166 |
-
"1M": "HODL 🤔",
|
| 167 |
-
"1Y": "HODL 🤔"
|
| 168 |
}
|
| 169 |
|
| 170 |
-
|
| 171 |
-
if
|
| 172 |
-
|
| 173 |
-
elif
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
# --- Agent Setup ---
|
|
@@ -188,8 +127,12 @@ agent = CodeAgent(
|
|
| 188 |
tools=[final_answer, fetch_btc_news, btc_market_insights, btc_decision],
|
| 189 |
max_steps=6,
|
| 190 |
verbosity_level=1,
|
| 191 |
-
prompt_templates=prompt_templates
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
# --- Launch UI ---
|
| 195 |
-
GradioUI(agent).launch()
|
|
|
|
| 17 |
custom_role_conversions=None,
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
# ✅ Load Prompt Templates
|
| 21 |
with open("prompts.yaml", 'r') as stream:
|
| 22 |
prompt_templates = yaml.safe_load(stream)
|
|
|
|
| 25 |
# --- Utility Functions ---
|
| 26 |
|
| 27 |
def fetch_data(url: str, params: Dict[str, Any] = None) -> Dict[str, Any]:
|
| 28 |
+
"""Make GET requests and return JSON response."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
+
response = requests.get(url, params=params, timeout=10)
|
| 31 |
response.raise_for_status()
|
| 32 |
return response.json()
|
| 33 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def compute_moving_average(prices: List[float], window: int) -> float:
|
| 38 |
+
"""Compute moving average for given price data."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return np.mean(prices[-window:]) if len(prices) >= window else np.mean(prices)
|
| 40 |
|
| 41 |
|
| 42 |
# --- Tools ---
|
| 43 |
|
|
|
|
| 44 |
@tool
|
| 45 |
def fetch_btc_news() -> List[Dict[str, str]]:
|
| 46 |
+
"""Fetch latest Bitcoin news from CryptoPanic API."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
url = "https://cryptopanic.com/api/v1/posts/?auth_token=ccbeafb47d266a78086f76c3f547546f6b747e88¤cies=BTC"
|
| 48 |
data = fetch_data(url)
|
| 49 |
return [{"title": post["title"], "url": post["url"]} for post in data.get("results", [])[:5]]
|
| 50 |
|
| 51 |
|
|
|
|
| 52 |
@tool
|
| 53 |
def btc_market_insights() -> Dict[str, Any]:
|
| 54 |
+
"""Fetch BTC market data and technical indicators."""
|
| 55 |
+
# Current price data
|
| 56 |
+
price_data = fetch_data(
|
| 57 |
+
"https://api.coingecko.com/api/v3/simple/price",
|
| 58 |
+
{"ids": "bitcoin", "vs_currencies": "usd", "include_24hr_change": "true"}
|
| 59 |
+
)
|
| 60 |
+
if "error" in price_data:
|
| 61 |
+
return price_data
|
| 62 |
+
|
| 63 |
+
# Historical data (90 days with hourly intervals)
|
| 64 |
+
hist_data = fetch_data(
|
| 65 |
+
"https://api.coingecko.com/api/v3/coins/bitcoin/market_chart",
|
| 66 |
+
{"vs_currency": "usd", "days": "90", "interval": "hourly"}
|
| 67 |
+
)
|
| 68 |
+
if "error" in hist_data:
|
| 69 |
+
return hist_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
prices = [entry[1] for entry in hist_data.get("prices", [])]
|
| 72 |
+
|
| 73 |
+
# Moving averages calculations (hours)
|
| 74 |
return {
|
| 75 |
+
"price": price_data["bitcoin"]["usd"],
|
| 76 |
+
"24h_change": price_data["bitcoin"]["usd_24h_change"],
|
| 77 |
+
"ma_4h": compute_moving_average(prices, 4),
|
| 78 |
+
"ma_12h": compute_moving_average(prices, 12),
|
| 79 |
+
"ma_24h": compute_moving_average(prices, 24),
|
| 80 |
+
"ma_7d": compute_moving_average(prices, 168),
|
| 81 |
+
"ma_30d": compute_moving_average(prices, 720),
|
| 82 |
+
**get_fear_greed_index()
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
|
|
|
|
| 86 |
@tool
|
| 87 |
def btc_decision(market_insights: Dict[str, Any]) -> Dict[str, str]:
|
| 88 |
+
"""Generate trading decisions based on market insights."""
|
| 89 |
+
decisions = {}
|
| 90 |
+
price = market_insights["price"]
|
| 91 |
+
fgi = int(market_insights.get("fear_greed", 50))
|
| 92 |
+
|
| 93 |
+
timeframes = {
|
| 94 |
+
"4H": market_insights["ma_4h"],
|
| 95 |
+
"12H": market_insights["ma_12h"],
|
| 96 |
+
"24H": market_insights["ma_24h"],
|
| 97 |
+
"7D": market_insights["ma_7d"],
|
| 98 |
+
"30D": market_insights["ma_30d"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
|
| 101 |
+
for tf, ma in timeframes.items():
|
| 102 |
+
if price > ma and fgi < 40:
|
| 103 |
+
decisions[tf] = "BUY 🚀"
|
| 104 |
+
elif price < ma and fgi > 60:
|
| 105 |
+
decisions[tf] = "SELL 📉"
|
| 106 |
+
else:
|
| 107 |
+
decisions[tf] = "HODL 🤔"
|
| 108 |
+
|
| 109 |
+
return decisions
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def get_fear_greed_index() -> Dict[str, Any]:
|
| 113 |
+
"""Fetch Fear & Greed Index data."""
|
| 114 |
+
fgi_data = fetch_data("https://api.alternative.me/fng/")
|
| 115 |
+
if "error" in fgi_data or not fgi_data.get("data"):
|
| 116 |
+
return {"fear_greed": 50, "fgi_text": "Neutral"}
|
| 117 |
+
|
| 118 |
+
return {
|
| 119 |
+
"fear_greed": int(fgi_data["data"][0]["value"]),
|
| 120 |
+
"fgi_text": fgi_data["data"][0]["value_classification"]
|
| 121 |
+
}
|
| 122 |
|
| 123 |
|
| 124 |
# --- Agent Setup ---
|
|
|
|
| 127 |
tools=[final_answer, fetch_btc_news, btc_market_insights, btc_decision],
|
| 128 |
max_steps=6,
|
| 129 |
verbosity_level=1,
|
| 130 |
+
prompt_templates=prompt_templates,
|
| 131 |
+
grammar=None,
|
| 132 |
+
planning_interval=None,
|
| 133 |
+
name=None,
|
| 134 |
+
description=None
|
| 135 |
)
|
| 136 |
|
| 137 |
# --- Launch UI ---
|
| 138 |
+
GradioUI(agent).launch()
|