siddani93's picture
Update app.py
ff67b38 verified
Raw History Blame Contribute Delete
2.7 kB
import streamlit as st
import pandas as pd
import time
import json
import websocket
from transformers import pipeline
# Initialize a Hugging Face model for text generation
# You can replace with your preferred model on Hugging Face
model = pipeline('text-generation', model='gpt2') # Example GPT model, replace with your custom model
# WebSocket to get real-time data (using Binance WebSocket as an example for BTC/USDT)
def on_message(ws, message):
data = json.loads(message)
price = float(data['k']['c']) # Close price from WebSocket data
signal = get_signal_from_model(price) # Get the signal from the model
update_signal(signal, price) # Update the signal in the Streamlit interface
def on_error(ws, error):
print("Error:", error)
def on_close(ws, close_status_code, close_msg):
print("Closed")
def on_open(ws):
print("Connection established")
# Function to get signal from the model
def get_signal_from_model(price):
# In this example, we are generating a signal based on a price input
# You can replace this with your own model logic or more advanced signal generation
input_text = f"Generate trading signal based on the current price: {price}"
prediction = model(input_text, max_length=50, num_return_sequences=1)
return prediction[0]['generated_text']
# Streamlit setup
st.title('Crypto Trading Signals with Hugging Face & Streamlit')
# Placeholder to display the signal
signal_placeholder = st.empty()
# Placeholder to display the current price
price_placeholder = st.empty()
# Function to update the signal on Streamlit interface
def update_signal(signal, price):
signal_placeholder.write(f"Signal: {signal}")
price_placeholder.write(f"Current Price: ${price}")
# Embedding TradingView SuperChart in Streamlit
st.markdown("""
<iframe src="https://www.tradingview.com/chart/?symbol=BINANCE%3ABTCUSDT"
width="100%" height="500px"></iframe>
""", unsafe_allow_html=True)
# WebSocket connection URL for Binance (you can change the symbol here)
ws_url = "wss://stream.binance.com:9443/ws/btcusdt@kline_1s"
# WebSocketApp to fetch real-time data every second
ws = websocket.WebSocketApp(ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
# Start WebSocket in a separate thread to allow real-time signal updates
import threading
def start_socket():
ws.run_forever()
thread = threading.Thread(target=start_socket)
thread.start()
# Run the Streamlit app with real-time updates every 10 seconds
while True:
time.sleep(10) # Update every 10 seconds, adjust as needed for your strategy