mertcobanov commited on
Commit
984392a
1 Parent(s): 139297a
Files changed (1) hide show
  1. app.py +112 -51
app.py CHANGED
@@ -5,32 +5,26 @@ import io
5
  import json
6
  import csv
7
  import openai
 
 
8
 
9
 
10
-
11
- openai.api_key = os.getenv("API_KEY")
12
  reader = Reader(["tr"])
13
 
14
 
15
-
16
  def get_text(input_img):
17
  result = reader.readtext(input_img, detail=0)
18
  return " ".join(result)
19
 
20
 
21
  def save_csv(mahalle, il, sokak, apartman):
 
22
 
23
- adres_full = f"{mahalle}, {il}, {sokak}, {apartman}"
24
  with open("adress_book.csv", "a", encoding="utf-8") as f:
25
- book = f.read()
26
- if adres_full not in book:
27
- with open("adress_book.csv", "a", encoding="utf-8") as f:
28
- write = csv.writer(f)
29
- write.writerow(adres_full)
30
- else:
31
- adres_full = "Bu adres daha önce raporlanmış."
32
-
33
- return adres_full
34
 
35
 
36
  def get_json(mahalle, il, sokak, apartman):
@@ -39,64 +33,131 @@ def get_json(mahalle, il, sokak, apartman):
39
  return dump
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def openai_response(ocr_input):
43
- prompt = f"""Address Extractor
44
- 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
45
- Examples:
46
 
47
- 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
48
- Output: 'Sehir:İstanbul', 'Ilce:Beşiktaş', 'Mahalle:Yıldız Mahallesi', 'Cadde: Cumhuriyet Caddesi', 'Apartman:no:35', 'Telefon: 5551231256', 'isim:Ahmet Yılmaz'
49
 
 
50
 
51
- Input: {ocr_input}
52
- Output:"""
53
 
54
 
 
 
55
 
56
- response = openai.Completion.create(
57
- model="text-davinci-003",
58
- prompt=prompt,
59
- temperature=0,
60
- max_tokens=100,
61
- top_p=1,
62
- frequency_penalty=0.0,
63
- presence_penalty=0.0,
64
- stop=["\n"],
65
- )
66
 
