deprem-ocr / app.py
merve's picture
merve HF staff
replaced openai with our NER model
27d2f67
from PIL import ImageFilter, Image
from easyocr import Reader
import gradio as gr
import numpy as np
import openai
import ast
from transformers import pipeline
import os
from openai_api import OpenAI_API
import utils
openai.api_key = os.getenv("API_KEY")
reader = Reader(["tr"])
def get_text(input_img):
img = Image.fromarray(input_img)
detailed = np.asarray(img.filter(ImageFilter.DETAIL))
result = reader.readtext(detailed, detail=0, paragraph=True)
return " ".join(result)
# Submit button
def get_parsed_address(input_img):
address_full_text = get_text(input_img)
return ner_response(address_full_text)
def save_deta_db(input):
eval_result = ast.literal_eval(input)
utils.write_db(eval_result)
return
def update_component():
return gr.update(value="Gönderildi, teşekkürler.", visible=True)
def clear_textbox(value):
return gr.update(value="")
def text_dict(input):
eval_result = ast.literal_eval(input)
return (
str(eval_result["il"]),
str(eval_result["ilce"]),
str(eval_result["mahalle"]),
str(eval_result["sokak"]),
str(eval_result["Apartman/site"]),
str(eval_result["no"]),
str(eval_result["ad-soyad"]),
str(eval_result["dis kapi no"]),
)
def ner_response(ocr_input):
ner_pipe = pipeline("token-classification","deprem-ml/deprem-ner", aggregation_strategy="first")
predictions = ner_pipe(ocr_input)
resp = {}
for item in predictions:
print(item)
key = item["entity_group"]
resp[key] = item["word"]
resp["input"] = ocr_input
dict_keys = ["il", "ilce", "mahalle", "sokak", "Apartman/site", "no", "ad-soyad", "dis kapi no"]
for key in dict_keys:
if key not in resp.keys():
resp[key] = ""
return resp
# User Interface
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():
with gr.Column():
img_area = gr.Image(label="Ekran Görüntüsü yükleyin 👇")
img_area_button = gr.Button(value="Görüntüyü İşle", label="Submit")
with gr.Column():
text_area = gr.Textbox(label="Metin yükleyin 👇 ", lines=8)
text_area_button = gr.Button(value="Metni Yükle", label="Submit")
open_api_text = gr.Textbox(label="Tam Adres")
with gr.Column():
with gr.Row():
il = gr.Textbox(label="İl", interactive=True, show_progress=False)
ilce = gr.Textbox(label="İlçe", interactive=True, show_progress=False)
with gr.Row():
mahalle = gr.Textbox(
label="Mahalle", interactive=True, show_progress=False
)
sokak = gr.Textbox(
label="Sokak/Cadde/Bulvar", interactive=True, show_progress=False
)
with gr.Row():
no = gr.Textbox(label="Telefon", interactive=True, show_progress=False)
with gr.Row():
ad_soyad = gr.Textbox(
label="İsim Soyisim", interactive=True, show_progress=False
)
apartman = gr.Textbox(label="apartman", interactive=True, show_progress=False)
with gr.Row():
dis_kapi_no = gr.Textbox(label="Kapı No", interactive=True, show_progress=False)
img_area_button.click(
get_parsed_address,
inputs=img_area,
outputs=open_api_text,
api_name="upload-image",
)
text_area_button.click(
ner_response, text_area, open_api_text, api_name="upload-text"
)
open_api_text.change(
text_dict,
open_api_text,
[il, ilce, mahalle, sokak, no, apartman, ad_soyad, dis_kapi_no],
)
ocr_button = gr.Button(value="Sadece OCR kullan")
ocr_button.click(
get_text,
inputs=img_area,
outputs=text_area,
api_name="get-ocr-output",
)
submit_button = gr.Button(value="Veriyi Birimlere Yolla")
submit_button.click(save_deta_db, open_api_text)
done_text = gr.Textbox(label="Done", value="Not Done", visible=False)
submit_button.click(update_component, outputs=done_text)
for txt in [il, ilce, mahalle, sokak, apartman, no, ad_soyad, dis_kapi_no]:
submit_button.click(fn=clear_textbox, inputs=txt, outputs=txt)
if __name__ == "__main__":
demo.launch()