Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,96 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import numpy as np
|
5 |
-
import pytesseract
|
6 |
from PIL import Image
|
|
|
|
|
7 |
import re
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Search functionality
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
f"<span style='background-color: yellow;'>{context[start-context_start:end-context_start]}</span>" +
|
48 |
-
context[end-context_start:]
|
49 |
-
)
|
50 |
-
st.markdown(f"...{highlighted_text}...")
|
51 |
else:
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
from google.colab import files
|
|
|
|
|
4 |
from PIL import Image
|
5 |
+
import io
|
6 |
+
from IPython.display import HTML, display
|
7 |
import re
|
8 |
|
9 |
+
# Google Gemini API Key
|
10 |
+
GOOGLE_API_KEY = os.getenv("AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
|
11 |
+
|
12 |
+
# Configure Google Gemini with your API key
|
13 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
14 |
+
|
15 |
+
# Create a GenerativeModel instance
|
16 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
17 |
+
|
18 |
+
def extract_text_with_gemini(image):
|
19 |
+
prompt = """
|
20 |
+
Extract all text from this image. Provide the output as plain text,
|
21 |
+
maintaining the general layout and structure of the document.
|
22 |
+
Include all visible text, headings, and any important information.
|
23 |
+
"""
|
24 |
+
response = model.generate_content([prompt, image])
|
25 |
+
return response.text
|
26 |
+
|
27 |
+
def search_and_highlight(full_text, keyword):
|
28 |
+
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
|
29 |
+
matches = list(pattern.finditer(full_text))
|
30 |
+
|
31 |
+
if not matches:
|
32 |
+
return [], full_text
|
33 |
+
|
34 |
+
highlighted_text = full_text
|
35 |
+
html_text = full_text
|
36 |
+
results = []
|
37 |
+
|
38 |
+
for match in reversed(matches):
|
39 |
+
start, end = match.span()
|
40 |
+
context_start = max(0, start - 50)
|
41 |
+
context_end = min(len(full_text), end + 50)
|
42 |
+
context = full_text[context_start:context_end]
|
43 |
+
|
44 |
+
# Highlight for console output
|
45 |
+
highlighted_context = (
|
46 |
+
context[:start-context_start] +
|
47 |
+
'\033[43m' + context[start-context_start:end-context_start] + '\033[0m' +
|
48 |
+
context[end-context_start:]
|
49 |
+
)
|
50 |
+
results.append(highlighted_context)
|
51 |
+
|
52 |
+
# Highlight for HTML output
|
53 |
+
html_text = (
|
54 |
+
html_text[:start] +
|
55 |
+
f'<mark>{html_text[start:end]}</mark>' +
|
56 |
+
html_text[end:]
|
57 |
+
)
|
58 |
+
|
59 |
+
return results, html_text
|
60 |
+
|
61 |
+
def app():
|
62 |
+
uploaded = files.upload()
|
63 |
+
|
64 |
+
for filename, file_content in uploaded.items():
|
65 |
+
# Open and display the image
|
66 |
+
image = Image.open(io.BytesIO(file_content))
|
67 |
+
display(image)
|
68 |
+
|
69 |
+
print("Extracting text from the image...")
|
70 |
+
extracted_text = extract_text_with_gemini(image)
|
71 |
+
|
72 |
+
print("Extracted Text:")
|
73 |
+
print(extracted_text)
|
74 |
|
75 |
# Search functionality
|
76 |
+
while True:
|
77 |
+
search_keyword = input("\nEnter a keyword to search (or press Enter to exit): ")
|
78 |
+
if not search_keyword:
|
79 |
+
break
|
80 |
+
|
81 |
+
results, html_text = search_and_highlight(extracted_text, search_keyword)
|
82 |
+
|
83 |
+
if results:
|
84 |
+
print(f"Keyword '{search_keyword}' found in the extracted text:")
|
85 |
+
for i, result in enumerate(results, 1):
|
86 |
+
print(f"{i}. ...{result}...")
|
87 |
+
|
88 |
+
# Display HTML with highlighted text
|
89 |
+
display(HTML(f"<p>{html_text}</p>"))
|
|
|
|
|
|
|
|
|
90 |
else:
|
91 |
+
print(f"Keyword '{search_keyword}' not found in the extracted text.")
|
92 |
+
|
93 |
+
print("OCR and search completed.")
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
app()
|