deprem-ocr / app.py
merve's picture
merve HF staff
Update app.py
7a505c5
import gradio as gr
from easyocr import Reader
from PIL import Image
import io
import json
import csv
import openai
openai.api_key = os.getenv("API_KEY")
reader = Reader(["tr"])
def get_text(input_img):
result = reader.readtext(input_img, detail=0)
return " ".join(result)
def save_csv(mahalle, il, sokak, apartman):
adres_full = f"{mahalle}, {il}, {sokak}, {apartman}"
with open("adress_book.csv", "a", encoding="utf-8") as f:
book = f.read()
if adres_full not in book:
with open("adress_book.csv", "a", encoding="utf-8") as f:
write = csv.writer(f)
write.writerow(adres_full)
else:
adres_full = "Bu adres daha önce raporlanmış."
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 openai_response(ocr_input):
prompt = f"""Address Extractor
You are a highly intelligent and accurate address 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
Examples:
Input: Deprem sırasında evimizde yer alan adresimiz: İstanbul, Beşiktaş, Yıldız Mahallesi, Cumhuriyet Caddesi No: 35, cep telefonu numaram 5551231256, adim Ahmet Yilmaz
Output: 'Sehir:İstanbul', 'Ilce:Beşiktaş', 'Mahalle:Yıldız Mahallesi', 'Cadde: Cumhuriyet Caddesi', 'Apartman:no:35', 'Telefon: 5551231256', 'isim:Ahmet Yılmaz'
Input: {ocr_input}
Output:"""
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0,
max_tokens=100,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\n"],
)
return response["choices"][0]['text']
with gr.Blocks() as demo:
gr.Markdown("# Enkaz Raporlama")
gr.Markdown("Bu aplikasyonda üzerinde adres olan bir görüntüyü sürükleyip bırakarak konumu afet koordinasyona raporlayabilirsiniz.")
with gr.Row():
img_area = gr.Image()
ocr_result = gr.Textbox(label="OCR")
open_api_text = gr.Textbox(label="OPENAI")
submit_button = gr.Button()
submit_button.click(get_text, img_area, ocr_result)
ocr_result.change(openai_response, ocr_result, open_api_text)
with gr.Column():
with gr.Row():
mahalle = gr.Textbox(label="mahalle")
sokak = gr.Textbox(label="sokak")
with gr.Row():
apartman = gr.Textbox(label="apartman")
il = gr.Textbox(label="il")
tarif = gr.Textbox(label="Tarif")
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)
demo.launch()