Spaces:
Sleeping
Sleeping
Commit
·
9aa487e
1
Parent(s):
caca740
made app
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from surya.ocr import run_ocr
|
| 4 |
+
from surya.model.detection.model import load_model as load_det_model, load_processor as load_det_processor
|
| 5 |
+
from surya.model.recognition.model import load_model as load_rec_model
|
| 6 |
+
from surya.model.recognition.processor import load_processor as load_rec_processor
|
| 7 |
+
import re
|
| 8 |
+
from transformers import AutoModel, AutoTokenizer
|
| 9 |
+
import torch
|
| 10 |
+
import tempfile
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
# device = "cuda"
|
| 14 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 15 |
+
print(device)
|
| 16 |
+
got_model_name = "stepfun-ai/GOT-OCR2_0" if device == 'cuda' else "aarishshahmohsin/got_ocr_2"
|
| 17 |
+
|
| 18 |
+
det_processor, det_model = load_det_processor(), load_det_model()
|
| 19 |
+
det_model.to(device)
|
| 20 |
+
rec_model, rec_processor = load_rec_model(), load_rec_processor()
|
| 21 |
+
rec_model.to(device)
|
| 22 |
+
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(got_model_name, trust_remote_code=True, device_map=device)
|
| 24 |
+
got_model = AutoModel.from_pretrained(got_model_name, trust_remote_code=True, low_cpu_mem_usage=True, device_map=device, use_safetensors=True)
|
| 25 |
+
got_model = got_model.eval().to(device)
|
| 26 |
+
|
| 27 |
+
def extract_hindi(text):
|
| 28 |
+
hindi_pattern = re.compile(r'[\u0900-\u097F]+') # Unicode range for Devanagari script
|
| 29 |
+
hindi_words = hindi_pattern.findall(text)
|
| 30 |
+
return ' '.join(hindi_words)
|
| 31 |
+
|
| 32 |
+
def process_image(image):
|
| 33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 34 |
+
image.save(temp_file.name)
|
| 35 |
+
temp_file_path = temp_file.name
|
| 36 |
+
|
| 37 |
+
image = Image.open(temp_file_path)
|
| 38 |
+
image = image.convert("RGB")
|
| 39 |
+
|
| 40 |
+
langs = ["hi"]
|
| 41 |
+
surya_predictions = run_ocr([image], [langs], det_model, det_processor, rec_model, rec_processor)
|
| 42 |
+
|
| 43 |
+
surya_text_list = re.findall(r"text='(.*?)'", str(surya_predictions[0]))
|
| 44 |
+
surya_text = '\n'.join(surya_text_list)
|
| 45 |
+
surya_text = extract_hindi(surya_text)
|
| 46 |
+
|
| 47 |
+
got_res = got_model.chat(tokenizer, temp_file_path, ocr_type='ocr')
|
| 48 |
+
|
| 49 |
+
combined_text = f"<h2> Hindi Text (Surya OCR) </h2> <br>{surya_text}<br> <br> <h2> English Text (GOT OCR) </h2> <br> {got_res}"
|
| 50 |
+
|
| 51 |
+
if os.path.exists(temp_file_path):
|
| 52 |
+
os.remove(temp_file_path)
|
| 53 |
+
|
| 54 |
+
return combined_text
|
| 55 |
+
|
| 56 |
+
def highlight_search(text, query):
|
| 57 |
+
if query:
|
| 58 |
+
pattern = re.compile(re.escape(query), re.IGNORECASE)
|
| 59 |
+
highlighted_text = pattern.sub(lambda m: f"<span style='background-color: limegreen;'>{m.group(0)}</span>", text)
|
| 60 |
+
return highlighted_text
|
| 61 |
+
return text
|
| 62 |
+
|
| 63 |
+
with gr.Blocks() as ocr_interface:
|
| 64 |
+
gr.Markdown("# OCR Application (IIT Roorkee Assignment)")
|
| 65 |
+
gr.Markdown("Upload an image for OCR processing. This uses Surya OCR (for both Hindi) and GOT-OCR (for English). The results from both models will be concatenated.")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
image_input = gr.Image(type="pil", label="Upload an Image")
|
| 70 |
+
run_ocr_button = gr.Button("Run OCR")
|
| 71 |
+
|
| 72 |
+
with gr.Column():
|
| 73 |
+
output_text = gr.HTML(label="Extracted Text")
|
| 74 |
+
query_input = gr.Textbox(label="Search in extracted text (optional)", placeholder="Type to search...")
|
| 75 |
+
search_button = gr.Button("Search")
|
| 76 |
+
|
| 77 |
+
def process_and_display(image):
|
| 78 |
+
combined_text = process_image(image)
|
| 79 |
+
return combined_text
|
| 80 |
+
|
| 81 |
+
def search_text(combined_text, query):
|
| 82 |
+
highlighted = highlight_search(combined_text, query)
|
| 83 |
+
return highlighted
|
| 84 |
+
|
| 85 |
+
run_ocr_button.click(fn=process_and_display, inputs=image_input, outputs=output_text)
|
| 86 |
+
|
| 87 |
+
search_button.click(fn=search_text, inputs=[output_text, query_input], outputs=output_text)
|
| 88 |
+
|
| 89 |
+
ocr_interface.launch()
|