File size: 1,588 Bytes
08fee89 4c8b1bb 08fee89 4c8b1bb 08fee89 |
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 |
import gradio as gr
from datasets import load_dataset
import random
dataset = load_dataset("hugfaceguy0001/TangshiDalle3Images")['train']
demo = gr.Blocks(theme=gr.themes.Default(text_size="lg"))
N = 149
j = 3 * N + 1
poem_text = dataset[j]['prompt']
poem_info = dataset[j+1]['prompt']
poem_title = poem_info[poem_info.index("γ")+1:poem_info.index("γ")]
poem_author = poem_info[3:poem_info.index("η")]
poem = f" {poem_title} {poem_author}\n{poem_text}"
k = j - 1
poem_image = dataset[k]['image']
def random_poem():
global N
global j
global k
N = random.randint(0,230)
j = 3 * N + 1
poem_text = dataset[j]['prompt']
poem_info = dataset[j+1]['prompt']
poem_title = poem_info[poem_info.index("γ")+1:poem_info.index("γ")]
poem_author = poem_info[3:poem_info.index("η")]
poem = f" {poem_title} {poem_author}\n{poem_text}"
k = j + random.randint(-1,1)
poem_image = dataset[k]['image']
return poem_image,poem
def change_pic():
global k
rand_shift = random.choice([i for i in [-1,0,1] if i != k-j])
k = j + rand_shift
return dataset[k]['image']
with demo:
demo_title = gr.HTML('<h1 align="center">εθ―欣θ΅</h1>')
with gr.Row():
img = gr.Image(poem_image,scale=2)
poem_textbox = gr.Text(poem,scale=1,label="εθ―",show_copy_button=True)
with gr.Row():
btn1 = gr.Button('εζ’εθ―')
btn2 = gr.Button('εζ’ζεΎ')
btn1.click(random_poem,inputs=[],outputs=[img,poem_textbox])
btn2.click(change_pic,inputs=[],outputs=img)
demo.launch() |