Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,113 @@
|
|
| 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
|
| 9 |
-
GOOGLE_API_KEY = os.getenv("AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
|
| 10 |
-
|
| 11 |
-
# Configure Google Gemini with your API key
|
| 12 |
-
genai.configure(api_key="AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
|
| 13 |
-
|
| 14 |
-
# Create a GenerativeModel instance
|
| 15 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
-
|
| 17 |
-
def extract_text_with_gemini(image):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
results
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Highlight for full text
|
| 55 |
highlighted_text = (
|
| 56 |
highlighted_text[:start] +
|
| 57 |
f'<span style="background-color: yellow;">{highlighted_text[start:end]}</span>' +
|
| 58 |
highlighted_text[end:]
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
return results,
|
| 62 |
-
|
| 63 |
-
def app():
|
| 64 |
-
st.title("Image OCR and Search")
|
| 65 |
-
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 66 |
-
|
| 67 |
-
if uploaded_file is not None:
|
| 68 |
-
# Open and display the image
|
| 69 |
-
image = Image.open(uploaded_file)
|
| 70 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
st.
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 9 |
+
GOOGLE_API_KEY = os.getenv("AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
|
| 10 |
+
|
| 11 |
+
# Configure Google Gemini with your API key
|
| 12 |
+
genai.configure(api_key="AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
|
| 13 |
+
|
| 14 |
+
# Create a GenerativeModel instance
|
| 15 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 16 |
+
|
| 17 |
+
def extract_text_with_gemini(image, keyword=None):
|
| 18 |
+
if keyword:
|
| 19 |
+
prompt = f"""
|
| 20 |
+
Extract all text from this image. Provide the output as HTML, maintaining the general layout and structure of the document. Include all visible text, headings, and any important information.
|
| 21 |
+
Highlight all instances of the keyword '{keyword}' (case-insensitive) with a yellow background using HTML span tags.
|
| 22 |
+
For example: <span style="background-color: yellow;">keyword</span>
|
| 23 |
+
"""
|
| 24 |
+
else:
|
| 25 |
+
prompt = """
|
| 26 |
+
Extract all text from this image. Provide the output as plain text, maintaining the general layout and structure of the document. Include all visible text, headings, and any important information.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
response = model.generate_content([prompt, image])
|
| 30 |
+
text = response.text
|
| 31 |
+
|
| 32 |
+
if not keyword:
|
| 33 |
+
# Remove HTML tags from the extracted text when no keyword is provided
|
| 34 |
+
text = re.sub(r'<[^>]+>', '', text)
|
| 35 |
+
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
def search_and_highlight(full_text, keyword):
|
| 39 |
+
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
|
| 40 |
+
matches = list(pattern.finditer(full_text))
|
| 41 |
+
if not matches:
|
| 42 |
+
return [], full_text
|
| 43 |
+
|
| 44 |
+
highlighted_text = full_text
|
| 45 |
+
results = []
|
| 46 |
+
for match in reversed(matches):
|
| 47 |
+
start, end = match.span()
|
| 48 |
+
context_start = max(0, start - 50)
|
| 49 |
+
context_end = min(len(full_text), end + 50)
|
| 50 |
+
context = full_text[context_start:context_end]
|
| 51 |
+
|
| 52 |
+
# Highlight for results list
|
| 53 |
+
highlighted_context = (
|
| 54 |
+
context[:start-context_start] +
|
| 55 |
+
f'<span style="background-color: yellow;">{context[start-context_start:end-context_start]}</span>' +
|
| 56 |
+
context[end-context_start:]
|
| 57 |
+
)
|
| 58 |
+
results.append(highlighted_context)
|
| 59 |
+
|
| 60 |
# Highlight for full text
|
| 61 |
highlighted_text = (
|
| 62 |
highlighted_text[:start] +
|
| 63 |
f'<span style="background-color: yellow;">{highlighted_text[start:end]}</span>' +
|
| 64 |
highlighted_text[end:]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
return results, highlighted_text
|
| 68 |
+
|
| 69 |
+
def app():
|
| 70 |
+
st.title("Image OCR and Search")
|
| 71 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 72 |
+
|
| 73 |
+
if uploaded_file is not None:
|
| 74 |
+
# Open and display the image
|
| 75 |
+
image = Image.open(uploaded_file)
|
| 76 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 77 |
+
|
| 78 |
+
# Select search method
|
| 79 |
+
search_method = st.radio("Choose search method:",
|
| 80 |
+
("Extract text first, then search",
|
| 81 |
+
"Search while extracting text"))
|
| 82 |
+
|
| 83 |
+
search_keyword = st.text_input("Enter a keyword to search (or press Enter to exit)")
|
| 84 |
+
|
| 85 |
+
if st.button("Process Image"):
|
| 86 |
+
if search_method == "Extract text first, then search":
|
| 87 |
+
print("Extracting text from the image...")
|
| 88 |
+
extracted_text = extract_text_with_gemini(image)
|
| 89 |
+
st.subheader("Extracted Text:")
|
| 90 |
+
st.write(extracted_text)
|
| 91 |
+
|
| 92 |
+
if search_keyword:
|
| 93 |
+
results, highlighted_text = search_and_highlight(extracted_text, search_keyword)
|
| 94 |
+
if results:
|
| 95 |
+
st.subheader(f"Keyword '{search_keyword}' found in the extracted text:")
|
| 96 |
+
for i, result in enumerate(results, 1):
|
| 97 |
+
st.markdown(f"{i}. ...{result}...", unsafe_allow_html=True)
|
| 98 |
+
|
| 99 |
+
st.subheader("Full Text with Highlighted Keywords:")
|
| 100 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
| 101 |
+
else:
|
| 102 |
+
st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
|
| 103 |
+
|
| 104 |
+
else: # Search while extracting text
|
| 105 |
+
print("Extracting text and highlighting keyword...")
|
| 106 |
+
highlighted_text = extract_text_with_gemini(image, search_keyword)
|
| 107 |
+
st.subheader("Extracted Text with Highlighted Keyword:")
|
| 108 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
| 109 |
+
|
| 110 |
+
st.write("OCR and search completed.")
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
app()
|