File size: 4,824 Bytes
5700e44
 
0d07a2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
!pip install datasets evaluate transformers[sentencepiece]
!pip install gradio
import transformers
import gradio as gr
from transformers import pipeline

demo = gr.Blocks()

gpt2_5x4 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x4-GPT2")
gpt2_7x4 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style7x4-GPT2")
gpt2_5x8 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x8-GPT2")
gpt2_7x8 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style7x8-GPT2")


def poems_5x4(st):
  poems=""
  sequences = gpt2_5x4('<|endoftext|>'+st, max_length=26, do_sample=True, top_k=20, top_p=0.9, repetition_penalty=1.2, num_return_sequences=20, eos_token_id=0)
  i=0
  for seq in sequences:
    if (len(seq.get('generated_text')) == 60):
      poems = poems + (str(seq.get('generated_text'))[13:60]) + "\n\n"
      i=i+1
      if(i==6):
        return(poems)

def poems_7x4(st):
  poems=""
  sequences = gpt2_7x4('<|endoftext|>'+st, max_length=34, do_sample=True, top_k=20, top_p=0.9, repetition_penalty=1.2, num_return_sequences=20, eos_token_id=0)
  i=0
  for seq in sequences:
    if (len(seq.get('generated_text')) == 76):
      poems = poems + (str(seq.get('generated_text'))[13:76]) + "\n\n"
      i=i+1
      if(i==6):
        return(poems)


def poems_5x8(st):
  poems=""
  sequences = gpt2_5x8('<|endoftext|>'+st, max_length=50, do_sample=True, top_k=20, top_p=0.9, repetition_penalty=1.2, num_return_sequences=30, eos_token_id=0)
  i=0
  for seq in sequences:
    if (len(seq.get('generated_text')) == 108):
      poems = poems + (str(seq.get('generated_text'))[13:108]) + "\n\n"
      i=i+1
      if(i==6):
        return(poems)


def poems_7x8(st):
  poems=""
  sequences = gpt2_7x8('<|endoftext|>'+st, max_length=66, do_sample=True, top_k=20, top_p=0.9, repetition_penalty=1.2, num_return_sequences=40, eos_token_id=0)
  i=0
  for seq in sequences:
    if (len(seq.get('generated_text')) == 140):
      poems = poems + (str(seq.get('generated_text'))[13:140]) + "\n\n"
      i=i+1
      if(i==6):
        return(poems)



with demo:
    gr.Markdown("## **古诗辅助生成器**")
    gr.Markdown("### **生活不止眼前的苟且,还有诗和远方**")
    gr.Markdown("使用说明:五言古诗的平仄、用韵相当自由,也不限长短,这里只提供四句、八句两种。若想生成五言绝句、律诗,需要自己按平仄、用韵作出选择,再慢慢优化,逐句完成.\
     比如,要用'雨'字开头写一首五言律诗:先输入一个'雨'字,点击生成六首诗。假设只对第二首的头两句诗比较满意,则复制这两句作为输入。第二次再生成六首诗,这次对第五首的前六句满意,\
     再复制这六句作为输入。多次重复即可得到满意的结果。标点符号跟普通文字一样处理,需要注意的是末尾不要是空格。七言诗同样处理. --- Lf-Z 工作室")
    with gr.Tabs():
        with gr.TabItem("五言四句"):
            with gr.Row():
                text_input_5x4 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概7秒。", placeholder="白 日 依 山 尽", lines=1)
                text_output_5x4 = gr.Textbox()
            button_5x4 = gr.Button("提交")
        with gr.TabItem("五言八句"):
            with gr.Row():
                text_input_5x8 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概15秒。", placeholder="白 日 依 山 尽", lines=1)
                text_output_5x8 = gr.Textbox()
            button_5x8 = gr.Button("提交")
        with gr.TabItem("七言四句"):
            with gr.Row():
                text_input_7x4 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概10秒。", placeholder="山 明 水 净 夜 来 霜", lines=1)
                text_output_7x4 = gr.Textbox()
            button_7x4 = gr.Button("提交")
        with gr.TabItem("七言八句"):
            with gr.Row():
                text_input_7x8 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概30秒。", placeholder="山 明 水 净 夜 来 霜", lines=1)
                text_output_7x8 = gr.Textbox()
            button_7x8 = gr.Button("提交")



    button_5x4.click(poems_5x4, inputs=text_input_5x4, outputs=text_output_5x4)
    button_5x8.click(poems_5x8, inputs=text_input_5x8, outputs=text_output_5x8)
    button_7x4.click(poems_7x4, inputs=text_input_7x4, outputs=text_output_7x4)
    button_7x8.click(poems_7x8, inputs=text_input_7x8, outputs=text_output_7x8)

demo.launch(share=True)