File size: 4,602 Bytes
c412499
 
 
 
 
 
 
 
 
 
 
 
 
5231271
c412499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5231271
 
 
c412499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5231271
 
470a2c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import requests
import json
import random
import gradio as gr
from PIL import Image
from io import BytesIO

from utils import get_token


def deliver_request(style, desc):
    number = random.randint(1, 90) % 3
    print("node: ", number)
    return generate_figure(style, desc, number)


def generate_figure(style, desc, number):
    url_node1 = os.environ["url_node1"]
    url_node2 = os.environ["url_node2"]
    url_node3 = os.environ["url_node3"]
    url_list = [url_node1, url_node2, url_node3]

    requests_json = {
        "user_name": "huggingface",
        "style": style,
        "desc": desc
    }

    headers = {
        "Content-Type": "application/json",
        "X-Auth-Token": token
    }

    response = requests.post(url_list[number], json=requests_json, headers=headers, verify=False)
    response = json.loads(response.text)
    status = response["status"]
    assert status == 200
    url_list = response["output_image_url"]

    image1 = Image.open(BytesIO(requests.get(url_list[0]).content))
    image2 = Image.open(BytesIO(requests.get(url_list[1]).content))
    image3 = Image.open(BytesIO(requests.get(url_list[2]).content))
    image4 = Image.open(BytesIO(requests.get(url_list[3]).content))

    return image1, image2, image3, image4


def read_content(file_path: str) -> str:
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    return content


token = get_token()

examples_style = ["宫崎骏", "新海诚", "梵高", "赛博朋克", "水彩", "莫奈"]
examples_desc = ["城市夜景", "海滩 美景", "一只猫", "摩天大楼", "鸢尾花", "秋水共长天一色"]

css = """
.gradio-container {background-image: url('file=./background.jpg'); background-size:cover; background-repeat: no-repeat;}

#generate {
    background: linear-gradient(#D8C5EB, #C5E8EB,#90CCF6);
    border: 1px solid #C5E8EB;
    border-radius: 8px;
    color: #26498B
}
"""

# warm up
generate_figure("梵高", "一只猫", 0)

with gr.Blocks(css=css) as demo:
    gr.HTML(read_content("./header.html"))

    gr.Markdown("# MindSpore Wukong-Huahua "
                "\nWukong-Huahua is a diffusion-based model that perfoms text-to-image task in Chinese, "
                "which was developed by the Huawei Noah's Ark Lab in cooperation with "
                "the Distributed & Parallel Software Lab and Ascend Product Develop Unit. "
                "It was trained on Wukong dataset and used MindSpore + Ascend,"
                " a software and hardware solution to implement. Welcome to try Wukong-Huahua by Our Online Platform.")

    with gr.Tab("图片生成 (Figure Generation)"):

        style_input = gr.Textbox(lines=1,
                                 placeholder="输入中文风格描述",
                                 label="Input the style of figure you want to generate. (use Chinese better)",
                                 elem_id="style-input")
        gr.Examples(
            examples=examples_style,
            inputs=style_input,
        )
        with gr.Row():
            gr.Markdown(" *** ")
        desc_input = gr.Textbox(lines=1,
                                placeholder="输入中文图片描述",
                                label="Input a sentence to describe the figure you want to generate. "
                                      "(use Chinese better)")
        gr.Examples(
            examples=examples_desc,
            inputs=desc_input,
        )
        generate_button = gr.Button("Generate", elem_id="generate")
        with gr.Row():
            img_output1 = gr.Image(type="pil")
            img_output2 = gr.Image(type="pil")
            img_output3 = gr.Image(type="pil")
            img_output4 = gr.Image(type="pil")

    with gr.Accordion("Open for More!"):
        gr.Markdown("- If you want to know more about the foundation models of MindSpore, please visit "
                    "[The Foundation Models Platform for Mindspore](https://xihe.mindspore.cn/)")
        gr.Markdown("- If you want to know more about MindSpore-related diffusion models, please visit "
                    "[minddiffusion](https://github.com/mindspore-lab/minddiffusion)")
        gr.Markdown("- Try [Wukong-Huahua model on the Foundation Models Platform for Mindspore]"
                    "(https://xihe.mindspore.cn/modelzoo/wukong)")

    generate_button.click(deliver_request,
                          inputs=[style_input, desc_input],
                          outputs=[img_output1, img_output2, img_output3, img_output4])

demo.queue(concurrency_count=5)
demo.launch(enable_queue=True)