File size: 7,000 Bytes
fdd3b76
46b1434
f66b5f5
fdd3b76
46b1434
 
 
 
f66b5f5
 
46b1434
f66b5f5
46b1434
 
 
 
fdd3b76
 
 
46b1434
 
 
f66b5f5
 
46b1434
42dba5e
f66b5f5
46b1434
 
827ed8f
 
 
074fb63
 
dbb1743
 
074fb63
dbb1743
 
074fb63
 
dbb1743
 
f66b5f5
46b1434
 
 
f66b5f5
 
 
 
 
 
 
 
46b1434
f66b5f5
 
46b1434
 
 
 
 
 
827ed8f
 
 
46b1434
827ed8f
 
 
 
 
 
 
 
 
 
46b1434
 
 
 
 
31c4737
46b1434
 
 
 
 
f66b5f5
 
 
 
 
 
 
 
 
46b1434
 
 
f66b5f5
46b1434
 
 
f66b5f5
46b1434
 
f66b5f5
46b1434
f66b5f5
 
 
 
 
46b1434
 
ec1043f
 
 
46b1434
ec1043f
46b1434
 
0e5938b
 
46b1434
0e5938b
 
46b1434
0e5938b
46b1434
0e5938b
 
46b1434
0e5938b
46b1434
0e5938b
f66b5f5
 
 
 
 
46b1434
ec1043f
 
f66b5f5
46b1434
f66b5f5
 
 
 
 
827ed8f
 
dbb1743
074fb63
dbb1743
074fb63
 
 
46b1434
 
f66b5f5
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
from PIL import ImageFilter, Image
from easyocr import Reader
import gradio as gr
import numpy as np
import openai
import ast
import os

from openai_api import OpenAI_API
import utils

openai.api_key = os.getenv("API_KEY")
reader = Reader(["tr"])


def get_text(input_img):
    img = Image.fromarray(input_img)
    detailed = np.asarray(img.filter(ImageFilter.DETAIL))
    result = reader.readtext(detailed, detail=0, paragraph=True)
    return " ".join(result)


# Submit button
def get_parsed_address(input_img):

    address_full_text = get_text(input_img)
    return openai_response(address_full_text)


def save_deta_db(input):
    eval_result = ast.literal_eval(input)
    utils.write_db(eval_result)
    return


def update_component():
    return gr.update(value="Gonderildi, tesekkurler.", visible=True)


def clear_textbox(value):
    return gr.update(value="")


# Open API on change
def text_dict(input):
    eval_result = ast.literal_eval(input)
    return (
        str(eval_result["city"]),
        str(eval_result["distinct"]),
        str(eval_result["neighbourhood"]),
        str(eval_result["street"]),
        str(eval_result["address"]),
        str(eval_result["tel"]),
        str(eval_result["name_surname"]),
        str(eval_result["no"]),
    )


