Spaces:
Running
Running
import os | |
import sys | |
import threading | |
import subprocess | |
import time | |
import streamlit as st | |
# Check if running on Hugging Face Spaces | |
is_hf_space = os.environ.get('SPACE_ID') is not None | |
# Function to start Flask backend | |
def start_flask_backend(): | |
# Use Python executable to ensure compatibility with Streamlit's environment | |
subprocess.run([sys.executable, 'api.py']) | |
# Only start the Flask server if it's not already running | |
def is_flask_running(): | |
try: | |
import requests | |
response = requests.get("http://localhost:5000/api/orders") | |
return response.status_code == 200 | |
except: | |
return False | |
# Start Flask in a separate thread if needed | |
if not is_flask_running(): | |
thread = threading.Thread(target=start_flask_backend) | |
thread.daemon = True # Daemon threads exit when the main program exits | |
thread.start() | |
# Wait a bit for Flask to start up | |
time.sleep(2) | |
# Check if Flask started successfully | |
if not is_flask_running(): | |
st.error("Failed to start the backend server. Please check the logs.") | |
st.stop() | |
# Now import and run the Streamlit app | |
import frontend_app | |
# Run the Streamlit app (this will call frontend_app.main()) | |
frontend_app.main() |