change flask
Browse files
app.py
CHANGED
|
@@ -8,6 +8,38 @@ from models.interface_types import InterfaceType
|
|
| 8 |
from constants import DEVICE
|
| 9 |
from state import get_settings
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
parser = ArgumentParser(description=f"FAST SD CPU {constants.APP_VERSION}")
|
| 12 |
parser.add_argument(
|
| 13 |
"-s",
|
|
@@ -161,3 +193,92 @@ print("Starting web UI mode")
|
|
| 161 |
start_webui(
|
| 162 |
args.share,
|
| 163 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from constants import DEVICE
|
| 9 |
from state import get_settings
|
| 10 |
|
| 11 |
+
|
| 12 |
+
from fastapi import FastAPI,Body
|
| 13 |
+
|
| 14 |
+
import uvicorn
|
| 15 |
+
import json
|
| 16 |
+
import logging
|
| 17 |
+
from PIL import Image
|
| 18 |
+
import time
|
| 19 |
+
from constants import DESCRIPTION, LOGO
|
| 20 |
+
from model import get_pipeline
|
| 21 |
+
from utils import replace_background
|
| 22 |
+
from diffusers.utils import load_image
|
| 23 |
+
import base64
|
| 24 |
+
import io
|
| 25 |
+
from datetime import datetime
|
| 26 |
+
|
| 27 |
+
app = FastAPI(name="mutilParam")
|
| 28 |
+
|
| 29 |
+
from typing import Any
|
| 30 |
+
from backend.models.lcmdiffusion_setting import DiffusionTask
|
| 31 |
+
|
| 32 |
+
from frontend.utils import is_reshape_required
|
| 33 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 34 |
+
|
| 35 |
+
app_settings = get_settings()
|
| 36 |
+
|
| 37 |
+
context = Context(InterfaceType.WEBUI)
|
| 38 |
+
previous_width = 0
|
| 39 |
+
previous_height = 0
|
| 40 |
+
previous_model_id = ""
|
| 41 |
+
previous_num_of_images = 0
|
| 42 |
+
|
| 43 |
parser = ArgumentParser(description=f"FAST SD CPU {constants.APP_VERSION}")
|
| 44 |
parser.add_argument(
|
| 45 |
"-s",
|
|
|
|
| 193 |
start_webui(
|
| 194 |
args.share,
|
| 195 |
)
|
| 196 |
+
|
| 197 |
+
app.get("/")
|
| 198 |
+
def root():
|
| 199 |
+
return {"API": "hello"}
|
| 200 |
+
|
| 201 |
+
@app.post("/img2img")
|
| 202 |
+
async def predict(prompt=Body(...),imgbase64data=Body(...),negative_prompt=Body(None),userId=Body(None)):
|
| 203 |
+
pipeline = get_pipeline()
|
| 204 |
+
MAX_QUEUE_SIZE = 4
|
| 205 |
+
start = time.time()
|
| 206 |
+
print("参数",imgbase64data,prompt)
|
| 207 |
+
image_data = base64.b64decode(imgbase64data)
|
| 208 |
+
image1 = Image.open(io.BytesIO(image_data))
|
| 209 |
+
w, h = image1.size
|
| 210 |
+
newW = 512
|
| 211 |
+
newH = int(h * newW / w)
|
| 212 |
+
img = image1.resize((newW, newH))
|
| 213 |
+
end1 = time.time()
|
| 214 |
+
now = datetime.now()
|
| 215 |
+
print(now)
|
| 216 |
+
print("图像:", img.size)
|
| 217 |
+
print("加载管道:", end1 - start)
|
| 218 |
+
global previous_height, previous_width, previous_model_id, previous_num_of_images, app_settings
|
| 219 |
+
|
| 220 |
+
app_settings.settings.lcm_diffusion_setting.prompt = prompt
|
| 221 |
+
app_settings.settings.lcm_diffusion_setting.negative_prompt = negative_prompt
|
| 222 |
+
app_settings.settings.lcm_diffusion_setting.init_image = img
|
| 223 |
+
app_settings.settings.lcm_diffusion_setting.strength = 0.6
|
| 224 |
+
|
| 225 |
+
app_settings.settings.lcm_diffusion_setting.diffusion_task = (
|
| 226 |
+
DiffusionTask.image_to_image.value
|
| 227 |
+
)
|
| 228 |
+
model_id = app_settings.settings.lcm_diffusion_setting.openvino_lcm_model_id
|
| 229 |
+
reshape = False
|
| 230 |
+
app_settings.settings.lcm_diffusion_setting.image_height=newH
|
| 231 |
+
image_width = app_settings.settings.lcm_diffusion_setting.image_width
|
| 232 |
+
image_height = app_settings.settings.lcm_diffusion_setting.image_height
|
| 233 |
+
num_images = app_settings.settings.lcm_diffusion_setting.number_of_images
|
| 234 |
+
reshape = is_reshape_required(
|
| 235 |
+
previous_width,
|
| 236 |
+
image_width,
|
| 237 |
+
previous_height,
|
| 238 |
+
image_height,
|
| 239 |
+
previous_model_id,
|
| 240 |
+
model_id,
|
| 241 |
+
previous_num_of_images,
|
| 242 |
+
num_images,
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
| 247 |
+
future = executor.submit(
|
| 248 |
+
context.generate_text_to_image,
|
| 249 |
+
app_settings.settings,
|
| 250 |
+
reshape,
|
| 251 |
+
DEVICE,
|
| 252 |
+
)
|
| 253 |
+
images = future.result()
|
| 254 |
+
# images = context.generate_text_to_image(
|
| 255 |
+
# app_settings.settings,
|
| 256 |
+
# reshape,
|
| 257 |
+
# DEVICE,
|
| 258 |
+
# )
|
| 259 |
+
previous_width = image_width
|
| 260 |
+
previous_height = image_height
|
| 261 |
+
previous_model_id = model_id
|
| 262 |
+
previous_num_of_images = num_images
|
| 263 |
+
output_image = images[0]
|
| 264 |
+
end2 = time.time()
|
| 265 |
+
print("测试",output_image)
|
| 266 |
+
print("s生成完成:", end2 - end1)
|
| 267 |
+
# 将图片对象转换为bytes
|
| 268 |
+
image_data = io.BytesIO()
|
| 269 |
+
|
| 270 |
+
# 将图像保存到BytesIO对象中,格式为JPEG
|
| 271 |
+
output_image.save(image_data, format='JPEG')
|
| 272 |
+
|
| 273 |
+
# 将BytesIO对象的内容转换为字节串
|
| 274 |
+
image_data_bytes = image_data.getvalue()
|
| 275 |
+
output_image_base64 = base64.b64encode(image_data_bytes).decode('utf-8')
|
| 276 |
+
print("完成的图片:", output_image_base64)
|
| 277 |
+
logger = logging.getLogger('')
|
| 278 |
+
logger.info(output_image_base64)
|
| 279 |
+
return output_image_base64
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
@app.post("/predict")
|
| 283 |
+
async def predict(prompt=Body(...)):
|
| 284 |
+
return f"您好,{prompt}"
|