Spaces:
Running
Running
SanyaAhmed
commited on
Commit
•
468dd54
1
Parent(s):
06fb25d
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,48 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
import pytesseract
|
4 |
-
import re
|
5 |
-
|
6 |
-
def highlight_text(text, keyword):
|
7 |
-
escaped_key = re.escape(keyword)
|
8 |
-
highlighted_text = re.sub(f'({escaped_key})', r'<mark>\1</mark>', text, flags = re.IGNORECASE)
|
9 |
-
return highlighted_text
|
10 |
-
|
11 |
-
st.title('OCR Document Search Web App')
|
12 |
-
st.divider()
|
13 |
-
|
14 |
-
'''
|
15 |
-
def got_ocr(image_path):
|
16 |
-
from transformers import AutoModel, AutoTokenizer
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True)
|
18 |
-
model = AutoModel.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
19 |
-
model=model.eval().cuda()
|
20 |
-
image = Image.open(image_path)
|
21 |
-
res = model.chat(tokenizer, image, ocr_type='ocr')
|
22 |
-
return res
|
23 |
-
'''
|
24 |
-
|
25 |
-
uploaded_img = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
|
26 |
-
|
27 |
-
if uploaded_img is not None:
|
28 |
-
image = Image.open(uploaded_img)
|
29 |
-
|
30 |
-
st.image(image, caption='Uploaded image', use_column_width=True)
|
31 |
-
|
32 |
-
extracted_text = pytesseract.image_to_string(image, lang='eng+hin')
|
33 |
-
|
34 |
-
st.subheader('Extracted text')
|
35 |
-
st.divider()
|
36 |
-
|
37 |
-
st.text(extracted_text)
|
38 |
-
|
39 |
-
st.divider()
|
40 |
-
|
41 |
-
search_query = st.text_input('Enter a keyword to search in the extracted text - ')
|
42 |
-
|
43 |
-
if search_query:
|
44 |
-
|
45 |
-
highlighted_text = highlight_text(extracted_text, search_query)
|
46 |
-
|
47 |
-
st.subheader('Text with Highlighted Keyword')
|
48 |
st.markdown(highlighted_text, unsafe_allow_html=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import pytesseract
|
4 |
+
import re
|
5 |
+
|
6 |
+
def highlight_text(text, keyword):
|
7 |
+
escaped_key = re.escape(keyword)
|
8 |
+
highlighted_text = re.sub(f'({escaped_key})', r'<mark>\1</mark>', text, flags = re.IGNORECASE)
|
9 |
+
return highlighted_text
|
10 |
+
|
11 |
+
st.title('OCR Document Search Web App')
|
12 |
+
st.divider()
|
13 |
+
|
14 |
+
_ = '''
|
15 |
+
def got_ocr(image_path):
|
16 |
+
from transformers import AutoModel, AutoTokenizer
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True)
|
18 |
+
model = AutoModel.from_pretrained("stepfun-ai/GOT-OCR2_0", trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
19 |
+
model=model.eval().cuda()
|
20 |
+
image = Image.open(image_path)
|
21 |
+
res = model.chat(tokenizer, image, ocr_type='ocr')
|
22 |
+
return res
|
23 |
+
'''
|
24 |
+
|
25 |
+
uploaded_img = st.file_uploader('Upload an image', type=['jpg', 'jpeg', 'png'])
|
26 |
+
|
27 |
+
if uploaded_img is not None:
|
28 |
+
image = Image.open(uploaded_img)
|
29 |
+
|
30 |
+
st.image(image, caption='Uploaded image', use_column_width=True)
|
31 |
+
|
32 |
+
extracted_text = pytesseract.image_to_string(image, lang='eng+hin')
|
33 |
+
|
34 |
+
st.subheader('Extracted text')
|
35 |
+
st.divider()
|
36 |
+
|
37 |
+
st.text(extracted_text)
|
38 |
+
|
39 |
+
st.divider()
|
40 |
+
|
41 |
+
search_query = st.text_input('Enter a keyword to search in the extracted text - ')
|
42 |
+
|
43 |
+
if search_query:
|
44 |
+
|
45 |
+
highlighted_text = highlight_text(extracted_text, search_query)
|
46 |
+
|
47 |
+
st.subheader('Text with Highlighted Keyword')
|
48 |
st.markdown(highlighted_text, unsafe_allow_html=True)
|