Abdurrehman015 commited on
Commit
09131f8
·
1 Parent(s): 067fda8

first commit

Browse files
Files changed (3) hide show
  1. Dockerfile +34 -0
  2. main.py +70 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as the base image
2
+ FROM python:3.10-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Set environment variables
8
+ ENV PYTHONDONTWRITEBYTECODE=1
9
+ ENV PYTHONUNBUFFERED=1
10
+
11
+ # Install system dependencies
12
+ RUN apt-get update && apt-get install -y \
13
+ git \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+ # Install Python dependencies
17
+ COPY requirements.txt .
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
+
20
+ # Copy the application code
21
+ COPY main.py .
22
+
23
+ # Install Hugging Face token as an environment variable (to be set at runtime)
24
+ # This will be provided via Docker run command or Hugging Face Secrets
25
+ ARG HF_TOKEN
26
+ ENV HF_TOKEN=${HF_TOKEN}
27
+
28
+ # Run the application
29
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "10000"]
30
+
31
+ # Optional: Add GPU support (uncomment and adjust if using NVIDIA GPU)
32
+ # FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime
33
+ # RUN pip install --no-cache-dir -r requirements.txt
34
+ # CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "10000"]
main.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import StreamingResponse
3
+ from pydantic import BaseModel
4
+ from diffusers import StableDiffusionPipeline
5
+ from huggingface_hub import login
6
+ from PIL import Image
7
+ import torch
8
+ import io
9
+ import os
10
+
11
+ # Get token from environment (set in Hugging Face Spaces Secrets)
12
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
13
+ if not HF_TOKEN:
14
+ raise ValueError("HF_TOKEN environment variable not set")
15
+
16
+ # Login to Hugging Face
17
+ login(token=HF_TOKEN)
18
+
19
+ # Initialize FastAPI app
20
+ app = FastAPI()
21
+
22
+ # Dynamically select device (CPU or GPU)
23
+ device = "cuda" if torch.cuda.is_available() else "cpu"
24
+ print(f"Using device: {device}")
25
+
26
+ # Load Stable Diffusion pipeline with dynamic device and mixed precision
27
+ pipe = StableDiffusionPipeline.from_pretrained(
28
+ "CompVis/stable-diffusion-v1-4",
29
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
30
+ use_safetensors=True, # Use safetensors for efficiency
31
+ ).to(device)
32
+
33
+ # Enable mixed precision training if GPU is available
34
+ if device == "cuda":
35
+ pipe.enable_attention_slicing() # Optimize memory usage
36
+ pipe.enable_xformers_memory_efficient_attention() # Optional, if xformers is installed
37
+
38
+ # Input schema
39
+ class PromptRequest(BaseModel):
40
+ prompt: str
41
+ num_inference_steps: int = 20
42
+ guidance_scale: float = 7.5
43
+
44
+ # Image generation endpoint
45
+ @app.post("/generate-image/")
46
+ def generate_image(req: PromptRequest):
47
+ try:
48
+ # Generate image
49
+ image = pipe(
50
+ prompt=req.prompt,
51
+ num_inference_steps=req.num_inference_steps,
52
+ guidance_scale=req.guidance_scale
53
+ ).images[0]
54
+
55
+ # Convert to bytes
56
+ img_bytes = io.BytesIO()
57
+ image.save(img_bytes, format="PNG")
58
+ img_bytes.seek(0)
59
+
60
+ return StreamingResponse(img_bytes, media_type="image/png")
61
+ except Exception as e:
62
+ raise HTTPException(status_code=500, detail=f"Generation error: {str(e)}")
63
+
64
+ # Health check
65
+ @app.get("/health/")
66
+ def health_check():
67
+ return {"status": "healthy"}
68
+
69
+ # Note: uvicorn.run is handled by Hugging Face Spaces automatically
70
+ # No need to run the server manually here
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ diffusers
4
+ torch
5
+ transformers
6
+ pillow
7
+ huggingface_hub
8
+ xformers # Optional, for memory-efficient attention