Upload 2 files
Browse files- api.py +35 -0
- auth_token.py +1 -0
api.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from auth_token import auth_token
|
2 |
+
from fastapi import FastAPI, Response
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
import torch
|
5 |
+
from torch import autocast
|
6 |
+
from diffusers import StableDiffusionPipeline
|
7 |
+
from io import BytesIO
|
8 |
+
import base64
|
9 |
+
#import transformers
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_credentials=True,
|
15 |
+
allow_origins=["*"],
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"]
|
18 |
+
)
|
19 |
+
|
20 |
+
device = "cuda"
|
21 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
22 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token)
|
23 |
+
pipe.to(device)
|
24 |
+
|
25 |
+
@app.get("/")
|
26 |
+
def generate(prompt: str):
|
27 |
+
with autocast(device):
|
28 |
+
image = pipe(prompt, guidance_scale=8.5).images[0]
|
29 |
+
|
30 |
+
image.save("testimage.png")
|
31 |
+
buffer = BytesIO()
|
32 |
+
image.save(buffer, format="PNG")
|
33 |
+
imgstr = base64.b64encode(buffer.getvalue())
|
34 |
+
|
35 |
+
return Response(content=imgstr, media_type="image/png")
|
auth_token.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
auth_token = "YOUR AUTH TOKEN HERE"
|