|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
|
|
|
import ast |
|
|
import os |
|
|
|
|
|
import utils |
|
|
|
|
|
|
|
|
pipe = pipeline("translation", model="eepj/wstcg-mt-ja-en") |
|
|
|
|
|
pipe.model.eval() |
|
|
|
|
|
|
|
|
def func(text_ja: str, sub_emoji: bool) -> str: |
|
|
|
|
|
text_ja = utils.format_input(text_ja) |
|
|
|
|
|
splits_ja = [s for s in text_ja.splitlines() if s] |
|
|
|
|
|
|
|
|
segs_en = [] |
|
|
for s in splits_ja: |
|
|
|
|
|
seg_ja = "".join(s) |
|
|
|
|
|
seg_ja, repls = utils.text_to_placeholder_tokens(seg_ja) |
|
|
|
|
|
seg_en = pipe.predict(seg_ja)[0]["translation_text"] |
|
|
|
|
|
seg_en = utils.placeholder_tokens_to_text(seg_en, repls) |
|
|
|
|
|
seg_en = utils.format_output(seg_en, sub_emoji) |
|
|
|
|
|
segs_en.append(seg_en) |
|
|
|
|
|
|
|
|
text_en = "\n".join(segs_en) |
|
|
|
|
|
return text_en |
|
|
|
|
|
|
|
|
with gr.Blocks() as app: |
|
|
gr.Markdown("# WS TCG Card Text Translator") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
input_box = gr.TextArea(label="Original Card Text", |
|
|
info="Put each ability on a new line") |
|
|
output_box = gr.TextArea(label="Translated Card Text") |
|
|
|
|
|
sub_emoji = gr.Checkbox(True, label="Show Trigger Icon Emojis", info="Optional") |
|
|
|
|
|
submit_btn = gr.Button(variant="primary") |
|
|
clear_btn = gr.ClearButton([input_box, output_box]) |
|
|
|
|
|
try: |
|
|
example_text = [[e] for e in ast.literal_eval(os.environ["EXAMPLE_TEXT"])] |
|
|
if len(example_text) > 0: |
|
|
with gr.Column(): |
|
|
gr.Examples( |
|
|
example_text, |
|
|
inputs=[input_box, sub_emoji], |
|
|
fn=func, outputs=[output_box], |
|
|
label="Example Text", api_name=False, cache_examples=False) |
|
|
|
|
|
except (KeyError, SyntaxError, ValueError) as err: |
|
|
print(err) |
|
|
|
|
|
submit_btn.click(func, [input_box, sub_emoji], [output_box]) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.launch(show_api=False) |
|
|
|