import gradio as gr import pandas as pd import requests import random import plotly.graph_objects as go from datetime import datetime # ----------------------------- # Fetch BTC Data via CoinGecko # ----------------------------- def fetch_btc_data(): try: url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=50" res = requests.get(url, timeout=10) data = res.json() prices = data['prices'] # list of [timestamp, price] df = pd.DataFrame(prices, columns=['time', 'close']) df['time'] = pd.to_datetime(df['time'], unit='ms') return df except: # fallback if API fails return pd.DataFrame({'time': [datetime.now()], 'close': [0]}) # ----------------------------- # Simulated LSTM Prediction # ----------------------------- def simulate_lstm_prediction(df): current_price = df.iloc[-1]['close'] momentum = (df.iloc[-1]['close'] - df.iloc[-5]['close']) / df.iloc[-5]['close'] variance = (random.random() - 0.5) * 0.03 pred_price = current_price * (1 + (momentum * 0.4) + variance) trend = "HIGH" if pred_price > current_price else "LOW" confidence = random.uniform(0.7, 0.95) reasons = [ "Local LSTM weights detected a hidden bullish divergence in the volume-price vector.", "Sequence analysis indicates the vanishing gradient problem is minimized for this 30-day window.", "Neural pattern matching suggests a 68% correlation with previous historical breakout cycles.", "Recursive hidden states are currently favoring a consolidation phase." ] return random.choice(reasons), f"${pred_price:,.2f}", f"{trend} ({int(confidence*100)}%)" # ----------------------------- # Plot BTC Price Chart # ----------------------------- def create_plot(df): fig = go.Figure() fig.add_trace(go.Scatter( x=df['time'], y=df['close'], mode='lines', name='BTC Price', line=dict(color='#3b82f6', width=4), fill='tozeroy', fillcolor='rgba(59, 130, 246, 0.1)' )) fig.update_layout( template="plotly_dark", paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', margin=dict(l=0, r=0, t=0, b=0), height=400, xaxis=dict(showgrid=False), yaxis=dict(showgrid=True, gridcolor='rgba(255,255,255,0.05)') ) return fig # ----------------------------- # Main Dashboard Logic # ----------------------------- def run_dashboard(): df = fetch_btc_data() if df.empty or df['close'].iloc[-1] == 0: plot = go.Figure() plot.update_layout(template="plotly_dark") current_price = "Data not available" price = "N/A" trend = "N/A" reasoning = "Failed to fetch BTC data. Try again later." return plot, current_price, price, trend, reasoning reasoning, price, trend = simulate_lstm_prediction(df) plot = create_plot(df) current_price = f"${df.iloc[-1]['close']:,.2f}" return plot, str(current_price), str(price), str(trend), str(reasoning) # ----------------------------- # Custom CSS # ----------------------------- custom_css = """ .gradio-container { background-color: #020617 !important; color: white !important; } .gr-button-primary { background: linear-gradient(90deg, #2563eb, #4f46e5) !important; border: none !important; } """ # ----------------------------- # Gradio UI # ----------------------------- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=custom_css) as demo: gr.HTML("""
Built By Nadish • LSTM Neural Engine