67
- return response["choices"][0]['text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
 
70
  with gr.Blocks() as demo:
71
- gr.Markdown("# Enkaz Raporlama")
72
- gr.Markdown("Bu aplikasyonda üzerinde adres olan bir görüntüyü sürükleyip bırakarak konumu afet koordinasyona raporlayabilirsiniz.")
73
  with gr.Row():
74
  img_area = gr.Image()
75
- ocr_result = gr.Textbox(label="Okunan Metin")
76
- open_api_text = gr.Textbox(label="Ayrılmış Çıktı")
77
-
78
- submit_button = gr.Button()
79
- submit_button.click(get_text, img_area, ocr_result)
80
-
81
- ocr_result.change(openai_response, ocr_result, open_api_text)
82
 
83
  with gr.Column():
84
  with gr.Row():
85
- mahalle = gr.Textbox(label="Mahalle")
86
- sokak = gr.Textbox(label="Sokak")
 
 
 
 
 
 
87
  with gr.Row():
88
- apartman = gr.Textbox(label="Apartman")
89
- il = gr.Textbox(label="İl")
90
- tarif = gr.Textbox(label="Tarif")
91
 
92
- json_out = gr.Textbox("Ayrılmış Adres")
93
- csv_out = gr.Textbox("Adres Kaydetme")
 
 
94
 
95
- adres_submit = gr.Button()
96
- adres_submit.click(get_json, [mahalle, il, sokak, apartman], json_out)
97
- adres_submit.click(save_csv, [mahalle, il, sokak, apartman], csv_out)
 
 
 
 
 
98
 
 
 
99
 
 
 
 
100
 
101
 
102
- demo.launch()
 
 
5
  import json
6
  import csv
7
  import openai
8
+ import ast
9
+ import os
10
 
11
 
12
+ openai.api_key = os.getenv['API_KEY']
 
13
  reader = Reader(["tr"])
14
 
15
 
 
16
  def get_text(input_img):
17
  result = reader.readtext(input_img, detail=0)
18
  return " ".join(result)
19
 
20
 
21
  def save_csv(mahalle, il, sokak, apartman):
22
+ adres_full = [mahalle, il, sokak, apartman]
23
 
 
24
  with open("adress_book.csv", "a", encoding="utf-8") as f:
25
+ write = csv.writer(f)
26
+ write.writerow(adres_full)
27
+ return adres_full
 
 
 
 
 
 
28
 
29
 
30
  def get_json(mahalle, il, sokak, apartman):
 
33
  return dump
34
 
35
 
36
+ def text_dict_il(input):
37
+ eval_result = ast.literal_eval(input)["il"]
38
+
39
+ return eval_result
40
+
41
+
42
+ def text_dict_mahalle(input):
43
+ eval_result = ast.literal_eval(input)["mahalle"]
44
+
45
+ return eval_result
46
+
47
+
48
+ def text_dict_ilce(input):
49
+ eval_result = ast.literal_eval(input)["ilçe"]
50
+
51
+ return eval_result
52
+
53
+
54
+ def text_dict_sokak(input):
55
+ eval_result = ast.literal_eval(input)["sokak"]
56
+
57
+ return eval_result
58
+
59
+
60
+ def text_dict_no(input):
61
+ eval_result = ast.literal_eval(input)["no"]
62
+
63
+ return eval_result
64
+
65
+
66
+ def text_dict_tel(input):
67
+ eval_result = ast.literal_eval(input)["tel"]
68
+
69
+ return eval_result
70
+
71
+
72
+ def text_dict_isim(input):
73
+ eval_result = ast.literal_eval(input)["isim_soyisim"]
74
+ return eval_result
75
+
76
+
77
+ def text_dict_adres(input):
78
+ eval_result = ast.literal_eval(input)["adres"]
79
+
80
+ return eval_result
81
+
82
+
83
  def openai_response(ocr_input):
84
+ prompt = f"""Tabular Data Extraction
85
+ 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
 
86
 
87
+ 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 and should be returned as an empty string
 
88
 
89
+ Have only il, ilçe, mahalle, sokak, no, tel, isim_soyisim, adres
90
 
91
+ Examples:
 
92
 
93
 
94
+ 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
95
+ Output: [{{'Tabular': '{{'il': 'İstanbul', 'ilçe': 'Beşiktaş', 'mahalle': 'Yıldız Mahallesi', 'sokak': 'Cumhuriyet Caddesi', 'no': 35, 'tel': 5551231256, 'isim_soyisim': 'Ahmet Yılmaz', 'adres': 'İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35'}}' }}]
96
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+ Input: {ocr_input}
99
+ Output:
100
+
101
+ """
102
+
103
+ response = openai.Completion.create(
104
+ model="text-davinci-003",
105
+ prompt=prompt,
106
+ temperature=0,
107
+ max_tokens=300,
108
+ top_p=1,
109
+ frequency_penalty=0.0,
110
+ presence_penalty=0.0,
111
+ stop=["\n"],
112
+ )
113
+ resp = response["choices"][0]["text"]
114
+ resp = eval(resp.replace("'{", "{").replace("}'", "}"))
115
+ resp = resp[0]["Tabular"]
116
+ return resp
117
 
118
 
119
  with gr.Blocks() as demo:
120
+ gr.Markdown(""" # Image to Text - Adres""")
 
121
  with gr.Row():
122
  img_area = gr.Image()
123
+ ocr_result = gr.Textbox(label="OCR")
124
+ open_api_text = gr.Textbox(label="OPENAI")
 
 
 
 
 
125
 
126
  with gr.Column():
127
  with gr.Row():
128
+ il = gr.Textbox(label="il")
129
+ ilce = gr.Textbox(label="ilce")
130
+ with gr.Row():
131
+ mahalle = gr.Textbox(label="mahalle")
132
+ sokak = gr.Textbox(label="sokak/cadde/bulvar")
133
+ with gr.Row():
134
+ no = gr.Textbox(label="no")
135
+ tel = gr.Textbox(label="tel")
136
  with gr.Row():
137
+ isim_soyisim = gr.Textbox(label="isim_soyisim")
138
+ adres = gr.Textbox(label="adres")
 
139
 
140
+ submit_button = gr.Button()
141
+ submit_button.click(get_text, img_area, ocr_result)
142
+
143
+ ocr_result.change(openai_response, ocr_result, open_api_text)
144
 
145
+ open_api_text.change(text_dict_il, [open_api_text], il)
146
+ open_api_text.change(text_dict_ilce, [open_api_text], ilce)
147
+ open_api_text.change(text_dict_mahalle, [open_api_text], mahalle)
148
+ open_api_text.change(text_dict_sokak, [open_api_text], sokak)
149
+ open_api_text.change(text_dict_no, [open_api_text], no)
150
+ open_api_text.change(text_dict_adres, [open_api_text], adres)
151
+ open_api_text.change(text_dict_tel, [open_api_text], tel)
152
+ open_api_text.change(text_dict_isim, [open_api_text], isim_soyisim)
153
 
154
+ # json_out = gr.Textbox()
155
+ # csv_out = gr.Textbox()
156
 
157
+ # adres_submit = gr.Button()
158
+ # adres_submit.click(get_json, [mahalle, il, sokak, apartman], json_out)
159
+ # adres_submit.click(save_csv, [mahalle, il, sokak, apartman], csv_out)
160
 
161
 
162
+ if __name__ == "__main__":
163
+ demo.launch()