Ron1006 commited on
Commit
1ad7c7a
·
verified ·
1 Parent(s): ccc3a41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
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
- login(token=hf_token)
 
13
 
14
- # 加载 Hugging Face 上的预训练模型
15
- pipe = DiffusionPipeline.from_pretrained("lamblabs/sd-image-variations-diffusers")
 
16
 
17
- # 图像生图像的核心函数,使用预训练模型处理图像
18
- def image_to_image(input_image):
19
- # 这里将输入图像转换为 Hugging Face 模型可以使用的格式
20
  input_image = input_image.convert("RGB")
21
 
22
- # 生成图片,使用模型进行处理
23
- result = pipe(input_image).images[0]
 
 
24
 
25
- return result # 返回生成的图像
26
 
27
  # 创建 Gradio 接口
28
- demo = gr.Interface(fn=image_to_image, inputs="image", outputs="image")
 
 
 
 
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