Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
import os
|
2 |
import google.generativeai as genai
|
3 |
-
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
-
|
7 |
import re
|
8 |
|
9 |
# Google Gemini API Key
|
@@ -59,38 +58,36 @@ def search_and_highlight(full_text, keyword):
|
|
59 |
return results, html_text
|
60 |
|
61 |
def app():
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
# Open and display the image
|
66 |
-
image = Image.open(
|
67 |
-
|
68 |
|
69 |
print("Extracting text from the image...")
|
70 |
extracted_text = extract_text_with_gemini(image)
|
71 |
|
72 |
-
|
73 |
-
|
74 |
|
75 |
# Search functionality
|
76 |
-
|
77 |
-
|
78 |
-
if not search_keyword:
|
79 |
-
break
|
80 |
-
|
81 |
results, html_text = search_and_highlight(extracted_text, search_keyword)
|
82 |
|
83 |
if results:
|
84 |
-
|
85 |
for i, result in enumerate(results, 1):
|
86 |
-
|
87 |
|
88 |
# Display HTML with highlighted text
|
89 |
-
|
90 |
else:
|
91 |
-
|
92 |
|
93 |
-
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
app()
|
|
|
1 |
import os
|
2 |
import google.generativeai as genai
|
|
|
3 |
from PIL import Image
|
4 |
import io
|
5 |
+
import streamlit as st
|
6 |
import re
|
7 |
|
8 |
# Google Gemini API Key
|
|
|
58 |
return results, html_text
|
59 |
|
60 |
def app():
|
61 |
+
st.title("Image OCR and Search")
|
62 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
63 |
|
64 |
+
if uploaded_file is not None:
|
65 |
# Open and display the image
|
66 |
+
image = Image.open(uploaded_file)
|
67 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
68 |
|
69 |
print("Extracting text from the image...")
|
70 |
extracted_text = extract_text_with_gemini(image)
|
71 |
|
72 |
+
st.subheader("Extracted Text:")
|
73 |
+
st.write(extracted_text)
|
74 |
|
75 |
# Search functionality
|
76 |
+
search_keyword = st.text_input("Enter a keyword to search (or press Enter to exit)")
|
77 |
+
if search_keyword:
|
|
|
|
|
|
|
78 |
results, html_text = search_and_highlight(extracted_text, search_keyword)
|
79 |
|
80 |
if results:
|
81 |
+
st.subheader(f"Keyword '{search_keyword}' found in the extracted text:")
|
82 |
for i, result in enumerate(results, 1):
|
83 |
+
st.write(f"{i}. ...{result}...")
|
84 |
|
85 |
# Display HTML with highlighted text
|
86 |
+
st.markdown(f"<p>{html_text}</p>", unsafe_allow_html=True)
|
87 |
else:
|
88 |
+
st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
|
89 |
|
90 |
+
st.write("OCR and search completed.")
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
app()
|