razakhan commited on
Commit
34c3391
1 Parent(s): b8b95e8

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ summarizer = pipeline('summarization')
5
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
6
+
7
+ model_name = "deepset/roberta-base-squad2"
8
+ nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)
9
+
10
+ examples = [
11
+ [ 'Question-Answer',
12
+ '',
13
+ 'The option to convert models between FARM and transformers gives freedom to the user and let people easily switch between frameworks.',
14
+ 'Why is model conversion important?'
15
+ ],
16
+ [ 'Question-Answer',
17
+ '',
18
+ "The Amazon rainforest is a moist broadleaf forest that covers most of the Amazon basin of South America",
19
+ "Which continent is the Amazon rainforest in?"
20
+ ],
21
+ [ 'Question-Answer',
22
+ '',
23
+ 'I am a Programmer.',
24
+ 'Who am I?'
25
+ ]
26
+ ]
27
+
28
+ def summarize_text(text):
29
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
30
+ summary = summary[0]['summary_text']
31
+ return summary
32
+
33
+ def question_answer(context, question):
34
+ QA_input = {
35
+ 'context': context,
36
+ 'question': question
37
+ }
38
+ res = nlp(QA_input)
39
+ return (res['answer'])
40
+
41
+ def home_func(model_choice, summ_text, qa_context, qa_question):
42
+ if model_choice=="Text Summarizer":
43
+ if summ_text == "":
44
+ return "Input correct text to be summarized"
45
+ return summarize_text(summ_text)
46
+ elif model_choice=="Question-Answer":
47
+ if qa_context == "" or qa_question == "":
48
+ return "Choose correct Context and associated questions"
49
+
50
+ return question_answer(qa_context, qa_question)
51
+
52
+ iface = gr.Interface(fn = home_func,
53
+ inputs = [gr.inputs.Dropdown(["Text Summarizer", "Question-Answer"], type="value"),
54
+ gr.inputs.Textbox(lines=5, placeholder="Enter your text here...", label="Text to be summarized"),
55
+ gr.inputs.Textbox(lines=5, placeholder="Choose from examples", label="Context"),
56
+ gr.inputs.Textbox(lines=5, placeholder="Choose from examples", label="Question")],
57
+ outputs="text",
58
+ examples=examples)
59
+
60
+ iface.launch()