Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,61 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
gr.Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from difflib import Differ
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
5 |
+
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Hnabil/t5-address-standardizer")
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("Hnabil/t5-address-standardizer")
|
8 |
+
|
9 |
+
|
10 |
+
def standardizer_adress(adress):
|
11 |
+
inputs = tokenizer(adress, return_tensors="pt")
|
12 |
+
outputs = model.generate(**inputs, max_length=100)
|
13 |
+
std_adress = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
14 |
+
return std_adress
|
15 |
+
|
16 |
+
|
17 |
+
def diff_texts(adress):
|
18 |
+
std_adress = standardizer_adress(adress)
|
19 |
+
words1 = adress.split()
|
20 |
+
words2 = std_adress.split()
|
21 |
+
d = Differ()
|
22 |
+
|
23 |
+
return [
|
24 |
+
(token[2:], "Non-Standard" if token[0] != " " else None)
|
25 |
+
for token in d.compare(words1, words2)
|
26 |
+
if token[0] != "+" and token[0] != "?"
|
27 |
+
] + [
|
28 |
+
("β", "β")
|
29 |
+
] + [
|
30 |
+
(token[2:], "Standard" if token[0] != " " else None)
|
31 |
+
for token in d.compare(words1, words2)
|
32 |
+
if token[0] != "-" and token[0] != "?"
|
33 |
+
]
|
34 |
+
|
35 |
+
|
36 |
+
examples = [
|
37 |
+
["940, north pennsylvania avneue, mason icty, iowa, 50401, us"],
|
38 |
+
["537, 6th st s, mason city, ia, 50401, us"]
|
39 |
+
]
|
40 |
+
color_map = {"Non-Standard": "LightSalmon", "Standard": "LightGreen", "β": "LightBlue"}
|
41 |
|
42 |
+
demo = gr.Interface(
|
43 |
+
diff_texts,
|
44 |
+
[
|
45 |
+
gr.Textbox(
|
46 |
+
label="Adress",
|
47 |
+
info="Enter an adress to standardize",
|
48 |
+
lines=3,
|
49 |
+
)
|
50 |
+
],
|
51 |
+
[
|
52 |
+
gr.HighlightedText(
|
53 |
+
label="Standardized Adress",
|
54 |
+
show_legend=True,
|
55 |
+
).style(color_map=color_map),
|
56 |
+
],
|
57 |
+
examples=examples,
|
58 |
+
theme=gr.themes.Base()
|
59 |
+
)
|
60 |
+
if __name__ == "__main__":
|
61 |
+
demo.launch(share=False)
|