ToddEverett commited on
Commit
cafec31
·
verified ·
1 Parent(s): 9329a8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -16
app.py CHANGED
@@ -1,17 +1,13 @@
1
  import gradio as gr
2
- import torch
3
  from diffusers import DiffusionPipeline
4
 
5
- # 设置模型和设备
6
- device = "cuda" if torch.cuda.is_available() else "cpu"
7
  model_repo_id = "stabilityai/stable-diffusion-3.5-large"
 
 
8
 
9
- # 加载模型
10
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
11
- pipe = pipe.to(device)
12
-
13
- # 推理函数,允许自定义宽度和高度
14
- def generate_image(prompt, width=1024, height=768, guidance_scale=7.5, num_inference_steps=50):
15
  image = pipe(
16
  prompt=prompt,
17
  width=width,
@@ -23,22 +19,21 @@ def generate_image(prompt, width=1024, height=768, guidance_scale=7.5, num_infer
23
 
24
  # 设置 Gradio 界面
25
  with gr.Blocks() as demo:
26
- gr.Markdown(" # Stable Diffusion 3.5 - Custom Width & Height")
27
 
28
  with gr.Row():
29
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
30
-
31
- # 允许用户设置宽度和高度
32
  width = gr.Slider(label="Width", minimum=512, maximum=1024, step=64, value=1024)
33
  height = gr.Slider(label="Height", minimum=512, maximum=1024, step=64, value=768)
34
 
35
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=15.0, step=0.1, value=7.5)
36
- num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=100, step=1, value=50)
37
 
38
- # 显示生成的图像
39
  output_image = gr.Image(label="Generated Image")
40
-
41
- # 点击按钮运行生成
42
  generate_btn = gr.Button("Generate")
43
  generate_btn.click(generate_image, inputs=[prompt, width, height, guidance_scale, num_inference_steps], outputs=output_image)
44
 
 
1
  import gradio as gr
 
2
  from diffusers import DiffusionPipeline
3
 
4
+ # 加载模型到 CPU
 
5
  model_repo_id = "stabilityai/stable-diffusion-3.5-large"
6
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id)
7
+ pipe.to("cpu") # 使用 CPU
8
 
9
+ # 定义图像生成函数,允许自定义宽度和高度
10
+ def generate_image(prompt, width=1024, height=768, guidance_scale=7.5, num_inference_steps=40):
 
 
 
 
11
  image = pipe(
12
  prompt=prompt,
13
  width=width,
 
19
 
20
  # 设置 Gradio 界面
21
  with gr.Blocks() as demo:
22
+ gr.Markdown(" # Stable Diffusion 3.5 - 自定义宽高比例")
23
 
24
  with gr.Row():
25
  prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here")
26
+
27
+ # 用户自定义宽度和高度
28
  width = gr.Slider(label="Width", minimum=512, maximum=1024, step=64, value=1024)
29
  height = gr.Slider(label="Height", minimum=512, maximum=1024, step=64, value=768)
30
 
31
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=15.0, step=0.1, value=7.5)
32
+ num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=1, maximum=50, step=1, value=40)
33
 
 
34
  output_image = gr.Image(label="Generated Image")
35
+
36
+ # 点击按钮生成图像
37
  generate_btn = gr.Button("Generate")
38
  generate_btn.click(generate_image, inputs=[prompt, width, height, guidance_scale, num_inference_steps], outputs=output_image)
39