Download app.py from fdbw/fzd1: direct link, hf CLI and curl.
- Browser
- Download file 814 Bytes
-
https://huggingface.co/spaces/fdbw/fzd1/resolve/main/app.py
- Command line
-
hf download hf://spaces/fdbw/fzd1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/fdbw/fzd1/resolve/main/app.py
814 Bytes
| 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() | |