File size: 4,661 Bytes
fdd3b76
46b1434
f66b5f5
fdd3b76
46b1434
 
27d2f67
46b1434
 
f66b5f5
 
46b1434
f66b5f5
46b1434
 
 
 
fdd3b76
 
 
46b1434
 
 
f66b5f5
 
46b1434
42dba5e
27d2f67
46b1434
 
827ed8f
 
 
074fb63
 
dbb1743
 
23690be
dbb1743
 
074fb63
 
dbb1743
 
46b1434
 
 
27d2f67
 
 
 
 
f66b5f5
27d2f67
 
46b1434
f66b5f5
 
27d2f67
 
 
 
 
46b1434
27d2f67
 
 
 
 
46b1434
27d2f67
46b1434
 
f66b5f5
46b1434
 
 
f66b5f5
46b1434
 
f66b5f5
46b1434
f66b5f5
 
 
 
 
46b1434
23690be
 
 
eccb5ee
ec1043f
23690be
 
 
46b1434
23690be
46b1434
 
27d2f67
 
46b1434
27d2f67
23690be
 
27d2f67
23690be
 
46b1434
27d2f67
46b1434
27d2f67
23690be
 
27d2f67
46b1434
27d2f67
46b1434
23690be
f66b5f5
 
 
23690be
f66b5f5
46b1434
ec1043f
27d2f67
f66b5f5
46b1434
27d2f67
f66b5f5
 
 
27d2f67
f66b5f5
48be82d
 
cbd5413
48be82d
742cd79
48be82d
 
827ed8f
23690be
074fb63
dbb1743
27d2f67
074fb63
 
46b1434
 
27d2f67
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
from PIL import ImageFilter, Image
from easyocr import Reader
import gradio as gr
import numpy as np
import openai
import ast
from transformers import pipeline
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 ner_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="Gönderildi, teşekkürler.", visible=True)


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


def text_dict(input):
    eval_result = ast.literal_eval(input)
    return (
        str(eval_result["il"]),
        str(eval_result["ilce"]),
        str(eval_result["mahalle"]),
        str(eval_result["sokak"]),
        str(eval_result["Apartman/site"]),
        str(eval_result["no"]),
        str(eval_result["ad-soyad"]),
        str(eval_result["dis kapi no"]),
    )


def ner_response(ocr_input):

    ner_pipe = pipeline("token-classification","deprem-ml/deprem-ner", aggregation_strategy="first")
    predictions = ner_pipe(ocr_input)
    resp = {}

    for item in predictions:
        print(item)
        key = item["entity_group"]
        resp[key] = item["word"]
    
    resp["input"] = ocr_input
    dict_keys = ["il", "ilce", "mahalle", "sokak", "Apartman/site", "no", "ad-soyad", "dis kapi no"]
    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():
        with gr.Column():
            img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
            img_area_button = gr.Button(value="Görüntüyü İşle", label="Submit")

        with gr.Column():
            text_area = gr.Textbox(label="Metin yükleyin 👇 ", lines=8)
            text_area_button = gr.Button(value="Metni Yükle", label="Submit")

    open_api_text = gr.Textbox(label="Tam Adres")

    with gr.Column():
        with gr.Row():
            il = gr.Textbox(label="İl", interactive=True, show_progress=False)
            ilce = gr.Textbox(label="İlçe", interactive=True, show_progress=False)
        with gr.Row():
            mahalle = gr.Textbox(
                label="Mahalle", interactive=True, show_progress=False
            )
            sokak = gr.Textbox(
                label="Sokak/Cadde/Bulvar", interactive=True, show_progress=False
            )
        with gr.Row():
            no = gr.Textbox(label="Telefon", interactive=True, show_progress=False)
        with gr.Row():
            ad_soyad = gr.Textbox(
                label="İsim Soyisim", interactive=True, show_progress=False
            )
            apartman = gr.Textbox(label="apartman", interactive=True, show_progress=False)
        with gr.Row():
            dis_kapi_no = gr.Textbox(label="Kapı No", interactive=True, show_progress=False)

    img_area_button.click(
        get_parsed_address,
        inputs=img_area,
        outputs=open_api_text,
        api_name="upload-image",
    )

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


    open_api_text.change(
        text_dict,
        open_api_text,
        [il, ilce, mahalle, sokak, no, apartman, ad_soyad, dis_kapi_no],
    )
    ocr_button = gr.Button(value="Sadece OCR kullan")
    ocr_button.click(
        get_text,
        inputs=img_area,
        outputs=text_area,
        api_name="get-ocr-output",
    )
    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 [il, ilce, mahalle, sokak, apartman, no, ad_soyad, dis_kapi_no]:
        submit_button.click(fn=clear_textbox, inputs=txt, outputs=txt)


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