aiqtech commited on
Commit
8805bba
·
verified ·
1 Parent(s): 0212dde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -25,34 +25,32 @@ from PIL import Image, ImageDraw, ImageFont
25
  def clear_memory():
26
  """메모리 정리 함수"""
27
  gc.collect()
28
- torch.cuda.empty_cache()
29
-
30
- # 사용하지 않는 캐시 정리
31
- if hasattr(torch.cuda, 'empty_cache'):
32
- torch.cuda.empty_cache()
33
- if hasattr(torch.cuda, 'ipc_collect'):
34
- torch.cuda.ipc_collect()
35
 
36
  def initialize_models():
 
37
  global segmenter, gd_model, gd_processor, pipe, translator
38
 
39
  try:
40
- clear_memory()
41
-
42
- # CPU에서만 실행되는 번역 모델
43
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name,
44
- low_cpu_mem_usage=True).to('cpu')
45
  tokenizer = AutoTokenizer.from_pretrained(model_name)
46
- translator = pipeline("translation", model=model, tokenizer=tokenizer,
47
- device=-1)
48
 
49
  # GroundingDINO 모델
50
  gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
51
  gd_model = GroundingDinoForObjectDetection.from_pretrained(
52
  gd_model_path,
53
  torch_dtype=torch.float16,
54
- device_map='auto',
55
- low_cpu_mem_usage=True
56
  )
57
 
58
  # Segmenter
@@ -62,11 +60,10 @@ def initialize_models():
62
  pipe = FluxPipeline.from_pretrained(
63
  "black-forest-labs/FLUX.1-dev",
64
  torch_dtype=torch.float16,
65
- device_map='auto',
66
  low_cpu_mem_usage=True
67
  )
68
  pipe.enable_attention_slicing()
69
- pipe.enable_model_cpu_offload()
70
 
71
  except Exception as e:
72
  print(f"Model initialization error: {str(e)}")
@@ -601,8 +598,7 @@ if __name__ == "__main__":
601
  torch.backends.cuda.matmul.allow_tf32 = True
602
  except Exception as e:
603
  print(f"CUDA setup warning: {e}")
604
-
605
- # HF 토큰 설정
606
  if HF_TOKEN:
607
  login(token=HF_TOKEN, add_to_git_credential=False)
608
 
@@ -615,7 +611,8 @@ if __name__ == "__main__":
615
  css=css,
616
  analytics_enabled=False,
617
  cache_examples=False
618
- ) as demo:
 
619
  # HTML 헤더
620
  gr.HTML("""
621
  <div class="main-title">
 
25
  def clear_memory():
26
  """메모리 정리 함수"""
27
  gc.collect()
28
+ if torch.cuda.is_available():
29
+ try:
30
+ with torch.cuda.device('cuda'):
31
+ torch.cuda.empty_cache()
32
+ except Exception as e:
33
+ print(f"GPU memory management warning: {e}")
 
34
 
35
  def initialize_models():
36
+ """모델 초기화 함수"""
37
  global segmenter, gd_model, gd_processor, pipe, translator
38
 
39
  try:
40
+ # CPU에서 실행되는 번역 모델
41
+ model = AutoModelForSeq2SeqLM.from_pretrained(
42
+ model_name,
43
+ low_cpu_mem_usage=True
44
+ ).to('cpu')
45
  tokenizer = AutoTokenizer.from_pretrained(model_name)
46
+ translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1)
 
47
 
48
  # GroundingDINO 모델
49
  gd_processor = GroundingDinoProcessor.from_pretrained(gd_model_path)
50
  gd_model = GroundingDinoForObjectDetection.from_pretrained(
51
  gd_model_path,
52
  torch_dtype=torch.float16,
53
+ device_map=None # device_map을 None으로 설정
 
54
  )
55
 
56
  # Segmenter
 
60
  pipe = FluxPipeline.from_pretrained(
61
  "black-forest-labs/FLUX.1-dev",
62
  torch_dtype=torch.float16,
63
+ device_map=None, # device_map을 None으로 설정
64
  low_cpu_mem_usage=True
65
  )
66
  pipe.enable_attention_slicing()
 
67
 
68
  except Exception as e:
69
  print(f"Model initialization error: {str(e)}")
 
598
  torch.backends.cuda.matmul.allow_tf32 = True
599
  except Exception as e:
600
  print(f"CUDA setup warning: {e}")
601
+
 
602
  if HF_TOKEN:
603
  login(token=HF_TOKEN, add_to_git_credential=False)
604
 
 
611
  css=css,
612
  analytics_enabled=False,
613
  cache_examples=False
614
+ ) as demo:
615
+
616
  # HTML 헤더
617
  gr.HTML("""
618
  <div class="main-title">