Saurabh Kumar commited on
Commit
0be6d6e
·
verified ·
1 Parent(s): 7a40854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -43
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2
  from qwen_vl_utils import process_vision_info
3
  import streamlit as st
4
  import torch
@@ -12,6 +12,7 @@ def init_qwen_model():
12
  return model, processor
13
 
14
  MODEL, PROCESSOR = init_qwen_model()
 
15
  # Streamlit app title
16
  st.title("OCR Image Text Extraction")
17
 
@@ -22,55 +23,63 @@ if uploaded_file is not None:
22
  # Open the uploaded image file
23
  image = Image.open(uploaded_file)
24
  st.image(image, caption="Uploaded Image", use_column_width=True)
25
-
26
- messages = [
27
- {
28
- "role": "user",
29
- "content": [
30
- {
31
- "type": "image",
32
- "image": image,
33
- },
34
- {"type": "text", "text": "Run Optical Character recognition on the image."},
35
- ],
36
- }
37
- ]
38
 
39
- # Preparation for inference
40
- text = PROCESSOR.apply_chat_template(
41
- messages, tokenize=False, add_generation_prompt=True
42
- )
43
- image_inputs, video_inputs = process_vision_info(messages)
44
- inputs = PROCESSOR(
45
- text=[text],
46
- images=image_inputs,
47
- videos=video_inputs,
48
- padding=True,
49
- return_tensors="pt",
50
- )
51
- inputs = inputs.to("cpu")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # Inference: Generation of the output
54
- generated_ids = MODEL.generate(**inputs, max_new_tokens=128)
55
- generated_ids_trimmed = [
56
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
57
- ]
58
- output_text = PROCESSOR.batch_decode(
59
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
60
- )
61
- st.subheader("Extracted Text:")
62
- st.write(output_text)
63
 
64
- # Keyword search functionality
65
  st.subheader("Keyword Search")
66
  search_query = st.text_input("Enter keywords to search within the extracted text")
67
 
68
  if search_query:
69
- # Check if the search query is in the extracted text
70
- if search_query.lower() in output.lower():
71
- highlighted_text = output.replace(search_query, f"**{search_query}**")
72
- st.write(f"Matching Text: {highlighted_text}")
 
73
  else:
74
  st.write("No matching text found.")
75
  else:
76
- st.info("Please upload an image to extract text.")
 
1
+ from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
2
  from qwen_vl_utils import process_vision_info
3
  import streamlit as st
4
  import torch
 
12
  return model, processor
13
 
14
  MODEL, PROCESSOR = init_qwen_model()
15
+
16
  # Streamlit app title
17
  st.title("OCR Image Text Extraction")
18
 
 
23
  # Open the uploaded image file
24
  image = Image.open(uploaded_file)
25
  st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Add the spinner here while the model is processing
28
+ with st.spinner("Extracting text..."):
29
+ messages = [
30
+ {
31
+ "role": "user",
32
+ "content": [
33
+ {
34
+ "type": "image",
35
+ "image": image,
36
+ },
37
+ {"type": "text", "text": "Run Optical Character recognition on the image."},
38
+ ],
39
+ }
40
+ ]
41
+
42
+ # Preparation for inference
43
+ text = PROCESSOR.apply_chat_template(
44
+ messages, tokenize=False, add_generation_prompt=True
45
+ )
46
+ image_inputs, video_inputs = process_vision_info(messages)
47
+ inputs = PROCESSOR(
48
+ text=[text],
49
+ images=image_inputs,
50
+ videos=video_inputs,
51
+ padding=True,
52
+ return_tensors="pt",
53
+ )
54
+ inputs = inputs.to("cpu")
55
+
56
+ # Inference: Generation of the output
57
+ generated_ids = MODEL.generate(**inputs, max_new_tokens=128)
58
+ generated_ids_trimmed = [
59
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
60
+ ]
61
+ structured_output = PROCESSOR.batch_decode(
62
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
63
+ )[0]
64
+
65
+ # Convert structured output to plain text
66
+ plain_text_output = " ".join(structured_output.split()) # Remove any extra spaces or line breaks
67
 
68
+ # Display extracted plain text after the spinner ends
69
+ st.subheader("Extracted Plain Text:")
70
+ st.write(plain_text_output)
 
 
 
 
 
 
 
71
 
72
+ # Keyword search functionality on plain text
73
  st.subheader("Keyword Search")
74
  search_query = st.text_input("Enter keywords to search within the extracted text")
75
 
76
  if search_query:
77
+ # Check if the search query is in the plain text output
78
+ if search_query.lower() in plain_text_output.lower():
79
+ # Highlight the search query in the plain text
80
+ highlighted_text = plain_text_output.replace(search_query, f"**{search_query}**", flags=re.IGNORECASE)
81
+ st.markdown(f"Matching Text: {highlighted_text}", unsafe_allow_html=True)
82
  else:
83
  st.write("No matching text found.")
84
  else:
85
+ st.info("Please upload an image to extract text.")