Viksluij commited on
Commit
339165d
1 Parent(s): 15f0190

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pip install gradio happytransformer
2
+
3
+ import gradio as gr
4
+ from transformers import pipeline
5
+ from happytransformer import HappyTextToText, TTSettings
6
+
7
+ def translate(language_1, language_2, message):
8
+ translator = pipeline(f"translation", model="Helsinki-NLP/opus-mt-{}-{}".format(language_1, language_2))
9
+ output = translator(message)
10
+ return output[0]['translation_text']
11
+
12
+ def summarize(text):
13
+ summerizer = pipeline("summarization", model="facebook/bart-large-cnn")
14
+ output = summerizer(text)
15
+ return output[0]['summary_text']
16
+
17
+ def question(question, context):
18
+ question_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
19
+ output = question_model({
20
+ 'question': question,
21
+ 'context' : context
22
+ })
23
+ return output['answer']
24
+
25
+
26
+ def grammar(user_input):
27
+ grammar_model = HappyTextToText("T5", "vennify/t5-base-grammar-correction")
28
+ args = TTSettings(num_beams=5, min_length=1)
29
+ output = grammar_model.generate_text('grammar: ' + user_input, args=args)
30
+ return output.text
31
+
32
+
33
+ def mask(text):
34
+ mask_model = pipeline("fill-mask", model="google-bert/bert-base-uncased")
35
+ output = mask_model(text)
36
+ return output[0]['sequence']
37
+
38
+ def multi_model(text):
39
+
40
+ mask_model = mask(text)
41
+
42
+ summerize_model = summarize(mask_model)
43
+
44
+ question_model = question('Is this text about politics?', summerize_model)
45
+
46
+ translation_model = translate('en', 'nl', question_model)
47
+
48
+ grammar_model = grammar(translation_model)
49
+
50
+ return grammar_model
51
+
52
+
53
+
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("Technology 2 - Ai interfaces")
56
+
57
+ #tab single model
58
+ with gr.Tab("Single-Models"):
59
+ with gr.Row(equal_height=True):
60
+
61
+ #column left (1)
62
+ with gr.Column(scale=1):
63
+
64
+ choice1 = gr.Dropdown(
65
+ choices=["nl", "fr", "en"],
66
+ label="Select an option")
67
+
68
+ choice2 = gr.Dropdown(
69
+ choices=["nl", "fr", "en"],
70
+ label="Select an option")
71
+
72
+ textbox = gr.Textbox(label="message")
73
+
74
+ translate_btn = gr.Button("Translate")
75
+
76
+ translated_box = gr.Textbox()
77
+
78
+ translate_btn.click(fn=translate,
79
+ inputs=[choice1, choice2, textbox],
80
+ outputs=translated_box )
81
+
82
+ #column right (2)
83
+ with gr.Column(scale=1):
84
+ textbox_summary = gr.Textbox(label="text you want to summarize")
85
+ textbox_summary_output = gr.Textbox()
86
+ summary_btn = gr.Button('summarize')
87
+
88
+ summary_btn.click(fn=summarize,
89
+ inputs=textbox_summary,
90
+ outputs=textbox_summary_output)
91
+
92
+ with gr.Column(scale=1):
93
+ textbox_question = gr.Textbox(label = 'question')
94
+ textbox_context = gr.Textbox(label = 'context')
95
+ textbox_question_output = gr.Textbox()
96
+ question_btn = gr.Button("Get your Answer")
97
+
98
+ question_btn.click(fn=question,
99
+ inputs=[textbox_question, textbox_context],
100
+ outputs = textbox_question_output)
101
+
102
+ with gr.Column(scale=1):
103
+ textbox_mask = gr.Textbox(label="Give me a sentance and use the word [MASK] to predict what will go there")
104
+ mask_button = gr.Button("Generate")
105
+ textbox_mask_output = gr.Textbox()
106
+
107
+ mask_button.click(fn=mask,
108
+ inputs = textbox_mask,
109
+ outputs = textbox_mask_output)
110
+
111
+ with gr.Column(scale=1):
112
+ textbox_grammar = gr.Textbox(label="I will correct your grammar mistakes.")
113
+ grammar_button = gr.Button("Fix grammar")
114
+ textbox_grammar_output = gr.Textbox()
115
+
116
+ grammar_button.click(fn=grammar,
117
+ inputs=textbox_grammar,
118
+ outputs=textbox_grammar_output)
119
+
120
+
121
+
122
+
123
+
124
+
125
+ #tab mulit model
126
+ with gr.Tab("Multi-Models"):
127
+ textblock = gr.Textbox(label="give me a sentence and replace some words with [MASK] and i will summerize and translate it for your")
128
+ btn1 = gr.Button("START")
129
+ response = gr.Textbox()
130
+
131
+ btn1.click(fn=multi_model, inputs = textblock, outputs=response)
132
+
133
+
134
+ demo.launch(debug=True)