File size: 1,564 Bytes
b38e725
 
 
bb4e44b
 
 
 
 
 
 
 
 
 
b38e725
bb4e44b
 
b38e725
 
 
bb4e44b
 
 
 
 
 
 
b38e725
bb4e44b
b38e725
bb4e44b
 
 
 
 
 
 
 
b38e725
bb4e44b
 
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
from easynmt import EasyNMT
import gradio as gr

# Initialize the translation model
try:
    # Replace "zh_en_m2m" with a valid model identifier or local path
    zh_en_naive_model = EasyNMT("m2m_100_418M")  # Example model; update as needed
    # Test model loading
    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 questions
example_sample = [
    "宁波在哪个省?",  # "Which province is Ningbo in?"
    "美国的货币是什么?"  # "What is the currency of the United States?"
]

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)}"}

# Create Gradio interface
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"  # Disable flagging for simplicity
)

# Launch the app (Hugging Face Spaces handles server configuration)
demo.launch()