deprem-ocr / app.py
merve's picture
merve HF staff
Update app.py
f779d60
raw
history blame
No virus
5.97 kB
import gradio as gr
from easyocr import Reader
from PIL import Image
import io
import json
import csv
import openai
import ast
import os
openai.api_key = os.getenv('API_KEY')
reader = Reader(["tr"])
def get_parsed_address(input_img):
address_full_text = get_text(input_img)
return openai_response(address_full_text)
def get_text(input_img):
result = reader.readtext(input_img, detail=0)
return " ".join(result)
def save_csv(mahalle, il, sokak, apartman):
adres_full = [mahalle, il, sokak, apartman]
with open("adress_book.csv", "a", encoding="utf-8") as f:
write = csv.writer(f)
write.writerow(adres_full)
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 text_dict_il(input):
eval_result = str(ast.literal_eval(input)["il"])
return eval_result
def text_dict_mahalle(input):
eval_result = str(ast.literal_eval(input)["mahalle"])
return eval_result
def text_dict_ilce(input):
eval_result = str(ast.literal_eval(input)["ilçe"])
return eval_result
def text_dict_sokak(input):
eval_result = str(ast.literal_eval(input)["sokak"])
return eval_result
def text_dict_no(input):
eval_result = str(ast.literal_eval(input)["no"])
return eval_result
def text_dict_tel(input):
eval_result = str(ast.literal_eval(input)["tel"])
return eval_result
def text_dict_isim(input):
eval_result = str(ast.literal_eval(input)["isim_soyisim"])
return eval_result
def text_dict_adres(input):
eval_result = str(ast.literal_eval(input)["adres"])
return eval_result
def text_dict_ickapi(input):
eval_result = str(ast.literal_eval(input)["ickapi_no"])
return eval_result
def text_dict_diskapi(input):
eval_result = str(ast.literal_eval(input)["diskapi_no"])
return eval_result
def openai_response(ocr_input):
prompt = f"""Tabular Data Extraction
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
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
Have only il, ilçe, mahalle, sokak, apartman, ickapi_no, diskapi_no, tel, isim_soyisim, adres
Examples:
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
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'}}' }}]
Input: {ocr_input}
Output:
"""
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=300,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n"],
)
resp = response["choices"][0]["text"]
resp = eval(resp.replace("'{", "{").replace("}'", "}"))
resp = resp[0]["Tabular"]
return resp
with gr.Blocks() as demo:
gr.Markdown(
"""
# Enkaz Bildirme Uygulaması
""")
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.")
with gr.Row():
img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
ocr_result = gr.Textbox(label="Metin yükleyin 👇 ")
open_api_text = gr.Textbox(label="Tam Adres")
submit_button = gr.Button(label="Yükle")
with gr.Column():
with gr.Row():
il = gr.Textbox(label="İl")
ilce = gr.Textbox(label="İlçe")
with gr.Row():
mahalle = gr.Textbox(label="Mahalle")
sokak = gr.Textbox(label="Sokak/Cadde/Bulvar")
with gr.Row():
tel = gr.Textbox(label="Telefon")
with gr.Row():
isim_soyisim = gr.Textbox(label="İsim Soyisim")
adres = gr.Textbox(label="Adres")
with gr.Row():
ickapi_no = gr.Textbox(label="İç Kapı No")
diskapi_no = gr.Textbox(label="Dış Kapı No")
submit_button.click(get_parsed_address, inputs = img_area, outputs = open_api_text, api_name="upload_image")
ocr_result.change(openai_response, ocr_result, open_api_text, api_name="upload-text")
open_api_text.change(text_dict_il, [open_api_text], il)
open_api_text.change(text_dict_ilce, [open_api_text], ilce)
open_api_text.change(text_dict_mahalle, [open_api_text], mahalle)
open_api_text.change(text_dict_sokak, [open_api_text], sokak)
open_api_text.change(text_dict_adres, [open_api_text], adres)
open_api_text.change(text_dict_tel, [open_api_text], tel)
open_api_text.change(text_dict_isim, [open_api_text], isim_soyisim)
open_api_text.change(text_dict_diskapi, [open_api_text], diskapi_no)
open_api_text.change(text_dict_ickapi, [open_api_text], ickapi_no)
# 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)
if __name__ == "__main__":
demo.launch()