Spaces:
Build error
Build error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
|
7 |
+
model_checkpoint = "penpen/novel-zh-en"
|
8 |
+
translator = pipeline("translation", model=model_checkpoint, max_time=7)
|
9 |
+
default_dict = pd.read_csv("example_dictionary.csv", names=["Chinese", "English"])
|
10 |
+
examples = pd.read_csv("examples.csv", header = None)
|
11 |
+
|
12 |
+
def predict(text, df):
|
13 |
+
translation = ""
|
14 |
+
terms_dict = {chinese: english for chinese, english in zip(df["Chinese"].tolist(), df["English"].tolist())}
|
15 |
+
for key in terms_dict:
|
16 |
+
if key in text:
|
17 |
+
masking = "MASK"*len(key)
|
18 |
+
text = text.replace(key, "<TERM>" + masking+ "<GLOS>" + terms_dict[key] + "</GLOS>")
|
19 |
+
|
20 |
+
split_text = text.splitlines()
|
21 |
+
for text in split_text:
|
22 |
+
text = text.strip()
|
23 |
+
if text:
|
24 |
+
if len(text) < 512:
|
25 |
+
sentence = translator(text)[0]["translation_text"] + '\n\n'
|
26 |
+
translation+=sentence
|
27 |
+
print(split_text)
|
28 |
+
else:
|
29 |
+
for i in range(0,len(text),512):
|
30 |
+
if i+512>len(text):
|
31 |
+
sentence = translator(text[i:])[0]["translation_text"]
|
32 |
+
else:
|
33 |
+
sentence = translator(text[i:i+512])[0]["translation_text"]
|
34 |
+
translation+=sentence
|
35 |
+
return translation
|
36 |
+
|
37 |
+
|
38 |
+
def load_dict(file):
|
39 |
+
df = pd.read_csv(file.name, names=["Chinese", "English"])
|
40 |
+
return df, df
|
41 |
+
|
42 |
+
def search_dict(query, df):
|
43 |
+
if not query:
|
44 |
+
return df
|
45 |
+
mask = np.column_stack([df[col].str.contains(query, na=False) for col in df])
|
46 |
+
return df.loc[mask.any(axis=1)]
|
47 |
+
|
48 |
+
with gr.Blocks() as project:
|
49 |
+
dict_hidden = gr.State(default_dict)
|
50 |
+
gr.Markdown("<center><h1>Chinese Webnovel Translator</h1> A translator that is fine-tuned on Chinese Webnovels</center>")
|
51 |
+
with gr.Tab("Translator"):
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column(scale=1, min_width=600):
|
54 |
+
translate_input = gr.Textbox(label="Chinese", lines=7, max_lines = 100, placeholder="Chinese...")
|
55 |
+
translate_button = gr.Button("Translate")
|
56 |
+
translate_hidden = gr.State("")
|
57 |
+
translate_output = gr.Textbox(label="English", lines=7, max_lines = 100, placeholder="English...")
|
58 |
+
example = gr.Examples(inputs = translate_input, examples=examples[0].tolist())
|
59 |
+
|
60 |
+
with gr.Tab("Proper Noun Dictionary"):
|
61 |
+
with gr.Row():
|
62 |
+
with gr.Column(scale=1, min_width=600):
|
63 |
+
dict_example_file = gr.File(label="Example Dictionary", value = "example_dictionary.csv")
|
64 |
+
dict_file = gr.File(interactive = True, label="Upload a custom dictionary (CSV File)")
|
65 |
+
dict_upload_button = gr.Button("Upload")
|
66 |
+
dict_search = gr.Textbox(label="Search Dictionary")
|
67 |
+
dict_search_button = gr.Button("Search")
|
68 |
+
|
69 |
+
dict_display = gr.Dataframe(value = default_dict, max_rows = 5, col_count=(2, "fixed"))
|
70 |
+
|
71 |
+
translate_button.click(predict, inputs=[translate_input, dict_hidden], outputs=translate_output)
|
72 |
+
|
73 |
+
dict_upload_button.click(load_dict, inputs=dict_file, outputs = [dict_hidden, dict_display])
|
74 |
+
dict_search_button.click(search_dict, inputs=[dict_search, dict_hidden], outputs = dict_display)
|
75 |
+
|
76 |
+
project.launch(debug=True)
|