File size: 8,701 Bytes
958e867
d10b4dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b96404b
d10b4dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d9cb25
ef6975f
 
 
 
 
 
ae9334c
ef6975f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10b4dc
 
 
 
 
 
b96404b
d10b4dc
b96404b
 
d10b4dc
 
 
b96404b
 
d10b4dc
 
b96404b
d10b4dc
b96404b
d10b4dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b96404b
d10b4dc
 
 
 
 
 
 
 
 
 
 
 
 
 
958e867
 
 
d10b4dc
 
 
958e867
d10b4dc
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import gradio as gr
import googlemaps
from dotenv import load_dotenv
import requests
import pyperclip
import os
from PIL import Image
from io import BytesIO
from transformers import pipeline

from messages import *

# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# PREPARATIONS
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #

# prepare Google Maps API
load_dotenv()
API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
gmaps = googlemaps.Client(key=API_KEY)

# prepare the HF model
save_path = "./saved_model"
oracle = pipeline('question-answering', tokenizer=save_path, model=save_path)

# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# FUNCTIONS
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
def extract_func(text, question_input, prev_addresses):
    oracle_output = oracle(question=question_input, context=text)
    answer = oracle_output['answer']
    prev_addresses += f'{answer}\n'
    return prev_addresses

def get_address(address):

    geocode_result = gmaps.geocode(address)
    formatted_address = geocode_result[0].get('formatted_address', address)
    # encoded_address = urllib.parse.quote(formatted_address)
    return formatted_address

def undo_func(addresses):
    new_addresses = [line for line in addresses.splitlines()]
    return_value = '\n'.join(new_addresses[0:-1])
    return_value += '\n'
    return return_value

def get_static_map(addresses, zoom=13):
    adr_list = []
    adr_str = ''
    line_id = 1
    for line in addresses.splitlines():
        if len(line) < 2:
            continue
        address = get_address(line)
        adr_list.append(address)
        adr_str += f'&markers=color:red|label:{line_id}|{address}'
        line_id += 1

    url = (
        "https://maps.googleapis.com/maps/api/staticmap"
        "?size=900x900"
        f"&zoom={zoom}"
        f"{adr_str}"
        f"&key={API_KEY}"
    )
    response = requests.get(url)
    image = Image.open(BytesIO(response.content))
    return image

# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# MAIN
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
with gr.Blocks() as demo:
    gr.Markdown("# ๐Ÿƒโ€โ™‚๏ธโ€โžก๏ธ๐Ÿ“ฆ๐Ÿ“ฆ๐Ÿ“ฆ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ‘จ๐Ÿ† Locate All Necessary Addresses on the Google Map By Couple Clicks")
    with gr.Accordion("About the project", open=False):
        gr.Markdown("""
        
        My wife orders _so many_ things online that sometimes I find myself chasing down a thousand packages scattered all 
        over the cityโ€”and occasionally even in multiple cities. The pickup locations are so random that I have to keep a 
        mental map of all of them and try to figure out the most efficient route, like Iโ€™m collecting Pokรฉmons.
    
        Thatโ€™s the pain. And thatโ€™s why I built this little spaceโ€”for me at first, and now for you too!
        Here, you can simply paste the message you get from the courier (limited only to Hebrew by now), 
        and the amazing model [dicta-il/dictabert-heq](https://huggingface.co/dicta-il/dictabert-heq) will extract the 
        address and add it to your list. Each new line in the address list represents a new location, 
        so feel free to correct or clean up the entries to help the system organize better.
        Then hit the "Locate" button, and itโ€™ll drop pins for all your packages on the map.
        When you can literally see all the locations in front of you, it becomes way easier to plan your route!
        
        A bit about me:
        Hi, Iโ€™m Arseniyโ€”and Iโ€™m currently looking for a job. This project is one of several Iโ€™ve created to support my 
        applications.
        If you like this demo, please consider sharing it, giving it a like on Hugging Face, mentioning it on LinkedIn โ€” 
        or OMG if you're hiring an algorithm developer / scientific researcher (please contact me!!), Iโ€™d be thrilled to connect. ๐Ÿ”ฅ
        
        Thanks so much :)
        
        """)
    with gr.Row():

        with gr.Column():
            # question = gr.Textbox(label='Question', value='ืžื” ื”ื›ืชื•ื‘ืช ืฉืœ ื”ืžืฉืœื•ื— ืฉื™ืฉ ื‘ื”ื•ื“ืขื”?')
            question = gr.Radio([
                "ืžื” ื”ื›ืชื•ื‘ืช?",
                "ืžื” ื”ื›ืชื•ื‘ืช ืฉืœ ื”ืžืฉืœื•ื— ืฉื™ืฉ ื‘ื”ื•ื“ืขื”?",
                "ืื™ื–ื• ื›ืชื•ื‘ืช ื‘ื™ืฉืจืืœ ื™ืฉ ื‘ื”ื•ื“ืขื”?"
            ], value="ืžื” ื”ื›ืชื•ื‘ืช?", label="Questions to the Courier's Message", info="Try different questions...")
            message_inp = gr.Textbox(label='Input Courier Message', placeholder='Enter the text message...', lines=5)
            with gr.Row():
                clear_btn_1 = gr.Button('Clear')
                new_paste_btn = gr.Button('Paste')
                extreact_btn = gr.Button('Extract Address', variant='primary')
            addresses_inp = gr.Textbox(placeholder='Enter the address...', label='Addresses (every new line is a new address and you can correct it, if needed)',
                                       lines=5, max_lines=None)
            undo_btn = gr.Button('Clear last row...')
            locate_btn = gr.Button('Locate All', variant='primary')
            clear_btn_2 = gr.Button('Clear')
            gr.Markdown('### Examples of messages of couriers')
            with gr.Row():
                example_1_btn = gr.Button('1')
                example_2_btn = gr.Button('2')
                example_3_btn = gr.Button('3')
                example_4_btn = gr.Button('4')

        with gr.Column():
            zoom = gr.Slider(1, 20, 14, step=1)
            with gr.Row():
                b_further = gr.Button('Zoom Out')
                b_closer = gr.Button('Zoom In')
            map_out = gr.Image()

    # events
    extreact_btn.click(fn=extract_func, inputs=[message_inp, question, addresses_inp], outputs=addresses_inp)
    # extreact_btn.click(fn=lambda x: '', inputs=message_inp, outputs=message_inp)
    clear_btn_1.click(fn=lambda x: '', inputs=message_inp, outputs=message_inp)
    new_paste_btn.click(fn=lambda _: pyperclip.paste(), inputs=message_inp, outputs=message_inp)
    undo_btn.click(fn=undo_func, inputs=addresses_inp, outputs=addresses_inp)
    locate_btn.click(fn=get_static_map, inputs=[addresses_inp, zoom], outputs=map_out)
    locate_btn.click(fn=lambda x: '', inputs=message_inp, outputs=message_inp)
    clear_btn_2.click(fn=lambda x: '', inputs=addresses_inp, outputs=addresses_inp)

    example_1_btn.click(fn=lambda x: examples_dict[x], inputs=example_1_btn, outputs=message_inp)
    example_2_btn.click(fn=lambda x: examples_dict[x], inputs=example_2_btn, outputs=message_inp)
    example_3_btn.click(fn=lambda x: examples_dict[x], inputs=example_3_btn, outputs=message_inp)
    example_4_btn.click(fn=lambda x: examples_dict[x], inputs=example_4_btn, outputs=message_inp)

    zoom.change(fn=get_static_map, inputs=[addresses_inp, zoom], outputs=map_out)
    b_further.click(fn=lambda x: max(x-1, 1), inputs=zoom, outputs=zoom)
    b_closer.click(fn=lambda x: min(x+1, 20), inputs=zoom, outputs=zoom)



demo.launch()
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #

# url = (
#         "https://maps.googleapis.com/maps/api/staticmap"
#         "?size=900x900"
#         f"&zoom={zoom}"
#         "&markers=color:blue|label:A|Williamsburg,Brooklyn,NY"
#         "&markers=color:red|label:B|Prospect+Park,Brooklyn,NY"
#         f"&key={API_KEY}"
#     )