harshilAmbliya commited on
Commit
c769f36
·
1 Parent(s): 013ef9d

Initial FastAPI SDXL Turbo deployment

Browse files
Files changed (4) hide show
  1. .gitignore +76 -0
  2. Dockerfile +27 -0
  3. app.py +72 -0
  4. requirements.txt +14 -0
.gitignore ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -----------------------------
2
+ # Python
3
+ # -----------------------------
4
+ __pycache__/
5
+ *.py[cod]
6
+ *.pyo
7
+ *.pyd
8
+ *.egg-info/
9
+ .eggs/
10
+
11
+ # -----------------------------
12
+ # Virtual Environments
13
+ # -----------------------------
14
+ venv/
15
+ .venv/
16
+ env/
17
+ ENV/
18
+
19
+ # -----------------------------
20
+ # Environment Variables
21
+ # -----------------------------
22
+ .env
23
+ .env.local
24
+ .env.*.local
25
+
26
+ # -----------------------------
27
+ # OS / Editor
28
+ # -----------------------------
29
+ .DS_Store
30
+ Thumbs.db
31
+ .idea/
32
+ .vscode/
33
+
34
+ # -----------------------------
35
+ # Logs
36
+ # -----------------------------
37
+ *.log
38
+
39
+ # -----------------------------
40
+ # Output / Generated Files
41
+ # -----------------------------
42
+ outputs/
43
+ *.png
44
+ *.jpg
45
+ *.jpeg
46
+ *.webp
47
+
48
+ # -----------------------------
49
+ # Hugging Face Cache
50
+ # -----------------------------
51
+ .cache/
52
+ .huggingface/
53
+ hf_cache/
54
+
55
+ # -----------------------------
56
+ # PyTorch / Diffusers Cache
57
+ # -----------------------------
58
+ torch_cache/
59
+ model_cache/
60
+
61
+ # -----------------------------
62
+ # Docker
63
+ # -----------------------------
64
+ *.tar
65
+ docker-compose.override.yml
66
+
67
+ # -----------------------------
68
+ # Jupyter
69
+ # -----------------------------
70
+ .ipynb_checkpoints/
71
+
72
+ # -----------------------------
73
+ # Build / Distribution
74
+ # -----------------------------
75
+ build/
76
+ dist/
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Prevent Python from writing pyc files
4
+ ENV PYTHONDONTWRITEBYTECODE=1
5
+ ENV PYTHONUNBUFFERED=1
6
+
7
+ WORKDIR /app
8
+
9
+ # System deps (needed for PIL, torch, etc.)
10
+ RUN apt-get update && apt-get install -y \
11
+ git \
12
+ libgl1 \
13
+ libglib2.0-0 \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+ # Copy requirements first (better cache)
17
+ COPY requirements.txt .
18
+
19
+ RUN pip install --no-cache-dir --upgrade pip \
20
+ && pip install --no-cache-dir -r requirements.txt
21
+
22
+ # Copy application
23
+ COPY . .
24
+
25
+ EXPOSE 7860
26
+
27
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import FileResponse
3
+ from pydantic import BaseModel
4
+ from diffusers import AutoPipelineForText2Image
5
+ import torch
6
+ import uuid
7
+ import os
8
+
9
+ # --------------------------------------------------
10
+ # 1. Create FastAPI app
11
+ # --------------------------------------------------
12
+ app = FastAPI(title="Text to Image API (SDXL Turbo)")
13
+
14
+ # --------------------------------------------------
15
+ # 2. Device & dtype
16
+ # --------------------------------------------------
17
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
18
+ DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
19
+
20
+ MODEL_ID = "stabilityai/sdxl-turbo"
21
+
22
+ # --------------------------------------------------
23
+ # 3. Load model once
24
+ # --------------------------------------------------
25
+ pipe = AutoPipelineForText2Image.from_pretrained(
26
+ MODEL_ID,
27
+ torch_dtype=DTYPE,
28
+ variant="fp16" if DEVICE == "cuda" else None
29
+ )
30
+ pipe = pipe.to(DEVICE)
31
+
32
+ # --------------------------------------------------
33
+ # 4. Output directory
34
+ # --------------------------------------------------
35
+ OUTPUT_DIR = "outputs"
36
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
37
+
38
+ # --------------------------------------------------
39
+ # 5. Request schema
40
+ # --------------------------------------------------
41
+ class PromptRequest(BaseModel):
42
+ prompt: str
43
+
44
+ # --------------------------------------------------
45
+ # 6. Health check
46
+ # --------------------------------------------------
47
+ @app.get("/")
48
+ def root():
49
+ return {"status": "ok", "message": "SDXL Turbo API is running"}
50
+
51
+ # --------------------------------------------------
52
+ # 7. Image generation endpoint
53
+ # --------------------------------------------------
54
+ @app.post("/generate")
55
+ def generate_image(request: PromptRequest):
56
+ result = pipe(
57
+ prompt=request.prompt,
58
+ num_inference_steps=4,
59
+ guidance_scale=0.0
60
+ )
61
+
62
+ image = result.images[0]
63
+
64
+ filename = f"{uuid.uuid4()}.png"
65
+ filepath = os.path.join(OUTPUT_DIR, filename)
66
+ image.save(filepath)
67
+
68
+ return FileResponse(
69
+ filepath,
70
+ media_type="image/png",
71
+ filename="generated.png"
72
+ )
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+
4
+ torch
5
+ torchvision
6
+ torchaudio
7
+
8
+ diffusers
9
+ transformers
10
+ accelerate
11
+ safetensors
12
+ pillow
13
+
14
+ huggingface_hub