kimpudding commited on
Commit
bcd922b
·
verified ·
1 Parent(s): e75fa96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import openai
3
  import gradio as gr
4
  from diffusers import StableDiffusionPipeline
5
- from peft import PeftModel
6
  import torch
7
 
8
  # OpenAI API 키 설정
@@ -13,9 +12,17 @@ base_model_id = "runwayml/stable-diffusion-v1-5"
13
  pipe = StableDiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float32).to("cpu")
14
 
15
  # LoRA 가중치 로드 함수
 
16
  def load_lora_weights(pipeline, lora_model_path):
17
- peft_model = PeftModel.from_pretrained(pipeline.text_encoder, lora_model_path)
18
- pipeline.text_encoder = peft_model
 
 
 
 
 
 
 
19
  print("LoRA weights successfully loaded!")
20
 
21
  # LoRA 가중치 로드
@@ -25,6 +32,7 @@ load_lora_weights(pipe, lora_model_path)
25
  # 메모리 최적화
26
  def optimize_pipeline(pipeline):
27
  pipeline.enable_attention_slicing()
 
28
  return pipeline
29
 
30
  pipe = optimize_pipeline(pipe)
@@ -98,4 +106,4 @@ interface = gr.Interface(
98
 
99
  # 앱 실행
100
  if __name__ == "__main__":
101
- interface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
 
2
  import openai
3
  import gradio as gr
4
  from diffusers import StableDiffusionPipeline
 
5
  import torch
6
 
7
  # OpenAI API 키 설정
 
12
  pipe = StableDiffusionPipeline.from_pretrained(base_model_id, torch_dtype=torch.float32).to("cpu")
13
 
14
  # LoRA 가중치 로드 함수
15
+ # adapter_config.json 파일이 없는 경우 기본 구성을 설정합니다.
16
  def load_lora_weights(pipeline, lora_model_path):
17
+ config = {
18
+ "lora_r": 16, # Rank of the low-rank matrix
19
+ "lora_alpha": 32, # Scaling factor
20
+ "lora_dropout": 0.1, # Dropout probability
21
+ "bias": "none", # Bias configuration
22
+ "task_type": "TEXT_TO_IMAGE" # Task type
23
+ }
24
+ # config를 기반으로 LoRA 가중치를 로드합니다.
25
+ pipeline.load_lora_weights(lora_model_path, config=config)
26
  print("LoRA weights successfully loaded!")
27
 
28
  # LoRA 가중치 로드
 
32
  # 메모리 최적화
33
  def optimize_pipeline(pipeline):
34
  pipeline.enable_attention_slicing()
35
+ pipeline.enable_sequential_cpu_offload()
36
  return pipeline
37
 
38
  pipe = optimize_pipeline(pipe)
 
106
 
107
  # 앱 실행
108
  if __name__ == "__main__":
109
+ interface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)), share=True)