filipvk commited on
Commit
4bce2e7
·
0 Parent(s):

initial commit

Browse files
Files changed (8) hide show
  1. .dockerignore +4 -0
  2. .gitignore +8 -0
  3. Dockerfile +17 -0
  4. README.md +12 -0
  5. app/__init__.py +0 -0
  6. app/main.py +53 -0
  7. app/model.py +29 -0
  8. requirements.txt +0 -0
.dockerignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ venv
2
+ __pycache__
3
+ .git
4
+ outputs
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ models/
2
+ outputs/
3
+
4
+ .venv/
5
+ __pycache__/
6
+ .env
7
+
8
+ *.pyc
Dockerfile ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY app ./app
10
+
11
+ RUN mkdir -p outputs
12
+ RUN mkdir -p models
13
+
14
+ EXPOSE 7860
15
+
16
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
17
+
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Tiny SD FastAPI
3
+ sdk: docker
4
+ app_port: 7860
5
+ emoji: 🎨
6
+ colorFrom: blue
7
+ colorTo: green
8
+ ---
9
+
10
+ # Tiny SD FastAPI
11
+
12
+ FastAPI + Docker + Tiny Stable Diffusion deployment.
app/__init__.py ADDED
File without changes
app/main.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from fastapi.responses import FileResponse
3
+ from pathlib import Path
4
+ from datetime import datetime
5
+ import re
6
+
7
+ from app.model import generate_image
8
+
9
+ app = FastAPI(
10
+ title="Tiny SD API",
11
+ docs_url="/docs",
12
+ redoc_url="/redoc",
13
+ openapi_url="/openapi.json",
14
+ swagger_ui_parameters={
15
+ "tryItOutEnabled": True
16
+ }
17
+ )
18
+
19
+
20
+ @app.get("/")
21
+ def root():
22
+ return {
23
+ "message": "Tiny SD API is running"
24
+ }
25
+
26
+
27
+ @app.get("/generate")
28
+ def generate(prompt: str):
29
+ image = generate_image(prompt)
30
+
31
+ output_dir = Path("outputs")
32
+ output_dir.mkdir(exist_ok=True)
33
+
34
+ safe_prompt = re.sub(
35
+ r'[\\/*?:"<>|]',
36
+ "_",
37
+ prompt
38
+ )[:40]
39
+
40
+ filename = (
41
+ f"{safe_prompt}-"
42
+ f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
43
+ )
44
+
45
+ path = output_dir / filename
46
+
47
+ image.save(path)
48
+
49
+ return FileResponse(
50
+ path=path,
51
+ media_type="image/png",
52
+ filename=filename
53
+ )
app/model.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline
2
+ import torch
3
+
4
+ MODEL_NAME = "segmind/tiny-sd"
5
+
6
+ pipe = StableDiffusionPipeline.from_pretrained(
7
+ MODEL_NAME,
8
+ cache_dir="./models",
9
+ # local_files_only=True,
10
+ torch_dtype=torch.float32,
11
+ safety_checker=None
12
+ )
13
+
14
+ pipe.enable_attention_slicing()
15
+
16
+ pipe.to("cpu")
17
+
18
+
19
+ @torch.inference_mode()
20
+ def generate_image(prompt):
21
+
22
+ image = pipe(
23
+ prompt,
24
+ height=256,
25
+ width=256,
26
+ num_inference_steps=10
27
+ ).images[0]
28
+
29
+ return image
requirements.txt ADDED
Binary file (256 Bytes). View file