import pandas as pd import numpy as np import gradio as gr import unicodedata register = pd.read_csv('register.csv', index_col='jp_name') register.en_name = register.en_name.str.replace(',', '') def translate_name(jp_name): jp_name = unicodedata.normalize('NFKC', jp_name) en_name = np.nan if jp_name in register.index: en_name = register.loc[jp_name].en_name elif jp_name in register.index.map(lambda name: name.split('.')[-1]): tmp_register = register.copy() tmp_register.index = tmp_register.index.map(lambda name: name.split('.')[-1]) en_name = tmp_register.loc[jp_name].en_name if isinstance(en_name, pd.DataFrame): unique_names = en_name.en_name.unique() en_name = unique_names[0] elif not isinstance(en_name, str): en_name = np.nan return en_name def translate_names(jp_names): return '\n'.join([str(translate_name(jp_name)) for jp_name in jp_names.splitlines()]) demo = gr.Interface(fn=translate_names, inputs="text", outputs=gr.Text(show_copy_button=True)) demo.launch(debug=True)