fantos commited on
Commit
168bf64
·
verified ·
1 Parent(s): 7de7fd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -57
app.py CHANGED
@@ -13,10 +13,30 @@ import torch
13
  import random
14
  from transformers import pipeline
15
 
16
- os.system("pip install -e ./controlnet_aux")
 
 
 
17
 
18
- from controlnet_aux import OpenposeDetector, CannyDetector
19
- from depth_anything_v2.dpt import DepthAnythingV2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  from huggingface_hub import hf_hub_download
22
 
@@ -26,18 +46,19 @@ login(token=hf_token)
26
 
27
  MAX_SEED = np.iinfo(np.int32).max
28
 
29
- # 번역기 설정
30
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
31
-
32
- def translate_to_english(text):
33
- if any('\uAC00' <= char <= '\uD7A3' for char in text):
34
- return translator(text, max_length=512)[0]['translation_text']
35
- return text
 
 
 
 
36
 
37
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
38
- if randomize_seed:
39
- seed = random.randint(0, MAX_SEED)
40
- return seed
41
 
42
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
43
  model_configs = {
@@ -66,11 +87,17 @@ controlnet = FluxMultiControlNetModel([controlnet])
66
  pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
67
  pipe.to("cuda")
68
 
69
- mode_mapping = {"캐니":0, "타일":1, "깊이":2, "블러":3, "오픈포즈":4, "그레이스케일":5, "저품질": 6}
70
- strength_mapping = {"캐니":0.65, "타일":0.45, "깊이":0.55, "블러":0.45, "오픈포즈":0.55, "그레이스케일":0.45, "저품질": 0.4}
 
71
 
72
- canny = CannyDetector()
73
- open_pose = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
 
 
 
 
 
74
 
75
  torch.backends.cuda.matmul.allow_tf32 = True
76
  pipe.vae.enable_tiling()
@@ -154,47 +181,51 @@ def resize_img(input_image, max_side=768, min_side=512, size=None,
154
 
155
  @spaces.GPU()
156
  def infer(cond_in, image_in, prompt, inference_steps, guidance_scale, control_mode, control_strength, seed, progress=gr.Progress(track_tqdm=True)):
 
 
 
157
 
158
- control_mode_num = mode_mapping[control_mode]
159
- prompt = translate_to_english(prompt)
160
-
161
- if cond_in is None:
162
- if image_in is not None:
163
- image_in = resize_img(load_image(image_in))
164
- if control_mode == "Canny":
165
- control_image = extract_canny(image_in)
166
- elif control_mode == "Depth":
167
- control_image = extract_depth(image_in)
168
- elif control_mode == "OpenPose":
169
- control_image = extract_openpose(image_in)
170
- elif control_mode == "Blur":
171
- control_image = apply_gaussian_blur(image_in)
172
- elif control_mode == "LowQuality":
173
- control_image = add_gaussian_noise(image_in)
174
- elif control_mode == "Grayscale":
175
- control_image = convert_to_grayscale(image_in)
176
- elif control_mode == "Tile":
177
- control_image = tile(image_in)
178
- else:
179
- control_image = resize_img(load_image(cond_in))
180
-
181
- width, height = control_image.size
182
-
183
- image = pipe(
184
- prompt,
185
- control_image=[control_image],
186
- control_mode=[control_mode_num],
187
- width=width,
188
- height=height,
189
- controlnet_conditioning_scale=[control_strength],
190
- num_inference_steps=inference_steps,
191
- guidance_scale=guidance_scale,
192
- generator=torch.manual_seed(seed),
193
- ).images[0]
194
-
195
- torch.cuda.empty_cache()
196
 
197
- return image, control_image, gr.update(visible=True)
 
 
198
 
199
 
200
  css = """
 
13
  import random
14
  from transformers import pipeline
15
 
16
+ # Fix 1: Install controlnet_aux properly
17
+ # os.system("pip install -e ./controlnet_aux")
18
+ # Instead, try installing directly from GitHub:
19
+ os.system("pip install git+https://github.com/lllyasviel/ControlNet-v1-1-nightly.git@main#subdirectory=annotator")
20
 
21
+ # Fix 2: Better error handling for the Korean translator
22
+ def translate_to_english(text):
23
+ # Check if Korean characters are present
24
+ if any('\uAC00' <= char <= '\uD7A3' for char in text):
25
+ try:
26
+ # Try to load the translator
27
+ try:
28
+ # First, try with from_tf=True as suggested in the error message
29
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", from_tf=True)
30
+ except:
31
+ # If that fails, try a different model
32
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en-m2m-100")
33
+
34
+ return translator(text, max_length=512)[0]['translation_text']
35
+ except Exception as e:
36
+ print(f"Translation error: {e}")
37
+ # Return original text if translation fails
38
+ return text
39
+ return text
40
 
41
  from huggingface_hub import hf_hub_download
42
 
 
46
 
47
  MAX_SEED = np.iinfo(np.int32).max
48
 
49
+ # Import ControlNet processors with better error handling
50
+ try:
51
+ from controlnet_aux import OpenposeDetector, CannyDetector
52
+ except ImportError:
53
+ print("Failed to import from controlnet_aux, trying alternate imports...")
54
+ try:
55
+ from annotator.openpose import OpenposeDetector
56
+ from annotator.canny import CannyDetector
57
+ except ImportError:
58
+ print("Could not import ControlNet processors. Using fallback implementations.")
59
+ # Define fallback implementations if needed
60
 
61
+ from depth_anything_v2.dpt import DepthAnythingV2
 
 
 
62
 
63
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
64
  model_configs = {
 
87
  pipe = FluxControlNetPipeline.from_pretrained(base_model, controlnet=controlnet, torch_dtype=torch.bfloat16)
88
  pipe.to("cuda")
89
 
90
+ # Fixed dictionary keys to use English for consistency
91
+ mode_mapping = {"Canny":0, "Tile":1, "Depth":2, "Blur":3, "OpenPose":4, "Grayscale":5, "LowQuality": 6}
92
+ strength_mapping = {"Canny":0.65, "Tile":0.45, "Depth":0.55, "Blur":0.45, "OpenPose":0.55, "Grayscale":0.45, "LowQuality": 0.4}
93
 
94
+ # Load processors with error handling
95
+ try:
96
+ canny = CannyDetector()
97
+ open_pose = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
98
+ except Exception as e:
99
+ print(f"Error loading processors: {e}")
100
+ # Define fallback functions if needed
101
 
102
  torch.backends.cuda.matmul.allow_tf32 = True
103
  pipe.vae.enable_tiling()
 
181
 
182
  @spaces.GPU()
183
  def infer(cond_in, image_in, prompt, inference_steps, guidance_scale, control_mode, control_strength, seed, progress=gr.Progress(track_tqdm=True)):
184
+ try:
185
+ control_mode_num = mode_mapping[control_mode]
186
+ prompt = translate_to_english(prompt)
187
 
188
+ if cond_in is None:
189
+ if image_in is not None:
190
+ image_in = resize_img(load_image(image_in))
191
+ if control_mode == "Canny":
192
+ control_image = extract_canny(image_in)
193
+ elif control_mode == "Depth":
194
+ control_image = extract_depth(image_in)
195
+ elif control_mode == "OpenPose":
196
+ control_image = extract_openpose(image_in)
197
+ elif control_mode == "Blur":
198
+ control_image = apply_gaussian_blur(image_in)
199
+ elif control_mode == "LowQuality":
200
+ control_image = add_gaussian_noise(image_in)
201
+ elif control_mode == "Grayscale":
202
+ control_image = convert_to_grayscale(image_in)
203
+ elif control_mode == "Tile":
204
+ control_image = tile(image_in)
205
+ else:
206
+ control_image = resize_img(load_image(cond_in))
207
+
208
+ width, height = control_image.size
209
+
210
+ image = pipe(
211
+ prompt,
212
+ control_image=[control_image],
213
+ control_mode=[control_mode_num],
214
+ width=width,
215
+ height=height,
216
+ controlnet_conditioning_scale=[control_strength],
217
+ num_inference_steps=inference_steps,
218
+ guidance_scale=guidance_scale,
219
+ generator=torch.manual_seed(seed),
220
+ ).images[0]
221
+
222
+ torch.cuda.empty_cache()
223
+
224
+ return image, control_image, gr.update(visible=True)
 
225
 
226
+ except Exception as e:
227
+ print(f"Error in inference: {e}")
228
+ return None, None, gr.update(visible=True)
229
 
230
 
231
  css = """