Skip to content

Instantly share code, notes, and snippets.

@ymoslem
Last active Aug 25, 2021
Embed
What would you like to do?
OpenNMT-py Translate GUI
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#OpenNMT-py GUI Alpha version by Yasmin Moslem
#Contact: yasmin {aatt} machinetranslation.io
#Built on OpenNMT-py v. 0.9.1 "translate.py"
from __future__ import unicode_literals
from itertools import repeat
from onmt.utils.logging import init_logger
from onmt.utils.misc import split_corpus
from onmt.translate.translator import build_translator
import onmt.opts as opts
from onmt.utils.parse import ArgumentParser
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
import webbrowser
def main(opt):
ArgumentParser.validate_translate_opts(opt)
logger = init_logger(opt.log_file)
translator = build_translator(opt, report_score=True)
src_shards = split_corpus(opt.src, opt.shard_size)
tgt_shards = split_corpus(opt.tgt, opt.shard_size) \
if opt.tgt is not None else repeat(None)
shard_pairs = zip(src_shards, tgt_shards)
for i, (src_shard, tgt_shard) in enumerate(shard_pairs):
logger.info("Translating shard %d." % i)
translator.translate(
src=src_shard,
tgt=tgt_shard,
src_dir=opt.src_dir,
batch_size=opt.batch_size,
attn_debug=opt.attn_debug
)
def _get_parser():
parser = ArgumentParser(description='translate.py')
opts.config_opts(parser)
opts.translate_opts(parser)
return parser
def go():
parser = _get_parser()
opt = parser.parse_args()
try:
opt.src = file_source
opt.models = [file_model]
opt.output = "yourtranslation.txt"
main(opt)
success = messagebox.askyesno('Success', 'Your source text has been successfully translated and saved as "yourtranslation.txt". Do you want to open the target file?')
if success == True:
webbrowser.open("yourtranslation.txt")
except:
messagebox.showerror('Error', 'Make sure you select the right Source and Model files.')
# GUI
Title = "OpenNMT-py Translator"
window = Tk()
window.title(Title)
window.geometry("406x200")
window.resizable(0,0)
frame0 = Frame(window)
frame1 = Frame(window)
frame2 = Frame(window)
frame3 = Frame(window)
# About
def about():
messagebox.showinfo('About', 'OpenNMT-py GUI Alpha version by Yasmin Moslem.\nContact: yasmin@machinetranslation.io')
btn_about = Button(frame0, text="?", command=about, relief=GROOVE).pack(side=RIGHT)
# Button Callbacks
def btnBrowseSourceCallback(event=None): # event=None solves the issue of argument, or create two functions one for browse and one for entry
window.update()
global file_source
file_source = askopenfilename()
if file_source != None:
path_source.delete(0, END)
path_source.insert(0, file_source)
def btnBrowseModelCallback(event=None):
window.update()
global file_model
file_model = askopenfilename()
if file_model != None:
path_model.delete(0, END)
path_model.insert(0, file_model)
# Source File Picker
label_source = Label(frame1, text="Source File").pack(side=LEFT)
path_source = Entry(frame1, width=43, textvariable="")
path_source.insert(0,"Browse to locate the source *.txt file")
path_source.pack(side=LEFT)
path_source.bind("<Button-1>", btnBrowseSourceCallback) # open file dialogue when clicking on the Entry
btn_browse_source = Button(frame1, text="Browse...", command=btnBrowseSourceCallback).pack(side=RIGHT)
# Model File Picker
label_model = Label(frame2, text="Model File").pack(side=LEFT)
path_model = Entry(frame2, width=43, textvariable="")
path_model.insert(0,"Browse to locate the model *.pt file")
path_model.pack(side=LEFT)
path_model.bind("<Button-2>", btnBrowseModelCallback) # open file dialogue when clicking on the Entry
btn_browse_model = Button(frame2, text="Browse...", command=btnBrowseModelCallback).pack(side=RIGHT)
btn_translate = Button(frame3, text="Translate", width=20, highlightbackground="#BBCAE8", command=go).pack(padx=1)
# use pack instead of grid to adjust position
frame0.pack(fill=BOTH, expand=NO, padx=10, pady=10)
frame1.pack(fill=BOTH, expand=NO, padx=10, pady=10)
frame2.pack(fill=BOTH, expand=NO, padx=10, pady=10)
frame3.pack(fill=BOTH, expand=NO, padx=10, pady=10)
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment