MegaTronX's picture
Update app.py
bb4e44b verified
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()