Spaces:
Runtime error
Runtime error
boheng.xie
commited on
Commit
·
cbdc720
1
Parent(s):
ed823cc
change app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# 初始化模型(免费版Space选择小模型)
|
| 7 |
+
model = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
"CompVis/stable-diffusion-v1-4",
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
use_safetensors=True,
|
| 11 |
+
low_cpu_mem_usage=True # 节省内存
|
| 12 |
+
).to("cuda") if torch.cuda.is_available() else None # 兼容CPU模式
|
| 13 |
|
| 14 |
+
def generate(prompt):
|
| 15 |
+
if model is None:
|
| 16 |
+
return "当前环境不支持图像生成"
|
| 17 |
+
image = model(prompt, num_inference_steps=15).images[0]
|
| 18 |
+
return image
|
| 19 |
+
|
| 20 |
+
# 基础界面
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=generate,
|
| 23 |
+
inputs=gr.Textbox(label="输入表情包描述"),
|
| 24 |
+
outputs=gr.Image(label="生成结果"),
|
| 25 |
+
examples=[["一只生气的猫"], ["跳舞的香蕉"]]
|
| 26 |
+
)
|
| 27 |
+
interface.launch()
|