File size: 3,064 Bytes
93e93f5
 
 
 
 
 
 
 
 
 
7a505c5
93e93f5
 
 
 
 
 
 
 
 
 
 
7a505c5
93e93f5
7a505c5
 
 
 
 
 
 
 
 
93e93f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a505c5
 
93e93f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a505c5
 
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
import gradio as gr
from easyocr import Reader
from PIL import Image
import io
import json
import csv
import openai



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



def get_text(input_img):
    result = reader.readtext(input_img, detail=0)
    return " ".join(result)


def save_csv(mahalle, il, sokak, apartman):

    adres_full = f"{mahalle}, {il}, {sokak}, {apartman}"
    with open("adress_book.csv", "a", encoding="utf-8") as f:
        book = f.read()
        if adres_full not in book:
            with open("adress_book.csv", "a", encoding="utf-8") as f:
                write = csv.writer(f)
                write.writerow(adres_full)
        else:
            adres_full = "Bu adres daha önce raporlanmış."
        
        return adres_full


def get_json(mahalle, il, sokak, apartman):
    adres = {"mahalle": mahalle, "il": il, "sokak": sokak, "apartman": apartman}
    dump = json.dumps(adres, indent=4, ensure_ascii=False)
    return dump


def openai_response(ocr_input):
    prompt = f"""Address Extractor
    You are a highly intelligent and accurate address  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
    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: 'Sehir:İstanbul', 'Ilce:Beşiktaş', 'Mahalle:Yıldız Mahallesi', 'Cadde: Cumhuriyet Caddesi', 'Apartman:no:35', 'Telefon: 5551231256', 'isim:Ahmet Yılmaz'


    Input: {ocr_input}
    Output:"""



    response = openai.Completion.create(
    model="text-davinci-003",
    prompt=prompt,
    temperature=0,
    max_tokens=100,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.0,
    stop=["\n"],
)

    return response["choices"][0]['text']


with gr.Blocks() as demo:
    gr.Markdown("# Enkaz Raporlama")
    gr.Markdown("Bu aplikasyonda üzerinde adres olan bir görüntüyü sürükleyip bırakarak konumu afet koordinasyona raporlayabilirsiniz.")
    with gr.Row():
        img_area = gr.Image()
        ocr_result = gr.Textbox(label="OCR")
        open_api_text = gr.Textbox(label="OPENAI")
    
    submit_button = gr.Button()
    submit_button.click(get_text, img_area, ocr_result)

    ocr_result.change(openai_response, ocr_result, open_api_text)

    with gr.Column():
        with gr.Row():
            mahalle = gr.Textbox(label="mahalle")
            sokak = gr.Textbox(label="sokak")
        with gr.Row():
            apartman = gr.Textbox(label="apartman")
            il = gr.Textbox(label="il")
        tarif = gr.Textbox(label="Tarif")

    json_out = gr.Textbox()
    csv_out = gr.Textbox()

    adres_submit = gr.Button()
    adres_submit.click(get_json, [mahalle, il, sokak, apartman], json_out)
    adres_submit.click(save_csv, [mahalle, il, sokak, apartman], csv_out)




demo.launch()