hugfaceguy0001 commited on
Commit
08fee89
β€’
1 Parent(s): e20d622

Add application file

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import load_dataset
3
+ import random
4
+
5
+ dataset = load_dataset("hugfaceguy0001/TangshiDalle3Images")['train']
6
+ demo = gr.Blocks(theme=gr.themes.Default(text_size="lg"))
7
+ N = random.randint(0,230)
8
+ j = 3 * N + 1
9
+ poem_text = dataset[j]['prompt']
10
+ poem_info = dataset[j+1]['prompt']
11
+ poem_title = poem_info[poem_info.index("γ€Š")+1:poem_info.index("》")]
12
+ poem_author = poem_info[3:poem_info.index("ηš„")]
13
+ poem = f" {poem_title} {poem_author}\n{poem_text}"
14
+ k = j + random.randint(-1,1)
15
+ poem_image = dataset[k]['image']
16
+ def random_poem():
17
+ global N
18
+ global j
19
+ global k
20
+ N = random.randint(0,230)
21
+ j = 3 * N + 1
22
+ poem_text = dataset[j]['prompt']
23
+ poem_info = dataset[j+1]['prompt']
24
+ poem_title = poem_info[poem_info.index("γ€Š")+1:poem_info.index("》")]
25
+ poem_author = poem_info[3:poem_info.index("ηš„")]
26
+ poem = f" {poem_title} {poem_author}\n{poem_text}"
27
+ k = j + random.randint(-1,1)
28
+ poem_image = dataset[k]['image']
29
+ return poem_image,poem
30
+
31
+ def change_pic():
32
+ global k
33
+ rand_shift = random.choice([i for i in [-1,0,1] if i != k-j])
34
+ k = j + rand_shift
35
+ return dataset[k]['image']
36
+
37
+ with demo:
38
+ demo_title = gr.HTML('<h1 align="center">唐诗欣衏</h1>')
39
+ with gr.Row():
40
+ img = gr.Image(poem_image,scale=2)
41
+ poem_textbox = gr.Text(poem,scale=1,label="唐诗",show_copy_button=True)
42
+ with gr.Row():
43
+ btn1 = gr.Button('εˆ‡ζ’ε”θ―—')
44
+ btn2 = gr.Button('εˆ‡ζ’ζ’ε›Ύ')
45
+ btn1.click(random_poem,inputs=[],outputs=[img,poem_textbox])
46
+ btn2.click(change_pic,inputs=[],outputs=img)
47
+ demo.launch()