Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
import re
|
5 |
+
import io
|
6 |
+
import torch
|
7 |
+
from transformers import AutoModel, AutoTokenizer
|
8 |
+
import tempfile
|
9 |
+
|
10 |
+
# Function to load model and tokenizer
|
11 |
+
@st.cache_resource
|
12 |
+
def load_model():
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
14 |
+
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
|
15 |
+
model = model.eval()
|
16 |
+
return tokenizer, model
|
17 |
+
|
18 |
+
def search_keyword(extracted_text, keyword):
|
19 |
+
if not keyword:
|
20 |
+
return extracted_text
|
21 |
+
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
|
22 |
+
highlighted_text = pattern.sub(lambda m: f'<span style="color: red; font-weight: bold;">{m.group()}</span>', extracted_text)
|
23 |
+
return highlighted_text
|
24 |
+
|
25 |
+
|
26 |
+
def main():
|
27 |
+
st.title("Simplified OCR Application")
|
28 |
+
|
29 |
+
# File uploader
|
30 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
31 |
+
|
32 |
+
# Placeholder for extracted text (simulating OCR result)
|
33 |
+
extracted_text = None
|
34 |
+
# Load model and tokenizer
|
35 |
+
tokenizer, model = load_model()
|
36 |
+
|
37 |
+
if uploaded_file is not None:
|
38 |
+
# Display the uploaded image
|
39 |
+
image = Image.open(uploaded_file)
|
40 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
41 |
+
|
42 |
+
# Simulate OCR extraction (replace this with actual OCR in your full app)
|
43 |
+
if st.button('Extract Text'):
|
44 |
+
# Save the uploaded file to a temporary file
|
45 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
|
46 |
+
temp_filename = temp_file.name
|
47 |
+
image.save(temp_filename, format='PNG')
|
48 |
+
|
49 |
+
# Perform OCR
|
50 |
+
with st.spinner('Extracting text...'):
|
51 |
+
res = model.chat(tokenizer, temp_filename, ocr_type='ocr')
|
52 |
+
|
53 |
+
# Display result
|
54 |
+
|
55 |
+
extracted_text = res
|
56 |
+
st.session_state['extracted_text'] = extracted_text
|
57 |
+
st.subheader("Extracted Text:")
|
58 |
+
st.write(extracted_text)
|
59 |
+
|
60 |
+
|
61 |
+
# Search functionality
|
62 |
+
if 'extracted_text' in st.session_state:
|
63 |
+
keyword = st.text_input("Enter a keyword to search:")
|
64 |
+
if st.button("Search"):
|
65 |
+
if keyword:
|
66 |
+
highlighted_text = search_keyword(st.session_state['extracted_text'], keyword)
|
67 |
+
st.subheader("Search Results:")
|
68 |
+
st.write(highlighted_text, unsafe_allow_html=True)
|
69 |
+
st.download_button(
|
70 |
+
label="Download highlighted text",
|
71 |
+
data=highlighted_text.encode('utf-8'),
|
72 |
+
file_name="highlighted_text.txt",
|
73 |
+
mime="text/plain"
|
74 |
+
)
|
75 |
+
else:
|
76 |
+
st.warning("Please enter a keyword to search.")
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
main()
|