Upload 2 files
Browse filesFiles changed (2)
- Dockerfile+17-5
- web_app.py+6
1234567891011121314151617181920# Use a lightweight Python imageFROM python:3.9-slim# Install pngquant, jpegoptim and zipRUN apt-get update && \apt-get install -y pngquant jpegoptim zip && \rm -rf /var/lib/apt/lists/*# Set working directoryWORKDIR /app# Copy application filesCOPY web_app.py .COPY index.html .# Expose the port (optional documentation)EXPOSE 8989# Start the applicationCMD ["python", "web_app.py"]1234567891011121314151617181920212223242526272829303132# Use a lightweight Python imageFROM python:3.9-slim# Set environment variables# PYTHONUNBUFFERED: Forces stdout/stderr to be flushed immediately (critical for logs)# PYTHONDONTWRITEBYTECODE: Prevents Python from writing .pyc filesENV PYTHONUNBUFFERED=1 \PYTHONDONTWRITEBYTECODE=1# Install pngquant, jpegoptim and zipRUN apt-get update && \apt-get install -y pngquant jpegoptim zip && \rm -rf /var/lib/apt/lists/*# Create a non-root user (Security Best Practice for HF Spaces)RUN useradd -m -u 1000 user# Set working directoryWORKDIR /app# Copy application files with correct ownershipCOPY --chown=user:user web_app.py .COPY --chown=user:user index.html .# Switch to the non-root userUSER user# Expose the port (Hugging Face Spaces defaults to 7860)EXPOSE 7860# Start the applicationCMD ["python", "web_app.py"]
221 unmodified lines222223224225226227228229230231232221 unmodified linesdef run():print(f"🐼 TinyPNG Local Server starting at port {PORT}")if not PNGQUANT_PATH:print("⚠️ Warning: pngquant not found. PNG compression will be disabled.")if not JPEGOPTIM_PATH:print("⚠️ Warning: jpegoptim not found. JPG compression will be disabled.")with socketserver.TCPServer(("0.0.0.0", PORT), TinyHandler) as httpd:try:httpd.serve_forever()221 unmodified lines222223224225226227228229230231232233234235236237238221 unmodified linesdef run():print(f"🐼 TinyPNG Local Server starting at port {PORT}")# Allow address reuse to prevent "Address already in use" errors on restartsocketserver.TCPServer.allow_reuse_address = Trueif not PNGQUANT_PATH:print("⚠️ Warning: pngquant not found. PNG compression will be disabled.")if not JPEGOPTIM_PATH:print("⚠️ Warning: jpegoptim not found. JPG compression will be disabled.")# Ensure logs are flushed immediatelysys.stdout.flush()with socketserver.TCPServer(("0.0.0.0", PORT), TinyHandler) as httpd:try:httpd.serve_forever()
Dockerfile+17-5
web_app.py+6