Spaces:
Runtime error
Runtime error
Upload 2024002452.py
Browse files- 2024002452.py +109 -0
2024002452.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import paddle
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
from ppdiffusers import StableDiffusionInpaintPipeline
|
6 |
+
from ppdiffusers import StableDiffusionImg2ImgPipeline
|
7 |
+
from ppdiffusers import PaintByExamplePipeline
|
8 |
+
from ppdiffusers.utils import load_image
|
9 |
+
|
10 |
+
pipe = None #全局变量,用于模型的导入
|
11 |
+
API_KEY = "j5HodGgjG2iQ87MenXrw2hot"
|
12 |
+
SECRET_KEY = "Ea1AYc1kjzv2MNExEZeMAEwzanDDlsdK"
|
13 |
+
def select_model(choice):#用于选择模型
|
14 |
+
global pipe
|
15 |
+
pipe = None
|
16 |
+
paddle.device.cuda.empty_cache() #释放GPU显存
|
17 |
+
if choice == '文本引导图片风格变化':
|
18 |
+
pipe1 = StableDiffusionImg2ImgPipeline.from_pretrained("/home/aistudio/model1")
|
19 |
+
elif choice == '文本引导图像区域生成':
|
20 |
+
pipe1 = StableDiffusionInpaintPipeline.from_pretrained("/home/aistudio/model2")
|
21 |
+
elif choice == '文图引导图像区域生成':
|
22 |
+
pipe1 = PaintByExamplePipeline.from_pretrained("/home/aistudio/model3")
|
23 |
+
pipe = pipe1
|
24 |
+
return pipe1 #用于输出导入的模型相关信息
|
25 |
+
|
26 |
+
def get_access_token():
|
27 |
+
"""
|
28 |
+
使用 AK,SK 生成鉴权签名(Access Token)
|
29 |
+
:return: access_token,或是None(如果错误)
|
30 |
+
"""
|
31 |
+
url = "https://aip.baidubce.com/oauth/2.0/token"
|
32 |
+
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
|
33 |
+
return str(requests.post(url, params=params).json().get("access_token"))
|
34 |
+
|
35 |
+
def translation(content): #可接受中文prompt 转换成英文
|
36 |
+
url = "https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + get_access_token()
|
37 |
+
payload = json.dumps({
|
38 |
+
"from": "zh",
|
39 |
+
"to": "en",
|
40 |
+
"q": content
|
41 |
+
})
|
42 |
+
headers = {
|
43 |
+
'Content-Type': 'application/json',
|
44 |
+
'Accept': 'application/json'
|
45 |
+
}
|
46 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
47 |
+
i = response.text.find("dst")+6
|
48 |
+
j = response.text.find("src")-2
|
49 |
+
return response.text[i:j]
|
50 |
+
|
51 |
+
|
52 |
+
def diffusion_image(image,text,strength,guidance_scale,choice):
|
53 |
+
global pipe
|
54 |
+
text = translation(text)
|
55 |
+
if choice == '文本引导图片风格变化':
|
56 |
+
image = load_image(image).resize((768, 512))
|
57 |
+
with paddle.amp.auto_cast(True): # 使用fp16加快生成速度
|
58 |
+
img = pipe(prompt=text, image=image, strength=strength, guidance_scale=guidance_scale).images[0]
|
59 |
+
elif choice == '文本引导图像区域生成':
|
60 |
+
init_image = load_image(image).resize((512, 512))
|
61 |
+
mask_image = load_image(strength).resize((512, 512))
|
62 |
+
with paddle.amp.auto_cast(True): # 使用fp16加快生成速度
|
63 |
+
img = pipe(prompt=text, image=init_image, mask_image=mask_image).images[0]
|
64 |
+
elif choice == '文图引导图像区域生成':
|
65 |
+
init_image = load_image(image).resize((512, 512))
|
66 |
+
mask_image = load_image(strength).resize((512, 512))
|
67 |
+
example_image = load_image(guidance_scale).resize((512, 512))
|
68 |
+
with paddle.amp.auto_cast(True): # 使用fp16加快生成速度
|
69 |
+
img = pipe(image=init_image, mask_image=mask_image, example_image=example_image).images[0]
|
70 |
+
return img
|
71 |
+
|
72 |
+
options = ["文本引导图片风格变化", "文本引导图像区域生成", "文图引导图像区域生成",'文图双引导生成'] # 定义下拉选项
|
73 |
+
|
74 |
+
def select_option(choice):
|
75 |
+
return select_model(choice)
|
76 |
+
|
77 |
+
|
78 |
+
with gr.Blocks(title='功能选择') as demo:
|
79 |
+
gr.Markdown("# 功能介绍")
|
80 |
+
gr.Markdown("根据提示输入,得到理想图像!")
|
81 |
+
with gr.Row():
|
82 |
+
pipeText = gr.Textbox(label ='模型信息')
|
83 |
+
with gr.Tab('加载模型'):
|
84 |
+
dropdown = gr.inputs.Dropdown(choices=options, label="选项") # 创建下拉选择框输入组件
|
85 |
+
sub_model = gr.Button("加载模型")
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Tab('文本引导图片风格变化'):
|
88 |
+
in_img_1 = gr.Image(label='输入图片',type="filepath")
|
89 |
+
text_1 = gr.Textbox(label="输入文字")
|
90 |
+
strength_1 = gr.Slider(label="强度", minimum=0, maximum=1, step=0.01,value=0.75,
|
91 |
+
info="控制条件或指导文本的强度")
|
92 |
+
guidance_scale_1 = gr.Slider(label="权重", minimum=0, maximum=15, step=0.1,value=7.5,
|
93 |
+
info="条件或指导的权重")
|
94 |
+
sub_img_1 = gr.Button("生成图片")
|
95 |
+
with gr.Tab('文本引导图像区域生成'):
|
96 |
+
in_img_2 = gr.Image(label='输入图片',type="filepath")
|
97 |
+
strength_2 = gr.Image(label='输入掩码',type="filepath")
|
98 |
+
text_2 = gr.Textbox(label="输入文字")
|
99 |
+
sub_img_2 = gr.Button("生成图片")
|
100 |
+
with gr.Tab('文图引导图像区域生成'):
|
101 |
+
in_img_3 = gr.Image(label='输入图片',type="filepath")
|
102 |
+
strength_3 = gr.Image(label='输入掩码',type="filepath")
|
103 |
+
guidance_scale_3 = gr.Image(label='��入目标图片',type="filepath")
|
104 |
+
sub_img_3 = gr.Button("生成图片")
|
105 |
+
out_img=gr.Image(label='输出图片')
|
106 |
+
sub_model.click(fn =select_option,inputs=dropdown,outputs=pipeText)
|
107 |
+
sub_img_1.click(fn=diffusion_image, inputs=[in_img_1,text_1,strength_1,guidance_scale_1,dropdown], outputs=out_img)
|
108 |
+
sub_img_2.click(fn=diffusion_image, inputs=[in_img_2,text_2,strength_2,guidance_scale_1,dropdown], outputs=out_img)
|
109 |
+
demo.launch()
|