Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,30 +4,20 @@ from qwen_vl_utils import process_vision_info
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
11 |
-
"Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.float32, device_map=None
|
12 |
-
).to("cpu") # Ensure the model is on CPU
|
13 |
-
|
14 |
-
min_pixels = 256*28*28
|
15 |
-
max_pixels = 1280*28*28
|
16 |
-
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
17 |
-
|
18 |
-
# Streamlit app
|
19 |
-
st.title("OCR Application with Keyword Search")
|
20 |
-
|
21 |
-
# Upload image
|
22 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
23 |
-
|
24 |
-
if uploaded_file is not None:
|
25 |
-
# Convert the uploaded file to an image
|
26 |
-
img = Image.open(uploaded_file)
|
27 |
-
|
28 |
-
# Display the uploaded image
|
29 |
-
st.image(img, caption="Uploaded Image", use_column_width=True)
|
30 |
-
|
31 |
# Prepare the image for the model
|
32 |
messages = [
|
33 |
{
|
@@ -72,17 +62,52 @@ if uploaded_file is not None:
|
|
72 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
73 |
)
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# Display the extracted text
|
76 |
-
extracted_text = output_text[0]
|
77 |
st.subheader("Extracted Text")
|
78 |
-
st.write(extracted_text)
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
# Load model on CPU
|
10 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
11 |
+
"Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.float32, device_map=None
|
12 |
+
).to("cpu") # type:ignore # Ensure the model is on CPU
|
13 |
|
14 |
+
min_pixels = 256*28*28
|
15 |
+
max_pixels = 1280*28*28
|
16 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
17 |
+
return model, processor
|
18 |
+
|
19 |
|
20 |
+
def process_file(img, model, processor):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# Prepare the image for the model
|
22 |
messages = [
|
23 |
{
|
|
|
62 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
63 |
)
|
64 |
|
65 |
+
return output_text[0]
|
66 |
+
|
67 |
+
|
68 |
+
# Streamlit app
|
69 |
+
st.title("OCR Application with Keyword Search")
|
70 |
+
|
71 |
+
|
72 |
+
# Initialize session state variables
|
73 |
+
if 'current_image' not in st.session_state:
|
74 |
+
st.session_state.current_image = None
|
75 |
+
if 'extracted_text' not in st.session_state:
|
76 |
+
st.session_state.extracted_text = None
|
77 |
+
|
78 |
+
|
79 |
+
model, processor = load_model()
|
80 |
+
|
81 |
+
# Upload image
|
82 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
83 |
+
|
84 |
+
if uploaded_file is not None:
|
85 |
+
# Convert the uploaded file to an image
|
86 |
+
img = Image.open(uploaded_file)
|
87 |
+
|
88 |
+
if st.session_state.current_image != uploaded_file:
|
89 |
+
st.session_state.current_image = uploaded_file
|
90 |
+
st.session_state.extracted_text = process_file(img, model, processor)
|
91 |
+
|
92 |
+
# Display the uploaded image
|
93 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
94 |
+
|
95 |
+
# if 'extracted_text' not in st.session_state:
|
96 |
+
# st.session_state.extracted_text = process_file(img, model, processor)
|
97 |
+
|
98 |
# Display the extracted text
|
|
|
99 |
st.subheader("Extracted Text")
|
100 |
+
st.write(st.session_state.extracted_text)
|
101 |
+
|
102 |
+
# Keyword Search
|
103 |
+
keyword = st.text_input("Enter keyword to search in the extracted text")
|
104 |
+
if keyword and st.session_state.extracted_text:
|
105 |
+
if keyword.lower() in st.session_state.extracted_text.lower():
|
106 |
+
highlighted_text = st.session_state.extracted_text.replace(keyword, f"**{keyword}**")
|
107 |
+
st.subheader("Keyword Found")
|
108 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
109 |
+
else:
|
110 |
+
st.write("Keyword not found in the extracted text.")
|
111 |
+
elif keyword:
|
112 |
+
st.write("Please upload an image first to extract text.")
|
113 |
+
|