abolfazle80 commited on
Commit
9f0e6b3
Β·
verified Β·
1 Parent(s): bf8b3ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -60
app.py CHANGED
@@ -5,10 +5,22 @@ from datetime import datetime
5
  import time
6
  import csv
7
  import threading
 
 
 
 
 
 
 
8
 
9
  # Define the markets (cryptocurrencies to monitor)
10
  markets = ['DOTUSDT', 'BTCUSDT', 'ADAUSDT', 'BNBUSDT', 'SUIUSDT', 'XRPUSDT']
11
- filename = 'USDT.csv'
 
 
 
 
 
12
 
13
  # Function to get price and volume data from CoinEx
14
  def get_crypto_price_from_coinex(symbol):
@@ -23,58 +35,29 @@ def get_crypto_price_from_coinex(symbol):
23
  if 'data' in data:
24
  price = data['data']['ticker']['last']
25
  volume = data['data']['ticker']['vol']
26
- return [price, volume] # Return the price and volume as a list
27
  else:
28
  return ["Symbol not found", "Symbol not found"]
29
  except requests.exceptions.RequestException as e:
30
- st.error(f"Request error: {e}")
31
  return ["Request error", "Request error"]
32
  except ValueError as e:
33
- st.error(f"JSON decode error: {e}")
34
  return ["JSON decode error", "JSON decode error"]
35
 
36
- # Initialize a flag to control the loop
37
- loop_running = False
38
-
39
- # Check if the file exists and write the header if it doesn't
40
- file_exists = False
41
- try:
42
- with open(filename, 'r') as file:
43
- file_exists = True
44
- except FileNotFoundError:
45
- file_exists = False
46
-
47
- if not file_exists:
48
- with open(filename, 'w', newline='') as file:
49
- writer = csv.writer(file)
50
- # Write header: price and volume for all cryptos in markets list, with a timestamp
51
- header = []
52
- for market in markets:
53
- header.append(f'{market} Price')
54
- header.append(f'{market} Volume')
55
- header.append('Timestamp') # Add timestamp to the end
56
- writer.writerow(header)
57
- from huggingface_hub import HfApi
58
- import os
59
-
60
- HF_USERNAME = "abolfazle80" # Your Hugging Face username
61
- REPO_NAME = "crypto_data" # Your dataset repo
62
-
63
- api = HfApi()
64
-
65
  def upload_to_huggingface():
66
- """Uploads the CSV file to Hugging Face, preserving all appended data."""
67
  try:
68
  api.upload_file(
69
- path_or_fileobj="USDT.csv", # Upload the local CSV
70
- path_in_repo="USDT.csv", # Save as USDT.csv in the repo
71
  repo_id=f"{HF_USERNAME}/{REPO_NAME}",
72
  repo_type="dataset",
73
  )
74
- print("CSV successfully uploaded to Hugging Face.")
75
  except Exception as e:
76
- print(f"Upload failed: {e}")
77
 
 
78
  def fetch_data():
79
  """Fetches crypto data, appends to CSV, and uploads to Hugging Face."""
80
  global loop_running
@@ -83,53 +66,52 @@ def fetch_data():
83
  all_data = []
84
  for market in markets:
85
  crypto_data = get_crypto_price_from_coinex(market)
86
- all_data += crypto_data # Add price and volume
87
 
88
- timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Get the current timestamp
89
- all_data.append(timestamp) # Append timestamp to the row
90
 
91
- # Append the data to the CSV file (do not overwrite)
92
- file_exists = os.path.isfile("USDT.csv") # Check if file exists
93
- with open("USDT.csv", "a", newline='') as file:
94
  writer = csv.writer(file)
95
  if not file_exists:
96
- # Write header if the file is newly created
97
  header = [f"{m} Price" for m in markets] + [f"{m} Volume" for m in markets] + ["Timestamp"]
98
  writer.writerow(header)
99
  writer.writerow(all_data) # Append new row
100
 
101
- print(f"{all_data} added to CSV")
102
 
103
- # Upload the updated CSV to Hugging Face
104
  upload_to_huggingface()
105
 
106
  time.sleep(1) # Fetch new data every second
107
  except Exception as e:
108
- st.error(f"An error occurred: {e}")
109
  loop_running = False # Stop loop if error occurs
110
 
111
  # Streamlit UI elements
