Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
-
import plotly.
|
| 5 |
|
| 6 |
# Function to fetch historical price data from CoinGecko
|
| 7 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 8 |
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
data = response.json()
|
| 12 |
-
prices = data
|
| 13 |
return prices
|
| 14 |
-
|
| 15 |
-
return
|
| 16 |
|
| 17 |
# Function to fetch current prices from CoinGecko
|
| 18 |
def fetch_current_price(coin_id):
|
| 19 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
data = response.json()
|
| 23 |
return data[coin_id]['usd']
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
|
| 27 |
# Function to convert dates to timestamps
|
| 28 |
def date_to_timestamp(date_str):
|
|
@@ -36,7 +38,7 @@ def plot_historical_prices(coin_name, from_date, to_date):
|
|
| 36 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 37 |
|
| 38 |
if isinstance(prices, str): # In case of error
|
| 39 |
-
return
|
| 40 |
|
| 41 |
df = pd.DataFrame(prices, columns=['timestamp', 'price'])
|
| 42 |
df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
|
|
@@ -47,7 +49,7 @@ def plot_historical_prices(coin_name, from_date, to_date):
|
|
| 47 |
return fig
|
| 48 |
|
| 49 |
# Function to provide TradingView widget HTML
|
| 50 |
-
def
|
| 51 |
widget_html = """
|
| 52 |
<iframe
|
| 53 |
src="https://www.tradingview.com/widgetembed/?frameElementId=tradingview_ea33b&symbol=BINANCE:BTCUSDT&interval=60"
|
|
@@ -61,13 +63,28 @@ def tradingview_widget():
|
|
| 61 |
"""
|
| 62 |
return widget_html
|
| 63 |
|
| 64 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def analyze_btc(prompt):
|
|
|
|
| 66 |
if "price" in prompt.lower():
|
| 67 |
-
return fetch_current_price(
|
|
|
|
|
|
|
| 68 |
elif "chart" in prompt.lower():
|
| 69 |
-
return
|
| 70 |
-
return "Try asking about the
|
| 71 |
|
| 72 |
# Gradio Interface Setup
|
| 73 |
|
|
@@ -85,8 +102,8 @@ crypto_price_chart_interface = gr.Interface(
|
|
| 85 |
|
| 86 |
# Real-Time Trade Data Interface
|
| 87 |
real_time_trade_interface = gr.Interface(
|
| 88 |
-
fn=
|
| 89 |
-
inputs=
|
| 90 |
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 91 |
title="Real-Time Cryptocurrency Trade Data"
|
| 92 |
)
|
|
@@ -97,11 +114,19 @@ ai_btc_analyzer_interface = gr.Interface(
|
|
| 97 |
inputs="text",
|
| 98 |
outputs="html", # Ensure output is HTML
|
| 99 |
title="AI Bitcoin Analyzer",
|
| 100 |
-
description="Ask about the current Bitcoin price or TradingView chart."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
gr.TabbedInterface(
|
| 105 |
-
[crypto_price_chart_interface, real_time_trade_interface, ai_btc_analyzer_interface],
|
| 106 |
-
tab_names=["Price Chart", "Real-Time Trade Data", "AI
|
| 107 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
|
| 6 |
# Function to fetch historical price data from CoinGecko
|
| 7 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
| 8 |
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart/range?vs_currency=usd&from={from_timestamp}&to={to_timestamp}"
|
| 9 |
+
try:
|
| 10 |
+
response = requests.get(url)
|
| 11 |
+
response.raise_for_status() # Will raise an HTTPError for bad responses
|
| 12 |
data = response.json()
|
| 13 |
+
prices = data.get('prices', [])
|
| 14 |
return prices
|
| 15 |
+
except requests.RequestException as e:
|
| 16 |
+
return str(e)
|
| 17 |
|
| 18 |
# Function to fetch current prices from CoinGecko
|
| 19 |
def fetch_current_price(coin_id):
|
| 20 |
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
|
| 21 |
+
try:
|
| 22 |
+
response = requests.get(url)
|
| 23 |
+
response.raise_for_status()
|
| 24 |
data = response.json()
|
| 25 |
return data[coin_id]['usd']
|
| 26 |
+
except requests.RequestException as e:
|
| 27 |
+
return str(e)
|
| 28 |
|
| 29 |
# Function to convert dates to timestamps
|
| 30 |
def date_to_timestamp(date_str):
|
|
|
|
| 38 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 39 |
|
| 40 |
if isinstance(prices, str): # In case of error
|
| 41 |
+
return go.Figure()
|
| 42 |
|
| 43 |
df = pd.DataFrame(prices, columns=['timestamp', 'price'])
|
| 44 |
df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
|
|
|
|
| 49 |
return fig
|
| 50 |
|
| 51 |
# Function to provide TradingView widget HTML
|
| 52 |
+
def get_tradingview_chart():
|
| 53 |
widget_html = """
|
| 54 |
<iframe
|
| 55 |
src="https://www.tradingview.com/widgetembed/?frameElementId=tradingview_ea33b&symbol=BINANCE:BTCUSDT&interval=60"
|
|
|
|
| 63 |
"""
|
| 64 |
return widget_html
|
| 65 |
|
| 66 |
+
# Function to fetch real-time trade data from CoinGecko
|
| 67 |
+
def get_real_time_trade_data():
|
| 68 |
+
url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1"
|
| 69 |
+
try:
|
| 70 |
+
response = requests.get(url)
|
| 71 |
+
response.raise_for_status()
|
| 72 |
+
data = response.json()
|
| 73 |
+
latest_price = data['prices'][-1][1] if data['prices'] else "N/A"
|
| 74 |
+
return f"Latest trading data for Bitcoin is: ${latest_price}"
|
| 75 |
+
except requests.RequestException as e:
|
| 76 |
+
return str(e)
|
| 77 |
+
|
| 78 |
+
# Function to analyze Bitcoin based on the prompt
|
| 79 |
def analyze_btc(prompt):
|
| 80 |
+
"""Analyze the prompt and provide information."""
|
| 81 |
if "price" in prompt.lower():
|
| 82 |
+
return f"Current Bitcoin price: ${fetch_current_price('bitcoin')}"
|
| 83 |
+
elif "trade data" in prompt.lower():
|
| 84 |
+
return get_real_time_trade_data()
|
| 85 |
elif "chart" in prompt.lower():
|
| 86 |
+
return get_tradingview_chart() # Use HTML component for TradingView chart
|
| 87 |
+
return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
|
| 88 |
|
| 89 |
# Gradio Interface Setup
|
| 90 |
|
|
|
|
| 102 |
|
| 103 |
# Real-Time Trade Data Interface
|
| 104 |
real_time_trade_interface = gr.Interface(
|
| 105 |
+
fn=get_real_time_trade_data,
|
| 106 |
+
inputs=[],
|
| 107 |
outputs=gr.Textbox(label="Real-Time Trade Data"),
|
| 108 |
title="Real-Time Cryptocurrency Trade Data"
|
| 109 |
)
|
|
|
|
| 114 |
inputs="text",
|
| 115 |
outputs="html", # Ensure output is HTML
|
| 116 |
title="AI Bitcoin Analyzer",
|
| 117 |
+
description="Ask about the current Bitcoin price, real-time trade data, or TradingView chart."
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# TradingView Interface for live charts
|
| 121 |
+
tradingview_interface = gr.Interface(
|
| 122 |
+
fn=get_tradingview_chart,
|
| 123 |
+
inputs=[],
|
| 124 |
+
outputs=gr.HTML(label="TradingView Chart"),
|
| 125 |
+
title="Live TradingView Chart"
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# Combining all interfaces into a single tabbed app
|
| 129 |
gr.TabbedInterface(
|
| 130 |
+
[crypto_price_chart_interface, real_time_trade_interface, ai_btc_analyzer_interface, tradingview_interface],
|
| 131 |
+
tab_names=["Price Chart", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
|
| 132 |
).launch()
|