Pranjal12345 commited on
Commit
b8cce11
·
1 Parent(s): 9df03e4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("potsawee/t5-large-generation-squad-QuestionAnswer")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained("potsawee/t5-large-generation-squad-QuestionAnswer")
7
+
8
+ def inference(input_text):
9
+ if input_text is None:
10
+ return "Please upload a text"
11
+
12
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
13
+
14
+ sentences = re.split(r'(?<=[.!?])', input_text)
15
+ question_answer_pairs = []
16
+
17
+ for i, sentence in enumerate(sentences):
18
+ input_ids_clone = tokenizer.encode(sentence, return_tensors="pt")
19
+ outputs = model.generate(input_ids_clone, max_length=100, num_return_sequences=1)
20
+ question_answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
+ question = question_answer.strip()
22
+ question_answer_pairs.append((f"Question:", question))
23
+
24
+ result = ''
25
+
26
+ for i in range(len(question_answer_pairs)):
27
+ if question_answer_pairs[i][1] == '':
28
+ break
29
+ question_part = question_answer_pairs[i][1].split("?")[0] + "?"
30
+ answer_part = question_answer_pairs[i][1].split("?")[1].strip()
31
+ result += f"Question: {question_part}\nAnswer: {answer_part}\n\n"
32
+
33
+ return result
34
+
35
+ title = "Question Answer Pairs Generator"
36
+
37
+ input_text = gr.Textbox(lines=4, label="Text:")
38
+
39
+ interface = gr.Interface(
40
+ fn=inference,
41
+ inputs=[input_text],
42
+ outputs= "text",
43
+ title=title,
44
+ )
45
+
46
+ interface.launch()