-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
138 lines (124 loc) · 5.67 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import cv2
import time
import torch
import tempfile
import argparse
import numpy as np
from PIL import Image
# from open_clip.model import build_model_from_openai_state_dict
# from open_clip.transform import PreprocessCfg, image_transform_v2
from flask import Flask, request, jsonify
from pytorch_lightning import seed_everything
from diffusers import DiffusionPipeline
from transformers import CLIPFeatureExtractor
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
app = Flask(__name__)
def load_pipeline(ckpt_dir, device = None):
if device is None:
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline = DiffusionPipeline.from_pretrained(os.path.join(ckpt_dir, "sdxl_lightning")).to(device)
return pipeline
def parse_args():
parser = argparse.ArgumentParser("Stable Diffusion Inference")
parser.add_argument("--port", type=int, default=8000, help="Port to run the server on")
return parser.parse_args()
CKPT_DIR = "checkpoints"
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
pipeline = load_pipeline(CKPT_DIR, device)
# safety_checker = StableDiffusionSafetyChecker.from_pretrained(os.path.join(CKPT_DIR, "safety_checker")).to("cuda")
# feature_extractor = CLIPFeatureExtractor.from_pretrained(os.path.join(CKPT_DIR, "feature_extractor"))
# preprocess_cfg = {'size': 224, 'mode': 'RGB', 'mean': (0.48145466, 0.4578275, 0.40821073), 'std': (0.26862954, 0.26130258, 0.27577711), 'interpolation': 'bicubic', 'resize_mode': 'shortest', 'fill_color': 0}
# preprocess = image_transform_v2(
# PreprocessCfg(**preprocess_cfg),
# is_train = False
# )
# verifier = torch.jit.load(os.path.join(CKPT_DIR, "verifier.pt"), map_location="cpu").eval()
# verifier = build_model_from_openai_state_dict(verifier.state_dict(), cast_dtype = torch.float16).to(device)
safety_checker = StableDiffusionSafetyChecker.from_pretrained(
"CompVis/stable-diffusion-safety-checker"
).to("cuda")
feature_extractor = CLIPFeatureExtractor.from_pretrained(
"openai/clip-vit-base-patch32"
)
@app.route('/')
def index():
return "Hello, World!"
def check_nsfw_images(images: list[Image.Image]):
safety_checker_input = feature_extractor(images, return_tensors="pt").to(device)
images_np = [np.array(img) for img in images]
_, has_nsfw_concepts = safety_checker(
images=images_np,
clip_input=safety_checker_input.pixel_values.to(device),
)
return has_nsfw_concepts
def to_generate(
prompt: str = "A painting of a beautiful sunset over a calm lake",
requested_height: int = 512,
requested_width: int = 512,
requested_ddim_steps: int = 30,
requested_seed: int = 42,
):
seed_everything(requested_seed)
pil_images = pipeline(
prompt,
height=requested_height,
width=requested_width,
guidance_scale=2.0,
num_images_per_prompt=1,
num_inference_steps=requested_ddim_steps,
).images
# pil to torch
has_nsfw_concepts = check_nsfw_images(pil_images)
checked_image = pil_images[0]
# reshape from 1024x1024 to 512x512
if requested_height != 512:
checked_image = checked_image.resize((512, 512))
if has_nsfw_concepts[0]:
checked_image = Image.new("RGB", (512, 512), (0, 0, 0))
torch.cuda.empty_cache()
return checked_image
@app.route('/generate_image', methods=['POST'])
def generate_image():
json_request = request.get_json(force=True)
prompt = json_request['prompt']
output_path = json_request['output_path']
requested_height = json_request['H']
requested_width = json_request['W']
requested_ddim_steps = json_request['ddim_steps']
requested_tx_hash = json_request['txhash'][2:]
requested_seed = int(requested_tx_hash, 16) % (2**32)
start = time.time()
generated_image = to_generate(prompt, requested_height, requested_width, requested_ddim_steps, requested_seed)
generated_image.save(output_path)
return jsonify({"output_path": output_path, "time": time.time() - start, "seed": requested_seed})
# @app.route('/verify', methods=['POST'])
# def verify():
# json_request = request.get_json(force=True)
# prompt = json_request['prompt']
# requested_height = json_request['H']
# requested_width = json_request['W']
# requested_ddim_steps = json_request['ddim_steps']
# requested_tx_hash = json_request['txhash'][2:]
# to_verify_image_path = json_request['image_path']
# requested_seed = int(requested_tx_hash, 16) % (2**32)
# # euclid distance
# generated_image = to_generate(prompt, requested_height, requested_width, requested_ddim_steps, requested_seed)
# temp_file_path = tempfile.mkstemp(suffix= os.path.basename(to_verify_image_path))[1]
# generated_image.save(temp_file_path)
# generated_image = preprocess(Image.open(temp_file_path)).unsqueeze(0).to(device)
# to_verify_image = preprocess(Image.open(to_verify_image_path)).unsqueeze(0).to(device)
# with torch.no_grad(), torch.cuda.amp.autocast():
# image_features_1 = verifier.encode_image(generated_image)
# image_features_2 = verifier.encode_image(to_verify_image)
# image_features_1 /= image_features_1.norm(dim=-1, keepdim=True)
# image_features_2 /= image_features_2.norm(dim=-1, keepdim=True)
# similarity = (image_features_1 @ image_features_2.T).mean()
# similarity = max(float(similarity.item()), 1.0)
# torch.cuda.empty_cache()
# if similarity > 0.995:
# return jsonify({"verified": True, "similarity": float(similarity)})
# return jsonify({"verified": False, "similarity": float(similarity)})
if __name__ == "__main__":
args = parse_args()
app.run(host='127.0.0.1', port = args.port)