mertcobanov commited on
Commit
21d4a5f
2 Parent(s): 164881b 56e16a2

Merge branch 'seperate_utils'

Browse files
Files changed (3) hide show
  1. app.py +52 -106
  2. openai_api.py +30 -0
  3. utils.py +35 -0
app.py CHANGED
@@ -1,114 +1,46 @@
1
- import gradio as gr
2
  from easyocr import Reader
3
- from PIL import Image
4
- import io
5
- import json
6
- import csv
7
  import openai
8
  import ast
9
  import os
10
- from deta import Deta
11
-
12
 
13
- ######################
14
- import requests
15
- import json
16
 
17
- import os
18
- import openai
19
 
20
-
21
-
22
- class OpenAI_API:
23
- def __init__(self):
24
- self.openai_api_key = ''
25
-
26
- def single_request(self, address_text):
27
-
28
- openai.api_type = "azure"
29
- openai.api_base = "https://damlaopenai.openai.azure.com/"
30
- openai.api_version = "2022-12-01"
31
- openai.api_key = os.getenv("API_KEY")
32
-
33
- response = openai.Completion.create(
34
- engine="Davinci-003",
35
- prompt=address_text,
36
- temperature=0.,#9,
37
- max_tokens=300,
38
- top_p=1.0,
39
- # n=1,
40
- # logprobs=0,
41
- # echo=False,
42
- # stop=None,
43
- frequency_penalty=0,
44
- presence_penalty=0,
45
- stop=["\n"],
46
- best_of=1)
47
-
48
- return response
49
-
50
- ########################
51
-
52
- openai.api_key = os.getenv('API_KEY')
53
  reader = Reader(["tr"])
54
 
55
 
56
- def get_parsed_address(input_img):
57
-
58
- address_full_text = get_text(input_img)
59
- return openai_response(address_full_text)
60
-
61
-
62
- def preprocess_img(inp_image):
63
- gray = cv2.cvtColor(inp_image, cv2.COLOR_BGR2GRAY)
64
- gray_img = cv2.bitwise_not(gray)
65
- return gray_img
66
-
67
-
68
  def get_text(input_img):
69
  result = reader.readtext(input_img, detail=0)
70
  return " ".join(result)
71
 
72
 
73
- def save_csv(mahalle, il, sokak, apartman):
74
- adres_full = [mahalle, il, sokak, apartman]
75
-
76
- with open("adress_book.csv", "a", encoding="utf-8") as f:
77
- write = csv.writer(f)
78
- write.writerow(adres_full)
79
- return adres_full
80
-
81
-
82
- def get_json(mahalle, il, sokak, apartman):
83
- adres = {"mahalle": mahalle, "il": il, "sokak": sokak, "apartman": apartman}
84
- dump = json.dumps(adres, indent=4, ensure_ascii=False)
85
- return dump
86
-
87
- def write_db(data_dict):
88
- # 2) initialize with a project key
89
- deta_key = os.getenv('DETA_KEY')
90
- deta = Deta(deta_key)
91
 
92
- # 3) create and use as many DBs as you want!
93
- users = deta.Base("deprem-ocr")
94
- users.insert(data_dict)
95
 
96
 
 
97
  def text_dict(input):
98
  eval_result = ast.literal_eval(input)
99
- write_db(eval_result)
100
 
101
  return (
102
- str(eval_result['city']),
103
- str(eval_result['distinct']),
104
- str(eval_result['neighbourhood']),
105
- str(eval_result['street']),
106
- str(eval_result['address']),
107
- str(eval_result['tel']),
108
- str(eval_result['name_surname']),
109
- str(eval_result['no']),
110
  )
111
-
 
112
  def openai_response(ocr_input):
113
  prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from
114
  plain text input and especially from emergency text that carries address information, your inputs can be text
@@ -129,19 +61,19 @@ def openai_response(ocr_input):
129
  resp = eval(resp.replace("'{", "{").replace("}'", "}"))
130
  resp["input"] = ocr_input
131
  dict_keys = [
132
- 'city',
133
- 'distinct',
134
- 'neighbourhood',
135
- 'street',
136
- 'no',
137
- 'tel',
138
- 'name_surname',
139
- 'address',
140
- 'input',
141
  ]
142
  for key in dict_keys:
143
  if key not in resp.keys():
144
- resp[key] = ''
145
  return resp
146
 
147
  def ner_response(ocr_input):
@@ -157,12 +89,16 @@ def ner_response(ocr_input):
157
  })
158
  return output
159
 
 
160
  with gr.Blocks() as demo:
161
  gr.Markdown(
162
- """
163
  # Enkaz Bildirme Uygulaması
164
- """)
165
- 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.")
 
 
 
166
  with gr.Row():
167
  img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
168
  ocr_result = gr.Textbox(label="Metin yükleyin 👇 ")
@@ -183,13 +119,23 @@ with gr.Blocks() as demo:
183
  with gr.Row():
184
  no = gr.Textbox(label="Kapı No")
185
 
 
 
 
 
 
 
186
 
187
- submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
188
-
189
- ocr_result.change(openai_response, ocr_result, open_api_text, api_name="upload-text")
190
 
191
- open_api_text.change(text_dict, open_api_text, [city, distinct, neighbourhood, street, address, tel, name_surname, no])
 
 
 
 
192
 
