abolfazle80 commited on
Commit
72b3c77
·
verified ·
1 Parent(s): bdd3fac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -1,29 +1,36 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
- import os
5
- # Hugging Face Dataset CSV URL
6
- CSV_URL = "https://huggingface.co/datasets/abolfazle80/crypto_data/resolve/main/USDT.csv"
7
 
8
- # Your Hugging Face Token (Keep it secret!)
9
- HF_TOKEN = os.getenv("HF_TOKEN")
10
- # Function to fetch CSV data with authentication
11
- def fetch_csv_data():
12
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
 
 
13
  try:
14
- response = requests.get(CSV_URL, headers=headers)
15
- response.raise_for_status() # Raise error for bad responses (4xx, 5xx)
16
- df = pd.read_csv(pd.compat.StringIO(response.text))
17
- return df
 
 
 
 
 
 
18
  except Exception as e:
19
- st.error(f"Failed to fetch data: {e}")
20
- return None
21
 
22
  # Streamlit UI
23
- st.title("Live Crypto Data from Hugging Face Dataset")
24
 
25
- # Button to fetch and display the data
26
  if st.button("Show Latest Data"):
27
- df = fetch_csv_data()
28
- if df is not None:
29
- st.write(df.tail()) # Show last few rows
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
+ import io # Import io for StringIO
 
 
5
 
6
+ # Hugging Face dataset URL
7
+ HF_USERNAME = "abolfazle80"
8
+ REPO_NAME = "crypto_data"
9
+ CSV_FILENAME = "USDT.csv"
10
+ HF_CSV_URL = f"https://huggingface.co/datasets/{HF_USERNAME}/{REPO_NAME}/resolve/main/{CSV_FILENAME}"
11
+
12
+ # Function to fetch latest data
13
+ def fetch_latest_data():
14
  try:
15
+ response = requests.get(HF_CSV_URL)
16
+ response.raise_for_status() # Raise an error if request fails
17
+
18
+ # Use io.StringIO instead of pandas.compat.StringIO
19
+ csv_data = io.StringIO(response.text)
20
+ df = pd.read_csv(csv_data)
21
+
22
+ return df.tail() # Show last few rows
23
+ except requests.exceptions.RequestException as e:
24
+ return f"Failed to fetch data: {e}"
25
  except Exception as e:
26
+ return f"Error processing data: {e}"
 
27
 
28
  # Streamlit UI
29
+ st.title("📊 View Latest Crypto Data")
30
 
 
31
  if st.button("Show Latest Data"):
32
+ latest_data = fetch_latest_data()
33
+ if isinstance(latest_data, str): # Check if error occurred
34
+ st.error(latest_data)
35
+ else:
36
+ st.write(latest_data) # Display DataFrame