Spaces:
Sleeping
Sleeping
File size: 11,597 Bytes
c92c0ec |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
import concurrent.futures
import random
import gradio as gr
import requests, os
import io, base64, json
import spaces
import torch
from PIL import Image
from openai import OpenAI
from .models import IMAGE_GENERATION_MODELS, VIDEO_GENERATION_MODELS, B2I_MODELS, load_pipeline
from serve.upload import get_random_mscoco_prompt, get_random_video_prompt, get_ssh_random_video_prompt, get_ssh_random_image_prompt
from serve.constants import SSH_CACHE_OPENSOURCE, SSH_CACHE_ADVANCE, SSH_CACHE_PIKA, SSH_CACHE_SORA, SSH_CACHE_IMAGE
class ModelManager:
def __init__(self):
self.model_ig_list = IMAGE_GENERATION_MODELS
self.model_ie_list = [] #IMAGE_EDITION_MODELS
self.model_vg_list = VIDEO_GENERATION_MODELS
self.model_b2i_list = B2I_MODELS
self.loaded_models = {}
def load_model_pipe(self, model_name):
if not model_name in self.loaded_models:
pipe = load_pipeline(model_name)
self.loaded_models[model_name] = pipe
else:
pipe = self.loaded_models[model_name]
return pipe
@spaces.GPU(duration=120)
def generate_image_ig(self, prompt, model_name):
pipe = self.load_model_pipe(model_name)
if 'Stable-cascade' not in model_name:
result = pipe(prompt=prompt).images[0]
else:
prior, decoder = pipe
prior.enable_model_cpu_offload()
prior_output = prior(
prompt=prompt,
height=512,
width=512,
negative_prompt='',
guidance_scale=4.0,
num_images_per_prompt=1,
num_inference_steps=20
)
decoder.enable_model_cpu_offload()
result = decoder(
image_embeddings=prior_output.image_embeddings.to(torch.float16),
prompt=prompt,
negative_prompt='',
guidance_scale=0.0,
output_type="pil",
num_inference_steps=10
).images[0]
return result
def generate_image_ig_api(self, prompt, model_name):
pipe = self.load_model_pipe(model_name)
result = pipe(prompt=prompt)
return result
def generate_image_ig_parallel_anony(self, prompt, model_A, model_B, model_C, model_D):
if model_A == "" and model_B == "" and model_C == "" and model_D == "":
from .matchmaker import matchmaker
not_run = [20,21,22, 25,26, 30] #12,13,14,15,16,17,18,19,20,21,22, #23,24,
model_ids = matchmaker(num_players=len(self.model_ig_list), not_run=not_run)
print(model_ids)
model_names = [self.model_ig_list[i] for i in model_ids]
print(model_names)
else:
model_names = [model_A, model_B, model_C, model_D]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(self.generate_image_ig, prompt, model) if model.startswith("huggingface")
else executor.submit(self.generate_image_ig_api, prompt, model) for model in model_names]
results = [future.result() for future in futures]
return results[0], results[1], results[2], results[3], \
model_names[0], model_names[1], model_names[2], model_names[3]
def generate_image_b2i(self, prompt, grounding_instruction, bbox, model_name):
pipe = self.load_model_pipe(model_name)
if model_name == "local_MIGC_b2i":
from model_bbox.MIGC.inference_single_image import inference_image
result = inference_image(pipe, prompt, grounding_instruction, bbox)
elif model_name == "huggingface_ReCo_b2i":
from model_bbox.ReCo.inference import inference_image
result = inference_image(pipe, prompt, grounding_instruction, bbox)
return result
def generate_image_b2i_parallel_anony(self, prompt, grounding_instruction, bbox, model_A, model_B, model_C, model_D):
if model_A == "" and model_B == "" and model_C == "" and model_D == "":
from .matchmaker import matchmaker
not_run = [] #12,13,14,15,16,17,18,19,20,21,22, #23,24,
# model_ids = matchmaker(num_players=len(self.model_ig_list), not_run=not_run)
model_ids = [0, 1]
print(model_ids)
model_names = [self.model_b2i_list[i] for i in model_ids]
print(model_names)
else:
model_names = [model_A, model_B, model_C, model_D]
from concurrent.futures import ProcessPoolExecutor
with ProcessPoolExecutor() as executor:
futures = [executor.submit(self.generate_image_b2i, prompt, grounding_instruction, bbox, model)
for model in model_names]
results = [future.result() for future in futures]
# with concurrent.futures.ThreadPoolExecutor() as executor:
# futures = [executor.submit(self.generate_image_b2i, prompt, grounding_instruction, bbox, model) for model in model_names]
# results = [future.result() for future in futures]
blank_image = None
final_results = []
for i in range(4):
if i < len(model_ids):
# 如果是有效模型,返回相应的生成结果
final_results.append(results[i])
else:
# 如果没有生成结果,则返回空白图像
final_results.append(blank_image)
final_model_names = []
for i in range(4):
if i < len(model_ids):
final_model_names.append(model_names[i])
else:
final_model_names.append("")
return final_results[0], final_results[1], final_results[2], final_results[3], \
final_model_names[0], final_model_names[1], final_model_names[2], final_model_names[3]
def generate_image_ig_cache_anony(self, model_A, model_B, model_C, model_D):
if model_A == "" and model_B == "" and model_C == "" and model_D == "":
from .matchmaker import matchmaker
not_run = [20,21,22]
model_ids = matchmaker(num_players=len(self.model_ig_list), not_run=not_run)
print(model_ids)
model_names = [self.model_ig_list[i] for i in model_ids]
print(model_names)
else:
model_names = [model_A, model_B, model_C, model_D]
root_dir = SSH_CACHE_IMAGE
local_dir = "./cache_image"
if not os.path.exists(local_dir):
os.makedirs(local_dir)
prompt, results = get_ssh_random_image_prompt(root_dir, local_dir, model_names)
return results[0], results[1], results[2], results[3], \
model_names[0], model_names[1], model_names[2], model_names[3], prompt
def generate_video_vg_parallel_anony(self, model_A, model_B, model_C, model_D):
if model_A == "" and model_B == "" and model_C == "" and model_D == "":
# model_names = random.sample([model for model in self.model_vg_list], 4)
from .matchmaker_video import matchmaker_video
model_ids = matchmaker_video(num_players=len(self.model_vg_list))
print(model_ids)
model_names = [self.model_vg_list[i] for i in model_ids]
print(model_names)
else:
model_names = [model_A, model_B, model_C, model_D]
root_dir = SSH_CACHE_OPENSOURCE
for name in model_names:
if "Runway-Gen3" in name or "Runway-Gen2" in name or "Pika-v1.0" in name:
root_dir = SSH_CACHE_ADVANCE
elif "Pika-beta" in name:
root_dir = SSH_CACHE_PIKA
elif "Sora" in name and "OpenSora" not in name:
root_dir = SSH_CACHE_SORA
local_dir = "./cache_video"
if not os.path.exists(local_dir):
os.makedirs(local_dir)
prompt, results = get_ssh_random_video_prompt(root_dir, local_dir, model_names)
cache_dir = local_dir
return results[0], results[1], results[2], results[3], \
model_names[0], model_names[1], model_names[2], model_names[3], prompt, cache_dir
def generate_image_ig_museum_parallel_anony(self, model_A, model_B, model_C, model_D):
if model_A == "" and model_B == "" and model_C == "" and model_D == "":
# model_names = random.sample([model for model in self.model_ig_list], 4)
from .matchmaker import matchmaker
model_ids = matchmaker(num_players=len(self.model_ig_list))
print(model_ids)
model_names = [self.model_ig_list[i] for i in model_ids]
print(model_names)
else:
model_names = [model_A, model_B, model_C, model_D]
prompt = get_random_mscoco_prompt()
print(prompt)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(self.generate_image_ig, prompt, model) if model.startswith("huggingface")
else executor.submit(self.generate_image_ig_api, prompt, model) for model in model_names]
results = [future.result() for future in futures]
return results[0], results[1], results[2], results[3], \
model_names[0], model_names[1], model_names[2], model_names[3], prompt
def generate_image_ig_parallel(self, prompt, model_A, model_B):
model_names = [model_A, model_B]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(self.generate_image_ig, prompt, model) if model.startswith("imagenhub")
else executor.submit(self.generate_image_ig_api, prompt, model) for model in model_names]
results = [future.result() for future in futures]
return results[0], results[1]
@spaces.GPU(duration=200)
def generate_image_ie(self, textbox_source, textbox_target, textbox_instruct, source_image, model_name):
pipe = self.load_model_pipe(model_name)
result = pipe(src_image = source_image, src_prompt = textbox_source, target_prompt = textbox_target, instruct_prompt = textbox_instruct)
return result
def generate_image_ie_parallel(self, textbox_source, textbox_target, textbox_instruct, source_image, model_A, model_B):
model_names = [model_A, model_B]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [
executor.submit(self.generate_image_ie, textbox_source, textbox_target, textbox_instruct, source_image,
model) for model in model_names]
results = [future.result() for future in futures]
return results[0], results[1]
def generate_image_ie_parallel_anony(self, textbox_source, textbox_target, textbox_instruct, source_image, model_A, model_B):
if model_A == "" and model_B == "":
model_names = random.sample([model for model in self.model_ie_list], 2)
else:
model_names = [model_A, model_B]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(self.generate_image_ie, textbox_source, textbox_target, textbox_instruct, source_image, model) for model in model_names]
results = [future.result() for future in futures]
return results[0], results[1], model_names[0], model_names[1]
|