fantos commited on
Commit
1afa398
·
verified ·
1 Parent(s): 5b9758d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -4,14 +4,15 @@ import torch
4
  from PIL import Image
5
  from diffusers import DiffusionPipeline
6
  import random
7
- from transformers import pipeline
 
8
 
9
  torch.backends.cudnn.deterministic = True
10
  torch.backends.cudnn.benchmark = False
11
  torch.backends.cuda.matmul.allow_tf32 = True
12
 
13
- # 번역 모델 초기화
14
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
15
 
16
  base_model = "black-forest-labs/FLUX.1-dev"
17
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
@@ -26,14 +27,22 @@ MAX_SEED = 2**32-1
26
 
27
  @spaces.GPU()
28
  def translate_and_generate(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
29
- # 한글 감지 번역
30
  def contains_korean(text):
31
- return any(ord('가') <= ord(char) <= ord('힣') for char in text)
 
32
 
33
  if contains_korean(prompt):
34
- # 한글을 영어로 번역
35
- translated = translator(prompt)[0]['translation_text']
36
- actual_prompt = translated
 
 
 
 
 
 
 
37
  else:
38
  actual_prompt = prompt
39
 
 
4
  from PIL import Image
5
  from diffusers import DiffusionPipeline
6
  import random
7
+ import re
8
+ import googletrans # Use googletrans instead of transformers pipeline
9
 
10
  torch.backends.cudnn.deterministic = True
11
  torch.backends.cudnn.benchmark = False
12
  torch.backends.cuda.matmul.allow_tf32 = True
13
 
14
+ # Initialize translator with googletrans (more reliable than the failing model)
15
+ translator = googletrans.Translator()
16
 
17
  base_model = "black-forest-labs/FLUX.1-dev"
18
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
 
27
 
28
  @spaces.GPU()
29
  def translate_and_generate(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
30
+ # Korean detection and translation
31
  def contains_korean(text):
32
+ # Korean Unicode range check
33
+ return bool(re.search(r'[가-힣]', text))
34
 
35
  if contains_korean(prompt):
36
+ try:
37
+ # Try to translate using googletrans
38
+ translated = translator.translate(prompt, src='ko', dest='en').text
39
+ actual_prompt = translated
40
+ except Exception as e:
41
+ # Fallback if translation fails
42
+ print(f"Translation error: {e}")
43
+ actual_prompt = prompt
44
+ # Add a note that we're proceeding with original text
45
+ print("Proceeding with original text without translation")
46
  else:
47
  actual_prompt = prompt
48