Spaces:
Sleeping
Sleeping
Divyansh12
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from transformers import AutoModel, AutoTokenizer
|
|
4 |
from PIL import Image
|
5 |
import uuid
|
6 |
|
7 |
-
# Cache the model loading function
|
8 |
@st.cache_resource
|
9 |
def load_model(model_name):
|
10 |
if model_name == "OCR for english or hindi (runs on CPU)":
|
@@ -18,6 +18,7 @@ def load_model(model_name):
|
|
18 |
return tokenizer, model
|
19 |
|
20 |
# Function to run the GOT model for multilingual OCR
|
|
|
21 |
def run_GOT(image, tokenizer, model):
|
22 |
unique_id = str(uuid.uuid4())
|
23 |
image_path = f"{unique_id}.png"
|
@@ -38,7 +39,12 @@ def run_GOT(image, tokenizer, model):
|
|
38 |
# Function to highlight keyword in text
|
39 |
def highlight_keyword(text, keyword):
|
40 |
if keyword:
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
return highlighted_text
|
43 |
return text
|
44 |
|
@@ -66,6 +72,8 @@ if uploaded_image:
|
|
66 |
with st.spinner("Processing..."):
|
67 |
# Load the selected model (cached using @st.cache_resource)
|
68 |
tokenizer, model = load_model(model_option)
|
|
|
|
|
69 |
result_text = run_GOT(image, tokenizer, model)
|
70 |
|
71 |
if "Error" not in result_text:
|
|
|
4 |
from PIL import Image
|
5 |
import uuid
|
6 |
|
7 |
+
# Cache the model loading function using @st.cache_resource
|
8 |
@st.cache_resource
|
9 |
def load_model(model_name):
|
10 |
if model_name == "OCR for english or hindi (runs on CPU)":
|
|
|
18 |
return tokenizer, model
|
19 |
|
20 |
# Function to run the GOT model for multilingual OCR
|
21 |
+
@st.cache_data
|
22 |
def run_GOT(image, tokenizer, model):
|
23 |
unique_id = str(uuid.uuid4())
|
24 |
image_path = f"{unique_id}.png"
|
|
|
39 |
# Function to highlight keyword in text
|
40 |
def highlight_keyword(text, keyword):
|
41 |
if keyword:
|
42 |
+
# Use a case-insensitive search for highlighting
|
43 |
+
highlighted_text = text
|
44 |
+
highlighted_text = highlighted_text.replace(keyword, f"<mark>{keyword}</mark>")
|
45 |
+
highlighted_text = highlighted_text.replace(keyword.lower(), f"<mark>{keyword.lower()}</mark>")
|
46 |
+
highlighted_text = highlighted_text.replace(keyword.upper(), f"<mark>{keyword.upper()}</mark>")
|
47 |
+
highlighted_text = highlighted_text.replace(keyword.capitalize(), f"<mark>{keyword.capitalize()}</mark>")
|
48 |
return highlighted_text
|
49 |
return text
|
50 |
|
|
|
72 |
with st.spinner("Processing..."):
|
73 |
# Load the selected model (cached using @st.cache_resource)
|
74 |
tokenizer, model = load_model(model_option)
|
75 |
+
|
76 |
+
# Run OCR and cache the result using @st.cache_data
|
77 |
result_text = run_GOT(image, tokenizer, model)
|
78 |
|
79 |
if "Error" not in result_text:
|