File size: 1,515 Bytes
ecbb694
44f548c
a220e3e
 
db19c0d
69ec052
 
 
 
 
 
 
 
 
 
b61920c
4a96c16
 
 
 
ecd242d
 
 
 
4e78963
4a96c16
 
 
 
 
eecdd62
4a96c16
 
 
 
 
 
 
3e0ee01
4a96c16
 
1acb0b1
758f1d9
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
#!pip install transformers # put transformers in the requirements.txt file

import gradio as gr

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

en_nl_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-nl")
en_nl_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-nl")

#translation_pipeline = pipeline("text-classification", model=finbert_sentiment_prosus, tokenizer=finbert_sentiment_prosus_tokenizer)
en_nl_translator = pipeline("translation_en_to_nl", model = en_nl_model, tokenizer=en_nl_tokenizer) #might ignore "translation_en_to_nl"
# 


#gr.Interface.load("models/Helsinki-NLP/opus-mt-en-nl").launch()

def translate(df):
    #translate the input_text
    
    for i in range(len(df['English'])):
        input_string = str(df.loc[i, 'English'])
        if input_string == '':
            pass
        else:
            df.loc[i, 'Dutch'] = en_nl_translator(input_string)[0]['translation_text'] #extract from list, then take the value associated to key 'translation_text'
    return df

input_df = gr.Dataframe(
            headers=["English", "Dutch"],
            datatype=["str", "str"],
            row_count=5,
            col_count=(2, "fixed"),
        )

batch_translate = gr.Interface(
    fn = translate,
    inputs = input_df,  #[input1, input2]
    outputs = "dataframe",
    description = "Hela menneke, fill in the left column with the English text you want to translate"
)

if __name__ == "__main__":
    batch_translate.launch()