Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,42 +15,12 @@ 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=256,
|
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 |
-
best_of=1)
|
46 |
-
|
47 |
-
return response
|
48 |
|
49 |
########################
|
50 |
|
51 |
-
openai.api_key = os.getenv('API_KEY')
|
52 |
reader = Reader(["tr"])
|
53 |
-
|
54 |
|
55 |
def get_parsed_address(input_img):
|
56 |
|
@@ -98,16 +68,75 @@ def text_dict(input):
|
|
98 |
write_db(eval_result)
|
99 |
|
100 |
return (
|
101 |
-
str(eval_result['
|
102 |
-
str(eval_result['
|
103 |
-
str(eval_result['
|
104 |
-
str(eval_result['
|
105 |
-
str(eval_result['
|
106 |
str(eval_result['tel']),
|
107 |
-
str(eval_result['
|
108 |
str(eval_result['no']),
|
109 |
)
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
def openai_response(ocr_input):
|
112 |
prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from
|
113 |
plain text input and especially from emergency text that carries address information, your inputs can be text
|
@@ -144,6 +173,7 @@ def openai_response(ocr_input):
|
|
144 |
return resp
|
145 |
|
146 |
|
|
|
147 |
with gr.Blocks() as demo:
|
148 |
gr.Markdown(
|
149 |
"""
|
@@ -173,7 +203,7 @@ with gr.Blocks() as demo:
|
|
173 |
|
174 |
submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
|
175 |
|
176 |
-
ocr_result.change(
|
177 |
|
178 |
open_api_text.change(text_dict, open_api_text, [city, distinct, neighbourhood, street, address, tel, name_surname, no])
|
179 |
|
|
|
15 |
import json
|
16 |
|
17 |
import os
|
|
|
|
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
########################
|
21 |
|
|
|
22 |
reader = Reader(["tr"])
|
23 |
+
API_TOKEN = os.getenv("HF_TOKEN")
|
24 |
|
25 |
def get_parsed_address(input_img):
|
26 |
|
|
|
68 |
write_db(eval_result)
|
69 |
|
70 |
return (
|
71 |
+
str(eval_result['il']),
|
72 |
+
str(eval_result['ilce']),
|
73 |
+
str(eval_result['mahalle']),
|
74 |
+
str(eval_result['sokak']),
|
75 |
+
str(eval_result['Apartman/site']),
|
76 |
str(eval_result['tel']),
|
77 |
+
str(eval_result['isim']) + str(eval_result['soyad']),
|
78 |
str(eval_result['no']),
|
79 |
)
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
import json
|
85 |
+
import requests
|
86 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
87 |
+
API_URL = "https://api-inference.huggingface.co/models/deprem-ml/deprem-ner"
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
def query(payload):
|
92 |
+
data = json.dumps(payload)
|
93 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
94 |
+
return json.loads(response.content.decode("utf-8"))
|
95 |
+
|
96 |
+
|
97 |
+
def infer(ocr_input)
|
98 |
+
return query({"inputs": ocr_input})
|
99 |
+
|
100 |
+
|
101 |
+
"""
|
102 |
+
ORNEK NER OUTPUT
|
103 |
+
[
|
104 |
+
{
|
105 |
+
"entity_group": "mahalle",
|
106 |
+
"score": 0.8160411715507507,
|
107 |
+
"word": "Akevler mahallesi",
|
108 |
+
"start": 14,
|
109 |
+
"end": 31
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"entity_group": "sokak",
|
113 |
+
"score": 0.940501868724823,
|
114 |
+
"word": "Rüzgar sokak",
|
115 |
+
"start": 32,
|
116 |
+
"end": 44
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"entity_group": "Apartman/Site",
|
120 |
+
"score": 0.8081040978431702,
|
121 |
+
"word": "Tuncay apartmanı",
|
122 |
+
"start": 45,
|
123 |
+
"end": 61
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"entity_group": "ilce",
|
127 |
+
"score": 0.854024350643158,
|
128 |
+
"word": "Antakya",
|
129 |
+
"start": 72,
|
130 |
+
"end": 79
|
131 |
+
}
|
132 |
+
]"""
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
# this is not in use due to content filter
|
140 |
def openai_response(ocr_input):
|
141 |
prompt = f"""Tabular Data Extraction You are a highly intelligent and accurate tabular data extractor from
|
142 |
plain text input and especially from emergency text that carries address information, your inputs can be text
|
|
|
173 |
return resp
|
174 |
|
175 |
|
176 |
+
|
177 |
with gr.Blocks() as demo:
|
178 |
gr.Markdown(
|
179 |
"""
|
|
|
203 |
|
204 |
submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
|
205 |
|
206 |
+
ocr_result.change(query, ocr_result, open_api_text, api_name="upload-text")
|
207 |
|
208 |
open_api_text.change(text_dict, open_api_text, [city, distinct, neighbourhood, street, address, tel, name_surname, no])
|
209 |
|