ansfarooq7 commited on
Commit
91b0256
1 Parent(s): 5fb5066

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -4,6 +4,7 @@ import wikipedia
4
  import re
5
  import random
6
  import nltk
 
7
  from aitextgen import aitextgen
8
  nltk.download('cmudict')
9
 
@@ -49,7 +50,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,7 +80,7 @@ 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
@@ -107,12 +108,16 @@ def get_rhyming_line(prompt, rhyming_word, inputs_len):
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:
@@ -190,7 +195,7 @@ def generate(topic, wiki=True):
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)
@@ -200,9 +205,8 @@ def compare_summaries(topic):
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. Please be patient as it can take up to a minute to generate both limericks."
206
  article = '<center><big><strong>Limerick Generation</strong></big></center>'\
207
  '<center><strong>By Ans Farooq</strong></center>'\
208
  '<center><small>Level 4 Individual Project</small></center>'\
@@ -220,14 +224,14 @@ article = '<center><big><strong>Limerick Generation</strong></big></center>'\
220
  'cohesive limericks automatically, using two different methods. The '\
221
  'results of this project are to be evaluated through human comparisons.'
222
 
223
- input = gr.inputs.Textbox(label='Topic')
224
- output1 = gr.outputs.Textbox(label='Method A')
225
- output2 = gr.outputs.Textbox(label='Method B')
226
 
227
  interface = gr.Interface(
228
  fn=compare_summaries,
229
- inputs=input,
230
- outputs=[output1, output2],
231
  title="Text-generation with rhyme and rhythm",
232
  layout="horizontal",
233
  theme="peach",
 
4
  import re
5
  import random
6
  import nltk
7
+ import gradio as gr
8
  from aitextgen import aitextgen
9
  nltk.download('cmudict')
10
 
 
50
  def get_inputs_length(input):
51
  input_ids = gpt2_tokenizer(input)['input_ids']
52
  return len(input_ids)
53
+
54
  def get_prediction(sent):
55
 
56
  token_ids = roberta_tokenizer.encode(sent, return_tensors='pt')
 
80
  best_guess = best_guess+" "+j[0]
81
 
82
  return best_guess
83
+
84
  def get_line(prompt, inputs_len):
85
  line = gpt2_model.generate_one(prompt=prompt + ".", max_length=inputs_len + 7, min_length=4)[len(prompt)+2:]
86
  return line
 
108
  return final_sentence
109
 
110
  def gpt2_summary(topic):
111
+ return gpt2_model.generate_one(prompt=f"Here is some information about {topic}.", top_k=50, top_p=0.95, min_length=200)
112
+
113
  def generate(topic, wiki=True):
114
  if wiki:
115
  try:
116
+ topic_search = wikipedia.search(topic, results=3)
117
+ topic_summary = remove_punctuation(wikipedia.summary(topic_search[0], auto_suggest=False))
118
+ except wikipedia.DisambiguationError as e:
119
+ page = e.options[0]
120
+ topic_summary = remove_punctuation(wikipedia.summary(page))
121
  except:
122
  return(f"Method A struggled to find information about {topic}, please try a different topic!")
123
  else:
 
195
  print(limerick)
196
 
197
  return limerick
198
+
199
  def compare_summaries(topic):
200
  wiki_limerick = generate(topic)
201
  gpt2_limerick = generate(topic, wiki=False)
 
205
  print(output1 + "\n" + output2)
206
 
207
  return output1, output2
208
+
209
+ description = "Generates limericks (five-line poems with a rhyme scheme of AABBA) via two different methods"
 
210
  article = '<center><big><strong>Limerick Generation</strong></big></center>'\
211
  '<center><strong>By Ans Farooq</strong></center>'\
212
  '<center><small>Level 4 Individual Project</small></center>'\
 
224
  'cohesive limericks automatically, using two different methods. The '\
225
  'results of this project are to be evaluated through human comparisons.'
226
 
227
+ gr_input = gr.inputs.Textbox(label='Topic')
228
+ gr_output1 = gr.outputs.Textbox(label='Method A')
229
+ gr_output2 = gr.outputs.Textbox(label='Method B')
230
 
231
  interface = gr.Interface(
232
  fn=compare_summaries,
233
+ inputs=gr_input,
234
+ outputs=[gr_output1, gr_output2],
235
  title="Text-generation with rhyme and rhythm",
236
  layout="horizontal",
237
  theme="peach",