Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import pandas as pd | |
from datetime import datetime | |
import time | |
import csv | |
import threading | |
from huggingface_hub import HfApi | |
import os | |
# Hugging Face Info | |
HF_USERNAME = "abolfazle80" | |
REPO_NAME = "crypto_data" | |
CSV_FILENAME = "USDT.csv" | |
HF_CSV_URL = f"https://huggingface.co/datasets/{HF_USERNAME}/{REPO_NAME}/resolve/main/{CSV_FILENAME}" | |
# Define the markets (cryptocurrencies to monitor) | |
markets = ['DOTUSDT', 'BTCUSDT', 'ADAUSDT', 'BNBUSDT', 'SUIUSDT', 'XRPUSDT'] | |
# Initialize Hugging Face API | |
api = HfApi() | |
# πΉ Initialize loop control variable (Fixed Global Issue) | |
loop_running = False | |
# Function to fetch data from CoinEx | |
def get_crypto_price_from_coinex(symbol): | |
url = 'https://api.coinex.com/v1/market/ticker' | |
params = {'market': symbol} | |
try: | |
response = requests.get(url, params=params) | |
response.raise_for_status() | |
data = response.json() | |
if 'data' in data: | |
price = data['data']['ticker']['last'] | |
volume = data['data']['ticker']['vol'] | |
return [price, volume] | |
else: | |
return ["Symbol not found", "Symbol not found"] | |
except Exception as e: | |
st.error(f"Error fetching data: {e}") | |
return ["Error", "Error"] | |
# Function to download CSV from Hugging Face and save it locally | |
def download_existing_csv(): | |
try: | |
response = requests.get(HF_CSV_URL) | |
response.raise_for_status() | |
with open(CSV_FILENAME, "wb") as file: | |
file.write(response.content) | |
df = pd.read_csv(CSV_FILENAME) | |
return df | |
except requests.exceptions.RequestException: | |
return pd.DataFrame() # Return empty DataFrame if file doesn't exist | |
# Function to upload the CSV back to Hugging Face | |
def upload_to_huggingface(): | |
try: | |
api.upload_file( | |
path_or_fileobj=CSV_FILENAME, | |
path_in_repo=CSV_FILENAME, | |
repo_id=f"{HF_USERNAME}/{REPO_NAME}", | |
repo_type="dataset", | |
) | |
print("β CSV successfully uploaded to Hugging Face.") | |
except Exception as e: | |
print(f"β Upload failed: {e}") | |
# Function to fetch, append & update the CSV | |
def fetch_data(): | |
global loop_running # β Fixed Scope Issue | |
while loop_running: | |
try: | |
all_data = [] | |
for market in markets: | |
crypto_data = get_crypto_price_from_coinex(market) | |
all_data += crypto_data | |
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
all_data.append(timestamp) | |
# Check if CSV exists locally; if not, download it | |
if not os.path.exists(CSV_FILENAME): | |
df_existing = download_existing_csv() | |
else: | |
df_existing = pd.read_csv(CSV_FILENAME) | |
# Convert new data to DataFrame | |
df_new = pd.DataFrame([all_data], columns=[f"{m} Price" for m in markets] + | |
[f"{m} Volume" for m in markets] + | |
["Timestamp"]) | |
# Append new data to existing data | |
df_combined = pd.concat([df_existing, df_new], ignore_index=True) | |
# Save updated data to CSV | |
df_combined.to_csv(CSV_FILENAME, index=False) | |
print(f"β {all_data} added to CSV and uploaded.") | |
# Upload to Hugging Face | |
upload_to_huggingface() | |
time.sleep(1) # Fetch new data every second | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
loop_running = False | |
# Streamlit UI | |
st.title("π Live Cryptocurrency Data Fetching") | |
# Start Fetching Button | |
if st.button("Start Fetching Data"): | |
if not loop_running: # β Fixed issue by ensuring variable scope | |
loop_running = True | |
threading.Thread(target=fetch_data, daemon=True).start() | |
st.success("β Started fetching and saving data.") | |
else: | |
st.warning("β οΈ Fetching is already running.") | |
# Stop Fetching Button | |
if st.button("Stop Fetching Data"): | |
loop_running = False | |
st.success("π Stopped fetching data.") | |
# Show Latest Data | |
st.text("π Latest Data from CSV:") | |
# Ensure the file exists before reading it | |
if not os.path.exists(CSV_FILENAME): | |
download_existing_csv() | |
try: | |
df = pd.read_csv(CSV_FILENAME) | |
st.write(df.tail()) # Show last 5 rows | |
except Exception as e: | |
st.error(f"Error reading CSV: {e}") | |