Upload OCRFINAL.py
Browse files- OCRFINAL.py +71 -0
OCRFINAL.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import shutil
|
| 5 |
+
import re
|
| 6 |
+
from transformers import AutoModel, AutoTokenizer
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
| 9 |
+
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cpu', use_safetensors=True)
|
| 10 |
+
model = model.eval()
|
| 11 |
+
|
| 12 |
+
UPLOAD_FOLDER = "./uploads"
|
| 13 |
+
RESULTS_FOLDER = "./results"
|
| 14 |
+
|
| 15 |
+
for folder in [UPLOAD_FOLDER, RESULTS_FOLDER]:
|
| 16 |
+
if not os.path.exists(folder):
|
| 17 |
+
os.makedirs(folder)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def run_GOT(image, search_term):
|
| 21 |
+
unique_id = str(uuid.uuid4())
|
| 22 |
+
image_path = os.path.join(UPLOAD_FOLDER, f"{unique_id}.png")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
shutil.copy(image, image_path)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
|
| 29 |
+
res = model.chat(tokenizer, image_path, ocr_type='ocr')
|
| 30 |
+
|
| 31 |
+
highlighted_text = highlight_text(res, search_term)
|
| 32 |
+
|
| 33 |
+
return highlighted_text, None
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {str(e)}", None
|
| 36 |
+
finally:
|
| 37 |
+
if os.path.exists(image_path):
|
| 38 |
+
os.remove(image_path)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def highlight_text(text, search_term):
|
| 42 |
+
if not search_term:
|
| 43 |
+
return text
|
| 44 |
+
pattern = re.compile(re.escape(search_term), re.IGNORECASE)
|
| 45 |
+
return pattern.sub(lambda m: f'<span style="background-color: yellow;">{m.group()}</span>', text)
|
| 46 |
+
|
| 47 |
+
title_html = """
|
| 48 |
+
<h2> <span class="gradient-text" id="text">General OCR Theory (GOT)</span>: Multi-Language OCR HINDI AND ENGLISH</h2>
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.HTML(title_html)
|
| 53 |
+
gr.Markdown("""
|
| 54 |
+
### Instructions
|
| 55 |
+
Upload your respective image below and click "Submit" to extract text in both English and Hindi. If you want you can select the word to be highlighted.
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
image_input = gr.Image(type="filepath", label="Upload your image")
|
| 61 |
+
search_input = gr.Textbox(label="Enter a word to search", placeholder="Search term")
|
| 62 |
+
submit_button = gr.Button("Submit")
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
ocr_result = gr.HTML(label="Extracted Text:")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
submit_button.click(run_GOT, inputs=[image_input, search_input], outputs=[ocr_result])
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|