Spaces:
Runtime error
Runtime error
Commit
·
2fc8898
1
Parent(s):
f8000bd
Upload AI-Translator.py
Browse files- AI-Translator.py +105 -0
AI-Translator.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %% [markdown]
|
2 |
+
# # AI-Translator
|
3 |
+
# This is an web app with an English/Chinese translation and grammar check function
|
4 |
+
|
5 |
+
# %% [markdown]
|
6 |
+
# install the following packages:
|
7 |
+
|
8 |
+
# %%
|
9 |
+
# !pip3 install torch==1.8.2+cu111 torchvision==0.9.2+cu111 torchaudio==0.8.2 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html
|
10 |
+
# !pip3 install pip==20.1.1
|
11 |
+
# !pip3 install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git
|
12 |
+
# !pip install spacy
|
13 |
+
# !python -m spacy download en
|
14 |
+
# !pip install gradio
|
15 |
+
# !pip install transformers
|
16 |
+
|
17 |
+
# %% [markdown]
|
18 |
+
# import depandancies
|
19 |
+
|
20 |
+
# %%
|
21 |
+
from gramformer import Gramformer
|
22 |
+
import spacy
|
23 |
+
import gradio as gr
|
24 |
+
from transformers import pipeline
|
25 |
+
from gramformer import Gramformer
|
26 |
+
spacy.load('en')
|
27 |
+
# from spacy.lang.en import English
|
28 |
+
|
29 |
+
# %% [markdown]
|
30 |
+
# define api for translation and grama check
|
31 |
+
|
32 |
+
# %%
|
33 |
+
def extract_str(text):
|
34 |
+
text=str(text)
|
35 |
+
start = text.find("{'")
|
36 |
+
end = text.find("'}")
|
37 |
+
return text[start+2:end]
|
38 |
+
|
39 |
+
def gramacorrect(sentence):
|
40 |
+
res = gf.correct(sentence) # Gramformer correct
|
41 |
+
return extract_str(res) # Return first value in res array
|
42 |
+
|
43 |
+
def translate_zh(from_text):
|
44 |
+
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
|
45 |
+
res = translation_pipeline(from_text)[0]
|
46 |
+
return res['translation_text']
|
47 |
+
|
48 |
+
def translate_en(from_text):
|
49 |
+
translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
50 |
+
res = translation_pipeline(from_text)[0]
|
51 |
+
return res['translation_text']
|
52 |
+
|
53 |
+
def generator(from_text):
|
54 |
+
english_generator = pipeline("text-generation", model="distilgpt2")
|
55 |
+
english_text = english_generator(from_text)[0]["generated_text"]
|
56 |
+
return english_text
|
57 |
+
|
58 |
+
# %% [markdown]
|
59 |
+
# build interface for web app
|
60 |
+
|
61 |
+
# %%
|
62 |
+
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
with gr.Tab("Translator"):
|
65 |
+
gr.Markdown("English to Chinese.")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
text_input1 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
|
69 |
+
chinese = gr.Textbox(lines=4, placeholder="Chinese")
|
70 |
+
zh_button = gr.Button("RUN")
|
71 |
+
gr.Markdown("Chinese to English.")
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
text_input2 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
|
75 |
+
english = gr.Textbox(lines=4, placeholder="English")
|
76 |
+
en_button = gr.Button("RUN")
|
77 |
+
|
78 |
+
with gr.Tab("Gramachecker"):
|
79 |
+
gr.Markdown("English grama checker.")
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
text_input3 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
|
83 |
+
check = gr.Textbox(lines=4, placeholder="Grama Check")
|
84 |
+
check_button = gr.Button("RUN")
|
85 |
+
|
86 |
+
gr.Markdown("English text generator.")
|
87 |
+
with gr.Row():
|
88 |
+
text_input4 = gr.Textbox(lines=2, placeholder="Enter sentence here...")
|
89 |
+
txtgenerator = gr.Textbox(lines=6, placeholder="Text Generator")
|
90 |
+
gen_button = gr.Button("RUN")
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
zh_button.click(translate_zh, inputs=text_input1, outputs=chinese)
|
95 |
+
en_button.click(translate_en, inputs=text_input2, outputs=english)
|
96 |
+
|
97 |
+
check_button.click(gramacorrect, inputs=text_input3, outputs=check)
|
98 |
+
gen_button.click(generator, inputs=text_input4, outputs=txtgenerator)
|
99 |
+
demo.launch(share=True)
|
100 |
+
|
101 |
+
|
102 |
+
# %%
|
103 |
+
|
104 |
+
|
105 |
+
|