hyder133 commited on
Commit
3d22bb0
·
verified ·
1 Parent(s): 283b054

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -53
app.py CHANGED
@@ -1,57 +1,13 @@
1
  from diffusers import StableDiffusionPipeline
2
- from huggingface_hub import hf_hub_download
3
  import torch
4
- import gradio as gr
5
-
6
- # 模型和 LoRA 存儲庫信息
7
- base_model = "stabilityai/stable-diffusion-xl-base-1.0" # 基礎模型
8
- repo_id = "hyder133/chiikawa_stype" # Hugging Face 存儲庫 ID
9
- lora_filename = "tkw1.safetensors" # LoRA 權重文件名
10
-
11
- # 嘗試加載到 GPU,如果不可用則回退到 CPU
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- dtype = torch.float16 if device == "cuda" else torch.float32
14
 
15
  # 加載基礎模型
16
- pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype)
17
- pipe = pipe.to(device)
18
-
19
- # 下載並加載 LoRA 權重
20
- lora_file_path = hf_hub_download(repo_id=repo_id, filename=lora_filename)
21
- pipe.load_lora_weights(lora_file_path)
22
-
23
- # 測試生成函數
24
- def generate_image(prompt, width, height, steps, guidance_scale):
25
- # 使用模型生成圖像
26
- image = pipe(
27
- prompt,
28
- width=width,
29
- height=height,
30
- num_inference_steps=steps,
31
- guidance_scale=guidance_scale
32
- ).images[0]
33
- return image
34
-
35
- # Gradio 界面
36
- with gr.Blocks() as demo:
37
- gr.Markdown("# Chiikawa Style Image Generator")
38
- with gr.Row():
39
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt")
40
- with gr.Row():
41
- width = gr.Slider(256, 1024, value=512, step=64, label="Width")
42
- height = gr.Slider(256, 1024, value=512, step=64, label="Height")
43
- with gr.Row():
44
- steps = gr.Slider(10, 50, value=30, step=5, label="Steps")
45
- guidance_scale = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
46
- with gr.Row():
47
- generate_button = gr.Button("Generate")
48
- output = gr.Image(label="Generated Image")
49
-
50
- generate_button.click(
51
- generate_image,
52
- inputs=[prompt, width, height, steps, guidance_scale],
53
- outputs=output
54
- )
55
-
56
- # 啟動應用
57
- demo.launch()
 
1
  from diffusers import StableDiffusionPipeline
 
2
  import torch
 
 
 
 
 
 
 
 
 
 
3
 
4
  # 加載基礎模型
5
+ pipe = StableDiffusionPipeline.from_pretrained(
6
+ "stabilityai/stable-diffusion-xl-base-1.0",
7
+ torch_dtype=torch.float16
8
+ ).to("cuda")
9
+
10
+ # 測試生成圖像
11
+ prompt = "A cute cartoon character in Chiikawa style, ultra-detailed"
12
+ image = pipe(prompt).images[0]
13
+ image.save("output.png")