Spaces:
Runtime error
Runtime error
drizzlezyk
commited on
Commit
·
c412499
1
Parent(s):
ba876f8
upload app utils header
Browse files- app.py +120 -0
- background.jpg +0 -0
- header.html +27 -0
- utils.py +48 -0
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import random
|
5 |
+
import gradio as gr
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
from utils import get_token
|
10 |
+
|
11 |
+
|
12 |
+
def deliver_request(style, desc):
|
13 |
+
number = random.randint(1, 90) % 3
|
14 |
+
return generate_figure(style, desc, number)
|
15 |
+
|
16 |
+
|
17 |
+
def generate_figure(style, desc, number):
|
18 |
+
url_node1 = os.environ["url_node1"]
|
19 |
+
url_node2 = os.environ["url_node2"]
|
20 |
+
url_node3 = os.environ["url_node3"]
|
21 |
+
url_list = [url_node1, url_node2, url_node3]
|
22 |
+
|
23 |
+
requests_json = {
|
24 |
+
"user_name": "huggingface",
|
25 |
+
"style": style,
|
26 |
+
"desc": desc
|
27 |
+
}
|
28 |
+
|
29 |
+
headers = {
|
30 |
+
"Content-Type": "application/json",
|
31 |
+
"X-Auth-Token": token
|
32 |
+
}
|
33 |
+
|
34 |
+
response = requests.post(url_list[number], json=requests_json, headers=headers, verify=False)
|
35 |
+
response = json.loads(response.text)
|
36 |
+
status = response["status"]
|
37 |
+
assert status == 200
|
38 |
+
url_list = response["output_image_url"]
|
39 |
+
|
40 |
+
image1 = Image.open(BytesIO(requests.get(url_list[0]).content))
|
41 |
+
image2 = Image.open(BytesIO(requests.get(url_list[1]).content))
|
42 |
+
image3 = Image.open(BytesIO(requests.get(url_list[2]).content))
|
43 |
+
image4 = Image.open(BytesIO(requests.get(url_list[3]).content))
|
44 |
+
|
45 |
+
return image1, image2, image3, image4
|
46 |
+
|
47 |
+
|
48 |
+
def read_content(file_path: str) -> str:
|
49 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
50 |
+
content = f.read()
|
51 |
+
|
52 |
+
return content
|
53 |
+
|
54 |
+
|
55 |
+
token = get_token()
|
56 |
+
|
57 |
+
examples_style = ["宫崎骏", "新海诚", "梵高", "赛博朋克", "水彩", "莫奈"]
|
58 |
+
examples_desc = ["城市夜景", "海滩 美景", "一只猫", "摩天大楼", "鸢尾花", "秋水共长天一色"]
|
59 |
+
|
60 |
+
css = """
|
61 |
+
.gradio-container {background-image: url('file=./background.jpg'); background-size:cover; background-repeat: no-repeat;}
|
62 |
+
|
63 |
+
#generate {
|
64 |
+
background: linear-gradient(#D8C5EB, #C5E8EB,#90CCF6);
|
65 |
+
border: 1px solid #C5E8EB;
|
66 |
+
border-radius: 8px;
|
67 |
+
color: #26498B
|
68 |
+
}
|
69 |
+
"""
|
70 |
+
|
71 |
+
with gr.Blocks(css=css) as demo:
|
72 |
+
gr.HTML(read_content("./header.html"))
|
73 |
+
|
74 |
+
gr.Markdown("# MindSpore Wukong-Huahua "
|
75 |
+
"\nWukong-Huahua is a diffusion-based model that perfoms text-to-image task in Chinese, "
|
76 |
+
"which was developed by the Huawei Noah's Ark Lab in cooperation with "
|
77 |
+
"the Distributed & Parallel Software Lab and Ascend Product Develop Unit. "
|
78 |
+
"It was trained on Wukong dataset and used MindSpore + Ascend,"
|
79 |
+
" a software and hardware solution to implement. Welcome to try Wukong-Huahua by Our Online Platform.")
|
80 |
+
|
81 |
+
with gr.Tab("图片生成 (Figure Generation)"):
|
82 |
+
|
83 |
+
style_input = gr.Textbox(lines=1,
|
84 |
+
placeholder="输入中文风格描述",
|
85 |
+
label="Input the style of figure you want to generate. (use Chinese better)",
|
86 |
+
elem_id="style-input")
|
87 |
+
gr.Examples(
|
88 |
+
examples=examples_style,
|
89 |
+
inputs=style_input,
|
90 |
+
)
|
91 |
+
with gr.Row():
|
92 |
+
gr.Markdown(" *** ")
|
93 |
+
desc_input = gr.Textbox(lines=1,
|
94 |
+
placeholder="输入中文图片描述",
|
95 |
+
label="Input a sentence to describe the figure you want to generate. "
|
96 |
+
"(use Chinese better)")
|
97 |
+
gr.Examples(
|
98 |
+
examples=examples_desc,
|
99 |
+
inputs=desc_input,
|
100 |
+
)
|
101 |
+
generate_button = gr.Button("Generate", elem_id="generate")
|
102 |
+
with gr.Row():
|
103 |
+
img_output1 = gr.Image(type="pil")
|
104 |
+
img_output2 = gr.Image(type="pil")
|
105 |
+
img_output3 = gr.Image(type="pil")
|
106 |
+
img_output4 = gr.Image(type="pil")
|
107 |
+
|
108 |
+
with gr.Accordion("Open for More!"):
|
109 |
+
gr.Markdown("- If you want to know more about the foundation models of MindSpore, please visit "
|
110 |
+
"[The Foundation Models Platform for Mindspore](https://xihe.mindspore.cn/)")
|
111 |
+
gr.Markdown("- If you want to know more about MindSpore-related diffusion models, please visit "
|
112 |
+
"[minddiffusion](https://github.com/mindspore-lab/minddiffusion)")
|
113 |
+
gr.Markdown("- Try [Wukong-Huahua model on the Foundation Models Platform for Mindspore]"
|
114 |
+
"(https://xihe.mindspore.cn/modelzoo/wukong)")
|
115 |
+
|
116 |
+
generate_button.click(deliver_request,
|
117 |
+
inputs=[style_input, desc_input],
|
118 |
+
outputs=[img_output1, img_output2, img_output3, img_output4])
|
119 |
+
|
120 |
+
demo.launch()
|
background.jpg
ADDED
header.html
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<div style="text-align: center; max-width: 1920px; margin: 0 auto;">
|
2 |
+
<div
|
3 |
+
style="
|
4 |
+
display: inline-flex;
|
5 |
+
gap: 0.8rem;
|
6 |
+
font-size: 1.75rem;
|
7 |
+
margin-bottom: 10px;
|
8 |
+
margin-left: 220px;
|
9 |
+
justify-content: center;
|
10 |
+
"
|
11 |
+
>
|
12 |
+
</div>
|
13 |
+
<div
|
14 |
+
style="
|
15 |
+
display: inline-flex;
|
16 |
+
align-items: center;
|
17 |
+
gap: 0.8rem;
|
18 |
+
font-size: 1.75rem;
|
19 |
+
margin-bottom: 10px;
|
20 |
+
justify-content: center;
|
21 |
+
">
|
22 |
+
<a href="https://github.com/mindspore-ai/mindspore"><h1 style="font-weight: 900; align-items: center; margin-bottom: 7px;">
|
23 |
+
</h1></a>
|
24 |
+
</div>
|
25 |
+
<a href="https://github.com/mindspore-ai/mindspore"><img src="https://obs-xihe-beijing4-test.obs.cn-north-4.myhuaweicloud.com/xihe-img/huggingface/wukong-banner-english.png" width="100%"></a>
|
26 |
+
|
27 |
+
</div>
|
utils.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
def get_token():
|
6 |
+
username = os.environ["username"]
|
7 |
+
domain_name = os.environ["domain_name"]
|
8 |
+
domain_pwd = os.environ["domain_pwd"]
|
9 |
+
url = os.environ["token_url"]
|
10 |
+
|
11 |
+
requests_json = {
|
12 |
+
"auth": {
|
13 |
+
"identity": {
|
14 |
+
"methods": ["password"],
|
15 |
+
"password": {
|
16 |
+
"user": {
|
17 |
+
"name": username,
|
18 |
+
"password": domain_pwd,
|
19 |
+
"domain": {
|
20 |
+
"name": domain_name
|
21 |
+
}
|
22 |
+
}
|
23 |
+
}
|
24 |
+
},
|
25 |
+
"scope": {
|
26 |
+
"project": {
|
27 |
+
"name": "cn-central-221"
|
28 |
+
}
|
29 |
+
}
|
30 |
+
}
|
31 |
+
}
|
32 |
+
|
33 |
+
headers = {
|
34 |
+
"Content-Type": "text/plain"
|
35 |
+
}
|
36 |
+
|
37 |
+
response = requests.post(url, json=requests_json, headers=headers)
|
38 |
+
|
39 |
+
assert response.status_code == 201
|
40 |
+
|
41 |
+
result = response.headers
|
42 |
+
print("token success")
|
43 |
+
|
44 |
+
return result['X-Subject-Token']
|
45 |
+
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
get_token()
|