Spaces:
Running
Running
File size: 1,633 Bytes
e6ab617 c13c8c3 2b04abf c13c8c3 deff717 e6ab617 deff717 e6ab617 639008d e6ab617 639008d e6ab617 c13c8c3 2b04abf c13c8c3 2b04abf e6ab617 639008d e6ab617 c13c8c3 2b04abf e6ab617 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import yfinance as yf
import pandas as pd
import time
from datasets import Dataset
import os
# Hugging Face Credentials
HF_USERNAME = "KavinduHansaka"
HF_DATASET_REPO = f"{HF_USERNAME}/btc-minute-data"
HF_TOKEN = os.getenv("Token")
# Function to Fetch BTC Prices from Yahoo Finance (Last 24h, 1-min interval)
def fetch_btc_price():
try:
btc = yf.Ticker("BTC-USD")
df = btc.history(period="1d", interval="1m") # Fetch last 24 hours with 1-min intervals
df = df.tail(240) # Keep only the last 4 hours (240 minutes)
df.reset_index(inplace=True)
df = df[["Datetime", "Close"]].rename(columns={"Datetime": "timestamp", "Close": "price"})
df["timestamp"] = df["timestamp"].astype(str)
return df
except Exception as e:
print(f"β Failed to fetch BTC price: {e}")
return None
# Convert & Upload Dataset
def create_hf_dataset(df):
return Dataset.from_pandas(df)
def upload_to_huggingface(dataset):
dataset.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
print(f"β
Dataset updated at: https://huggingface.co/datasets/{HF_USERNAME}/btc-minute-data")
def update_dataset():
print("Fetching latest BTC minute data...")
df = fetch_btc_price()
if df is None or df.empty:
print("β Failed to fetch BTC price.")
return
print("Uploading BTC dataset to Hugging Face...")
dataset = create_hf_dataset(df)
upload_to_huggingface(dataset)
print("β
BTC Dataset updated successfully!")
if __name__ == "__main__":
while True:
update_dataset()
time.sleep(3600) # Update dataset every hour
|