Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,42 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import gradio as gr
|
|
|
3 |
from PIL import Image
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
from huggingface_hub import login
|
6 |
import os
|
|
|
7 |
|
8 |
# 从环境变量中获取访问令牌
|
9 |
hf_token = os.getenv("hf_token")
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
13 |
|
14 |
-
#
|
15 |
-
pipe =
|
|
|
16 |
|
17 |
-
#
|
18 |
-
def image_to_image(input_image):
|
19 |
-
#
|
20 |
input_image = input_image.convert("RGB")
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
return
|
26 |
|
27 |
# 创建 Gradio 接口
|
28 |
-
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
29 |
|
30 |
if __name__ == "__main__":
|
31 |
demo.launch()
|
32 |
|
33 |
|
34 |
-
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import gradio as gr
|
3 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
4 |
from PIL import Image
|
|
|
5 |
from huggingface_hub import login
|
6 |
import os
|
7 |
+
import torch
|
8 |
|
9 |
# 从环境变量中获取访问令牌
|
10 |
hf_token = os.getenv("hf_token")
|
11 |
|
12 |
+
# 使用令牌进行登录(如果不用Hugging Face的模型,可以跳过这一步)
|
13 |
+
if hf_token:
|
14 |
+
login(token=hf_token)
|
15 |
|
16 |
+
# 加载预训练的图像到图像的 Stable Diffusion 模型
|
17 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
18 |
+
pipe.to("cuda") # 如果有GPU, 可以使用GPU来加速
|
19 |
|
20 |
+
# 定义 Gradio 接口中调用的函数
|
21 |
+
def image_to_image(input_image, prompt):
|
22 |
+
# 将用户上传的输入图像转化为RGB格式
|
23 |
input_image = input_image.convert("RGB")
|
24 |
|
25 |
+
# 使用预训练模型生成新图像
|
26 |
+
generated_image = pipe(
|
27 |
+
prompt=prompt, image=input_image, strength=0.75, guidance_scale=7.5
|
28 |
+
).images[0]
|
29 |
|
30 |
+
return generated_image # 返回生成的图像
|
31 |
|
32 |
# 创建 Gradio 接口
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=image_to_image,
|
35 |
+
inputs=["image", "text"], # 接收一个图像和一个文本作为输入
|
36 |
+
outputs="image" # 输出图像
|
37 |
+
)
|
38 |
|
39 |
if __name__ == "__main__":
|
40 |
demo.launch()
|
41 |
|
42 |
|
|