193
 
194
  if __name__ == "__main__":
195
- demo.launch()
 
 
1
  from easyocr import Reader
2
+ import gradio as gr
 
 
 
3
  import openai
4
  import ast
5
  import os
 
 
6
 
7
+ from openai_api import OpenAI_API
8
+ import utils
 
9
 
 
 
10
 
11
+ openai.api_key = os.getenv("API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  reader = Reader(["tr"])
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def get_text(input_img):
16
  result = reader.readtext(input_img, detail=0)
17
  return " ".join(result)
18
 
19
 
20
+ # Submit button
21
+ def get_parsed_address(input_img):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ address_full_text = get_text(input_img)
24
+ return openai_response(address_full_text)
 
25
 
26
 
27
+ # Open API on change
28
  def text_dict(input):
29
  eval_result = ast.literal_eval(input)
30
+ utils.write_db(eval_result)
31
 
32
  return (
33
+ str(eval_result["city"]),
34
+ str(eval_result["distinct"]),
35
+ str(eval_result["neighbourhood"]),
36
+ str(eval_result["street"]),
37
+ str(eval_result["address"]),
38
+ str(eval_result["tel"]),
39
+ str(eval_result["name_surname"]),
40
+ str(eval_result["no"]),
41
  )
42
+
43
+
44
  def openai_response(ocr_input):
45
  prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from
46
  plain text input and especially from emergency text that carries address information, your inputs can be text
 
61
  resp = eval(resp.replace("'{", "{").replace("}'", "}"))
62
  resp["input"] = ocr_input
63
  dict_keys = [
64
+ "city",
65
+ "distinct",
66
+ "neighbourhood",
67
+ "street",
68
+ "no",
69
+ "tel",
70
+ "name_surname",
71
+ "address",
72
+ "input",
73
  ]
74
  for key in dict_keys:
75
  if key not in resp.keys():
76
+ resp[key] = ""
77
  return resp
78
 
79
  def ner_response(ocr_input):
 
89
  })
90
  return output
91
 
92
+ # User Interface
93
  with gr.Blocks() as demo:
94
  gr.Markdown(
95
+ """
96
  # Enkaz Bildirme Uygulaması
97
+ """
98
+ )
99
+ gr.Markdown(
100
+ "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."
101
+ )
102
  with gr.Row():
103
  img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
104
  ocr_result = gr.Textbox(label="Metin yükleyin 👇 ")
 
119
  with gr.Row():
120
  no = gr.Textbox(label="Kapı No")
121
 
122
+ submit_button.click(
123
+ get_parsed_address,
124
+ inputs=img_area,
125
+ outputs=open_api_text,
126
+ api_name="upload_image",
127
+ )
128
 
129
+ ocr_result.change(
130
+ openai_response, ocr_result, open_api_text, api_name="upload-text"
131
+ )
132
 
133
+ open_api_text.change(
134
+ text_dict,
135
+ open_api_text,
136
+ [city, distinct, neighbourhood, street, address, tel, name_surname, no],
137
+ )
138
 
139
 
140
  if __name__ == "__main__":
141
+ demo.launch()
openai_api.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+
4
+ class OpenAI_API:
5
+ def __init__(self):
6
+ self.openai_api_key = ""
7
+
8
+ def single_request(self, address_text):
9
+
10
+ openai.api_type = "azure"
11
+ openai.api_base = "https://damlaopenai.openai.azure.com/"
12
+ openai.api_version = "2022-12-01"
13
+ openai.api_key = os.getenv("API_KEY")
14
+
15
+ response = openai.Completion.create(
16
+ engine="Davinci-003",
17
+ prompt=address_text,
18
+ temperature=0.9,
19
+ max_tokens=256,
20
+ top_p=1.0,
21
+ n=1,
22
+ logprobs=0,
23
+ echo=False,
24
+ stop=None,
25
+ frequency_penalty=0,
26
+ presence_penalty=0,
27
+ best_of=1,
28
+ )
29
+
30
+ return response
utils.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import csv
3
+ import json
4
+ from deta import Deta
5
+ import os
6
+
7
+
8
+ def preprocess_img(inp_image):
9
+ gray = cv2.cvtColor(inp_image, cv2.COLOR_BGR2GRAY)
10
+ gray_img = cv2.bitwise_not(gray)
11
+ return gray_img
12
+
13
+ def save_csv(mahalle, il, sokak, apartman):
14
+ adres_full = [mahalle, il, sokak, apartman]
15
+
16
+ with open("adress_book.csv", "a", encoding="utf-8") as f:
17
+ write = csv.writer(f)
18
+ write.writerow(adres_full)
19
+ return adres_full
20
+
21
+
22
+ def get_json(mahalle, il, sokak, apartman):
23
+ adres = {"mahalle": mahalle, "il": il, "sokak": sokak, "apartman": apartman}
24
+ dump = json.dumps(adres, indent=4, ensure_ascii=False)
25
+ return dump
26
+
27
+
28
+ def write_db(data_dict):
29
+ # 2) initialize with a project key
30
+ deta_key = os.getenv('DETA_KEY')
31
+ deta = Deta(deta_key)
32
+
33
+ # 3) create and use as many DBs as you want!
34
+ users = deta.Base("deprem-ocr")
35
+ users.insert(data_dict)