UniquePratham commited on
Commit
95bd226
1 Parent(s): 1c8f168

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -26
app.py CHANGED
@@ -11,6 +11,7 @@ import base64
11
  from groq import Groq
12
  from st_keyup import st_keyup
13
  from st_img_pastebutton import paste
 
14
 
15
  # Page configuration
16
  st.set_page_config(page_title="DualTextOCRFusion",
@@ -102,13 +103,14 @@ def extract_text_qwen(image_file, model, processor):
102
  # Function to highlight the keyword in the text
103
 
104
 
105
- def highlight_text(text, search_term):
106
- if not search_term: # If no search term is provided, return the original text
107
- return text
108
- # Use a regular expression to search for the term, case insensitive
109
- pattern = re.compile(re.escape(search_term), re.IGNORECASE)
110
- # Highlight matched terms with yellow background
111
- return pattern.sub(lambda m: f'<span style="background-color: yellow;">{m.group()}</span>', text)
 
112
 
113
 
114
  # Title and UI
@@ -123,11 +125,11 @@ model_choice = st.sidebar.selectbox(
123
 
124
  # Upload Section
125
  uploaded_file = st.sidebar.file_uploader(
126
- "Choose an image...", type=["png", "jpg", "jpeg"])
127
 
128
  # Input from clipboard
129
  # Paste image button
130
- image_data = paste(label="Paste From Clipboard", key="image_clipboard")
131
  if image_data is not None:
132
  clipboard_use = True
133
  header, encoded = image_data.split(",", 1)
@@ -136,7 +138,7 @@ if image_data is not None:
136
  uploaded_file = img_stream
137
 
138
  # Input from camera
139
- camera_file = st.sidebar.camera_input("Capture from Camera")
140
  if camera_file:
141
  uploaded_file = camera_file
142
 
@@ -206,20 +208,15 @@ if uploaded_file:
206
  st.markdown(cleaned_text, unsafe_allow_html=True)
207
  st.markdown(polished_text, unsafe_allow_html=True)
208
 
209
- # Search functionality
210
- def update_search():
211
- if search_query:
212
- highlighted_text = highlight_text(extracted_text, search_query)
213
- st.session_state["highlighted_result"] = highlighted_text
214
- else:
215
- st.session_state["highlighted_result"] = extracted_text
216
-
217
  # Input search term with real-time update on key press
218
- search_query = st_keyup(
219
- "Search in extracted text:", key="search_key", on_change=update_search)
220
-
221
- # Display highlighted results if they exist in session state
222
- if "highlighted_result" in st.session_state:
223
- st.markdown("### Highlighted Search Results:")
224
- st.markdown(
225
- st.session_state["highlighted_result"], unsafe_allow_html=True)
 
 
 
 
11
  from groq import Groq
12
  from st_keyup import st_keyup
13
  from st_img_pastebutton import paste
14
+ from text_highlighter import text_highlighter
15
 
16
  # Page configuration
17
  st.set_page_config(page_title="DualTextOCRFusion",
 
103
  # Function to highlight the keyword in the text
104
 
105
 
106
+ def highlight_text(cleaned_text,start,end):
107
+ result = text_highlighter(
108
+ text=cleaned_text,
109
+ labels=[("KEYWORD", "#0000FF")],
110
+ annotations=[
111
+ {"start": start, "end": end, "tag": "KEYWORD"},
112
+ ],
113
+ )
114
 
115
 
116
  # Title and UI
 
125
 
126
  # Upload Section
127
  uploaded_file = st.sidebar.file_uploader(
128
+ "Choose An Image : ", type=["png", "jpg", "jpeg"])
129
 
130
  # Input from clipboard
131
  # Paste image button
132
+ image_data = st.sidebar.paste(label="Paste From Clipboard", key="image_clipboard")
133
  if image_data is not None:
134
  clipboard_use = True
135
  header, encoded = image_data.split(",", 1)
 
138
  uploaded_file = img_stream
139
 
140
  # Input from camera
141
+ camera_file = st.sidebar.camera_input("Capture From Camera : ")
142
  if camera_file:
143
  uploaded_file = camera_file
144
 
 
208
  st.markdown(cleaned_text, unsafe_allow_html=True)
209
  st.markdown(polished_text, unsafe_allow_html=True)
210
 
 
 
 
 
 
 
 
 
211
  # Input search term with real-time update on key press
212
+ search_query = st_keyup("Search in extracted text:")
213
+
214
+ if search_query:
215
+ index = cleaned_text.find(search_query)
216
+ start = index
217
+ len = search_query.length
218
+ end = index + len
219
+ if index != -1:
220
+ highlight_text(cleaned_text,start,end)
221
+ else:
222
+ st.write("No Search Found.")