File size: 2,791 Bytes
2fc8898
 
 
 
 
f23973d
153bccc
2fc8898
 
 
 
 
 
 
 
 
3e33805
 
 
2fc8898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc58543
 
 
2fc8898
 
 
 
 
cc58543
 
 
2fc8898
 
 
 
 
 
 
cc58543
 
 
2fc8898
 
 
 
 
cc58543
 
 
2fc8898
 
 
 
 
 
 
153bccc
 
2fc8898
153bccc
 
57071ff
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

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


def extract_str(text):
    text=str(text)
    start = text.find("{'")
    end = text.find("'}")
    return text[start+2:end]
    
def gramacorrect(sentence):
    gf = Gramformer(models=1, use_gpu=False)
    res = gf.correct(sentence) 
    return extract_str(res) 

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


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,api_name="translate_zh")
    en_button.click(translate_en, inputs=text_input2, outputs=english,api_name="translate_en")
    
    check_button.click(gramacorrect, inputs=text_input3, outputs=check,api_name="gramacorrect")
    gen_button.click(generator, inputs=text_input4, outputs=txtgenerator,api_name="generator")
demo.launch()