aiqtech commited on
Commit
3bd9247
ยท
verified ยท
1 Parent(s): a4cba99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -50
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
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
 
 
 
 
 
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
- prompt_embeds,
67
- negative_prompt_embeds,
68
- pooled_prompt_embeds,
69
- negative_pooled_prompt_embeds,
70
- ) = pipe.encode_prompt(translated_prompt, "cuda", True)
71
-
72
- source = image["background"]
73
- mask = image["layers"][0]
74
-
75
- alpha_channel = mask.split()[3]
76
- binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
77
- cnet_image = source.copy()
78
- cnet_image.paste(0, (0, 0), binary_mask)
79
-
80
- for image in pipe(
81
- prompt_embeds=prompt_embeds,
82
- negative_prompt_embeds=negative_prompt_embeds,
83
- pooled_prompt_embeds=pooled_prompt_embeds,
84
- negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
85
- image=cnet_image,
86
- ):
87
- yield image, cnet_image
88
-
89
- image = image.convert("RGBA")
90
- cnet_image.paste(image, (0, 0), binary_mask)
91
-
92
- yield source, cnet_image
 
 
 
 
 
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("์ž…๋ ฅ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉ", visible=False)
138
 
139
- # ์ƒ˜ํ”Œ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€
140
  with gr.Row(elem_classes="sample-image"):
141
- sample_image = gr.Image("sample.png", label="์ƒ˜ํ”Œ ์ด๋ฏธ์ง€", height=256, width=256)
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])