Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import subprocess | |
| import sacrebleu | |
| MOSES_INI = "/workspace/model/model/moses.ini" | |
| # --- Translation Function --- | |
| def translate_zomi_to_english(text): | |
| result = subprocess.run( | |
| ["/workspace/mosesdecoder/bin/moses", "-f", MOSES_INI], | |
| input=text.encode("utf-8"), | |
| stdout=subprocess.PIPE | |
| ) | |
| return result.stdout.decode("utf-8") | |
| # --- BLEU Evaluation Function --- | |
| def evaluate_bleu(test_src_file, test_ref_file): | |
| with open(test_src_file.name, "r", encoding="utf-8") as fsrc: | |
| src_text = fsrc.read() | |
| result = subprocess.run( | |
| ["/workspace/mosesdecoder/bin/moses", "-f", MOSES_INI], | |
| input=src_text.encode("utf-8"), | |
| stdout=subprocess.PIPE | |
| ) | |
| hyp_text = result.stdout.decode("utf-8").strip().splitlines() | |
| with open(test_ref_file.name, "r", encoding="utf-8") as fref: | |
| ref_text = fref.read().strip().splitlines() | |
| bleu = sacrebleu.corpus_bleu(hyp_text, [ref_text]) | |
| return f"BLEU score: {bleu.score:.2f}" | |
| # --- Training Function --- | |
| def train_model(): | |
| result = subprocess.run( | |
| ["bash", "/workspace/run_training.sh"], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE | |
| ) | |
| return result.stdout.decode("utf-8") + "\n" + result.stderr.decode("utf-8") | |
| # --- Gradio Interfaces --- | |
| translation_iface = gr.Interface( | |
| fn=translate_zomi_to_english, | |
| inputs="text", | |
| outputs="text", | |
| title="Zomi β English Translation (Moses SMT)" | |
| ) | |
| bleu_iface = gr.Interface( | |
| fn=evaluate_bleu, | |
| inputs=[ | |
| gr.File(label="Upload Zomi test set (source)", file_types=[".txt"]), | |
| gr.File(label="Upload English test set (reference)", file_types=[".txt"]) | |
| ], | |
| outputs="text", | |
| title="BLEU Evaluation (Zomi β English Moses SMT)" | |
| ) | |
| train_iface = gr.Interface( | |
| fn=train_model, | |
| inputs=[], | |
| outputs="text", | |
| title="Train Moses Model" | |
| ) | |
| # --- Combine into Tabs --- | |
| app = gr.TabbedInterface( | |
| [translation_iface, bleu_iface, train_iface], | |
| ["Translation", "BLEU Evaluation", "Train Model"] | |
| ) | |
| if __name__ == "__main__": | |
| app.launch(server_name="0.0.0.0", server_port=7860) | |