ethanrom commited on
Commit
35d1b2d
·
1 Parent(s): d90d622

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -38
app.py CHANGED
@@ -1,48 +1,76 @@
1
  import gradio as gr
 
2
  import torch
3
- from transformers import pipeline, PegasusForConditionalGeneration, PegasusTokenizer
 
4
 
5
- # Q&A model
6
- qa_classifier = pipeline(
7
- "question-answering",
8
  model="deepset/roberta-base-squad2",
9
  tokenizer="deepset/roberta-base-squad2"
10
  )
11
 
12
- # paraphrase model
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 find_answer_and_paraphrase(text_input, question):
19
- # Find the answer
20
- answer = qa_classifier(context=text_input, question=question)["answer"]
21
-
22
- # Find the sentence containing the answer
23
- sentences = text_input.split(".")
24
- sentence_with_answer = ""
25
- for sentence in sentences:
26
- if answer in sentence:
27
- sentence_with_answer = sentence.strip() + "."
 
 
 
 
 
 
28
  break
29
-
30
- # Paraphrase the sentence containing the answer
31
- input_text = "paraphrase: " + sentence_with_answer
32
- num_return_sequences = 1
33
- num_beams = 5
34
- batch = paraphrase_tokenizer([input_text], truncation=True, padding='longest', max_length=60, return_tensors="pt").to(torch_device)
35
- translated = paraphrase_model.generate(**batch, max_length=60, num_beams=num_beams, num_return_sequences=num_return_sequences, temperature=1.5)
36
- paraphrase_text = paraphrase_tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
37
-
38
- return {"Answer": answer, "Paraphrase": paraphrase_text}
39
-
40
-
41
- inputs = [
42
- gr.inputs.Textbox(label="Text Input"),
43
- gr.inputs.Textbox(label="Question")
44
- ]
45
- outputs = [gr.outputs.Textbox(label="Answer"), gr.outputs.Textbox(label="Paraphrase")]
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()