Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ from diffusers import AutoencoderKL, TCDScheduler
|
|
5 |
from diffusers.models.model_loading_utils import load_state_dict
|
6 |
from gradio_imageslider import ImageSlider
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
-
from transformers import pipeline
|
9 |
|
10 |
from controlnet_union import ControlNetModel_Union
|
11 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
@@ -14,8 +13,13 @@ MODELS = {
|
|
14 |
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
|
15 |
}
|
16 |
|
17 |
-
#
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
config_file = hf_hub_download(
|
21 |
"xinsir/controlnet-union-sdxl-1.0",
|
@@ -48,48 +52,44 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
48 |
|
49 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
50 |
|
51 |
-
def translate_if_korean(text):
|
52 |
-
# ์
๋ ฅ๋ ํ
์คํธ๊ฐ ํ๊ธ์ ํฌํจํ๊ณ ์๋์ง ํ์ธ
|
53 |
-
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in text):
|
54 |
-
# ํ๊ธ์ด ํฌํจ๋์ด ์๋ค๋ฉด ๋ฒ์ญ
|
55 |
-
translated = translator(text)[0]['translation_text']
|
56 |
-
print(f"Translated prompt: {translated}") # ๋๋ฒ๊น
์ ์ํ ์ถ๋ ฅ
|
57 |
-
return translated
|
58 |
-
return text
|
59 |
-
|
60 |
@spaces.GPU
|
61 |
def fill_image(prompt, image, model_selection):
|
62 |
-
#
|
63 |
translated_prompt = translate_if_korean(prompt)
|
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 |
def clear_result():
|
95 |
return gr.update(value=None)
|
@@ -109,36 +109,36 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
109 |
with gr.Row():
|
110 |
with gr.Column():
|
111 |
prompt = gr.Textbox(
|
112 |
-
label="
|
113 |
-
info="
|
114 |
lines=3,
|
115 |
)
|
116 |
with gr.Column():
|
117 |
model_selection = gr.Dropdown(
|
118 |
choices=list(MODELS.keys()),
|
119 |
value="RealVisXL V5.0 Lightning",
|
120 |
-
label="
|
121 |
)
|
122 |
-
run_button = gr.Button("
|
123 |
|
124 |
with gr.Row():
|
125 |
input_image = gr.ImageMask(
|
126 |
type="pil",
|
127 |
-
label="
|
128 |
crop_size=(1024, 1024),
|
129 |
layers=False
|
130 |
)
|
131 |
|
132 |
result = ImageSlider(
|
133 |
interactive=False,
|
134 |
-
label="
|
135 |
)
|
136 |
|
137 |
-
use_as_input_button = gr.Button("
|
138 |
|
139 |
-
#
|
140 |
with gr.Row(elem_classes="sample-image"):
|
141 |
-
sample_image = gr.Image("sample.png", label="
|
142 |
|
143 |
def use_output_as_input(output_image):
|
144 |
return gr.update(value=output_image[1])
|
|
|
5 |
from diffusers.models.model_loading_utils import load_state_dict
|
6 |
from gradio_imageslider import ImageSlider
|
7 |
from huggingface_hub import hf_hub_download
|
|
|
8 |
|
9 |
from controlnet_union import ControlNetModel_Union
|
10 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
|
|
13 |
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
|
14 |
}
|
15 |
|
16 |
+
# Replace the problematic translation model with a simpler function
|
17 |
+
def translate_if_korean(text):
|
18 |
+
# Just log that Korean was detected but return the original text
|
19 |
+
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in text):
|
20 |
+
print(f"Korean text detected: {text}")
|
21 |
+
print("Translation is disabled - using original text")
|
22 |
+
return text
|
23 |
|
24 |
config_file = hf_hub_download(
|
25 |
"xinsir/controlnet-union-sdxl-1.0",
|
|
|
52 |
|
53 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
@spaces.GPU
|
56 |
def fill_image(prompt, image, model_selection):
|
57 |
+
# Translate prompt if needed
|
58 |
translated_prompt = translate_if_korean(prompt)
|
59 |
|
60 |
+
try:
|
61 |
+
(
|
62 |
+
prompt_embeds,
|
63 |
+
negative_prompt_embeds,
|
64 |
+
pooled_prompt_embeds,
|
65 |
+
negative_pooled_prompt_embeds,
|
66 |
+
) = pipe.encode_prompt(translated_prompt, "cuda", True)
|
67 |
+
|
68 |
+
source = image["background"]
|
69 |
+
mask = image["layers"][0]
|
70 |
+
|
71 |
+
alpha_channel = mask.split()[3]
|
72 |
+
binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
|
73 |
+
cnet_image = source.copy()
|
74 |
+
cnet_image.paste(0, (0, 0), binary_mask)
|
75 |
+
|
76 |
+
for image in pipe(
|
77 |
+
prompt_embeds=prompt_embeds,
|
78 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
79 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
80 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
81 |
+
image=cnet_image,
|
82 |
+
):
|
83 |
+
yield image, cnet_image
|
84 |
+
|
85 |
+
image = image.convert("RGBA")
|
86 |
+
cnet_image.paste(image, (0, 0), binary_mask)
|
87 |
+
|
88 |
+
yield source, cnet_image
|
89 |
+
except Exception as e:
|
90 |
+
print(f"Error during image generation: {e}")
|
91 |
+
# Return the original image in case of error
|
92 |
+
return source, source
|
93 |
|
94 |
def clear_result():
|
95 |
return gr.update(value=None)
|
|
|
109 |
with gr.Row():
|
110 |
with gr.Column():
|
111 |
prompt = gr.Textbox(
|
112 |
+
label="Prompt",
|
113 |
+
info="Describe what to fill in the mask area (Korean or English)",
|
114 |
lines=3,
|
115 |
)
|
116 |
with gr.Column():
|
117 |
model_selection = gr.Dropdown(
|
118 |
choices=list(MODELS.keys()),
|
119 |
value="RealVisXL V5.0 Lightning",
|
120 |
+
label="Model",
|
121 |
)
|
122 |
+
run_button = gr.Button("Generate")
|
123 |
|
124 |
with gr.Row():
|
125 |
input_image = gr.ImageMask(
|
126 |
type="pil",
|
127 |
+
label="Input Image",
|
128 |
crop_size=(1024, 1024),
|
129 |
layers=False
|
130 |
)
|
131 |
|
132 |
result = ImageSlider(
|
133 |
interactive=False,
|
134 |
+
label="Generated Image",
|
135 |
)
|
136 |
|
137 |
+
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
138 |
|
139 |
+
# Add sample image
|
140 |
with gr.Row(elem_classes="sample-image"):
|
141 |
+
sample_image = gr.Image("sample.png", label="Sample Image", height=256, width=256)
|
142 |
|
143 |
def use_output_as_input(output_image):
|
144 |
return gr.update(value=output_image[1])
|