Spaces:
Sleeping
Sleeping
TM
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import AutoModel, AutoTokenizer
|
5 |
+
|
6 |
+
# Set up page configuration for wide layout
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="OCR and Document Search Web Application Prototype Using GOT-OCR 2.0",
|
9 |
+
layout="wide",
|
10 |
+
)
|
11 |
+
|
12 |
+
|
13 |
+
# Function to initialize model and tokenizer
|
14 |
+
@st.cache_resource
|
15 |
+
def init_model():
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
17 |
+
"srimanth-d/GOT_CPU", trust_remote_code=True
|
18 |
+
)
|
19 |
+
model = AutoModel.from_pretrained(
|
20 |
+
"srimanth-d/GOT_CPU",
|
21 |
+
trust_remote_code=True,
|
22 |
+
use_safetensors=True,
|
23 |
+
pad_token_id=tokenizer.eos_token_id,
|
24 |
+
)
|
25 |
+
return model.eval(), tokenizer
|
26 |
+
|
27 |
+
|
28 |
+
# Function to extract text from the image using OCR model
|
29 |
+
@st.cache_data
|
30 |
+
def extract_text(image_file, _model, _tokenizer):
|
31 |
+
res = _model.chat(_tokenizer, image_file, ocr_type="ocr")
|
32 |
+
return res
|
33 |
+
|
34 |
+
|
35 |
+
# Function to highlight search term in extracted text
|
36 |
+
def highlight_text(text, search_term):
|
37 |
+
if not search_term:
|
38 |
+
return text
|
39 |
+
pattern = re.compile(re.escape(search_term), re.IGNORECASE)
|
40 |
+
return pattern.sub(
|
41 |
+
lambda m: f'<span style="background-color: #FFFF00; font-weight: bold;">{m.group()}</span>',
|
42 |
+
text,
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
# Streamlit UI components
|
47 |
+
st.title("OCR and Document Search Web Application Prototype Using GOT-OCR 2.0")
|
48 |
+
st.write("Upload an image for OCR")
|
49 |
+
|
50 |
+
# Initialize model and tokenizer
|
51 |
+
model, tokenizer = init_model()
|
52 |
+
|
53 |
+
# Create columns for layout
|
54 |
+
col1, col2 = st.columns([1, 2]) # Adjust proportions as needed
|
55 |
+
|
56 |
+
with col1:
|
57 |
+
# File uploader for images
|
58 |
+
uploaded_image = st.file_uploader(
|
59 |
+
"Upload Image", type=["jpg", "png", "jpeg"], key="image_upload"
|
60 |
+
)
|
61 |
+
|
62 |
+
# If an image is uploaded
|
63 |
+
if uploaded_image:
|
64 |
+
# Save the uploaded image to a local directory
|
65 |
+
if not os.path.exists("images"):
|
66 |
+
os.makedirs("images")
|
67 |
+
image_path = os.path.join("images", uploaded_image.name)
|
68 |
+
with open(image_path, "wb") as f:
|
69 |
+
f.write(uploaded_image.getbuffer())
|
70 |
+
|
71 |
+
# Create buttons for viewing the full image and clearing the image
|
72 |
+
col1a, col1b = st.columns([0.5, 0.5])
|
73 |
+
with col1a:
|
74 |
+
if st.button("View Full Image"):
|
75 |
+
# Show full image on demand
|
76 |
+
st.image(image_path, caption="Full Size Image", use_column_width=True)
|
77 |
+
|
78 |
+
else:
|
79 |
+
st.info("Please upload an image to perform OCR.")
|
80 |
+
|
81 |
+
# Fallback text in case no image is uploaded
|
82 |
+
extracted_text = ""
|
83 |
+
|
84 |
+
# Once the image is uploaded, extract the text
|
85 |
+
if uploaded_image:
|
86 |
+
extracted_text = extract_text(image_path, model, tokenizer)
|
87 |
+
|
88 |
+
with col2:
|
89 |
+
# Input field for keyword search
|
90 |
+
search_term = st.text_input("Enter a word or phrase to search:")
|
91 |
+
|
92 |
+
# Highlight search term in extracted text
|
93 |
+
highlighted_text = highlight_text(extracted_text, search_term)
|
94 |
+
|
95 |
+
# Display search results
|
96 |
+
if search_term:
|
97 |
+
if search_term.lower() in highlighted_text.lower():
|
98 |
+
st.success(f"Word **'{search_term}'** found in the text!")
|
99 |
+
else:
|
100 |
+
st.error(f"Word **'{search_term}'** not found.")
|
101 |
+
|
102 |
+
# Show the extracted text with highlighted search terms
|
103 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|