|
from easynmt import EasyNMT |
|
import gradio as gr |
|
|
|
|
|
try: |
|
|
|
zh_en_naive_model = EasyNMT("m2m_100_418M") |
|
|
|
zh_en_naive_model.translate(["Where is Ningbo?"], source_lang="zh", target_lang="en") |
|
except Exception as e: |
|
raise Exception(f"Failed to load EasyNMT model: {str(e)}") |
|
|
|
|
|
example_sample = [ |
|
"宁波在哪个省?", |
|
"美国的货币是什么?" |
|
] |
|
|
|
def demo_func(zh_question): |
|
if not isinstance(zh_question, str) or not zh_question.strip(): |
|
return {"Error": "Please provide a valid Chinese question."} |
|
try: |
|
en_question = zh_en_naive_model.translate([zh_question], source_lang="zh", target_lang="en")[0] |
|
return {"English Question": en_question} |
|
except Exception as e: |
|
return {"Error": f"Translation failed: {str(e)}"} |
|
|
|
|
|
demo = gr.Interface( |
|
fn=demo_func, |
|
inputs=gr.Textbox(label="Chinese Question", placeholder="Enter a Chinese question here..."), |
|
outputs=gr.JSON(label="Translated Output"), |
|
title="Translate Chinese to English 🐱 Demonstration", |
|
description="Enter a Chinese question to translate it into English.", |
|
examples=example_sample, |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
demo.launch() |