mertcobanov commited on
Commit
93e93f5
1 Parent(s): c3c83a9
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+
10
+
11
+ openai.api_key = "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"
12
+ reader = Reader(["tr"])
13
+
14
+
15
+
16
+ def get_text(input_img):
17
+ result = reader.readtext(input_img, detail=0)
18
+ return " ".join(result)
19
+
20
+
21
+ def save_csv(mahalle, il, sokak, apartman):
22
+ adres_full = [mahalle, il, sokak, apartman]
23
+
24
+ with open("adress_book.csv", "a", encoding="utf-8") as f:
25
+ write = csv.writer(f)
26
+ write.writerow(adres_full)
27
+ return adres_full
28
+
29
+
30
+ def get_json(mahalle, il, sokak, apartman):
31
+ adres = {"mahalle": mahalle, "il": il, "sokak": sokak, "apartman": apartman}
32
+ dump = json.dumps(adres, indent=4, ensure_ascii=False)
33
+ return dump
34
+
35
+
36
+ def openai_response(ocr_input):
37
+ prompt = f"""Address Extractor
38
+ 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
39
+ Examples:
40
+
41
+ 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
42
+ Output: 'Sehir:İstanbul', 'Ilce:Beşiktaş', 'Mahalle:Yıldız Mahallesi', 'Cadde: Cumhuriyet Caddesi', 'Apartman:no:35', 'Telefon: 5551231256', 'isim:Ahmet Yılmaz'
43
+
44
+
45
+ Input: {ocr_input}
46
+ Output:"""
47
+
48
+
49
+
50
+ response = openai.Completion.create(
51
+ model="text-davinci-003",
52
+ prompt=prompt,
53
+ temperature=0,
54
+ max_tokens=100,
55
+ top_p=1,
56
+ frequency_penalty=0.0,
57
+ presence_penalty=0.0,
58
+ stop=["\n"],
59
+ )
60
+
61
+ return response["choices"][0]['text']
62
+
63
+
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown(""" # Image to Text - Adres""")
66
+ with gr.Row():
67
+ img_area = gr.Image()
68
+ ocr_result = gr.Textbox(label="OCR")
69
+ open_api_text = gr.Textbox(label="OPENAI")
70
+
71
+ submit_button = gr.Button()
72
+ submit_button.click(get_text, img_area, ocr_result)
73
+
74
+ ocr_result.change(openai_response, ocr_result, open_api_text)
75
+
76
+ with gr.Column():
77
+ with gr.Row():
78
+ mahalle = gr.Textbox(label="mahalle")
79
+ sokak = gr.Textbox(label="sokak")
80
+ with gr.Row():
81
+ apartman = gr.Textbox(label="apartman")
82
+ il = gr.Textbox(label="il")
83
+ tarif = gr.Textbox(label="Tarif")
84
+
85
+ json_out = gr.Textbox()
86
+ csv_out = gr.Textbox()
87
+
88
+ adres_submit = gr.Button()
89
+ adres_submit.click(get_json, [mahalle, il, sokak, apartman], json_out)
90
+ adres_submit.click(save_csv, [mahalle, il, sokak, apartman], csv_out)
91
+
92
+
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ Pillow
3
+ easyocr
4
+ gradio