File size: 1,690 Bytes
ef09f71
 
e692492
ef09f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e692492
ef09f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 difflib import Differ

import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

model = AutoModelForSeq2SeqLM.from_pretrained("Hnabil/t5-address-standardizer")
tokenizer = AutoTokenizer.from_pretrained("Hnabil/t5-address-standardizer")


def standardizer_adress(adress):
    inputs = tokenizer(adress, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=100)
    std_adress = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
    return std_adress


def diff_texts(adress):
    std_adress = standardizer_adress(adress)
    words1 = adress.split()
    words2 = std_adress.split()
    d = Differ()

    return [
        (token[2:], "Non-Standard" if token[0] != " " else None)
        for token in d.compare(words1, words2)
        if token[0] != "+" and token[0] != "?"
    ] + [
        ("β†’", "β†’")
    ] + [
        (token[2:], "Standard" if token[0] != " " else None)
        for token in d.compare(words1, words2)
        if token[0] != "-" and token[0] != "?"
    ]


examples = [
    ["940, north pennsylvania avneue, mason icty, iowa, 50401, us"],
    ["537, 6th st s, mason city, ia, 50401, us"]
]
color_map = {"Non-Standard": "LightSalmon", "Standard": "LightGreen", "β†’": "LightBlue"}

demo = gr.Interface(
    diff_texts,
    [
        gr.Textbox(
            label="Adress",
            info="Enter an adress to standardize",
            lines=3,
        )
    ],
    [
      gr.HighlightedText(
          label="Standardized Adress",
          show_legend=True,
      ).style(color_map=color_map),
    ],
    examples=examples,
    theme=gr.themes.Base()
)
if __name__ == "__main__":
    demo.launch(share=False)