merve HF staff commited on
Commit
557995c
1 Parent(s): f779d60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -62
app.py CHANGED
@@ -38,26 +38,26 @@ def get_json(mahalle, il, sokak, apartman):
38
  return dump
39
 
40
 
41
- def text_dict_il(input):
42
- eval_result = str(ast.literal_eval(input)["il"])
43
 
44
  return eval_result
45
 
46
 
47
- def text_dict_mahalle(input):
48
- eval_result = str(ast.literal_eval(input)["mahalle"])
49
 
50
  return eval_result
51
 
52
 
53
- def text_dict_ilce(input):
54
- eval_result = str(ast.literal_eval(input)["ilçe"])
55
 
56
  return eval_result
57
 
58
 
59
- def text_dict_sokak(input):
60
- eval_result = str(ast.literal_eval(input)["sokak"])
61
 
62
  return eval_result
63
 
@@ -74,46 +74,35 @@ def text_dict_tel(input):
74
  return eval_result
75
 
76
 
77
- def text_dict_isim(input):
78
- eval_result = str(ast.literal_eval(input)["isim_soyisim"])
79
  return eval_result
80
 
81
 
82
- def text_dict_adres(input):
83
- eval_result = str(ast.literal_eval(input)["adres"])
84
 
85
  return eval_result
86
 
87
- def text_dict_ickapi(input):
88
- eval_result = str(ast.literal_eval(input)["ickapi_no"])
89
-
90
- return eval_result
91
-
92
- def text_dict_diskapi(input):
93
- eval_result = str(ast.literal_eval(input)["diskapi_no"])
94
 
95
  return eval_result
96
 
97
 
 
98
  def openai_response(ocr_input):
99
- prompt = f"""Tabular Data Extraction
100
- 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
101
-
102
- 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
103
-
104
- Have only il, ilçe, mahalle, sokak, apartman, ickapi_no, diskapi_no, tel, isim_soyisim, adres
105
-
106
- Examples:
107
-
108
-
109
- Input: Deprem sırasında evimizde yer alan adresimiz: İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35/8, tuna çamlık evler sitesi, cep telefonu numaram 5551231256, adim Ahmet Yilmaz
110
- Output: [{{'Tabular': '{{'il': 'İstanbul', 'ilçe': 'Beşiktaş', 'mahalle': 'Yıldız Mahallesi', 'sokak': 'Cumhuriyet Caddesi', 'apartman': 'tuna çamlık evler sitesi', 'diskapi_no': 35, 'ickapi_no': 8, 'tel': 5551231256, 'isim_soyisim': 'Ahmet Yılmaz', 'adres': 'İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35'}}' }}]
111
-
112
-
113
- Input: {ocr_input}
114
- Output:
115
-
116
- """
117
 
118
  response = openai.Completion.create(
119
  model="text-davinci-003",
@@ -127,7 +116,21 @@ Output:
127
  )
128
  resp = response["choices"][0]["text"]
129
  resp = eval(resp.replace("'{", "{").replace("}'", "}"))
