Lifan-Z commited on
Commit
9b68e36
·
verified ·
1 Parent(s): 61e2bef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -96
app.py CHANGED
@@ -1,96 +1 @@
1
- import transformers
2
- import gradio as gr
3
- from transformers import pipeline
4
-
5
- demo = gr.Blocks()
6
-
7
- gpt2_5x4 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x4-GPT2")
8
- gpt2_7x4 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style7x4-GPT2")
9
- gpt2_5x8 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style5x8-GPT2")
10
- gpt2_7x8 = pipeline('text-generation', model = "Lifan-Z/Chinese-Classic-Poem-Generator-style7x8-GPT2")
11
-
12
-
13
- def poems_5x4(st):
14
- poems=""
15
- 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)
16
- i=0
17
- for seq in sequences:
18
- if (len(seq.get('generated_text')) == 60):
19
- poems = poems + (str(seq.get('generated_text'))[13:60]) + "\n\n"
20
- i=i+1
21
- if(i==6):
22
- return(poems)
23
-
24
- def poems_7x4(st):
25
- poems=""
26
- 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)
27
- i=0
28
- for seq in sequences:
29
- if (len(seq.get('generated_text')) == 76):
30
- poems = poems + (str(seq.get('generated_text'))[13:76]) + "\n\n"
31
- i=i+1
32
- if(i==6):
33
- return(poems)
34
-
35
-
36
- def poems_5x8(st):
37
- poems=""
38
- 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)
39
- i=0
40
- for seq in sequences:
41
- if (len(seq.get('generated_text')) == 108):
42
- poems = poems + (str(seq.get('generated_text'))[13:108]) + "\n\n"
43
- i=i+1
44
- if(i==6):
45
- return(poems)
46
-
47
-
48
- def poems_7x8(st):
49
- poems=""
50
- 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)
51
- i=0
52
- for seq in sequences:
53
- if (len(seq.get('generated_text')) == 140):
54
- poems = poems + (str(seq.get('generated_text'))[13:140]) + "\n\n"
55
- i=i+1
56
- if(i==6):
57
- return(poems)
58
-
59
-
60
-
61
- with demo:
62
- gr.Markdown("## **古诗辅助生成器**")
63
- gr.Markdown("### **生活不止眼前的苟且,还有诗和远方**")
64
- gr.Markdown("使用说明:五言古诗的平仄、用韵相当自由,也不限长短,这里只提供四句、八句两种。若想生成五言绝句、律诗,需要自己按平仄、用韵作出选择,再慢慢优化,逐句完成.\
65
- 比如,要用'雨'字开头写一首五言律诗:先输入一个'雨'字,点击生成六首诗。假设只对第二首的头两句诗比较满意,则复制这两句作为输入。第二次再生成六首诗,这次对第五首的前六句满意,\
66
- 再复制这六句作为输入。多次重复即可得到满意的结果。标点符号跟普通文字一样处理,需要注意的是末尾不要是空格。七言诗同样处理. --- Lf-Z 工作室")
67
- with gr.Tabs():
68
- with gr.TabItem("五言四句"):
69
- with gr.Row():
70
- text_input_5x4 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概7秒。", placeholder="白 日 依 山 尽", lines=1)
71
- text_output_5x4 = gr.Textbox()
72
- button_5x4 = gr.Button("提交")
73
- with gr.TabItem("五言八句"):
74
- with gr.Row():
75
- text_input_5x8 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概15秒。", placeholder="白 日 依 山 尽", lines=1)
76
- text_output_5x8 = gr.Textbox()
77
- button_5x8 = gr.Button("提交")
78
- with gr.TabItem("七言四句"):
79
- with gr.Row():
80
- text_input_7x4 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概10秒。", placeholder="山 明 水 净 夜 来 霜", lines=1)
81
- text_output_7x4 = gr.Textbox()
82
- button_7x4 = gr.Button("提交")
83
- with gr.TabItem("七言八句"):
84
- with gr.Row():
85
- text_input_7x8 = gr.Textbox(label="输入诗的开头文字,以英文空格分隔,末尾不用空格。一次六首,大概30秒。", placeholder="山 明 水 净 夜 来 霜", lines=1)
86
- text_output_7x8 = gr.Textbox()
87
- button_7x8 = gr.Button("提交")
88
-
89
-
90
-
91
- button_5x4.click(poems_5x4, inputs=text_input_5x4, outputs=text_output_5x4)
92
- button_5x8.click(poems_5x8, inputs=text_input_5x8, outputs=text_output_5x8)
93
- button_7x4.click(poems_7x4, inputs=text_input_7x4, outputs=text_output_7x4)
94
- button_7x8.click(poems_7x8, inputs=text_input_7x8, outputs=text_output_7x8)
95
-
96
- demo.launch(share=True)
 
1
+ if