import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration


tokenizer = T5Tokenizer.from_pretrained("t5-small")
model = T5ForConditionalGeneration.from_pretrained("t5-small")

def translation(english, language):
    if language == 'German':
        input_ids = tokenizer("translate English to German: " + english, return_tensors="pt").input_ids
    elif language == 'French':
        input_ids = tokenizer("translate English to French: " + english, return_tensors="pt").input_ids
    else:
        input_ids = tokenizer("translate English to Romanian: " + english, return_tensors="pt").input_ids
    outputs = model.generate(input_ids)

    return tokenizer.decode(outputs[0], skip_special_tokens=True)

demo = gr.Interface(fn=translation, 
                    inputs=['text', gr.inputs.Radio(['German','French','Romanian'], type='value', default='German', label='language')], 
                    outputs='text',
                    title = "翻译"
                    )


if __name__ == "__main__":
    demo.queue(concurrency_count=3)
    demo.launch(server_name = "0.0.0.0", server_port = 7028)
