-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (81 loc) · 3.34 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
from PIL import Image
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
import torch
from qwen_vl_utils import process_vision_info
# Load the model and processor
@st.cache_resource # Cache the model loading for performance
def load_model():
model = Qwen2VLForConditionalGeneration.from_pretrained(
"Qwen/Qwen2-VL-2B-Instruct",
trust_remote_code=True,
torch_dtype=torch.bfloat16
).cpu().eval()
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
return model, processor
# Preprocess the image
def preprocess_image(image):
image = image.convert("RGB")
return image
# Extract text from the image using the model
def extract_text_from_image(image, model, processor):
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image}, # Replace 'image' with your actual image data
{"type": "text", "text": "Extract all text from this image, including both Hindi and English."}
]
}
]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt"
).to("cpu")
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
return output_text
# Highlight search term in text
def highlight_text(text, search_term):
highlighted_text = text.replace(search_term, f"<mark>{search_term}</mark>")
return highlighted_text
# Streamlit App
st.title("OCR Web Application")
# Upload the image file
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
input_image = Image.open(uploaded_file)
st.image(input_image, caption="Uploaded Image.", use_column_width=True)
# Load the OCR model and processor
model, processor = load_model()
# Preprocess and run OCR
processed_image = preprocess_image(input_image)
extracted_text = extract_text_from_image(processed_image, model, processor)
# extracted_text is a list, so join it into a single string
extracted_text_str = " ".join(extracted_text) # Convert list to a single string
extracted_text_str = " ".join(extracted_text_str.split()) # Clean up spaces
# Now you can safely use extracted_text_str as a normal string
print("Extracted text:", extracted_text_str)
# Display extracted text
st.subheader("Extracted Text:")
st.write(extracted_text_str)
# Search functionality
search_query = st.text_input("Search for keywords in extracted text")
if search_query:
if search_query.lower() in extracted_text_str.lower():
highlighted_result = highlight_text(extracted_text_str, search_query)
st.subheader("Search Results:")
st.markdown(f"<p>{highlighted_result}</p>", unsafe_allow_html=True)
else:
st.subheader("Search Results:")
st.write("No matches found.")