Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import requests
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
-
# Tool to fetch Bitcoin price from CoinGecko
|
| 8 |
@tool
|
| 9 |
def get_btc_price() -> str:
|
| 10 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
@@ -18,33 +17,10 @@ def get_btc_price() -> str:
|
|
| 18 |
return '{"error": "Bitcoin price not found"}'
|
| 19 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 20 |
|
| 21 |
-
# Fetch Bitcoin price data from CoinGecko for calculating fear and greed
|
| 22 |
-
def fetch_btc_price_data():
|
| 23 |
-
url = 'https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=30'
|
| 24 |
-
response = requests.get(url)
|
| 25 |
-
data = response.json()
|
| 26 |
-
prices = [item[1] for item in data['prices']]
|
| 27 |
-
volumes = [item[1] for item in data['total_volumes']]
|
| 28 |
-
return np.array(prices), np.array(volumes)
|
| 29 |
-
|
| 30 |
-
# Function to calculate price volatility
|
| 31 |
-
def calculate_volatility(prices):
|
| 32 |
-
returns = np.diff(prices) / prices[:-1]
|
| 33 |
-
volatility = np.std(returns) * np.sqrt(len(returns))
|
| 34 |
-
return volatility
|
| 35 |
-
|
| 36 |
-
# Function to calculate trading volume average
|
| 37 |
-
def calculate_volume_average(volumes):
|
| 38 |
-
return np.mean(volumes)
|
| 39 |
-
|
| 40 |
-
# Function to normalize values to 0-100 range
|
| 41 |
-
def normalize(value, min_value, max_value):
|
| 42 |
-
return (value - min_value) / (max_value - min_value) * 100
|
| 43 |
-
|
| 44 |
# Initialize the FinalAnswerTool
|
| 45 |
final_answer = FinalAnswerTool()
|
| 46 |
|
| 47 |
-
# Initialize the model
|
| 48 |
model = HfApiModel(
|
| 49 |
max_tokens=2096,
|
| 50 |
temperature=0.5,
|
|
@@ -52,51 +28,22 @@ model = HfApiModel(
|
|
| 52 |
custom_role_conversions=None,
|
| 53 |
)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Fetch BTC price and volume data
|
| 59 |
-
prices, volumes = fetch_btc_price_data()
|
| 60 |
-
|
| 61 |
-
# Calculate metrics
|
| 62 |
-
volatility = calculate_volatility(prices)
|
| 63 |
-
volume_avg = calculate_volume_average(volumes)
|
| 64 |
-
|
| 65 |
-
# Normalize the values
|
| 66 |
-
volatility_normalized = normalize(volatility, 0, 0.1)
|
| 67 |
-
volume_normalized = normalize(volume_avg, 0, 1000000000)
|
| 68 |
-
|
| 69 |
-
# Calculate sub-indices
|
| 70 |
-
volatility_index = 100 - volatility_normalized
|
| 71 |
-
volume_index = volume_normalized
|
| 72 |
-
|
| 73 |
-
# Aggregate Fear and Greed Index
|
| 74 |
-
fear_greed_index = np.mean([volatility_index, volume_index])
|
| 75 |
-
|
| 76 |
-
# Interpretation of the Fear and Greed Index
|
| 77 |
-
if fear_greed_index <= 24:
|
| 78 |
-
sentiment = "Extreme Fear"
|
| 79 |
-
elif 25 <= fear_greed_index <= 49:
|
| 80 |
-
sentiment = "Fear"
|
| 81 |
-
elif 50 <= fear_greed_index <= 74:
|
| 82 |
-
sentiment = "Neutral"
|
| 83 |
-
elif 75 <= fear_greed_index <= 100:
|
| 84 |
-
sentiment = "Greed"
|
| 85 |
-
|
| 86 |
-
return f"Fear and Greed Index: {fear_greed_index:.2f} ({sentiment})"
|
| 87 |
|
| 88 |
# Create the agent
|
| 89 |
agent = CodeAgent(
|
| 90 |
model=model,
|
| 91 |
-
tools=[get_btc_price, final_answer
|
| 92 |
max_steps=6,
|
| 93 |
verbosity_level=1,
|
| 94 |
grammar=None,
|
| 95 |
planning_interval=None,
|
| 96 |
-
name=
|
| 97 |
-
description=
|
| 98 |
-
prompt_templates=None
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# Launch the Gradio interface
|
| 102 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
|
| 2 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 3 |
import requests
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
|
|
|
| 6 |
|
|
|
|
| 7 |
@tool
|
| 8 |
def get_btc_price() -> str:
|
| 9 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
|
|
| 17 |
return '{"error": "Bitcoin price not found"}'
|
| 18 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Initialize the FinalAnswerTool
|
| 21 |
final_answer = FinalAnswerTool()
|
| 22 |
|
| 23 |
+
# Initialize the model
|
| 24 |
model = HfApiModel(
|
| 25 |
max_tokens=2096,
|
| 26 |
temperature=0.5,
|
|
|
|
| 28 |
custom_role_conversions=None,
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Load additional tools if needed
|
| 32 |
+
# For example, to load an image generation tool:
|
| 33 |
+
# image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Create the agent
|
| 36 |
agent = CodeAgent(
|
| 37 |
model=model,
|
| 38 |
+
tools=[get_btc_price, final_answer], # Add your tools here
|
| 39 |
max_steps=6,
|
| 40 |
verbosity_level=1,
|
| 41 |
grammar=None,
|
| 42 |
planning_interval=None,
|
| 43 |
+
name=None,
|
| 44 |
+
description=None,
|
| 45 |
+
prompt_templates=None # Set to None if not using prompt templates
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Launch the Gradio interface
|
| 49 |
GradioUI(agent).launch()
|