Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,11 @@ import time
|
|
6 |
import csv
|
7 |
import threading
|
8 |
import os
|
9 |
-
from huggingface_hub import HfApi
|
10 |
|
11 |
# Hugging Face credentials
|
12 |
HF_USERNAME = "abolfazle80" # Your Hugging Face username
|
13 |
-
REPO_NAME = "
|
14 |
CSV_FILENAME = "USDT.csv" # File in your dataset
|
15 |
|
16 |
# Define the markets (cryptocurrencies to monitor)
|
@@ -22,26 +22,20 @@ api = HfApi()
|
|
22 |
# Flag to control fetching loop
|
23 |
loop_running = False
|
24 |
|
25 |
-
# Function to
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
else:
|
40 |
-
return ["Symbol not found", "Symbol not found"]
|
41 |
-
except requests.exceptions.RequestException as e:
|
42 |
-
return ["Request error", "Request error"]
|
43 |
-
except ValueError as e:
|
44 |
-
return ["JSON decode error", "JSON decode error"]
|
45 |
|
46 |
# Function to upload CSV to Hugging Face
|
47 |
def upload_to_huggingface():
|
@@ -91,9 +85,33 @@ def fetch_data():
|
|
91 |
print(f"β Error: {e}")
|
92 |
loop_running = False # Stop loop if error occurs
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Streamlit UI elements
|
95 |
st.title("π Live Crypto Data Fetcher")
|
96 |
|
|
|
|
|
|
|
97 |
# Start button
|
98 |
if st.button("Start Fetching Data"):
|
99 |
if not loop_running:
|
|
|
6 |
import csv
|
7 |
import threading
|
8 |
import os
|
9 |
+
from huggingface_hub import HfApi, hf_hub_download
|
10 |
|
11 |
# Hugging Face credentials
|
12 |
HF_USERNAME = "abolfazle80" # Your Hugging Face username
|
13 |
+
REPO_NAME = "crypto_data" # Your dataset repo name
|
14 |
CSV_FILENAME = "USDT.csv" # File in your dataset
|
15 |
|
16 |
# Define the markets (cryptocurrencies to monitor)
|
|
|
22 |
# Flag to control fetching loop
|
23 |
loop_running = False
|
24 |
|
25 |
+
# Function to download CSV if missing
|
26 |
+
def download_csv_if_needed():
|
27 |
+
"""Downloads CSV from Hugging Face if not present locally."""
|
28 |
+
if not os.path.exists(CSV_FILENAME):
|
29 |
+
try:
|
30 |
+
hf_hub_download(
|
31 |
+
repo_id=f"{HF_USERNAME}/{REPO_NAME}",
|
32 |
+
filename=CSV_FILENAME,
|
33 |
+
repo_type="dataset",
|
34 |
+
local_dir=".", # Save in current directory
|
35 |
+
)
|
36 |
+
print("β
CSV downloaded successfully!")
|
37 |
+
except Exception as e:
|
38 |
+
print(f"β Download failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Function to upload CSV to Hugging Face
|
41 |
def upload_to_huggingface():
|
|
|
85 |
print(f"β Error: {e}")
|
86 |
loop_running = False # Stop loop if error occurs
|
87 |
|
88 |
+
# Function to get price and volume data from CoinEx
|
89 |
+
def get_crypto_price_from_coinex(symbol):
|
90 |
+
url = 'https://api.coinex.com/v1/market/ticker'
|
91 |
+
params = {'market': symbol}
|
92 |
+
|
93 |
+
try:
|
94 |
+
response = requests.get(url, params=params)
|
95 |
+
response.raise_for_status() # Check for HTTP errors
|
96 |
+
data = response.json()
|
97 |
+
|
98 |
+
if 'data' in data:
|
99 |
+
price = data['data']['ticker']['last']
|
100 |
+
volume = data['data']['ticker']['vol']
|
101 |
+
return [price, volume] # Return as list
|
102 |
+
else:
|
103 |
+
return ["Symbol not found", "Symbol not found"]
|
104 |
+
except requests.exceptions.RequestException as e:
|
105 |
+
return ["Request error", "Request error"]
|
106 |
+
except ValueError as e:
|
107 |
+
return ["JSON decode error", "JSON decode error"]
|
108 |
+
|
109 |
# Streamlit UI elements
|
110 |
st.title("π Live Crypto Data Fetcher")
|
111 |
|
112 |
+
# Ensure CSV is available
|
113 |
+
download_csv_if_needed()
|
114 |
+
|
115 |
# Start button
|
116 |
if st.button("Start Fetching Data"):
|
117 |
if not loop_running:
|