File size: 1,322 Bytes
f609671
 
 
 
 
 
 
 
c9dfef5
f609671
 
3420384
eca58c4
 
3420384
 
 
 
 
 
 
acc15fe
f609671
3420384
f609671
 
3420384
f609671
3420384
 
b502db9
3420384
 
f609671
3420384
f609671
 
 
 
 
3420384
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("facebook/bart-base")
infer = AutoModelForSeq2SeqLM.from_pretrained("Qilex/bart-largeEN-ME")

def translate(sentence):
    input_ids = tokenizer(sentence, return_tensors="pt").input_ids
    outputs = infer.generate(input_ids, max_new_tokens = len(sentence.split(' '))*10)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

def translate_multiline(sentence):
#    if len(sentence.split()) > 300:
#        print('Please insert less text')
    if '\n' in sentence:
        lines = sentence.split('\n')
        translated_lines = [translate(line) for line in lines if len(line) > 0]
        return '\n'.join(translated_lines)
    else:
        return translate(sentence)
    
title = "Modern English to Middle English Translator"
description = """
This translator is trained on about 70,000 English/Middle English paired sentences. 
<br>
It's still a work in progress.
<br>
"""
article = '''
<br>
You can improve results by removing contractions (hadn't -> had not)
'''

gr.Interface(
    fn=translate_multiline,
    inputs=gr.Textbox(lines=1, placeholder="Enter text to translate."),
    outputs="text",
    title=title,
    description=description,
    article = article,
).launch()