missbaj commited on
Commit
f4d3c75
·
verified ·
1 Parent(s): e4865ec
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ response = requests.get(url)
10
+ if response.status_code == 200:
11
+ data = response.json()
12
+ prices = data['prices']
13
+ return prices
14
+ else:
15
+ return f"Error fetching historical data for {coin_id}"
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
+ response = requests.get(url)
21
+ if response.status_code == 200:
22
+ data = response.json()
23
+ return data[coin_id]['usd']
24
+ else:
25
+ return f"Error fetching current price for {coin_id}"
26
+
27
+ # Function to convert dates to timestamps
28
+ def date_to_timestamp(date_str):
29
+ return int(pd.Timestamp(date_str).timestamp())
30
+
31
+ # Function to plot historical prices using Plotly
32
+ def plot_historical_prices(coin_name, from_date, to_date):
33
+ from_timestamp = date_to_timestamp(from_date)
34
+ to_timestamp = date_to_timestamp(to_date)
35
+
36
+ prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
37
+
38
+ if isinstance(prices, str): # In case of error
39
+ return prices
40
+
41
+ df = pd.DataFrame(prices, columns=['timestamp', 'price'])
42
+ df['date'] = pd.to_datetime(df['timestamp'], unit='ms')
43
+
44
+ fig = go.Figure()
45
+ fig.add_trace(go.Scatter(x=df['date'], y=df['price'], mode='lines', name=coin_name))
46
+ fig.update_layout(title=f'{coin_name.capitalize()} Prices from {from_date} to {to_date}', xaxis_title='Date', yaxis_title='Price (USD)')
47
+ return fig
48
+
49
+ # Function to provide TradingView widget HTML
50
+ def get_tradingview_chart():
51
+ widget_html = """
52
+ <iframe
53
+ src="https://www.tradingview.com/widgetembed/?frameElementId=tradingview_ea33b&symbol=BINANCE:BTCUSDT&interval=60"
54
+ width="800"
55
+ height="600"
56
+ frameborder="0"
57
+ allowtransparency="true"
58
+ scrolling="no"
59
+ allowfullscreen="true">
60
+ </iframe>
61
+ """
62
+ return widget_html
63
+
64
+ # Function to fetch real-time trade data from CoinGecko
65
+ def get_real_time_trade_data():
66
+ url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1"
67
+ response = requests.get(url)
68
+ if response.status_code == 200:
69
+ data = response.json()
70
+ return f"Latest trading data for Bitcoin is: ${data['prices'][-1][1]}"
71
+ else:
72
+ return "Error fetching real-time trade data for Bitcoin."
73
+
74
+ # Function to analyze Bitcoin based on the prompt
75
+ def analyze_btc(prompt):
76
+ """Analyze the prompt and provide information."""
77
+ if "price" in prompt.lower():
78
+ return f"Current Bitcoin price: ${fetch_current_price('bitcoin')}"
79
+ elif "trade data" in prompt.lower():
80
+ return get_real_time_trade_data()
81
+ elif "chart" in prompt.lower():
82
+ return gr.HTML(get_tradingview_chart()) # Use HTML component for TradingView chart
83
+ return "Try asking about the Bitcoin price, real-time trade data, or TradingView chart."
84
+
85
+ # Gradio Interface Setup
86
+
87
+ # AI BTC Analysis Interface
88
+ btc_analysis_interface = gr.Interface(
89
+ fn=analyze_btc,
90
+ inputs="text",
91
+ outputs="html", # Ensure output is HTML
92
+ title="Bitcoin Analyzer",
93
+ description="Ask about the current Bitcoin price, real-time trade data, or TradingView chart."
94
+ )
95
+
96
+ # TradingView Interface for live charts
97
+ tradingview_interface = gr.Interface(
98
+ fn=get_tradingview_chart,
99
+ inputs=[],
100
+ outputs=gr.HTML(label="TradingView Chart"),
101
+ title="Live TradingView Chart"
102
+ )
103
+
104
+ # Real-time Trade Data Interface
105
+ real_time_trade_interface = gr.Interface(
106
+ fn=get_real_time_trade_data,
107
+ inputs=[],
108
+ outputs=gr.Textbox(label="Real-Time Trade Data"),
109
+ title="Real-Time Cryptocurrency Trade Data"
110
+ )
111
+
112
+ # Price Chart Interface
113
+ crypto_price_chart_interface = gr.Interface(
114
+ fn=plot_historical_prices,
115
+ inputs=[
116
+ gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
117
+ gr.Textbox(value="2024-01-01", label="From Date (YYYY-MM-DD)"),
118
+ gr.Textbox(value="2025-12-31", label="To Date (YYYY-MM-DD)")
119
+ ],
120
+ outputs=gr.Plot(label="Cryptocurrency Price Chart"),
121
+ title="Cryptocurrency Price Chart (2024-2025)"
122
+ )
123
+
124
+ # Current Price Interface
125
+ current_price_interface = gr.Interface(
126
+ fn=fetch_current_price,
127
+ inputs=gr.Dropdown(choices=['bitcoin'], label="Cryptocurrency"),
128
+ outputs=gr.Textbox(label="Current Price"),
129
+ title="Current Cryptocurrency Price"
130
+ )
131
+
132
+ # Combining all interfaces into a single tabbed app
133
+ gr.TabbedInterface(
134
+ [crypto_price_chart_interface, current_price_interface, real_time_trade_interface, btc_analysis_interface, tradingview_interface],
135
+ tab_names=["Price Chart", "Current Price", "Real-Time Trade Data", "AI BTC Analysis", "TradingView Chart"]
136
+ ).launch()