Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import spaces
|
|
5 |
import torch
|
6 |
import re
|
7 |
from diffusers import DiffusionPipeline
|
8 |
-
from compel import Compel, ReturnedEmbeddingsType
|
9 |
from PIL import Image, PngImagePlugin
|
10 |
import json
|
11 |
import io
|
@@ -34,14 +33,9 @@ def add_comma_after_pattern_ti(text):
|
|
34 |
modified_text = pattern.sub(lambda x: x.group() + ',', text)
|
35 |
return modified_text
|
36 |
|
37 |
-
def
|
38 |
-
"""
|
39 |
-
|
40 |
-
# 简单处理,添加逗号分隔
|
41 |
-
return add_comma_after_pattern_ti(prompt)
|
42 |
-
else:
|
43 |
-
# 如果需要更复杂的处理,可以在这里添加
|
44 |
-
return prompt
|
45 |
|
46 |
DESCRIPTION = "梦羽的模型生成器 - 快速生成 MiaomiaoHarem vPred Dogma 1.1 模型的图片"
|
47 |
|
@@ -53,7 +47,12 @@ MAX_IMAGE_SIZE = 2048
|
|
53 |
|
54 |
if torch.cuda.is_available():
|
55 |
dtype = torch.bfloat16
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
59 |
if randomize_seed:
|
@@ -68,42 +67,40 @@ def infer(
|
|
68 |
seed: int = 7,
|
69 |
width: int = 1024,
|
70 |
height: int = 1536,
|
71 |
-
guidance_scale: float =
|
72 |
-
num_inference_steps: int =
|
73 |
randomize_seed: bool = True,
|
74 |
-
use_resolution_binning: bool = True,
|
75 |
):
|
76 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
77 |
-
generator = torch.Generator().manual_seed(seed)
|
78 |
-
|
79 |
-
compel = Compel(
|
80 |
-
tokenizer=[pipe.tokenizer, pipe.tokenizer_2],
|
81 |
-
text_encoder=[pipe.text_encoder, pipe.text_encoder_2],
|
82 |
-
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
83 |
-
requires_pooled=[False, True],
|
84 |
-
truncate_long_prompts=False
|
85 |
-
)
|
86 |
-
# 在 infer 函数中调用 get_embed_new
|
87 |
if not use_negative_prompt:
|
88 |
negative_prompt = ""
|
89 |
-
|
90 |
original_prompt = prompt # Store original prompt for metadata
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
-
#
|
96 |
image = pipe(
|
97 |
-
|
98 |
-
|
99 |
-
negative_prompt_embeds=conditioning[1:2],
|
100 |
-
negative_pooled_prompt_embeds=pooled[1:2],
|
101 |
width=width,
|
102 |
height=height,
|
103 |
-
|
104 |
num_inference_steps=num_inference_steps,
|
105 |
generator=generator,
|
106 |
-
use_resolution_binning=use_resolution_binning,
|
107 |
).images[0]
|
108 |
|
109 |
# Create metadata dictionary
|
@@ -114,11 +111,10 @@ def infer(
|
|
114 |
"seed": seed,
|
115 |
"width": width,
|
116 |
"height": height,
|
117 |
-
"
|
118 |
"num_inference_steps": num_inference_steps,
|
119 |
"model": "qwen-image",
|
120 |
-
"
|
121 |
-
"PreUrl": "https://huggingface.co/spaces/Menyu/miaomiaoHaremDogma11"
|
122 |
}
|
123 |
# Add metadata to the image
|
124 |
image_with_metadata = add_metadata_to_image(image, metadata)
|
@@ -189,20 +185,19 @@ with gr.Blocks(css=css) as demo:
|
|
189 |
)
|
190 |
with gr.Row():
|
191 |
guidance_scale = gr.Slider(
|
192 |
-
label="
|
193 |
-
minimum=0
|
194 |
-
maximum=10,
|
195 |
step=0.1,
|
196 |
-
value=
|
197 |
)
|
198 |
num_inference_steps = gr.Slider(
|
199 |
label="生成步数",
|
200 |
minimum=1,
|
201 |
-
maximum=
|
202 |
step=1,
|
203 |
-
value=
|
204 |
)
|
205 |
-
use_resolution_binning = gr.Checkbox(label="使用分辨率分箱", value=True)
|
206 |
|
207 |
gr.Examples(
|
208 |
examples=examples,
|
@@ -230,7 +225,6 @@ with gr.Blocks(css=css) as demo:
|
|
230 |
guidance_scale,
|
231 |
num_inference_steps,
|
232 |
randomize_seed,
|
233 |
-
use_resolution_binning,
|
234 |
],
|
235 |
outputs=[result, seed],
|
236 |
)
|
|
|
5 |
import torch
|
6 |
import re
|
7 |
from diffusers import DiffusionPipeline
|
|
|
8 |
from PIL import Image, PngImagePlugin
|
9 |
import json
|
10 |
import io
|
|
|
33 |
modified_text = pattern.sub(lambda x: x.group() + ',', text)
|
34 |
return modified_text
|
35 |
|
36 |
+
def process_prompt(prompt):
|
37 |
+
"""简单的提示词处理函数"""
|
38 |
+
return add_comma_after_pattern_ti(prompt)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
DESCRIPTION = "梦羽的模型生成器 - 快速生成 MiaomiaoHarem vPred Dogma 1.1 模型的图片"
|
41 |
|
|
|
47 |
|
48 |
if torch.cuda.is_available():
|
49 |
dtype = torch.bfloat16
|
50 |
+
device = "cuda"
|
51 |
+
else:
|
52 |
+
dtype = torch.float32
|
53 |
+
device = "cpu"
|
54 |
+
|
55 |
+
pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype).to(device)
|
56 |
|
57 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
58 |
if randomize_seed:
|
|
|
67 |
seed: int = 7,
|
68 |
width: int = 1024,
|
69 |
height: int = 1536,
|
70 |
+
guidance_scale: float = 4.0,
|
71 |
+
num_inference_steps: int = 50,
|
72 |
randomize_seed: bool = True,
|
|
|
73 |
):
|
74 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
75 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
if not use_negative_prompt:
|
78 |
negative_prompt = ""
|
79 |
+
|
80 |
original_prompt = prompt # Store original prompt for metadata
|
81 |
+
|
82 |
+
# 处理提示词
|
83 |
+
prompt = process_prompt(prompt)
|
84 |
+
|
85 |
+
# 为 Qwen-Image 添加质量提升词条
|
86 |
+
positive_magic_en = "Ultra HD, 4K, cinematic composition."
|
87 |
+
positive_magic_zh = "超清,4K,电影级构图"
|
88 |
+
|
89 |
+
# 简单判断是否包含中文字符
|
90 |
+
if any('\u4e00' <= char <= '\u9fff' for char in prompt):
|
91 |
+
prompt = prompt + " " + positive_magic_zh
|
92 |
+
else:
|
93 |
+
prompt = prompt + " " + positive_magic_en
|
94 |
|
95 |
+
# 使用 Qwen-Image 的参数格式
|
96 |
image = pipe(
|
97 |
+
prompt=prompt,
|
98 |
+
negative_prompt=negative_prompt,
|
|
|
|
|
99 |
width=width,
|
100 |
height=height,
|
101 |
+
true_cfg_scale=guidance_scale,
|
102 |
num_inference_steps=num_inference_steps,
|
103 |
generator=generator,
|
|
|
104 |
).images[0]
|
105 |
|
106 |
# Create metadata dictionary
|
|
|
111 |
"seed": seed,
|
112 |
"width": width,
|
113 |
"height": height,
|
114 |
+
"true_cfg_scale": guidance_scale,
|
115 |
"num_inference_steps": num_inference_steps,
|
116 |
"model": "qwen-image",
|
117 |
+
"PreUrl": "https://huggingface.co/Qwen/Qwen-Image"
|
|
|
118 |
}
|
119 |
# Add metadata to the image
|
120 |
image_with_metadata = add_metadata_to_image(image, metadata)
|
|
|
185 |
)
|
186 |
with gr.Row():
|
187 |
guidance_scale = gr.Slider(
|
188 |
+
label="True CFG Scale",
|
189 |
+
minimum=1.0,
|
190 |
+
maximum=10.0,
|
191 |
step=0.1,
|
192 |
+
value=4.0,
|
193 |
)
|
194 |
num_inference_steps = gr.Slider(
|
195 |
label="生成步数",
|
196 |
minimum=1,
|
197 |
+
maximum=100,
|
198 |
step=1,
|
199 |
+
value=50,
|
200 |
)
|
|
|
201 |
|
202 |
gr.Examples(
|
203 |
examples=examples,
|
|
|
225 |
guidance_scale,
|
226 |
num_inference_steps,
|
227 |
randomize_seed,
|
|
|
228 |
],
|
229 |
outputs=[result, seed],
|
230 |
)
|