clayton07 commited on
Commit
5e12612
·
verified ·
1 Parent(s): 4faaefc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -4,6 +4,7 @@ import torch
4
  from byaldi import RAGMultiModalModel
5
  from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
6
  from qwen_vl_utils import process_vision_info
 
7
 
8
  # Check for CUDA availability
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -33,10 +34,10 @@ processor = load_processor()
33
 
34
  st.title("Multimodal RAG App")
35
 
36
- st.warning("⚠️ Disclaimer: This app is currently running on CPU, which may result in slow processing times (only loading the example image takes ~5 minutes. For optimal performance, download and run the app locally on a machine with GPU support.")
37
 
38
  # Add download link
39
- st.markdown("[📥 Download the app code](https://huggingface.co/spaces/clayton07/colpali-qwen2-ocr/blob/main/app.py)")
40
 
41
  # Initialize session state for tracking if index is created
42
  if 'index_created' not in st.session_state:
@@ -80,7 +81,8 @@ if uploaded_file is not None:
80
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
81
 
82
  # Text query input
83
- text_query = st.text_input("Enter your query about the image:")
 
84
 
85
  max_new_tokens = st.slider("Max new tokens for response", min_value=100, max_value=1000, value=100, step=10)
86
 
@@ -99,7 +101,7 @@ if uploaded_file is not None:
99
  "type": "image",
100
  "image": image_path,
101
  },
102
- {"type": "text", "text": text_query},
103
  ],
104
  }
105
  ]
@@ -123,9 +125,28 @@ if uploaded_file is not None:
123
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
124
  )
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  # Display results
127
- st.subheader("Results:")
128
- st.write(output_text[0])
 
 
 
 
 
 
129
 
130
  # Clean up temporary file
131
  if image_source == "Upload an image":
 
4
  from byaldi import RAGMultiModalModel
5
  from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
6
  from qwen_vl_utils import process_vision_info
7
+ import re
8
 
9
  # Check for CUDA availability
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
34
 
35
  st.title("Multimodal RAG App")
36
 
37
+ st.warning("⚠️ Disclaimer: This app is currently running on CPU, which may result in slow processing times (even loading the image may take more than 10 minutes). For optimal performance, download and run the app locally on a machine with GPU support.")
38
 
39
  # Add download link
40
+ st.markdown("[📥 Download the app code](https://github.com/Claytonn7/qwen2-colpali-ocr)")
41
 
42
  # Initialize session state for tracking if index is created
43
  if 'index_created' not in st.session_state:
 
81
  st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
82
 
83
  # Text query input
84
+ text_query = st.text_input("Enter a single word to search for:")
85
+ extract_query = "extract text from the image"
86
 
87
  max_new_tokens = st.slider("Max new tokens for response", min_value=100, max_value=1000, value=100, step=10)
88
 
 
101
  "type": "image",
102
  "image": image_path,
103
  },
104
+ {"type": "text", "text": extract_query},
105
  ],
106
  }
107
  ]
 
125
  generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
126
  )
127
 
128
+ def highlight_text(text, query):
129
+ if not query.strip():
130
+ return text
131
+
132
+ escaped_query = re.escape(query)
133
+ pattern = r'\b' + escaped_query + r'\b'
134
+
135
+ def replacer(match):
136
+ return f'<span style="background-color: green;">{match.group(0)}</span>'
137
+
138
+ highlighted_text = re.sub(pattern, replacer, text, flags=re.IGNORECASE)
139
+ return highlighted_text
140
+
141
  # Display results
142
+ highlighted_output = highlight_text(output_text[0], text_query)
143
+
144
+ # Display results
145
+ st.subheader("Extracted Text (with query highlighted):")
146
+ st.markdown(highlighted_output, unsafe_allow_html=True)
147
+ # st.subheader("Results:")
148
+ # st.write(output_text[0])
149
+
150
 
151
  # Clean up temporary file
152
  if image_source == "Upload an image":