Spaces:
Running
Running
UniquePratham
commited on
Commit
•
1a5d3d0
1
Parent(s):
7da5361
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import torch
|
|
9 |
import tempfile
|
10 |
import os
|
11 |
import re
|
|
|
12 |
from groq import Groq
|
13 |
|
14 |
# Page configuration
|
@@ -65,7 +66,7 @@ def polish_text_with_ai(cleaned_text):
|
|
65 |
],
|
66 |
model="gemma2-9b-it",
|
67 |
)
|
68 |
-
polished_text=chat_completion.choices[0].message.content
|
69 |
return polished_text
|
70 |
|
71 |
# Extract text using GOT
|
@@ -106,6 +107,24 @@ model_choice = st.sidebar.selectbox("Select OCR Model:", ("GOT_CPU", "GOT_GPU",
|
|
106 |
# Upload Section
|
107 |
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
# Predict button
|
110 |
predict_button = st.sidebar.button("Predict")
|
111 |
|
@@ -151,10 +170,7 @@ if predict_button and uploaded_file:
|
|
151 |
cleaned_text = clean_extracted_text(extracted_text)
|
152 |
|
153 |
# Optionally, polish text with AI model for better language flow
|
154 |
-
if model_choice in ["GOT_CPU", "GOT_GPU"]
|
155 |
-
polished_text = polish_text_with_ai(cleaned_text)
|
156 |
-
else:
|
157 |
-
polished_text = cleaned_text
|
158 |
|
159 |
# Delete temp file
|
160 |
if os.path.exists(temp_file_path):
|
@@ -166,14 +182,13 @@ if predict_button and uploaded_file:
|
|
166 |
|
167 |
# Input box for real-time search
|
168 |
search_query = st.text_input("Search in extracted text:", key="search_query", placeholder="Type to search...")
|
|
|
169 |
# Update results dynamically based on the search term
|
170 |
if search_query:
|
171 |
# Highlight the search term in the text
|
172 |
highlighted_text = highlight_text(polished_text, search_query)
|
173 |
st.markdown("### Highlighted Search Results:")
|
174 |
-
# Render the highlighted text, allowing HTML rendering for the highlight
|
175 |
st.markdown(highlighted_text, unsafe_allow_html=True)
|
176 |
else:
|
177 |
-
# If no search term is provided, display the original text
|
178 |
st.markdown("### Extracted Text:")
|
179 |
st.markdown(polished_text)
|
|
|
9 |
import tempfile
|
10 |
import os
|
11 |
import re
|
12 |
+
import base64
|
13 |
from groq import Groq
|
14 |
|
15 |
# Page configuration
|
|
|
66 |
],
|
67 |
model="gemma2-9b-it",
|
68 |
)
|
69 |
+
polished_text = chat_completion.choices[0].message.content
|
70 |
return polished_text
|
71 |
|
72 |
# Extract text using GOT
|
|
|
107 |
# Upload Section
|
108 |
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
109 |
|
110 |
+
# Input from clipboard
|
111 |
+
if st.sidebar.button("Paste from Clipboard"):
|
112 |
+
try:
|
113 |
+
clipboard_data = st.experimental_get_clipboard()
|
114 |
+
if clipboard_data:
|
115 |
+
# Assuming clipboard data is base64 encoded image
|
116 |
+
image_data = base64.b64decode(clipboard_data)
|
117 |
+
uploaded_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
118 |
+
uploaded_file.write(image_data)
|
119 |
+
uploaded_file.seek(0)
|
120 |
+
except:
|
121 |
+
st.sidebar.warning("Clipboard data is not an image.")
|
122 |
+
|
123 |
+
# Input from camera
|
124 |
+
camera_file = st.sidebar.camera_input("Capture from Camera")
|
125 |
+
if camera_file:
|
126 |
+
uploaded_file = camera_file
|
127 |
+
|
128 |
# Predict button
|
129 |
predict_button = st.sidebar.button("Predict")
|
130 |
|
|
|
170 |
cleaned_text = clean_extracted_text(extracted_text)
|
171 |
|
172 |
# Optionally, polish text with AI model for better language flow
|
173 |
+
polished_text = polish_text_with_ai(cleaned_text) if model_choice in ["GOT_CPU", "GOT_GPU"] else cleaned_text
|
|
|
|
|
|
|
174 |
|
175 |
# Delete temp file
|
176 |
if os.path.exists(temp_file_path):
|
|
|
182 |
|
183 |
# Input box for real-time search
|
184 |
search_query = st.text_input("Search in extracted text:", key="search_query", placeholder="Type to search...")
|
185 |
+
|
186 |
# Update results dynamically based on the search term
|
187 |
if search_query:
|
188 |
# Highlight the search term in the text
|
189 |
highlighted_text = highlight_text(polished_text, search_query)
|
190 |
st.markdown("### Highlighted Search Results:")
|
|
|
191 |
st.markdown(highlighted_text, unsafe_allow_html=True)
|
192 |
else:
|
|
|
193 |
st.markdown("### Extracted Text:")
|
194 |
st.markdown(polished_text)
|