wjmcat commited on
Commit
934409f
1 Parent(s): c8466a0

create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from uuid import uuid4
4
+ from PIL import Image
5
+ import gradio as gr
6
+
7
+ def gen_image(prompt: str):
8
+ """generate the image from the chinese stable diffusion model of paddlenlp server
9
+
10
+ Args:
11
+ prompt (str): the source of the prompt
12
+ """
13
+ if not prompt:
14
+ return
15
+ access_token = os.environ['token']
16
+ url = f"https://aip.baidubce.com/rpc/2.0/nlp-itec/poc/stable_diffusion?access_token={access_token}&text={prompt}"
17
+ content = requests.get(url).content
18
+ cache_dir = 'images'
19
+ os.makedirs(cache_dir, exist_ok=True)
20
+
21
+ tempfile = os.path.join(cache_dir, f'{str(uuid4())}.png')
22
+ with open(tempfile, 'wb') as f:
23
+ f.write(content)
24
+ image = Image.open(tempfile)
25
+ os.remove(tempfile)
26
+ return [image]
27
+
28
+ def read_content(file_path: str) -> str:
29
+ """read the content of target file
30
+ """
31
+ with open(file_path, 'r', encoding='utf-8') as f:
32
+ content = f.read()
33
+
34
+ return content
35
+
36
+ block = gr.Blocks(css=read_content('assets/style.css'))
37
+
38
+ examples = [
39
+ '贝尼·赖特森、丹·蒙福德、亚伦·霍尔基的黑白血腥维多利亚小镇夜景特写街景,恐怖,月亮升起,交叉阴影,高对比度,超精细,极简主义构图,4k',
40
+ '马头雕塑插图印刷,超精细,丹·蒙福德,亚伦·霍基,高对比度,低聚合风格',
41
+ '一只熊从篝火旁的冰箱里偷食物,黑白雕刻版画,交叉影线',
42
+ '厚涂层油画《悲伤男孩的特写肖像》,本·科迪,希卡里·希莫达'
43
+ ]
44
+
45
+ with block:
46
+ gr.HTML(read_content("assets/header.html"))
47
+ gr.Markdown("[![Stargazers repo roster for @PaddlePaddle/PaddleNLP](https://reporoster.com/stars/PaddlePaddle/PaddleNLP)](https://github.com/PaddlePaddle/PaddleNLP)")
48
+ with gr.Group():
49
+ with gr.Box():
50
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
51
+ text = gr.Textbox(
52
+ label="Prompt",
53
+ show_label=False,
54
+ max_lines=1,
55
+ placeholder="输入中文,生成图片",
56
+ ).style(
57
+ border=(True, False, True, True),
58
+ rounded=(True, False, False, True),
59
+ container=False,
60
+ )
61
+
62
+ btn = gr.Button("Generate image").style(
63
+ margin=False,
64
+ rounded=(False, True, True, False),
65
+ )
66
+
67
+ gallery = gr.Gallery(
68
+ label="Generated images", show_label=False, elem_id="gallery"
69
+ ).style(grid=[1, 1], height="auto")
70
+
71
+ gr.Examples(examples=examples, fn=gen_image, inputs=text, outputs=gallery)
72
+ text.submit(gen_image, inputs=text, outputs=gallery)
73
+ btn.click(gen_image, inputs=text, outputs=gallery)
74
+
75
+ gr.Image('./assets/paddlenlp-preview.jpeg')
76
+ gr.HTML(read_content("assets/footer.html"))
77
+
78
+ block.queue(concurrency_count=5).launch(debug=True)