Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,13 @@
|
|
|
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
|
4 |
def get_crypto_compare_price():
|
5 |
"""Fetch the Bitcoin price from CryptoCompare."""
|
6 |
try:
|
7 |
-
|
|
|
8 |
headers = {
|
9 |
'Authorization': f'Apikey {api_key}'
|
10 |
}
|
@@ -13,67 +16,25 @@ def get_crypto_compare_price():
|
|
13 |
params={"fsym": "BTC", "tsyms": "USD"},
|
14 |
headers=headers
|
15 |
)
|
16 |
-
response.raise_for_status()
|
17 |
data = response.json()
|
18 |
return f"BTC Price (CryptoCompare): ${data['USD']:.2f}"
|
19 |
except requests.exceptions.RequestException as e:
|
20 |
return f"Error fetching CryptoCompare data: {str(e)}"
|
21 |
|
22 |
-
def get_real_time_trade_data():
|
23 |
-
"""Fetch real-time trade data."""
|
24 |
-
try:
|
25 |
-
# Example endpoint (replace with your real trade data API endpoint)
|
26 |
-
response = requests.get("https://api.example.com/realtime-trade-data?symbol=BTCUSD")
|
27 |
-
response.raise_for_status()
|
28 |
-
data = response.json()
|
29 |
-
return f"Real-time Trade Data: {data}"
|
30 |
-
except requests.exceptions.RequestException as e:
|
31 |
-
return f"Error fetching trade data: {str(e)}"
|
32 |
-
|
33 |
-
def get_tradingview_chart():
|
34 |
-
"""Generate TradingView embed code."""
|
35 |
-
widget_code = """
|
36 |
-
<div class="tradingview-widget-container">
|
37 |
-
<div id="tradingview_abcdef"></div>
|
38 |
-
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
|
39 |
-
<script type="text/javascript">
|
40 |
-
new TradingView.widget({
|
41 |
-
"width": 800,
|
42 |
-
"height": 600,
|
43 |
-
"symbol": "BINANCE:BTCUSDT",
|
44 |
-
"interval": "D",
|
45 |
-
"timezone": "Etc/UTC",
|
46 |
-
"theme": "light",
|
47 |
-
"style": "1",
|
48 |
-
"locale": "en",
|
49 |
-
"toolbar_bg": "#f1f3f6",
|
50 |
-
"enable_publishing": false,
|
51 |
-
"hide_top_toolbar": true,
|
52 |
-
"save_image": false,
|
53 |
-
"container_id": "tradingview_abcdef"
|
54 |
-
});
|
55 |
-
</script>
|
56 |
-
</div>
|
57 |
-
"""
|
58 |
-
return widget_code
|
59 |
-
|
60 |
def analyze_btc(prompt):
|
61 |
-
"""Analyze the prompt and provide information."""
|
62 |
if "price" in prompt.lower():
|
63 |
return get_crypto_compare_price()
|
64 |
-
|
65 |
-
return get_real_time_trade_data()
|
66 |
-
elif "chart" in prompt.lower():
|
67 |
-
return gr.HTML(get_tradingview_chart()) # Use HTML component for TradingView chart
|
68 |
-
return "Try asking about the BTC price, real-time trade data, or TradingView chart."
|
69 |
|
70 |
# Set up the Gradio interface
|
71 |
interface = gr.Interface(
|
72 |
fn=analyze_btc,
|
73 |
inputs="text",
|
74 |
-
outputs="
|
75 |
-
title="Bitcoin Analyzer",
|
76 |
-
description="Ask about the current Bitcoin price
|
77 |
)
|
78 |
|
79 |
# Launch the Gradio app
|
|
|
1 |
+
|
2 |
+
|
3 |
import requests
|
4 |
import gradio as gr
|
5 |
|
6 |
def get_crypto_compare_price():
|
7 |
"""Fetch the Bitcoin price from CryptoCompare."""
|
8 |
try:
|
9 |
+
# Replace 'YOUR_API_KEY' with your actual CryptoCompare API key
|
10 |
+
api_key = 'YOUR_API_KEY'
|
11 |
headers = {
|
12 |
'Authorization': f'Apikey {api_key}'
|
13 |
}
|
|
|
16 |
params={"fsym": "BTC", "tsyms": "USD"},
|
17 |
headers=headers
|
18 |
)
|
19 |
+
response.raise_for_status() # Raise an HTTPError for bad responses
|
20 |
data = response.json()
|
21 |
return f"BTC Price (CryptoCompare): ${data['USD']:.2f}"
|
22 |
except requests.exceptions.RequestException as e:
|
23 |
return f"Error fetching CryptoCompare data: {str(e)}"
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def analyze_btc(prompt):
|
26 |
+
"""Analyze the prompt and provide information on Bitcoin price."""
|
27 |
if "price" in prompt.lower():
|
28 |
return get_crypto_compare_price()
|
29 |
+
return "Try asking about the BTC price."
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Set up the Gradio interface
|
32 |
interface = gr.Interface(
|
33 |
fn=analyze_btc,
|
34 |
inputs="text",
|
35 |
+
outputs="text",
|
36 |
+
title="Bitcoin Price Analyzer",
|
37 |
+
description="Ask about the current Bitcoin price. For example: 'What's the price of Bitcoin?'"
|
38 |
)
|
39 |
|
40 |
# Launch the Gradio app
|