-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdeploy_server.py
More file actions
144 lines (118 loc) · 4.53 KB
/
Copy pathdeploy_server.py
File metadata and controls
144 lines (118 loc) · 4.53 KB
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
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Copyright 2025 IDDM Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
@Date : 2024/11/4 23:04
@Author : chairc
@Site : https://github.com/chairc
"""
import os
import sys
import json
import uuid
import uvicorn
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from torchvision import transforms
sys.path.append(os.path.dirname(sys.path[0]))
from iddm.config.version import get_version_banner
from iddm.tools.generate import Generator, init_generate_args
from iddm.utils import save_images
from iddm.utils.processing import image_to_base64
from iddm.utils.logger import get_logger
logger = get_logger(name=__name__)
app = FastAPI()
@app.get("/")
def index():
logger.info(msg="Route -> Hello IDDM")
return "Hello, IDDM!"
@app.post("/api/generate/df")
def generate_diffusion_model_api(data: dict):
"""
Generate a diffusion model
"""
logger.info(msg="Route -> /api/df")
logger.info(msg=f"Send json: {data}")
# Latent
latent = data.get("latent", False)
# Sample type
sample = data.get("sample", "ddpm")
# Image size
image_size = 512 if latent else data.get("image_size", 64)
# Number of images
num_images = data.get("num_images") if data.get("num_images", 1) > 1 else 1
# Use ema
use_ema = data.get("use_ema", False)
# Weight path
weight_path = data.get("weight_path", None)
result_path = data.get("result_path", "./results")
# Autoencoder weight path
autoencoder_ckpt = data.get("autoencoder_ckpt", None)
# Recommend use base64 in server app
# Return mode, base64 or url
re_type = data.get("type", None)
logger.info(msg="[Web]: Start generation.")
# Type is url or base64
re_json = {"image": [], "type": str(re_type)}
if any(param is None for param in [sample, image_size, num_images, weight_path, result_path, re_type]):
return JSONResponse({"code": 400, "msg": "Illegal parameters.", "data": None})
# Init args
args = init_generate_args()
args.sample = sample
args.image_size = image_size
args.use_ema = use_ema
args.weight_path = weight_path
args.result_path = result_path
args.latent = latent
args.autoencoder_ckpt = autoencoder_ckpt
# Only generate 1 image per
args.num_images = 1
try:
# Init server model
server_model = Generator(gen_args=args, deploy=True)
logger.info(msg=f"[Web]: A total of {num_images} images are generated.")
# Generate images by diffusion models
for i in range(num_images):
logger.info(msg=f"[Web]: Current generate {i + 1} of {num_images}.")
# Generation name
generate_name = uuid.uuid1()
# Generate image
x = server_model.generate(index=i)
# Select mode
# Recommend use base64
if re_type == "base64":
x = transforms.ToPILImage()(x[0])
re_x = image_to_base64(image=x)
else:
# Save images
re_x = os.path.join(result_path, f"{generate_name}.png")
save_images(images=x, path=re_x)
# Append return json
image_json = {"image_id": str(generate_name), "type": re_type,
"image": str(re_x)}
re_json["image"].append(image_json)
logger.info(msg="[Web]: Finish generation.")
return JSONResponse({"code": 200, "msg": "success!", "data": json.dumps(re_json, ensure_ascii=False)})
except Exception as e:
logger.exception("Exception occurred while generating diffusion model")
return JSONResponse({"code": 500, "msg": "An internal error has occurred.", "data": None})
@app.post("/api/generate/sr")
def generate_super_resolution_model_api():
logger.info(msg="Route -> /api/sr")
# TODO: super resolution api
return "SR!"
if __name__ == "__main__":
host = "127.0.0.1"
port = 12341
logger.info(msg=f"Run -> {host}:{port}")
get_version_banner()
uvicorn.run(app=app, host=host, port=port)