Jethro85
		
	commited on
		
		
					Commit 
							
							·
						
						1ebdc66
	
1
								Parent(s):
							
							38ddd3f
								
HF Space: Dockerized Flask app
Browse files- Dockerfile +30 -0
 - app/__init__.py +8 -2
 - run.py +4 -2
 - space.yml +7 -0
 
    	
        Dockerfile
    ADDED
    
    | 
         @@ -0,0 +1,30 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            # ---- Base image ----
         
     | 
| 2 | 
         
            +
            FROM python:3.11-slim
         
     | 
| 3 | 
         
            +
             
     | 
| 4 | 
         
            +
            # ---- System setup (add OS libs your deps may need) ----
         
     | 
| 5 | 
         
            +
            # Add or remove packages as needed. Keep it small to speed builds.
         
     | 
| 6 | 
         
            +
            RUN apt-get update && apt-get install -y --no-install-recommends \
         
     | 
| 7 | 
         
            +
                build-essential \
         
     | 
| 8 | 
         
            +
                && rm -rf /var/lib/apt/lists/*
         
     | 
| 9 | 
         
            +
             
     | 
| 10 | 
         
            +
            # ---- App files ----
         
     | 
| 11 | 
         
            +
            WORKDIR /app
         
     | 
| 12 | 
         
            +
            # Copy only requirements first to leverage Docker layer caching
         
     | 
| 13 | 
         
            +
            COPY requirements.txt /app/requirements.txt
         
     | 
| 14 | 
         
            +
            RUN pip install --no-cache-dir -r requirements.txt
         
     | 
| 15 | 
         
            +
             
     | 
| 16 | 
         
            +
            # Now copy the rest of your code
         
     | 
| 17 | 
         
            +
            COPY . /app
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
            # ---- Runtime env ----
         
     | 
| 20 | 
         
            +
            # HF Spaces provides a PORT env var; default to 7860 for local runs
         
     | 
| 21 | 
         
            +
            ENV PORT=7860
         
     | 
| 22 | 
         
            +
            ENV PYTHONUNBUFFERED=1
         
     | 
| 23 | 
         
            +
             
     | 
| 24 | 
         
            +
            # Make sure Flask binds publicly; gunicorn will use $PORT
         
     | 
| 25 | 
         
            +
            EXPOSE 7860
         
     | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
            +
            # ---- Start command ----
         
     | 
| 28 | 
         
            +
            # Gunicorn will import your Flask app named "app" from run.py
         
     | 
| 29 | 
         
            +
            # If your app variable has a different name, change run:app accordingly.
         
     | 
| 30 | 
         
            +
            CMD ["gunicorn", "run:app", "--workers", "2", "--threads", "8", "--timeout", "120", "--bind", "0.0.0.0:${PORT}"]
         
     | 
    	
        app/__init__.py
    CHANGED
    
    | 
         @@ -9,7 +9,13 @@ def create_app(): 
     | 
|
| 9 | 
         
             
                # Configure CORS
         
     | 
| 10 | 
         
             
                CORS(app, resources={
         
     | 
| 11 | 
         
             
                    r"/*": {
         
     | 
| 12 | 
         
            -
                        "origins": [ 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 13 | 
         
             
                        "methods": ["GET", "POST", "OPTIONS"],
         
     | 
| 14 | 
         
             
                        "allow_headers": ["Content-Type"]
         
     | 
| 15 | 
         
             
                    }
         
     | 
| 
         @@ -27,4 +33,4 @@ def create_app(): 
     | 
|
| 27 | 
         
             
                from app.routes import main
         
     | 
| 28 | 
         
             
                app.register_blueprint(main)
         
     | 
| 29 | 
         | 
| 30 | 
         
            -
                return app 
     | 
| 
         | 
|
| 9 | 
         
             
                # Configure CORS
         
     | 
| 10 | 
         
             
                CORS(app, resources={
         
     | 
| 11 | 
         
             
                    r"/*": {
         
     | 
| 12 | 
         
            +
                        "origins": [
         
     | 
| 13 | 
         
            +
                            r"https://.*\.hf\.space",        # any HF Space subdomain
         
     | 
| 14 | 
         
            +
                            "https://<your-username>.github.io",
         
     | 
| 15 | 
         
            +
                            "https://www.<your-domain>.com",
         
     | 
| 16 | 
         
            +
                            "http://localhost:5000",
         
     | 
| 17 | 
         
            +
                            "http://127.0.0.1:5000".
         
     | 
| 18 | 
         
            +
                        ],
         
     | 
| 19 | 
         
             
                        "methods": ["GET", "POST", "OPTIONS"],
         
     | 
| 20 | 
         
             
                        "allow_headers": ["Content-Type"]
         
     | 
| 21 | 
         
             
                    }
         
     | 
| 
         | 
|
| 33 | 
         
             
                from app.routes import main
         
     | 
| 34 | 
         
             
                app.register_blueprint(main)
         
     | 
| 35 | 
         | 
| 36 | 
         
            +
                return app
         
     | 
    	
        run.py
    CHANGED
    
    | 
         @@ -9,7 +9,9 @@ if __name__ == '__main__': 
     | 
|
| 9 | 
         
             
                # Parse command line arguments
         
     | 
| 10 | 
         
             
                parser = argparse.ArgumentParser(description='Run DP-SGD Explorer')
         
     | 
| 11 | 
         
             
                parser.add_argument('--port', type=int, default=5000, help='Port to run the server on (default: 5000)')
         
     | 
| 12 | 
         
            -
                parser.add_argument('--host', type=str, default='127.0.0.1', help='Host to run the server on (default: 127.0.0.1)')
         
     | 
| 
         | 
|
| 
         | 
|
| 13 | 
         
             
                args = parser.parse_args()
         
     | 
| 14 | 
         | 
| 15 | 
         
             
                # Enable debug mode for development
         
     | 
| 
         @@ -20,4 +22,4 @@ if __name__ == '__main__': 
     | 
|
| 20 | 
         
             
                print(f"Starting server on http://{args.host}:{args.port}")
         
     | 
| 21 | 
         | 
| 22 | 
         
             
                # Run the application
         
     | 
| 23 | 
         
            -
                app.run(host=args.host, port=args.port, debug=True) 
     | 
| 
         | 
|
| 9 | 
         
             
                # Parse command line arguments
         
     | 
| 10 | 
         
             
                parser = argparse.ArgumentParser(description='Run DP-SGD Explorer')
         
     | 
| 11 | 
         
             
                parser.add_argument('--port', type=int, default=5000, help='Port to run the server on (default: 5000)')
         
     | 
| 12 | 
         
            +
                # parser.add_argument('--host', type=str, default='127.0.0.1', help='Host to run the server on (default: 127.0.0.1)')
         
     | 
| 13 | 
         
            +
                parser.add_argument("--host", type=str, default=os.getenv("HOST", "0.0.0.0"),
         
     | 
| 14 | 
         
            +
                                    help="Host to run the server on (default: 0.0.0.0)")
         
     | 
| 15 | 
         
             
                args = parser.parse_args()
         
     | 
| 16 | 
         | 
| 17 | 
         
             
                # Enable debug mode for development
         
     | 
| 
         | 
|
| 22 | 
         
             
                print(f"Starting server on http://{args.host}:{args.port}")
         
     | 
| 23 | 
         | 
| 24 | 
         
             
                # Run the application
         
     | 
| 25 | 
         
            +
                app.run(host=args.host, port=args.port, debug=True)
         
     | 
    	
        space.yml
    ADDED
    
    | 
         @@ -0,0 +1,7 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            title: DPSGDTool
         
     | 
| 2 | 
         
            +
            emoji: 🛡️
         
     | 
| 3 | 
         
            +
            colorFrom: blue
         
     | 
| 4 | 
         
            +
            colorTo: purple
         
     | 
| 5 | 
         
            +
            sdk: docker
         
     | 
| 6 | 
         
            +
            pinned: false
         
     | 
| 7 | 
         
            +
            license: mit
         
     |