neural-os / Dockerfile
da03
.
d83ec2c
raw
history blame
3.44 kB
# 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"]