Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ from typing import List, Dict, Any
|
|
| 3 |
import numpy as np
|
| 4 |
import yaml
|
| 5 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 6 |
-
from textblob import TextBlob
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
@@ -71,30 +70,7 @@ def fetch_btc_news() -> List[Dict[str, str]]:
|
|
| 71 |
return [{"title": post["title"], "url": post["url"]} for post in data.get("results", [])[:5]]
|
| 72 |
|
| 73 |
|
| 74 |
-
### 🔹 2.
|
| 75 |
-
@tool
|
| 76 |
-
def analyze_btc_news(articles: List[Dict[str, str]]) -> Dict[str, Any]:
|
| 77 |
-
"""
|
| 78 |
-
Analyzes the sentiment of Bitcoin news articles.
|
| 79 |
-
|
| 80 |
-
:param articles: A list of dictionaries, each with keys 'title' and 'url' representing a news article.
|
| 81 |
-
:return: A dictionary with the sentiment analysis results, including:
|
| 82 |
-
- 'sentiment_score': The average sentiment polarity.
|
| 83 |
-
- 'sentiment_label': A label ("Positive 😊", "Negative 😡", or "Neutral 😐") based on the score.
|
| 84 |
-
- 'keywords': A list of keywords extracted from the titles.
|
| 85 |
-
"""
|
| 86 |
-
sentiments = [TextBlob(article["title"]).sentiment.polarity for article in articles]
|
| 87 |
-
avg_sentiment = np.mean(sentiments) if sentiments else 0
|
| 88 |
-
sentiment_label = "Positive 😊" if avg_sentiment > 0 else "Negative 😡" if avg_sentiment < 0 else "Neutral 😐"
|
| 89 |
-
keywords = list(set(word for article in articles for word in article["title"].split()[:5]))
|
| 90 |
-
return {
|
| 91 |
-
"sentiment_score": avg_sentiment,
|
| 92 |
-
"sentiment_label": sentiment_label,
|
| 93 |
-
"keywords": keywords
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
### 🔹 3. Get BTC Market Insights ###
|
| 98 |
@tool
|
| 99 |
def btc_market_insights() -> Dict[str, Any]:
|
| 100 |
"""
|
|
@@ -124,11 +100,11 @@ def btc_market_insights() -> Dict[str, Any]:
|
|
| 124 |
prices = [entry[1] for entry in hist_data.get("prices", [])]
|
| 125 |
|
| 126 |
# Compute moving averages for different timeframes
|
| 127 |
-
ma_5m = compute_moving_average(prices, 1)
|
| 128 |
-
ma_1h = compute_moving_average(prices, 12)
|
| 129 |
-
ma_1d = compute_moving_average(prices, 24)
|
| 130 |
-
ma_7d = compute_moving_average(prices, 168)
|
| 131 |
-
ma_1m = compute_moving_average(prices, 720)
|
| 132 |
ma_1y = compute_moving_average(prices, 8760) # 1-year
|
| 133 |
|
| 134 |
# Fetch Fear & Greed Index
|
|
@@ -151,19 +127,17 @@ def btc_market_insights() -> Dict[str, Any]:
|
|
| 151 |
}
|
| 152 |
|
| 153 |
|
| 154 |
-
### 🔹
|
| 155 |
@tool
|
| 156 |
-
def btc_decision(
|
| 157 |
"""
|
| 158 |
-
Aggregates
|
| 159 |
|
| 160 |
-
:param news_analysis: Output from the news sentiment analysis tool.
|
| 161 |
:param market_insights: BTC market data from the market insights tool.
|
| 162 |
:return: A dictionary mapping timeframes to a decision ("BUY 🚀", "SELL 📉", or "HODL 🤔").
|
| 163 |
"""
|
| 164 |
-
sentiment_score = news_analysis["sentiment_score"]
|
| 165 |
-
fgi = int(market_insights["fear_greed"])
|
| 166 |
btc_price = market_insights["price"]
|
|
|
|
| 167 |
|
| 168 |
# Moving averages for different timeframes
|
| 169 |
ma_5m = market_insights["ma_5m"]
|
|
@@ -182,20 +156,18 @@ def btc_decision(news_analysis: Dict[str, Any], market_insights: Dict[str, Any])
|
|
| 182 |
"1Y": "HODL 🤔"
|
| 183 |
}
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
"""
|
| 189 |
-
if price > ma and fear_greed < 40 and sentiment > 0:
|
| 190 |
decision[timeframe] = "BUY 🚀"
|
| 191 |
-
elif
|
| 192 |
decision[timeframe] = "SELL 📉"
|
| 193 |
return decision[timeframe]
|
| 194 |
|
| 195 |
-
decision["5m-1H"] = make_decision("5m-1H", ma_5m
|
| 196 |
-
decision["1D"] = make_decision("1D", ma_1d
|
| 197 |
-
decision["7D"] = make_decision("7D", ma_7d
|
| 198 |
-
decision["1M"] = make_decision("1M", ma_1m
|
| 199 |
|
| 200 |
return decision
|
| 201 |
|
|
@@ -203,7 +175,7 @@ def btc_decision(news_analysis: Dict[str, Any], market_insights: Dict[str, Any])
|
|
| 203 |
# --- Agent Setup ---
|
| 204 |
agent = CodeAgent(
|
| 205 |
model=model,
|
| 206 |
-
tools=[final_answer, fetch_btc_news,
|
| 207 |
max_steps=6,
|
| 208 |
verbosity_level=1,
|
| 209 |
prompt_templates=prompt_templates
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import yaml
|
| 5 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 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 |
"""
|
|
|
|
| 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
|
|
|
|
| 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 |
+
Aggregates market insights to suggest a Buy, Sell, or HODL decision.
|
| 135 |
|
|
|
|
| 136 |
:param market_insights: BTC market data from the market insights tool.
|
| 137 |
:return: A dictionary mapping timeframes to a decision ("BUY 🚀", "SELL 📉", or "HODL 🤔").
|
| 138 |
"""
|
|
|
|
|
|
|
| 139 |
btc_price = market_insights["price"]
|
| 140 |
+
fgi = int(market_insights["fear_greed"])
|
| 141 |
|
| 142 |
# Moving averages for different timeframes
|
| 143 |
ma_5m = market_insights["ma_5m"]
|
|
|
|
| 156 |
"1Y": "HODL 🤔"
|
| 157 |
}
|
| 158 |
|
| 159 |
+
# Helper function to update decision based on parameters
|
| 160 |
+
def make_decision(timeframe: str, ma: float) -> str:
|
| 161 |
+
if btc_price > ma and fgi < 40:
|
|
|
|
|
|
|
| 162 |
decision[timeframe] = "BUY 🚀"
|
| 163 |
+
elif btc_price < ma and fgi > 60:
|
| 164 |
decision[timeframe] = "SELL 📉"
|
| 165 |
return decision[timeframe]
|
| 166 |
|
| 167 |
+
decision["5m-1H"] = make_decision("5m-1H", ma_5m)
|
| 168 |
+
decision["1D"] = make_decision("1D", ma_1d)
|
| 169 |
+
decision["7D"] = make_decision("7D", ma_7d)
|
| 170 |
+
decision["1M"] = make_decision("1M", ma_1m)
|
| 171 |
|
| 172 |
return decision
|
| 173 |
|
|
|
|
| 175 |
# --- Agent Setup ---
|
| 176 |
agent = CodeAgent(
|
| 177 |
model=model,
|
| 178 |
+
tools=[final_answer, fetch_btc_news, btc_market_insights, btc_decision],
|
| 179 |
max_steps=6,
|
| 180 |
verbosity_level=1,
|
| 181 |
prompt_templates=prompt_templates
|