File size: 1,462 Bytes
637d1b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import os

AUTH_TOKEN = os.environ.get("TOKEN")
model = AutoModelForSeq2SeqLM.from_pretrained('bowphs/ancient-t5-translation', use_auth_token=AUTH_TOKEN)
tokenizer = AutoTokenizer.from_pretrained('bowphs/ancient-t5-translation', use_auth_token=AUTH_TOKEN)
generator = pipeline('text2text-generation', model=model, tokenizer=tokenizer)


def generate(text, generation_args):
    arguments = {}
    if generation_args:
        pairs = generation_args.split(",")
        for pair in pairs:
            key, value = pair.strip().split('=')
            arguments[key] = eval(value)

    result = generator(text, max_length=30, num_return_sequences=1, **arguments)
    return result[0]["generated_text"]

examples = [
    ["translate english to latin: and he took the sword and killed the man."],
    ["translate english to latin: and he took the sword and killed the man."],
]

demo = gr.Interface(
    fn=generate,
    inputs=[gr.components.Textbox(value="translate greek to english: ὁ ἄνθρωπος τὸν οἶνον πίνειν ἐθέλων τὸν κρατῆρα ἔλαβεν.", label="Input Text"), gr.components.Textbox(value="do_sample=False, num_beams=3", label="Generation Parameters")],
    outputs=gr.components.Textbox(value="the man took the bowl with the intention of drinking wine.", label="Generated Text"),
    examples=examples
)

demo.launch()