File size: 2,819 Bytes
2fc8898
 
 
 
 
 
 
 
 
 
 
 
 
 
26e68d3
2fc8898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# %% [markdown]
# # AI-Translator
# This is an web app with an English/Chinese translation and grammar check function

# %% [markdown]
# import depandancies

# %%
from gramformer import Gramformer
import spacy
import gradio as gr
from transformers import pipeline
from gramformer import Gramformer
spacy.load('en')
from spacy.lang.en import English

# %% [markdown]
# define api for translation and grama check

# %%
def extract_str(text):
    text=str(text)
    start = text.find("{'")
    end = text.find("'}")
    return text[start+2:end]
    
def gramacorrect(sentence):
    res = gf.correct(sentence) # Gramformer correct
    return extract_str(res) # Return first value in res array

def translate_zh(from_text):
    translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-en-zh")
    res = translation_pipeline(from_text)[0]      
    return res['translation_text']

def translate_en(from_text):
    translation_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
    res = translation_pipeline(from_text)[0]      
    return res['translation_text']

def generator(from_text):
    english_generator = pipeline("text-generation", model="distilgpt2")
    english_text = english_generator(from_text)[0]["generated_text"]
    return english_text

# %% [markdown]
# build interface for web app

# %%

with gr.Blocks() as demo:
    with gr.Tab("Translator"):
        gr.Markdown("English to Chinese.")

        with gr.Row():
            text_input1 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
            chinese = gr.Textbox(lines=4, placeholder="Chinese")
        zh_button = gr.Button("RUN")
        gr.Markdown("Chinese to English.")

        with gr.Row():
            text_input2 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
            english = gr.Textbox(lines=4, placeholder="English")
        en_button = gr.Button("RUN")            
    
    with gr.Tab("Gramachecker"):
        gr.Markdown("English grama checker.")

        with gr.Row():
            text_input3 = gr.Textbox(lines=4, placeholder="Enter sentence here...")
            check = gr.Textbox(lines=4, placeholder="Grama Check")
        check_button = gr.Button("RUN")

        gr.Markdown("English text generator.")
        with gr.Row():
            text_input4 = gr.Textbox(lines=2, placeholder="Enter sentence here...")
            txtgenerator = gr.Textbox(lines=6, placeholder="Text Generator")
        gen_button = gr.Button("RUN")    



    zh_button.click(translate_zh, inputs=text_input1, outputs=chinese)
    en_button.click(translate_en, inputs=text_input2, outputs=english)
    
    check_button.click(gramacorrect, inputs=text_input3, outputs=check)
    gen_button.click(generator, inputs=text_input4, outputs=txtgenerator)
demo.launch(share=True)


# %%