Spaces:
Sleeping
Sleeping
ansfarooq7
commited on
Commit
•
96ac6d4
1
Parent(s):
6acfcb8
Update app.py
Browse files
app.py
CHANGED
@@ -49,7 +49,7 @@ def get_rhymes(inp, level):
|
|
49 |
def get_inputs_length(input):
|
50 |
input_ids = gpt2_tokenizer(input)['input_ids']
|
51 |
return len(input_ids)
|
52 |
-
|
53 |
def get_prediction(sent):
|
54 |
|
55 |
token_ids = roberta_tokenizer.encode(sent, return_tensors='pt')
|
@@ -79,13 +79,16 @@ def get_prediction(sent):
|
|
79 |
best_guess = best_guess+" "+j[0]
|
80 |
|
81 |
return best_guess
|
82 |
-
|
83 |
def get_line(prompt, inputs_len):
|
84 |
line = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 7, min_length=4)[len(prompt)+2:]
|
85 |
return line
|
86 |
|
87 |
def get_rhyming_line(prompt, rhyming_word, inputs_len):
|
88 |
gpt2_sentence = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 4, min_length=2)[len(prompt)+2:]
|
|
|
|
|
|
|
89 |
gpt2_sentence = gpt2_sentence.replace("\n", "")
|
90 |
print(f"\nGetting rhyming line starting with '{gpt2_sentence}' and ending with rhyming word '{rhyming_word}'")
|
91 |
sentence = gpt2_sentence + " ___ ___ ___ " + rhyming_word
|
@@ -104,11 +107,14 @@ def get_rhyming_line(prompt, rhyming_word, inputs_len):
|
|
104 |
return final_sentence
|
105 |
|
106 |
def gpt2_summary(topic):
|
107 |
-
return gpt2_model.generate_one(prompt=f"Here is some information about {topic}", top_k=100, top_p=0.95)
|
108 |
-
|
109 |
def generate(topic, wiki=True):
|
110 |
if wiki:
|
111 |
-
|
|
|
|
|
|
|
112 |
else:
|
113 |
topic_summary = remove_punctuation(gpt2_summary(topic))
|
114 |
|
@@ -184,25 +190,44 @@ def generate(topic, wiki=True):
|
|
184 |
print(limerick)
|
185 |
|
186 |
return limerick
|
187 |
-
|
188 |
def compare_summaries(topic):
|
189 |
wiki_limerick = generate(topic)
|
190 |
gpt2_limerick = generate(topic, wiki=False)
|
191 |
|
192 |
-
output1 =
|
193 |
-
|
194 |
-
|
195 |
-
output2 += gpt2_limerick
|
196 |
|
197 |
return output1, output2
|
198 |
-
|
199 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
interface = gr.Interface(
|
202 |
fn=compare_summaries,
|
203 |
-
inputs=
|
204 |
-
outputs=[
|
205 |
title="Text-generation with rhyme and rhythm",
|
206 |
layout="horizontal",
|
207 |
-
theme="peach"
|
|
|
|
|
208 |
interface.launch(debug=True)
|
|
|
49 |
def get_inputs_length(input):
|
50 |
input_ids = gpt2_tokenizer(input)['input_ids']
|
51 |
return len(input_ids)
|
52 |
+
|
53 |
def get_prediction(sent):
|
54 |
|
55 |
token_ids = roberta_tokenizer.encode(sent, return_tensors='pt')
|
|
|
79 |
best_guess = best_guess+" "+j[0]
|
80 |
|
81 |
return best_guess
|
82 |
+
|
83 |
def get_line(prompt, inputs_len):
|
84 |
line = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 7, min_length=4)[len(prompt)+2:]
|
85 |
return line
|
86 |
|
87 |
def get_rhyming_line(prompt, rhyming_word, inputs_len):
|
88 |
gpt2_sentence = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 4, min_length=2)[len(prompt)+2:]
|
89 |
+
while len(gpt2_sentence) == 0:
|
90 |
+
gpt2_sentence = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 4, min_length=2)[len(prompt)+2:]
|
91 |
+
|
92 |
gpt2_sentence = gpt2_sentence.replace("\n", "")
|
93 |
print(f"\nGetting rhyming line starting with '{gpt2_sentence}' and ending with rhyming word '{rhyming_word}'")
|
94 |
sentence = gpt2_sentence + " ___ ___ ___ " + rhyming_word
|
|
|
107 |
return final_sentence
|
108 |
|
109 |
def gpt2_summary(topic):
|
110 |
+
return gpt2_model.generate_one(prompt=f"Here is some information about {topic}.", top_k=100, top_p=0.95, min_length=200)
|
111 |
+
|
112 |
def generate(topic, wiki=True):
|
113 |
if wiki:
|
114 |
+
try:
|
115 |
+
topic_summary = remove_punctuation(wikipedia.summary(topic))
|
116 |
+
except:
|
117 |
+
return(f"Method A struggled to find information about {topic}, please try a different topic!")
|
118 |
else:
|
119 |
topic_summary = remove_punctuation(gpt2_summary(topic))
|
120 |
|
|
|
190 |
print(limerick)
|
191 |
|
192 |
return limerick
|
193 |
+
|
194 |
def compare_summaries(topic):
|
195 |
wiki_limerick = generate(topic)
|
196 |
gpt2_limerick = generate(topic, wiki=False)
|
197 |
|
198 |
+
output1 = wiki_limerick
|
199 |
+
output2 = gpt2_limerick
|
200 |
+
print(output1 + "\n" + output2)
|
|
|
201 |
|
202 |
return output1, output2
|
203 |
+
|
204 |
import gradio as gr
|
205 |
+
description = "Generates limericks (five-line poems with a rhyme scheme of AABBA) via two different methods"
|
206 |
+
article = '<center><big><strong>Limerick Generation</strong></big></center>'\
|
207 |
+
'<center>By Ans Farooq</center>'\
|
208 |
+
'<strong>Description</strong><br>'\
|
209 |
+
'Recent advances in natural language processing (NLP) have shown '\
|
210 |
+
'incredible promise at generating human-quality language. Poetry '\
|
211 |
+
'presents an additional challenge as it often relies on rhyme and '\
|
212 |
+
'rhythm of language. Factoring these in presents an interesting '\
|
213 |
+
'challenge to new deep learning-based methods. This text-generation '\
|
214 |
+
'project examines the use of transformer-based deep learning methods '\
|
215 |
+
'and the addition of constraints for length, rhyme and rhythm given '\
|
216 |
+
'example words to seed a poem. This interface allows you to produce two '\
|
217 |
+
'cohesive limericks automatically, using two different methods. The '\
|
218 |
+
'results of this project are to be evaluated through human comparisons.'
|
219 |
+
|
220 |
+
input = gr.inputs.Textbox(label='Topic')
|
221 |
+
output1 = gr.outputs.Textbox(label='Method A')
|
222 |
+
output2 = gr.outputs.Textbox(label='Method B')
|
223 |
|
224 |
interface = gr.Interface(
|
225 |
fn=compare_summaries,
|
226 |
+
inputs=input,
|
227 |
+
outputs=[output1, output2],
|
228 |
title="Text-generation with rhyme and rhythm",
|
229 |
layout="horizontal",
|
230 |
+
theme="peach",
|
231 |
+
description=description,
|
232 |
+
article=article)
|
233 |
interface.launch(debug=True)
|