Spaces:
Runtime error
Runtime error
| # Use the official Python 3.9 image | |
| FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 | |
| RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 curl -y | |
| # Set the working directory to /code | |
| WORKDIR /code | |
| # Copy the current directory contents into the container at /code | |
| COPY ./requirements.txt /code/requirements.txt | |
| # Install requirements.txt | |
| RUN pip install pip==24.0 | |
| RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt | |
| # Set up a new user named "user" with user ID 1000 | |
| RUN useradd -m -u 1000 user | |
| # Switch to the "user" user | |
| USER user | |
| # Set home to the user's home directory | |
| ENV HOME=/home/user \ | |
| PATH=/home/user/.local/bin:$PATH | |
| # Set the working directory to the user's home directory | |
| WORKDIR $HOME/app | |
| # Copy the current directory contents into the container at $HOME/app setting the owner to the user | |
| COPY --chown=user . $HOME/app | |
| # Create a startup script for HF Spaces | |
| RUN cat > start_hf_spaces.sh << 'EOF' | |
| #!/bin/bash | |
| set -e | |
| echo "π Starting Neural OS for HF Spaces" | |
| echo "====================================" | |
| echo "π Current directory: $(pwd)" | |
| echo "π Files in current directory:" | |
| ls -la | |
| # Check if required files exist | |
| if [[ ! -f "dispatcher.py" ]]; then | |
| echo "β Error: dispatcher.py not found" | |
| exit 1 | |
| fi | |
| if [[ ! -f "worker.py" ]]; then | |
| echo "β Error: worker.py not found" | |
| exit 1 | |
| fi | |
| if [[ ! -f "static/index.html" ]]; then | |
| echo "β Error: static/index.html not found" | |
| exit 1 | |
| fi | |
| echo "β All required files found" | |
| # Start dispatcher in background | |
| echo "π― Starting dispatcher..." | |
| python dispatcher.py --port 7860 > dispatcher.log 2>&1 & | |
| DISPATCHER_PID=$! | |
| echo "π Dispatcher PID: $DISPATCHER_PID" | |
| # Wait for dispatcher to start and check if it's running | |
| echo "β³ Waiting for dispatcher to initialize..." | |
| sleep 5 | |
| if ! kill -0 $DISPATCHER_PID 2>/dev/null; then | |
| echo "β Dispatcher failed to start" | |
| echo "π Dispatcher log:" | |
| cat dispatcher.log | |
| exit 1 | |
| fi | |
| # Test if dispatcher is responding | |
| echo "π Testing dispatcher health..." | |
| curl -f http://localhost:7860/ > /dev/null 2>&1 | |
| if [ $? -eq 0 ]; then | |
| echo "β Dispatcher is responding to HTTP requests" | |
| else | |
| echo "β οΈ Dispatcher HTTP test failed, but continuing..." | |
| fi | |
| # Start single worker | |
| echo "π§ Starting worker..." | |
| python worker.py --worker-address localhost:8001 --dispatcher-url http://localhost:7860 > worker.log 2>&1 & | |
| WORKER_PID=$! | |
| echo "π Worker PID: $WORKER_PID" | |
| # Wait for worker to initialize | |
| echo "β³ Waiting for worker to initialize..." | |
| sleep 30 | |
| # Check if worker is still running | |
| if ! kill -0 $WORKER_PID 2>/dev/null; then | |
| echo "β Worker failed to start" | |
| echo "π Worker log:" | |
| cat worker.log | |
| echo "π Dispatcher log:" | |
| cat dispatcher.log | |
| exit 1 | |
| fi | |
| echo "β System ready!" | |
| echo "π Web interface: http://localhost:7860" | |
| echo "π Dispatcher PID: $DISPATCHER_PID" | |
| echo "π Worker PID: $WORKER_PID" | |
| # Function to cleanup | |
| cleanup() { | |
| echo "π Shutting down..." | |
| kill $DISPATCHER_PID $WORKER_PID 2>/dev/null || true | |
| exit 0 | |
| } | |
| trap cleanup SIGINT SIGTERM | |
| # Keep the script running by following the dispatcher log | |
| echo "π Following dispatcher log (Ctrl+C to stop):" | |
| tail -f dispatcher.log & | |
| TAIL_PID=$! | |
| # Wait for dispatcher (main process) | |
| wait $DISPATCHER_PID | |
| # Clean up tail process | |
| kill $TAIL_PID 2>/dev/null || true | |
| EOF | |
| RUN chmod +x start_hf_spaces.sh | |
| CMD ["bash", "start_hf_spaces.sh"] |