Spaces:
Sleeping
Sleeping
Translation of comments from Chinese to English
Browse filesTranslation of comments from Chinese to English language.
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# OCR Translate v0.2
|
2 |
-
# 创建人:曾逸夫
|
3 |
-
#
|
4 |
|
5 |
import os
|
6 |
|
@@ -19,23 +19,23 @@ nltk.download punkt
|
|
19 |
OCR_TR_DESCRIPTION = '''# OCR Translate v0.2
|
20 |
<div id="content_align">OCR translation system based on Tesseract</div>'''
|
21 |
|
22 |
-
#
|
23 |
img_dir = "./data"
|
24 |
|
25 |
-
#
|
26 |
choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
|
27 |
|
28 |
|
29 |
-
#
|
30 |
def model_choice(src, trg):
|
31 |
# https://huggingface.co/Helsinki-NLP/opus-mt-tc-big-pt-en
|
32 |
# https://huggingface.co/Helsinki-NLP/opus-mt-tc-big-en-pt
|
33 |
# https://huggingface.co/unicamp-dl/translation-pt-en-t5
|
34 |
# https://huggingface.co/unicamp-dl/translation-en-pt-t5
|
35 |
-
model_name = f"unicamp-dl/translation-{src}-{trg}-t5" #
|
36 |
|
37 |
-
#tokenizer = MarianTokenizer.from_pretrained(model_name) #
|
38 |
-
#model = MarianMTModel.from_pretrained(model_name) #
|
39 |
|
40 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
41 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
@@ -43,7 +43,7 @@ def model_choice(src, trg):
|
|
43 |
return tokenizer, model
|
44 |
|
45 |
|
46 |
-
# tesseract
|
47 |
def ocr_lang(lang_list):
|
48 |
lang_str = ""
|
49 |
lang_len = len(lang_list)
|
@@ -63,12 +63,12 @@ def ocr_tesseract(img, languages):
|
|
63 |
return ocr_str
|
64 |
|
65 |
|
66 |
-
#
|
67 |
def clear_content():
|
68 |
return None
|
69 |
|
70 |
|
71 |
-
#
|
72 |
def cp_text(input_text):
|
73 |
# sudo apt-get install xclip
|
74 |
try:
|
@@ -78,18 +78,18 @@ def cp_text(input_text):
|
|
78 |
print(e)
|
79 |
|
80 |
|
81 |
-
#
|
82 |
def cp_clear():
|
83 |
pyclip.clear()
|
84 |
|
85 |
|
86 |
-
#
|
87 |
def translate(input_text, inputs_transStyle):
|
88 |
-
#
|
89 |
if input_text is None or input_text == "":
|
90 |
return "System prompt: There is no content to translate!"
|
91 |
|
92 |
-
#
|
93 |
trans_src, trans_trg = inputs_transStyle.split("-")[0], inputs_transStyle.split("-")[1]
|
94 |
tokenizer, model = model_choice(trans_src, trans_trg)
|
95 |
|
@@ -116,7 +116,7 @@ def main():
|
|
116 |
with gr.Blocks(css='style.css') as ocr_tr:
|
117 |
gr.Markdown(OCR_TR_DESCRIPTION)
|
118 |
|
119 |
-
# -------------- OCR
|
120 |
with gr.Blocks():
|
121 |
|
122 |
with gr.Row():
|
@@ -153,7 +153,7 @@ def main():
|
|
153 |
["./data/test03.png", ["por"]]]
|
154 |
gr.Examples(example_list, [inputs_img, inputs_lang], outputs_text, ocr_tesseract, cache_examples=False)
|
155 |
|
156 |
-
# --------------
|
157 |
with gr.Blocks():
|
158 |
|
159 |
with gr.Row():
|
@@ -171,11 +171,11 @@ def main():
|
|
171 |
outputs_text,])
|
172 |
clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
|
173 |
|
174 |
-
# ----------------------
|
175 |
translate_btn.click(fn=translate, inputs=[outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
|
176 |
clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
|
177 |
|
178 |
-
# ----------------------
|
179 |
cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
|
180 |
cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
|
181 |
|
|
|
1 |
# OCR Translate v0.2
|
2 |
+
# 创建人:曾逸夫 (Founder: Zeng Yifu)
|
3 |
+
# Creation time:2022-07-19
|
4 |
|
5 |
import os
|
6 |
|
|
|
19 |
OCR_TR_DESCRIPTION = '''# OCR Translate v0.2
|
20 |
<div id="content_align">OCR translation system based on Tesseract</div>'''
|
21 |
|
22 |
+
# Image Path
|
23 |
img_dir = "./data"
|
24 |
|
25 |
+
# Get tesseract language list
|
26 |
choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
|
27 |
|
28 |
|
29 |
+
# Translation model selection
|
30 |
def model_choice(src, trg):
|
31 |
# https://huggingface.co/Helsinki-NLP/opus-mt-tc-big-pt-en
|
32 |
# https://huggingface.co/Helsinki-NLP/opus-mt-tc-big-en-pt
|
33 |
# https://huggingface.co/unicamp-dl/translation-pt-en-t5
|
34 |
# https://huggingface.co/unicamp-dl/translation-en-pt-t5
|
35 |
+
model_name = f"unicamp-dl/translation-{src}-{trg}-t5" # Model Name
|
36 |
|
37 |
+
#tokenizer = MarianTokenizer.from_pretrained(model_name) # Tokenizer
|
38 |
+
#model = MarianMTModel.from_pretrained(model_name) # Model
|
39 |
|
40 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
41 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
|
|
43 |
return tokenizer, model
|
44 |
|
45 |
|
46 |
+
# Convert tesseract language list to pytesseract language
|
47 |
def ocr_lang(lang_list):
|
48 |
lang_str = ""
|
49 |
lang_len = len(lang_list)
|
|
|
63 |
return ocr_str
|
64 |
|
65 |
|
66 |
+
# Clear
|
67 |
def clear_content():
|
68 |
return None
|
69 |
|
70 |
|
71 |
+
# Copy to Clipboard
|
72 |
def cp_text(input_text):
|
73 |
# sudo apt-get install xclip
|
74 |
try:
|
|
|
78 |
print(e)
|
79 |
|
80 |
|
81 |
+
# Clear Clipboard
|
82 |
def cp_clear():
|
83 |
pyclip.clear()
|
84 |
|
85 |
|
86 |
+
# Translate
|
87 |
def translate(input_text, inputs_transStyle):
|
88 |
+
# Reference:https://huggingface.co/docs/transformers/model_doc/marian
|
89 |
if input_text is None or input_text == "":
|
90 |
return "System prompt: There is no content to translate!"
|
91 |
|
92 |
+
# Selecting a translation model
|
93 |
trans_src, trans_trg = inputs_transStyle.split("-")[0], inputs_transStyle.split("-")[1]
|
94 |
tokenizer, model = model_choice(trans_src, trans_trg)
|
95 |
|
|
|
116 |
with gr.Blocks(css='style.css') as ocr_tr:
|
117 |
gr.Markdown(OCR_TR_DESCRIPTION)
|
118 |
|
119 |
+
# -------------- OCR Text Extraction --------------
|
120 |
with gr.Blocks():
|
121 |
|
122 |
with gr.Row():
|
|
|
153 |
["./data/test03.png", ["por"]]]
|
154 |
gr.Examples(example_list, [inputs_img, inputs_lang], outputs_text, ocr_tesseract, cache_examples=False)
|
155 |
|
156 |
+
# -------------- Translate --------------
|
157 |
with gr.Blocks():
|
158 |
|
159 |
with gr.Row():
|
|
|
171 |
outputs_text,])
|
172 |
clear_img_btn.click(fn=clear_content, inputs=[], outputs=[inputs_img])
|
173 |
|
174 |
+
# ---------------------- Translate ----------------------
|
175 |
translate_btn.click(fn=translate, inputs=[outputs_text, inputs_transStyle], outputs=[outputs_tr_text])
|
176 |
clear_text_btn.click(fn=clear_content, inputs=[], outputs=[outputs_text])
|
177 |
|
178 |
+
# ---------------------- Copy to Clipboard ----------------------
|
179 |
cp_btn.click(fn=cp_text, inputs=[outputs_tr_text], outputs=[])
|
180 |
cp_clear_btn.click(fn=cp_clear, inputs=[], outputs=[])
|
181 |
|