-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
49 lines (38 loc) · 1.26 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from flask import Flask, request, send_file
from torch import autocast, cuda
import torch, os
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
app = Flask(__name__)
access_token = "enter access token"
# this will substitute the default PNDM scheduler for K-LMS
lms = LMSDiscreteScheduler(
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear"
)
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
scheduler=lms,
torch_dtype=torch.float16,
use_auth_token=access_token
)
if cuda.is_available():
print("using cuda")
pipe = pipe.to("cuda")
@app.route("/stable_diffusion")
def get_result():
prompt = request.args.get("prompt")
prompt_path = os.path.join(root_dir(), "data",prompt.replace(" ", "_") + ".png")
print(prompt, prompt_path)
if cuda.is_available():
with autocast("cuda"):
image = pipe(prompt)["sample"][0]
else:
image = pipe(prompt)["sample"][0]
image.save(prompt_path)
torch.cuda.empty_cache()
return send_file(prompt_path, mimetype='image/png')
def root_dir():
return os.path.abspath(os.path.dirname(__file__))
if __name__=="__main__":
app.run(host="127.0.0.1", port="5555",debug = True)