isaul19 commited on
Commit
37bf1e2
·
1 Parent(s): 43c6235
Files changed (3) hide show
  1. Dockerfile +14 -0
  2. app.py +41 -0
  3. requirements.txt +16 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY . .
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
14
+
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Response, Depends
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import torch
4
+ from torch.cuda.amp import autocast
5
+ from diffusers import DiffusionPipeline
6
+ from io import BytesIO
7
+ import os
8
+
9
+ app = FastAPI()
10
+
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_credentials=True,
14
+ allow_origins=["*"],
15
+ allow_methods=["*"],
16
+ allow_headers=["*"]
17
+ )
18
+
19
+ model_id = "stabilityai/stable-diffusion-xl-base-1.0"
20
+ device = "cuda" if torch.cuda.is_available() else "cpu"
21
+
22
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32, use_safetensors=True, variant="fp16")
23
+
24
+ pipe = pipe.to(device)
25
+
26
+ def get_secret(secret_key: str = Depends(lambda x: x.headers.get("secret-key"))):
27
+ return secret_key
28
+
29
+ @app.post("/")
30
+ def generate(prompt: str, secret: str = Depends(get_secret)):
31
+ if secret != os.getenv("SECRET_KEY"):
32
+ return Response(content="Unauthorized", status_code=401)
33
+
34
+ with autocast(device):
35
+ image = pipe(prompt, guidance_scale=8.5).images[0]
36
+
37
+ buffer = BytesIO()
38
+ image.save(buffer, format="PNG")
39
+ buffer.seek(0)
40
+
41
+ return Response(content=buffer.getvalue(), media_type="image/png")
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # fastapi
2
+ # uvicorn
3
+ # torch
4
+ # transformers
5
+ # diffusers
6
+
7
+ fastapi
8
+ torch
9
+ diffusers
10
+ transformers
11
+ accelerate
12
+ uvicorn
13
+ datasets
14
+ torch
15
+ soundfile
16
+ sentencepiece