File size: 3,081 Bytes
934409f
 
 
 
 
 
f534897
934409f
 
 
 
 
 
 
 
 
c1c65e4
f534897
 
9e0eb99
0027f9c
 
e8174e6
0027f9c
 
 
 
c1c65e4
934409f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb92a6
934409f
 
e8174e6
 
 
 
 
2106097
 
 
 
934409f
 
b8b09f2
934409f
fdb92a6
478d0cd
934409f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478d0cd
 
934409f
fdb92a6
 
934409f
 
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
import os
import requests
from uuid import uuid4
from PIL import Image
import gradio as gr


def gen_image(prompt: str):
    """generate the image from the chinese stable diffusion model of paddlenlp server

    Args:
        prompt (str): the source of the prompt
    """
    if not prompt:
        return
    access_token = os.environ['token']

    url = f"https://aip.baidubce.com/rpc/2.0/nlp-itec/poc/chinese_stable_diffusion?access_token={access_token}"

    content = requests.post(url, json={"text": prompt}).content
    try:
        new_content = content.decode(encoding='utf-8')

        if new_content.startswith("error: "):
            return []
    except:
        pass

    cache_dir = 'images'
    os.makedirs(cache_dir, exist_ok=True)

    tempfile = os.path.join(cache_dir, f'{str(uuid4())}.png')
    with open(tempfile, 'wb') as f:
        f.write(content)
    image = Image.open(tempfile)
    os.remove(tempfile)
    return [image]

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

    return content

block = gr.Blocks(css=read_content('style.css'))

examples = [
    '枯藤老树昏鸦,水墨画',
    '小桥流水人家,水墨画',
    '古道西风瘦马,水墨画',
    '夕阳西下,水墨画',
    '断肠人在天涯,水墨画',
    '白日依山尽,水墨画',
    '黄河入海流,水墨画',
    '欲穷千里目,水墨画',
    '更上一层楼,水墨画',
]


with block:
    gr.HTML(read_content("header.html"))
    gr.Markdown("> 非常抱歉,由于一些原因,此Space临时停止服务,等国庆之后会重新发布。", elem_id='warning', visible=True)
    gr.Markdown("[![Stargazers repo roster for @PaddlePaddle/PaddleNLP](https://reporoster.com/stars/PaddlePaddle/PaddleNLP)](https://github.com/PaddlePaddle/PaddleNLP)")
    with gr.Group():
        with gr.Box():
            with gr.Row().style(mobile_collapse=False, equal_height=True):
                text = gr.Textbox(
                    label="Prompt",
                    show_label=False,
                    max_lines=1,
                    placeholder="输入中文,生成图片",
                ).style(
                    border=(True, False, True, True),
                    rounded=(True, False, False, True),
                    container=False,
                )

                btn = gr.Button("Generate image").style(
                    margin=False,
                    rounded=(False, True, True, False),
                )

        gallery = gr.Gallery(
            label="Generated images", show_label=False, elem_id="gallery"
        ).style(grid=[1, 1], height="auto")

        gr.Examples(examples=examples, fn=gen_image, inputs=text, outputs=gallery)
        # text.submit(gen_image, inputs=text, outputs=gallery)
        # btn.click(gen_image, inputs=text, outputs=gallery)

    gr.Image('./paddlenlp-preview.jpeg')
    gr.HTML(read_content("footer.html"))

block.queue(concurrency_count=5).launch(debug=True)