Technozam commited on
Commit
f74e052
1 Parent(s): c7b4ba4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -45
app.py CHANGED
@@ -196,58 +196,83 @@ def get_question(context,answer,model,tokenizer):
196
 
197
  import gradio as gr
198
  import random
 
199
 
200
- context = gr.Textbox(lines=10, placeholder="Enter paragraph/content here...", label="Text")
 
201
  total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
202
- subject = gr.Textbox(placeholder="Enter subject/title here...", label="Text")
 
203
  output = gr.Markdown( label="Question and Answers")
204
 
205
 
206
  def generate_question_text(context,subject,total):
207
- summary_text = summarizer(context,summary_model,summary_tokenizer)
208
- for wrp in wrap(summary_text, 150):
209
- print (wrp)
210
- np = get_keywords(context,summary_text,total)
211
- random.shuffle(np)
212
- print ("\n\nNoun phrases",np)
213
-
214
- output="<b>Select/Match the correct answers to the given questions.</b><br/>"
215
- answerlist=[]
216
-
217
- for answer in np:
218
- answerlist.append(answer)
219
- random.shuffle(answerlist)
220
- print(answerlist)
221
 
222
- for answer in answerlist:
223
- output = output + "<b style='margin-right:30px;'>" + " "*5 +answer.capitalize()+ "</b>"
224
- output = output + "<br/>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
- i=1
227
- for answer in np:
228
- ques = get_question(summary_text,answer,question_model,question_tokenizer)
229
- # output= output + ques + "\n" + "Ans: "+answer.capitalize() + "\n\n"
230
- output = output + "<b> Q "+ str(i) + ") " + ques + "</b><br/>"
231
- i += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
- output = output + "<br/><b>" + "Correct Answer Key:</b><br/>"
 
 
234
 
235
- i=1
236
- for answer in np:
237
- # output= output + ques + "\n" + "Ans: "+answer.capitalize() + "\n\n"
238
- output = output + "<b style='color:green;'>Ans "+ str(i) + ": " +answer.capitalize()+ "</b>"
239
- # random.shuffle(output)
240
- output = output + "<br/>"
241
- i += 1
242
 
243
-
244
- return output
 
245
 
246
  iface = gr.Interface(
247
  fn=generate_question_text,
248
- inputs=[context,subject, total],
249
- outputs=output,
250
- allow_flagging="manual",flagging_options=["Save Data"])
251
 
252
  # iface.launch(debug=True, share=True)
253
 
@@ -301,27 +326,50 @@ def filecreate(x,subject,total):
301
  with open(x.name) as fo:
302
  text = fo.read()
303
  # print(text)
304
- generated = generate_question(text,subject, total)
 
 
 
 
 
 
 
 
 
 
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
- return generated
308
 
309
- # filecreate(file,2)
310
 
311
  import gradio as gr
312
 
313
  context = gr.HTML(label="Text")
314
- file = gr.File()
315
  total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
316
- subject = gr.Textbox(placeholder="Enter subject/title here...", label="Text")
317
 
318
  fface = gr.Interface(
319
  fn=filecreate,
320
  inputs=[file,subject,total],
321
  outputs=context,
322
- allow_flagging="manual",flagging_options=["Save Data"])
 
323
 
324
  # fface.launch(debug=True, share=True)
325
 
326
  demo = gr.TabbedInterface([iface, fface], ["Text", "Upload File"])
327
- demo.launch(debug=True)
 
196
 
197
  import gradio as gr
198
  import random
199
+ import re
200
 
201
+
202
+ context = gr.Textbox(lines=10, placeholder="Enter paragraph/content here...", label="Enter your content (words input must be more than 150 words).")
203
  total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
204
+ subject = gr.Textbox(placeholder="Enter subject/title here...", label="Enter your title (title must contain 1 word)")
205
+
206
  output = gr.Markdown( label="Question and Answers")
207
 
208
 
209
  def generate_question_text(context,subject,total):
210
+
211
+ words_text = len(re.findall(r'\w+', context))
212
+ words_subject = len(re.findall(r'\w+', subject))
 
 
 
 
 
 
 
 
 
 
 
213
 
