maximuspowers commited on
Commit
090b1b7
1 Parent(s): 60ec004

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import nlpaug.augmenter.word as naw
3
+
4
+ # augmentations
5
+ def augment_text(text, aug_type, model_type=None, model_path=None, aug_p=0.25, aug_max=3):
6
+ if aug_type == 'Word Embedding Substitution':
7
+ aug = naw.WordEmbsAug(
8
+ model_type=model_type,
9
+ model_path=model_path,
10
+ action="substitute",
11
+ aug_p=aug_p
12
+ )
13
+ elif aug_type == 'Contextual Insertion':
14
+ aug = naw.ContextualWordEmbsAug(
15
+ model_path='bert-base-uncased',
16
+ action="insert",
17
+ aug_p=aug_p
18
+ )
19
+ elif aug_type == 'Synonym Replacement':
20
+ aug = naw.SynonymAug(
21
+ aug_src="wordnet",
22
+ aug_max=aug_max
23
+ )
24
+ elif aug_type == 'Back Translation':
25
+ aug = naw.BackTranslationAug(
26
+ from_model_name='facebook/wmt19-en-de',
27
+ to_model_name='facebook/wmt19-de-en'
28
+ )
29
+ else:
30
+ return text
31
+
32
+ augmented_text = aug.augment(text)
33
+ return augmented_text
34
+
35
+ with gr.Blocks() as iface:
36
+ text_input = gr.Textbox(label="Input Text")
37
+ aug_type_input = gr.Radio(
38
+ choices=['Word Embedding Substitution', 'Contextual Insertion', 'Synonym Replacement', 'Back Translation'],
39
+ label="Augmentation Type",
40
+ value='Word Embedding Substitution'
41
+ )
42
+
43
+ model_type_input = gr.Dropdown(
44
+ choices=['word2vec', 'fasttext', 'glove'],
45
+ label="Model Type (for Word Embedding Substitution)",
46
+ value='word2vec',
47
+ visible=True
48
+ )
49
+ model_path_input = gr.Textbox(
50
+ label="Model Path (for Word Embedding Substitution)",
51
+ value="GoogleNews-vectors-negative300.bin",
52
+ visible=True
53
+ )
54
+ aug_p_input = gr.Slider(
55
+ minimum=0, maximum=1, step=0.05, value=0.25,
56
+ label="Probability of Augmentation (for Embedding Substitution or Contextual Insertion)"
57
+ )
58
+ aug_max_input = gr.Slider(
59
+ minimum=1, maximum=10, step=1, value=3,
60
+ label="Max Number of Words to Change (for Synonym Replacement)",
61
+ visible=False
62
+ )
63
+
64
+ augmented_output = gr.Textbox(label="Augmented Text")
65
+
66
+ # update input block visibility based on aug type
67
+ def update_inputs(aug_type):
68
+ if aug_type == 'Word Embedding Substitution':
69
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
70
+ elif aug_type == 'Contextual Insertion':
71
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
72
+ elif aug_type == 'Synonym Replacement':
73
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
74
+ elif aug_type == 'Back Translation':
75
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
76
+
77
+ # update inputs when aug type changes
78
+ aug_type_input.change(
79
+ update_inputs,
80
+ inputs=[aug_type_input],
81
+ outputs=[model_type_input, model_path_input, aug_max_input]
82
+ )
83
+
84
+ apply_button = gr.Button("Apply Augmentation")
85
+
86
+ apply_button.click(
87
+ augment_text,
88
+ inputs=[text_input, aug_type_input, model_type_input, model_path_input, aug_p_input, aug_max_input],
89
+ outputs=[augmented_output]
90
+ )
91
+
92
+ iface.launch()