Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
from diffusers import StableDiffusionPipeline
|
| 6 |
-
from
|
| 7 |
import torch
|
| 8 |
|
| 9 |
# OpenAI API ํค ์ค์
|
|
@@ -11,20 +10,25 @@ client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))
|
|
| 11 |
|
| 12 |
# Stable Diffusion ๋ชจ๋ธ ๋ก๋ (CPU ๋ชจ๋)
|
| 13 |
base_model_id = "runwayml/stable-diffusion-v1-5"
|
| 14 |
-
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# LoRA ๊ฐ์ค์น ๋ก๋
|
| 21 |
-
|
| 22 |
-
lora_state_dict = load_file(lora_path)
|
| 23 |
-
pipe.unet.load_attn_procs(lora_state_dict)
|
| 24 |
-
|
| 25 |
-
# LoRA ์ ์ฉ
|
| 26 |
load_lora_weights(pipe, lora_model_path)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# API ํธ์ถ ํจ์ ์ ์
|
| 29 |
def generate_response(user_prompt):
|
| 30 |
try:
|
|
@@ -94,4 +98,4 @@ interface = gr.Interface(
|
|
| 94 |
|
| 95 |
# ์ฑ ์คํ
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
interface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860))
|
|
|
|
| 1 |
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 ํค ์ค์
|
|
|
|
| 10 |
|
| 11 |
# Stable Diffusion ๋ชจ๋ธ ๋ก๋ (CPU ๋ชจ๋)
|
| 12 |
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 ๊ฐ์ค์น ๋ก๋
|
| 22 |
+
lora_model_path = "./1211TeeniePing_Taesu-10.safetensors"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
load_lora_weights(pipe, lora_model_path)
|
| 24 |
|
| 25 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 26 |
+
def optimize_pipeline(pipeline):
|
| 27 |
+
pipeline.enable_attention_slicing()
|
| 28 |
+
return pipeline
|
| 29 |
+
|
| 30 |
+
pipe = optimize_pipeline(pipe)
|
| 31 |
+
|
| 32 |
# API ํธ์ถ ํจ์ ์ ์
|
| 33 |
def generate_response(user_prompt):
|
| 34 |
try:
|
|
|
|
| 98 |
|
| 99 |
# ์ฑ ์คํ
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
interface.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|