File size: 814 Bytes
0f0010b
3a018f7
 
c78b4f8
3a018f7
c78b4f8
3a018f7
0f0010b
c78b4f8
3a018f7
2de85a6
 
 
0f0010b
3a018f7
c78b4f8
0f0010b
2de85a6
 
 
fba0bc3
0f0010b
3a018f7
2de85a6
 
c78b4f8
0f0010b
c78b4f8
 
0f0010b
 
2de85a6
c78b4f8
3a018f7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline

hf_token = os.getenv("HF_TOKEN")

# 模型ID可以根据需求更改
model_id = "runwayml/stable-diffusion-v1-5"

device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32

# 加载模型
pipe = StableDiffusionPipeline.from_pretrained(
    model_id,
    dtype=dtype,  # 使用 dtype
    token=hf_token,
    safety_checker=None
).to(device)

# 生成图像的回调函数
def generate_image(prompt):
    image = pipe(prompt).images[0]
    return image

# 设置 Gradio 界面
demo = gr.Interface(
    fn=generate_image,
    inputs="text",  # 输入是一个文本框
    outputs="image",  # 输出是生成的图像
    title="SD1.5 文生图 Demo"
)

demo.launch()