address-parser / app.py
louisdanghn's picture
Update app.py
16bdc89 verified
import gradio as gr
import siameser
import norm_typing
import json
import logging
# import os
# import parameters
# import stat
# import utils
std = siameser.Siameser(stadard_scope='all')
# directory = parameters.LOG_DIRECTORY
# file_path = os.path.join(directory, parameters.LOG_RESULT_FILE)
# if not os.path.exists(directory):
# os.mkdir(directory)
# os.chmod(directory, stat.S_IRWXU)
# data = []
# with open(file_path, 'w', encoding='utf8') as f:
# json.dump(data, f, ensure_ascii=False, indent=4)
# os.chmod(file_path, stat.S_IRUSR | stat.S_IWUSR)
count = 0
def standardize(raw_address_input):
global count
raw_address = norm_typing.norm_vietnamese_sentence_accent(raw_address_input)
top_1, top_5 = std.get_top_k(raw_address, 5)
# utils.save_result(file_path, top_5)
count += 1
if count % 10 == 9:
print(f'Request: {count}')
return top_1, top_5
with gr.Blocks() as iface:
gr.Markdown("## Chuẩn hóa địa chỉ tiếng Việt")
gr.Markdown(
"""
Công cụ sử dụng để chuẩn hóa địa chỉ tiếng Việt. <br>
Nhập vào 1 câu địa chỉ thô (ví dụ ở dưới), mô hình sẽ chuẩn hóa thành địa chỉ chuẩn dưới dạng bảng, gồm 2 phần: <br>
* **Địa chỉ chi tiết (Detail Address):** thông tin về số nhà, ngõ ngách, hẻm,... <br>
* **Địa chỉ chính (Main Address):** hiển thị tối đa 3 trên 4 trường: đường/phố, phường/xã, quận/huyện, tỉnh/thành phố. <br>
"""
)
with gr.Row():
raw_address_input = gr.Textbox(
label='Raw Address',
placeholder="Nhập địa chỉ thô"
)
with gr.Row():
clear_button = gr.Button("Clear", scale=1)
submit_button = gr.Button("Submit", variant="primary", scale=1)
with gr.Row():
standard_address_output = gr.JSON(
label='Standard Address',
# headers=["Detail Address", "Street", "Ward", "District", "City"]
)
with gr.Row():
top5_addresses_output = gr.JSON(
label='Top 5 Standard Addresses',
# headers=["Detail Address", "Street", "Ward", "District", "City"]
)
submit_button.click(standardize, inputs=raw_address_input, outputs=[standard_address_output, top5_addresses_output], api_name="standardize")
clear_button.click(lambda: "", inputs=[], outputs=[raw_address_input])
gr.Examples([
['1 dong khoi str., dist. 1 ,hcmc'],
['112/21 bạch đằng, p.2, tân bình, tp. hồ chí minh'],
['văn phòng và căn hộ cao cấp licogi 13 tower , thanh xuân , hn'],
['dablend hostel, 417/2 hoà hảo, phường 5, quận 10, hồ chí minh, vietnam'],
['17-05, tower 4,the sun avenue, 28 mai chi tho, district 2, ho chi minh city']
], inputs=raw_address_input)
if __name__ == "__main__":
iface.launch()