File size: 941 Bytes
029e036
 
 
 
 
 
 
 
1808e26
029e036
 
 
 
 
 
 
 
1808e26
029e036
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import transformers
import gradio as gr
from transformers import pipeline

gpt2 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x8-GPT2")

def poems(st):
  poems=""
  sequences = gpt2('<|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)

textbox = gr.Textbox(label="输入0~5个汉字作为诗的开头,以英文空格分隔,最后一个字后面不用空格。一次生成六首诗,大概耗时15秒。(Enter 0~5 Chinese characters to be the beginning of the poem, separated by a space. )", placeholder="白 日 依 山 尽", lines=17)

iface = gr.Interface(fn=poems, inputs=textbox, outputs="text")
iface.launch()