File size: 1,634 Bytes
44db343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Union

import sys
sys.path.append("..")
from params import *
from dataset.vocab import Vocab
from models.corrector import Corrector
from models.model import ModelWrapper
from models.util import load_weights
from dataset.noise import SynthesizeData
from utils.api_utils import correctFunction, postprocessing_result


model_name = "tfmwtr"
dataset = "binhvq"
vocab_path = f'data/{dataset}/{dataset}.vocab.pkl'
weight_path = f'data/checkpoints/tfmwtr/{dataset}.weights.pth'
vocab = Vocab("vi")
vocab.load_vocab_dict(vocab_path)
noiser = SynthesizeData(vocab)
model_wrapper = ModelWrapper(f"{model_name}", vocab)
corrector = Corrector(model_wrapper)
load_weights(corrector.model, weight_path)

def correct(string: str):
    out = correctFunction(string, corrector)
    result = postprocessing_result(out)

    ret = []
    for r in result:
        r = [s.strip() for s in r if isinstance(s, str)]

        if len(r) == 2:
            ret.append((r[0], r[1]))
        else:
            ret.append((r[0], None))
        ret.append((" ", None))
    ret.pop()
    print(ret, "RET")
    return ret

import gradio as gr
if __name__ == "__main__":
    css = """
    #output {
        .label {
            background-color: green !important;
        }
    }
    """
    gr.Interface(
        correct,
        inputs=gr.Textbox(label="Input", placeholder="Enter text to be corrected here..."),
        outputs=gr.HighlightedText(
            label="Output",
            combine_adjacent=True,
            show_label=True,
            elem_id="output"
        ),
        theme=gr.themes.Default(),
        css=css
    ).launch()