Lifan-Z commited on
Commit
029e036
1 Parent(s): 5a22f7d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ gpt2 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x8-GPT2")
6
+
7
+ def poems(st):
8
+ poems=""
9
+ sequences = gpt2('<|endoftext|>'+st, max_length=50, do_sample=True, top_k=950, repetition_penalty=1.2, num_return_sequences=20, eos_token_id=0)
10
+ i=0
11
+ for seq in sequences:
12
+ if (len(seq.get('generated_text')) == 108):
13
+ poems = poems + (str(seq.get('generated_text'))[13:108]) + "\n\n"
14
+ i=i+1
15
+ if(i==6):
16
+ return(poems)
17
+
18
+ textbox = gr.Textbox(label="输入0~5个汉字作为诗的开头,以英文空格分隔。(Enter 0~5 Chinese characters to be the beginning of the poem, separated by a space. )", placeholder="白 日 依 山 尽", lines=11)
19
+
20
+ iface = gr.Interface(fn=poems, inputs=textbox, outputs="text")
21
+ iface.launch()