def openai_response(ocr_input):
    prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from 
            plain text input and especially from emergency text that carries address information, your inputs can be text 
            of arbitrary size, but the output should be in [{{'tabular': {{'entity_type': 'entity'}} }}] JSON format Force it 
            to only extract keys that are shared as an example in the examples section, if a key value is not found in the 
            text input, then it should be ignored. Have only city, distinct, neighbourhood, 
            street, no, tel, name_surname, address Examples:
            
            Input: Deprem sırasında evimizde yer alan adresimiz: İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35, cep telefonu numaram 5551231256, adim Ahmet Yilmaz 
            Output: {{'city': 'İstanbul', 'distinct': 'Beşiktaş', 'neighbourhood': 'Yıldız Mahallesi', 'street': 'Cumhuriyet Caddesi', 'no': '35', 'tel': '5551231256', 'name_surname': 'Ahmet Yılmaz', 'address': 'İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35'}}
            
            Input: 5.29 PMO $ 0 87 DEVREMİZ ÖZGÜR ORÇAN ARKADAŞIMIZA ULAŞAMIYORUZ BEYOĞLU MAH FEVZİ ÇAKMAK CAD. NO.58-TÜRKOĞLUI KAHRAMANMARAŞ 5524357578 AdReSe YaKIN OLANLAR VEYA ULASANLAR LÜTFEN BiLGILENDIRSIN .
            Output: {{'city': 'Kahramanmaraş', 'distinct': 'Türkoğlu', 'neighbourhood': 'Beyoğlu Mahallesi', 'street': 'Çakmak Caddesi', 'no': '58', 'tel': '5524357578', 'name_surname': 'Özgür Orçan', 'address': 'Beyoğlu Mahallesi, Çakmak Caddesi, No:58 Türkoğlu/Kahramanmaraş'}}

            Input: Ahmet @ozknhmt Ekim 2021 tarihinde katıldı - 2 Takipçi Takip ettiğin kimse takip etmiyor AKEVLER MAH. 432SK RÜYA APT ANT(BEDİİ SABUNCU KARŞISI) ANTAKYA HATAY MERVE BELANLI ses veriyor ancak hiçbiryardım ekibi olmadığı için kurtaramryoruz içeri girip, lütfen acil yardım_ İsim: Merve Belanlı tel 542 757 5484 Ö0 12.07
            Output: {{'city': 'Hatay', 'distinct': 'Antakya', 'neighbourhood': 'Akevler Mahallesi', 'street': '432 Sokak', 'no': '', 'tel': '5427575484', 'name_surname': 'Merve Belanlı', 'address': 'Akevler Mahallesi, 432 Sokak, Rüya Apt. Antakya/Hatay'}}
            
            Input: 14:04 Sümerler Cemil Şükrü Çolokoğlu ilköğretim okulu karşısı 3 9öçük altında yardım bekyouk Lütfen herkes paylogsın
            Output: {{'city': '', 'distinct': '', 'neighbourhood': 'Sümerler Mahallesi', 'street': 'Cemil Şükrü Çolokoğlu İlköğretim Okulu Karşısı', 'no': '', 'tel': '', 'name_surname': '', 'address': 'Sümerler Mahallesi, Cemil Şükrü Çolokoğlu İlköğretim Okulu Karşısı'}}
                        
            Input: {ocr_input}
            Output:
        """

    openai_client = OpenAI_API()
    response = openai_client.single_request(prompt)
    resp = response["choices"][0]["text"]
    print(resp)
    resp = eval(resp.replace("'{", "{").replace("}'", "}"))
    resp["input"] = ocr_input
    dict_keys = [
        "city",
        "distinct",
        "neighbourhood",
        "street",
        "no",
        "tel",
        "name_surname",
        "address",
        "input",
    ]
    for key in dict_keys:
        if key not in resp.keys():
            resp[key] = ""
    return resp


# User Interface
with gr.Blocks() as demo:
    gr.Markdown(
        """
    # Enkaz Bildirme Uygulaması
    """
    )
    gr.Markdown(
        "Bu uygulamada ekran görüntüsü sürükleyip bırakarak AFAD'a enkaz bildirimi yapabilirsiniz. Mesajı metin olarak da girebilirsiniz, tam adresi ayrıştırıp döndürür. API olarak kullanmak isterseniz sayfanın en altında use via api'ya tıklayın."
    )
    with gr.Row():
        img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
        with gr.Column():
            text_area = gr.Textbox(label="Metin yükleyin 👇 ")
            text_area_button = gr.Button(value='Metni Yukle', label='Submit')
    open_api_text = gr.Textbox(label="Tam Adres")
    run_button = gr.Button(value="Veriyi İşle")
    with gr.Column():
        with gr.Row():
            city = gr.Textbox(label="İl", interactive=True)
            distinct = gr.Textbox(label="İlçe", interactive=True)
        with gr.Row():
            neighbourhood = gr.Textbox(label="Mahalle", interactive=True)
            street = gr.Textbox(label="Sokak/Cadde/Bulvar", interactive=True)
        with gr.Row():
            tel = gr.Textbox(label="Telefon", interactive=True)
        with gr.Row():
            name_surname = gr.Textbox(label="İsim Soyisim", interactive=True)
            address = gr.Textbox(label="Adres", interactive=True)
        with gr.Row():
            no = gr.Textbox(label="Kapı No", interactive=True)

    run_button.click(
        get_parsed_address,
        inputs=img_area,
        outputs=open_api_text,
        api_name="upload_image",
    )

    text_area_button.click(
        openai_response, text_area, open_api_text, api_name="upload-text"
    )

    open_api_text.change(
        text_dict,
        open_api_text,
        [city, distinct, neighbourhood, street, address, tel, name_surname, no],
    )

    submit_button = gr.Button(value="Veriyi Birimlere Yolla")
    # submit_button.click(save_deta_db, open_api_text)
    done_text = gr.Textbox(label="Done", value="Not Done", visible=False)
    submit_button.click(update_component, outputs=done_text)
    for txt in [city, distinct, neighbourhood, street, address, tel, name_surname, no]:
        submit_button.click(fn=clear_textbox, inputs=txt, outputs=txt)


if __name__ == "__main__":
    demo.launch()