Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,76 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
"question-answering",
|
| 8 |
model="deepset/roberta-base-squad2",
|
| 9 |
tokenizer="deepset/roberta-base-squad2"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
paraphrase_model_name = 'tuner007/pegasus_paraphrase'
|
| 14 |
-
paraphrase_tokenizer = PegasusTokenizer.from_pretrained(paraphrase_model_name)
|
| 15 |
-
paraphrase_model = PegasusForConditionalGeneration.from_pretrained(paraphrase_model_name)
|
| 16 |
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
break
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
iface = gr.Interface(fn=find_answer_and_paraphrase, inputs=inputs, outputs=outputs, title="Question Answering and Paraphrasing", description="Enter the text and question to find the answer and paraphrase the sentence containing the answer.")
|
| 48 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
| 6 |
|
| 7 |
+
classifier = pipeline(
|
| 8 |
+
"question-answering",
|
|
|
|
| 9 |
model="deepset/roberta-base-squad2",
|
| 10 |
tokenizer="deepset/roberta-base-squad2"
|
| 11 |
)
|
| 12 |
|
| 13 |
+
model_name = 'tuner007/pegasus_paraphrase'
|
|
|
|
|
|
|
|
|
|
| 14 |
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 15 |
+
tokenizer3 = PegasusTokenizer.from_pretrained(model_name)
|
| 16 |
+
model3 = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def paraphrase(text_input):
|
| 20 |
+
question = "What is the Sri Lanka's rank on human development Index?"
|
| 21 |
+
prediction = classifier(
|
| 22 |
+
context=text_input,
|
| 23 |
+
question=question,
|
| 24 |
+
truncation=True,
|
| 25 |
+
max_length=512,
|
| 26 |
+
padding=True,
|
| 27 |
+
)
|
| 28 |
+
answer = prediction['answer']
|
| 29 |
+
answer_start = prediction['start']
|
| 30 |
+
answer_end = prediction['end']
|
| 31 |
+
context = text_input.split(".")
|
| 32 |
+
for i in range(len(context)):
|
| 33 |
+
if answer in context[i]:
|
| 34 |
+
sentence = context[i].strip() + "."
|
| 35 |
+
break
|
| 36 |
+
batch = tokenizer3([sentence],truncation=True,padding='longest',max_length=60, return_tensors="pt").to(torch_device)
|
| 37 |
+
translated = model3.generate(**batch,max_length=60,num_beams=10, num_return_sequences=1, temperature=1.5)
|
| 38 |
+
paraphrase = tokenizer3.batch_decode(translated, skip_special_tokens=True)[0]
|
| 39 |
+
return paraphrase
|
| 40 |
+
|
| 41 |
|
| 42 |
+
def qa_paraphrase(text_input):
|
| 43 |
+
question = "What is the Sri Lanka's rank on human development Index?"
|
| 44 |
+
prediction = classifier(
|
| 45 |
+
context=text_input,
|
| 46 |
+
question=question,
|
| 47 |
+
truncation=True,
|
| 48 |
+
max_length=512,
|
| 49 |
+
padding=True,
|
| 50 |
+
)
|
| 51 |
+
answer = prediction['answer']
|
| 52 |
+
answer_start = prediction['start']
|
| 53 |
+
answer_end = prediction['end']
|
| 54 |
+
context = text_input.split(".")
|
| 55 |
+
for i in range(len(context)):
|
| 56 |
+
if answer in context[i]:
|
| 57 |
+
sentence = context[i].strip() + "."
|
| 58 |
break
|
| 59 |
+
batch = tokenizer3([sentence],truncation=True,padding='longest',max_length=60, return_tensors="pt").to(torch_device)
|
| 60 |
+
translated = model3.generate(**batch,max_length=60,num_beams=10, num_return_sequences=1, temperature=1.5)
|
| 61 |
+
paraphrase = tokenizer3.batch_decode(translated, skip_special_tokens=True)[0]
|
| 62 |
+
return {"Answer": answer, "Paraphrased Sentence": paraphrase}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=qa_paraphrase,
|
| 67 |
+
inputs=gr.inputs.Textbox(label="Text Input"),
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.outputs.Textbox(label="Answer"),
|
| 70 |
+
gr.outputs.Textbox(label="Paraphrased Sentence")
|
| 71 |
+
],
|
| 72 |
+
title="Question Answering and Paraphrasing",
|
| 73 |
+
description="Answer a given question from a given text and paraphrase the sentence containing the answer."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
iface.launch()
|
|
|
|
|
|