214
+ if (words_text < 150):
215
+ raise gr.Error("Invalid Input (Words limit must be more than 150 words).")
216
+ # print("Number of words:", words)
217
+
218
+ elif (words_subject < 1):
219
+ raise gr.Error("Invalid Input (Title must be one or more than one word).")
220
+
221
+ else:
222
+ summary_text = summarizer(context,summary_model,summary_tokenizer)
223
+ for wrp in wrap(summary_text, 150):
224
+ print (wrp)
225
+ np = get_keywords(context,summary_text,total)
226
+ random.shuffle(np)
227
+ print ("\n\nNoun phrases",np)
228
+
229
+ output="<b>Select/Match the correct answers to the given questions.</b><br/>"
230
+ answerlist=[]
231
+
232
+ for answer in np:
233
+ answerlist.append(answer)
234
+ random.shuffle(answerlist)
235
+ print(answerlist)
236
+
237
+ for answer in answerlist:
238
+ output = output + "<b style='margin-right:30px;'>" + " "*5 +answer.capitalize()+ "</b>"
239
+ output = output + "<br/>"
240
 
241
+ i=1
242
+ for answer in np:
243
+ ques = get_question(summary_text,answer,question_model,question_tokenizer)
244
+ # output= output + ques + "\n" + "Ans: "+answer.capitalize() + "\n\n"
245
+ output = output + "<b> Q "+ str(i) + ") " + ques + "</b><br/>"
246
+ i += 1
247
+
248
+ output = output + "<br/><b>" + "Correct Answer Key:</b><br/>"
249
+
250
+ i=1
251
+ for answer in np:
252
+ # output= output + ques + "\n" + "Ans: "+answer.capitalize() + "\n\n"
253
+ output = output + "<b style='color:green;'>Ans "+ str(i) + ": " +answer.capitalize()+ "</b>"
254
+ # random.shuffle(output)
255
+ output = output + "<br/>"
256
+ i += 1
257
+
258
+ # mycursor = mydb.cursor()
259
+ # timedate = datetime.datetime.now()
260
 
261
+ # sql = "INSERT INTO matchtexts (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
262
+ # val = (subject, context, output, timedate)
263
+ # mycursor.execute(sql, val)
264
 
265
+ # mydb.commit()
 
 
 
 
 
 
266
 
267
+ # print(mycursor.rowcount, "record inserted.")
268
+
269
+ return output
270
 
271
  iface = gr.Interface(
272
  fn=generate_question_text,
273
+ inputs=[context,subject, total], outputs=output,
274
+ # css=".gradio-container {background-image: url('file=blue.jpg')}",
275
+ allow_flagging="never",flagging_options=["Save Data"])
276
 
277
  # iface.launch(debug=True, share=True)
278
 
 
326
  with open(x.name) as fo:
327
  text = fo.read()
328
  # print(text)
329
+
330
+ words_text = len(re.findall(r'\w+', text))
331
+ words_subject = len(re.findall(r'\w+', subject))
332
+
333
+
334
+ if (words_text < 150):
335
+ raise gr.Error("Invalid Input (Words limit must be more than 150 words).")
336
+ # print("Number of words:", words)
337
+
338
+ elif (words_subject < 1):
339
+ raise gr.Error("Invalid Input (Title must be one or more than one word).")
340
 
341
+ else:
342
+ generated = generate_question(text,subject, total)
343
+
344
+ # mycursor = mydb.cursor()
345
+ # timedate= datetime.datetime.now()
346
+
347
+ # sql = "INSERT INTO matchfiles (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
348
+ # val = (subject, text, generated, timedate)
349
+ # mycursor.execute(sql, val)
350
+
351
+ # mydb.commit()
352
+
353
+ # print(mycursor.rowcount, "record inserted.")
354
 
355
+ return generated
356
 
 
357
 
358
  import gradio as gr
359
 
360
  context = gr.HTML(label="Text")
361
+ file = gr.File(label="Upload your file (File must contain more than 150 words).")
362
  total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
363
+ subject = gr.Textbox(placeholder="Enter subject/title here...", label="Enter your title (title must contain 1 word).")
364
 
365
  fface = gr.Interface(
366
  fn=filecreate,
367
  inputs=[file,subject,total],
368
  outputs=context,
369
+ # css=".gradio-container {background-image: url('file=blue.jpg')}",
370
+ allow_flagging="never",flagging_options=["Save Data"])
371
 
372
  # fface.launch(debug=True, share=True)
373
 
374
  demo = gr.TabbedInterface([iface, fface], ["Text", "Upload File"])
375
+ demo.launch(debug=True, show_api=False)