File size: 1,904 Bytes
090b1b7
 
ba275d7
e36bf2f
c19dfc1
a2ce6e9
c19dfc1
 
 
a2ce6e9
 
 
c19dfc1
 
 
 
 
 
 
 
ba275d7
 
 
 
 
c19dfc1
 
ba275d7
 
 
 
 
090b1b7
 
 
ba275d7
 
 
 
090b1b7
ba275d7
 
 
 
 
 
 
 
 
090b1b7
c19dfc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
import nlpaug.augmenter.word as naw
import nlpaug.augmenter.char as nac
import nlpaug.augmenter.sentence as nas
import os
import nltk
from nlpaug.util.file.download import DownloadUtil

# Download necessary NLTK resources
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')

# Function to download Word2Vec model if not available
def download_word2vec():
    word2vec_path = "GoogleNews-vectors-negative300.bin"
    if not os.path.exists(word2vec_path):
        print("Downloading Word2Vec model...")
        DownloadUtil.download_word2vec(dest_dir='.')
    return word2vec_path

# Function for NLP augmentation
def augment_text(text, method):
    if method == "Synonym Replacement":
        aug = naw.SynonymAug(aug_src="wordnet", aug_max=3)
    elif method == "Word Embedding Substitution":
        word2vec_path = download_word2vec()  # Ensure the model is downloaded
        aug = naw.WordEmbsAug(model_type='word2vec', model_path=word2vec_path, action="substitute")
    elif method == "Contextual Word Insertion":
        aug = naw.ContextualWordEmbsAug(model_path="bert-base-uncased", action="insert")
    elif method == "Back Translation":
        aug = naw.BackTranslationAug(from_model_name='facebook/wmt19-en-de', to_model_name='facebook/wmt19-de-en')
    
    augmented_text = aug.augment(text)
    return augmented_text

# Gradio Interface
def nlp_augmentor_interface(text, method):
    augmented_text = augment_text(text, method)
    return augmented_text

iface = gr.Interface(
    fn=nlp_augmentor_interface,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter sentence to augment here..."),
        gr.Radio(["Synonym Replacement", "Word Embedding Substitution", "Contextual Word Insertion", "Back Translation"], label="Augmentation Method")
    ],
    outputs="text",
    title="NLP Text Augmentation with Gradio"
)

iface.launch(share=True)