Spaces:
Runtime error
Runtime error
# A helper script to run the translations API inside Docker container | |
# This ensures the correct working directory and environment | |
# Works for both local development and Hugging Face Spaces | |
# Change to the server directory | |
cd $HOME/app/server | |
# Set up models directory - prefer /data for HF Spaces, fallback to local | |
if [ -d "/data" ] && [ -w "/data" ]; then | |
echo "Using /data directory for persistent storage (HF Spaces)" | |
export MODELS_DIR="/data/models" | |
mkdir -p "$MODELS_DIR" | |
chmod 755 "$MODELS_DIR" 2>/dev/null || true | |
else | |
echo "Using local models directory" | |
export MODELS_DIR="$HOME/app/models" | |
mkdir -p "$MODELS_DIR" | |
chmod 755 "$MODELS_DIR" | |
fi | |
# Debug: Check directory permissions | |
echo "Models directory: $MODELS_DIR" | |
echo "Current user: $(whoami)" | |
echo "Directory exists: $([ -d "$MODELS_DIR" ] && echo "yes" || echo "no")" | |
echo "Directory writable: $([ -w "$MODELS_DIR" ] && echo "yes" || echo "no")" | |
if [ -d "$MODELS_DIR" ]; then | |
echo "Directory permissions: $(ls -la "$MODELS_DIR" 2>/dev/null || echo "cannot list")" | |
fi | |
# Export MODELS_DIR so Python scripts can use it | |
export MODELS_DIR | |
# Download models on startup (will be cached for subsequent runs) | |
echo "Ensuring MMS models are available in $MODELS_DIR..." | |
bash ./download_models.sh | |
# Add current directory to PYTHONPATH to make modules importable | |
export PYTHONPATH=$PYTHONPATH:$(pwd) | |
echo "Updated PYTHONPATH: $PYTHONPATH" | |
# Determine port - use PORT env var for HF Spaces, default to 7860 | |
PORT=${PORT:-7860} | |
echo "Starting server on port $PORT" | |
# Start the Flask server with single worker to avoid multiple model loading | |
# Large ML models should use single worker to prevent OOM issues | |
# Increased timeout for long-running ML inference tasks | |
gunicorn --worker-tmp-dir /dev/shm server:app --access-logfile /dev/stdout --log-file /dev/stderr -b 0.0.0.0:$PORT \ | |
--worker-class gthread --workers 1 --threads 20 \ | |
--worker-connections 1000 --backlog 2048 --keep-alive 60 --timeout 600 | |