-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
65 lines (55 loc) · 2.18 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request, send_file, render_template
from diffusers import (
StableDiffusionPipeline,
EulerDiscreteScheduler,
StableDiffusionImg2ImgPipeline,
)
import torch
from PIL import Image
from io import BytesIO
import requests
from flask_cors import CORS, cross_origin
app = Flask(__name__, template_folder="frontend", static_folder="frontend")
CORS(app, support_credentials=True)
@app.route("/")
def index():
return render_template("index.html")
@app.get("/health")
def health_check():
return "Healthy", 200
@app.post("/txt2img")
def text_to_img():
data = request.json
#model_id = "andite/anything-v4.0" # 默认的,高品质、高细节的动漫风格
#model_id = 'Envvi/Inkpunk-Diffusion' # 温克朋克风格,提示词 nvinkpunk
#model_id = 'nousr/robo-diffusion-2-base' # 看起来很酷的机器人,提示词 nousr robot
#model_id = 'prompthero/openjourney' # openjorney 风格,提示词 mdjrny-v4 style
#model_id = 'dreamlike-art/dreamlike-photoreal-2.0' #写实,真实风格,提示词 photo
model_id = "stabilityai/stable-diffusion-2"
output = "output_txt2img.png"
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(
model_id, scheduler=scheduler, torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
image = pipe(data["prompt"], guidance_scale=7.5, num_inference_steps=20,height=data["height"], width=data["width"]).images[0]
image.save(output)
return send_file(output), 200
@app.post("/img2img")
def img_to_img():
data = request.json
model_id = "runwayml/stable-diffusion-v1-5"
output = "output_img2img.png"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
model_id, torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
response = requests.get(data["url"])
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))
images = pipe(
prompt=data["prompt"], image=init_image, strength=0.75, guidance_scale=7.5
).images
images[0].save(output)
return send_file(output), 200
app.run(host='0.0.0.0', port=5000)