Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,51 @@
|
|
1 |
from diffusers import StableDiffusionPipeline
|
|
|
2 |
import torch
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
6 |
|
7 |
# 加載基礎模型
|
8 |
-
base_model = "stabilityai/stable-diffusion-xl-base-1.0" # 基礎模型
|
9 |
pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
|
|
|
10 |
|
11 |
# 加載 LoRA 權重
|
12 |
-
pipe.load_lora_weights(
|
13 |
-
pipe = pipe.to("cuda") # 使用 GPU 加速
|
14 |
|
15 |
-
#
|
16 |
-
def generate_image(prompt):
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return image
|
19 |
|
20 |
# Gradio 界面
|
21 |
-
import gradio as gr
|
22 |
with gr.Blocks() as demo:
|
23 |
-
gr.Markdown("
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# 啟動應用
|
31 |
demo.launch()
|
|
|
1 |
from diffusers import StableDiffusionPipeline
|
2 |
+
from peft import PeftModel # PEFT 支持
|
3 |
import torch
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# 模型和 LoRA 權重的 URL
|
7 |
+
base_model = "stabilityai/stable-diffusion-xl-base-1.0" # 基礎模型
|
8 |
+
lora_weights_url = "https://huggingface.co/hyder133/chiikawa_stype/resolve/main/tkw1.safetensors" # 替換為您的 LoRA 權重 URL
|
9 |
|
10 |
# 加載基礎模型
|
|
|
11 |
pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)
|
12 |
+
pipe = pipe.to("cuda") # 使用 GPU 加速
|
13 |
|
14 |
# 加載 LoRA 權重
|
15 |
+
pipe.load_lora_weights(lora_weights_url) # 確保權重 URL 正確
|
|
|
16 |
|
17 |
+
# 測試生成函數
|
18 |
+
def generate_image(prompt, width, height, steps, guidance_scale):
|
19 |
+
# 使用模型生成圖像
|
20 |
+
image = pipe(
|
21 |
+
prompt,
|
22 |
+
width=width,
|
23 |
+
height=height,
|
24 |
+
num_inference_steps=steps,
|
25 |
+
guidance_scale=guidance_scale
|
26 |
+
).images[0]
|
27 |
return image
|
28 |
|
29 |
# Gradio 界面
|
|
|
30 |
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("# Chiikawa Style Image Generator")
|
32 |
+
with gr.Row():
|
33 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt")
|
34 |
+
with gr.Row():
|
35 |
+
width = gr.Slider(256, 1024, value=512, step=64, label="Width")
|
36 |
+
height = gr.Slider(256, 1024, value=512, step=64, label="Height")
|
37 |
+
with gr.Row():
|
38 |
+
steps = gr.Slider(10, 50, value=30, step=5, label="Steps")
|
39 |
+
guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
|
40 |
+
with gr.Row():
|
41 |
+
generate_button = gr.Button("Generate")
|
42 |
+
output = gr.Image(label="Generated Image")
|
43 |
|
44 |
+
generate_button.click(
|
45 |
+
generate_image,
|
46 |
+
inputs=[prompt, width, height, steps, guidance_scale],
|
47 |
+
outputs=output
|
48 |
+
)
|
49 |
|
50 |
# 啟動應用
|
51 |
demo.launch()
|