112
- st.title("Live Cryptocurrency Data with Start/Stop Loop")
113
 
114
- # Button to start the loop
115
  if st.button("Start Fetching Data"):
116
  if not loop_running:
117
  loop_running = True
118
- # Run the fetch data function in a separate thread so it doesn't block the Streamlit UI
119
  threading.Thread(target=fetch_data, daemon=True).start()
120
- st.success("Started fetching data and saving it to CSV.")
121
  else:
122
- st.warning("Data fetching is already running.")
123
 
124
- # Button to stop the loop
125
  if st.button("Stop Fetching Data"):
126
  loop_running = False
127
- st.success("Stopped fetching data.")
128
 
129
- # Display the latest data from the CSV (if available)
130
- st.text("Latest Data from CSV:")
131
  try:
132
- df = pd.read_csv(filename)
133
- st.write(df.tail()) # Show the last few rows of the CSV
134
  except Exception as e:
135
- st.error(f"Error reading the CSV: {e}")
 
5
  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 = "crypto_data" # Your dataset repo name
14
+ CSV_FILENAME = "USDT.csv" # File in your dataset
15
 
16
  # Define the markets (cryptocurrencies to monitor)
17
  markets = ['DOTUSDT', 'BTCUSDT', 'ADAUSDT', 'BNBUSDT', 'SUIUSDT', 'XRPUSDT']
18
+
19
+ # Initialize Hugging Face API
20
+ api = HfApi()
21
+
22
+ # Flag to control fetching loop
23
+ loop_running = False
24
 
25
  # Function to get price and volume data from CoinEx
26
  def get_crypto_price_from_coinex(symbol):
 
35
  if 'data' in data:
36
  price = data['data']['ticker']['last']
37
  volume = data['data']['ticker']['vol']
38
+ return [price, volume] # Return as list
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():
48
+ """Uploads the CSV file to the Hugging Face dataset repo."""
49
  try:
50
  api.upload_file(
51
+ path_or_fileobj=CSV_FILENAME,
52
+ path_in_repo=CSV_FILENAME,
53
  repo_id=f"{HF_USERNAME}/{REPO_NAME}",
54
  repo_type="dataset",
55
  )
56
+ print("βœ… CSV successfully uploaded to Hugging Face.")
57
  except Exception as e:
58
+ print(f"❌ Upload failed: {e}")
59
 
60
+ # Function to fetch and save data
61
  def fetch_data():
62
  """Fetches crypto data, appends to CSV, and uploads to Hugging Face."""
63
  global loop_running
 
66
  all_data = []
67
  for market in markets:
68
  crypto_data = get_crypto_price_from_coinex(market)
69
+ all_data += crypto_data # Append price and volume
70
 
71
+ timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # Timestamp
72
+ all_data.append(timestamp) # Append timestamp to row
73
 
74
+ # Append data to the local CSV file
75
+ file_exists = os.path.isfile(CSV_FILENAME)
76
+ with open(CSV_FILENAME, "a", newline="") as file:
77
  writer = csv.writer(file)
78
  if not file_exists:
79
+ # Write header if the file was just created
80
  header = [f"{m} Price" for m in markets] + [f"{m} Volume" for m in markets] + ["Timestamp"]
81
  writer.writerow(header)
82
  writer.writerow(all_data) # Append new row
83
 
84
+ print(f"βœ… {all_data} added to CSV")
85
 
86
+ # Upload updated CSV to Hugging Face
87
  upload_to_huggingface()
88
 
89
  time.sleep(1) # Fetch new data every second
90
  except Exception as e:
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:
100
  loop_running = True
 
101
  threading.Thread(target=fetch_data, daemon=True).start()
102
+ st.success("βœ… Started fetching data and saving to CSV.")
103
  else:
104
+ st.warning("⚠️ Data fetching is already running.")
105
 
106
+ # Stop button
107
  if st.button("Stop Fetching Data"):
108
  loop_running = False
109
+ st.success("πŸ›‘ Stopped fetching data.")
110
 
111
+ # Show latest data
112
+ st.subheader("πŸ“Œ Latest Data from CSV:")
113
  try:
114
+ df = pd.read_csv(CSV_FILENAME)
115
+ st.write(df.tail()) # Show last few rows
116
  except Exception as e:
117
+ st.error(f"❌ Error reading CSV: {e}")