130
- resp = resp[0]["Tabular"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  return resp
132
 
133
 
@@ -144,42 +147,34 @@ with gr.Blocks() as demo:
144
  submit_button = gr.Button(label="Yükle")
145
  with gr.Column():
146
  with gr.Row():
147
- il = gr.Textbox(label="İl")
148
- ilce = gr.Textbox(label="İlçe")
149
  with gr.Row():
150
- mahalle = gr.Textbox(label="Mahalle")
151
- sokak = gr.Textbox(label="Sokak/Cadde/Bulvar")
152
  with gr.Row():
153
  tel = gr.Textbox(label="Telefon")
154
  with gr.Row():
155
- isim_soyisim = gr.Textbox(label="İsim Soyisim")
156
- adres = gr.Textbox(label="Adres")
157
  with gr.Row():
158
- ickapi_no = gr.Textbox(label="İç Kapı No")
159
- diskapi_no = gr.Textbox(label="Dış Kapı No")
160
 
161
  submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
162
 
163
  ocr_result.change(openai_response, ocr_result, open_api_text, api_name="upload-text")
164
 
165
- open_api_text.change(text_dict_il, [open_api_text], il)
166
- open_api_text.change(text_dict_ilce, [open_api_text], ilce)
167
- open_api_text.change(text_dict_mahalle, [open_api_text], mahalle)
168
- open_api_text.change(text_dict_sokak, [open_api_text], sokak)
169
- open_api_text.change(text_dict_adres, [open_api_text], adres)
170
  open_api_text.change(text_dict_tel, [open_api_text], tel)
171
- open_api_text.change(text_dict_isim, [open_api_text], isim_soyisim)
172
- open_api_text.change(text_dict_diskapi, [open_api_text], diskapi_no)
173
- open_api_text.change(text_dict_ickapi, [open_api_text], ickapi_no)
174
 
175
 
176
- # json_out = gr.Textbox()
177
- # csv_out = gr.Textbox()
178
-
179
- # adres_submit = gr.Button()
180
- # adres_submit.click(get_json, [mahalle, il, sokak, apartman], json_out)
181
- # adres_submit.click(save_csv, [mahalle, il, sokak, apartman], csv_out)
182
-
183
 
184
  if __name__ == "__main__":
185
- demo.launch()
 
38
  return dump
39
 
40
 
41
+ def text_dict_city(input):
42
+ eval_result = str(ast.literal_eval(input)["city"])
43
 
44
  return eval_result
45
 
46
 
47
+ def text_dict_neighbourhood(input):
48
+ eval_result = str(ast.literal_eval(input)["neighbourhood"])
49
 
50
  return eval_result
51
 
52
 
53
+ def text_dict_distinct(input):
54
+ eval_result = str(ast.literal_eval(input)["distinct"])
55
 
56
  return eval_result
57
 
58
 
59
+ def text_dict_street(input):
60
+ eval_result = str(ast.literal_eval(input)["street"])
61
 
62
  return eval_result
63
 
 
74
  return eval_result
75
 
76
 
77
+ def text_dict_name(input):
78
+ eval_result = str(ast.literal_eval(input)["name_surname"])
79
  return eval_result
80
 
81
 
82
+ def text_dict_address(input):
83
+ eval_result = str(ast.literal_eval(input)["address"])
84
 
85
  return eval_result
86
 
87
+ def text_dict_no(input):
88
+ eval_result = str(ast.literal_eval(input)["no"])
 
 
 
 
 
89
 
90
  return eval_result
91
 
92
 
93
+
94
  def openai_response(ocr_input):
95
+ prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from
96
+ plain text input and especially from emergency text that carries address information, your inputs can be text
97
+ of arbitrary size, but the output should be in [{{'tabular': {{'entity_type': 'entity'}} }}] JSON format Force it
98
+ to only extract keys that are shared as an example in the examples section, if a key value is not found in the
99
+ text input, then it should be ignored. Have only city, distinct, neighbourhood,
100
+ street, no, tel, name_surname, address Examples: Input: Deprem sırasında evimizde yer alan adresimiz: İstanbul,
101
+ Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35, cep telefonu numaram 5551231256, adim Ahmet Yilmaz
102
+ 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'}}
103
+ Input: {ocr_input}
104
+ Output:
105
+ """
 
 
 
 
 
 
 
106
 
107
  response = openai.Completion.create(
108
  model="text-davinci-003",
 
116
  )
117
  resp = response["choices"][0]["text"]
118
  resp = eval(resp.replace("'{", "{").replace("}'", "}"))
119
+ resp["input"] = ocr_input
120
+ dict_keys = [
121
+ 'city',
122
+ 'distinct',
123
+ 'neighbourhood',
124
+ 'street',
125
+ 'no',
126
+ 'tel',
127
+ 'name_surname',
128
+ 'address',
129
+ 'input',
130
+ ]
131
+ for key in dict_keys:
132
+ if key not in resp.keys():
133
+ resp[key] = ''
134
  return resp
135
 
136
 
 
147
  submit_button = gr.Button(label="Yükle")
148
  with gr.Column():
149
  with gr.Row():
150
+ city = gr.Textbox(label="İl")
151
+ distinct = gr.Textbox(label="İlçe")
152
  with gr.Row():
153
+ neighbourhood = gr.Textbox(label="Mahalle")
154
+ street = gr.Textbox(label="Sokak/Cadde/Bulvar")
155
  with gr.Row():
156
  tel = gr.Textbox(label="Telefon")
157
  with gr.Row():
158
+ name_surname = gr.Textbox(label="İsim Soyisim")
159
+ address = gr.Textbox(label="Adres")
160
  with gr.Row():
161
+ no = gr.Textbox(label="Kapı No")
162
+
163
 
164
  submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
165
 
166
  ocr_result.change(openai_response, ocr_result, open_api_text, api_name="upload-text")
167
 
168
+ open_api_text.change(text_dict_city, [open_api_text], city)
169
+ open_api_text.change(text_dict_distinct, [open_api_text], distinct)
170
+ open_api_text.change(text_dict_neighbourhood, [open_api_text], neighbourhood)
171
+ open_api_text.change(text_dict_street, [open_api_text], street)
172
+ open_api_text.change(text_dict_address, [open_api_text], address)
173
  open_api_text.change(text_dict_tel, [open_api_text], tel)
174
+ open_api_text.change(text_dict_name, [open_api_text], name_surname)
175
+ open_api_text.change(text_dict_no, [open_api_text], no)
 
176
 
177
 
 
 
 
 
 
 
 
178
 
179
  if __name__ == "__main__":
180
+ demo.launch()