PRIYANSHUDHAKED commited on
Commit
35f540b
·
verified ·
1 Parent(s): 77105cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -90
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
- prompt = """
19
- Extract all text from this image. Provide the output as plain text,
20
- maintaining the general layout and structure of the document.
21
- Include all visible text, headings, and any important information.
22
- """
23
- response = model.generate_content([prompt, image])
24
- text = response.text
25
- # Remove HTML tags from the extracted text
26
- text = re.sub(r'<[^>]+>', '', text)
27
- return text
28
-
29
- def search_and_highlight(full_text, keyword):
30
- pattern = re.compile(re.escape(keyword), re.IGNORECASE)
31
- matches = list(pattern.finditer(full_text))
32
-
33
- if not matches:
34
- return [], full_text
35
-
36
- highlighted_text = full_text
37
- html_text = full_text
38
- results = []
39
-
40
- for match in reversed(matches):
41
- start, end = match.span()
42
- context_start = max(0, start - 50)
43
- context_end = min(len(full_text), end + 50)
44
- context = full_text[context_start:context_end]
45
-
46
- # Highlight for console output
47
- highlighted_context = (
48
- context[:start-context_start] +
49
- '\033[43m' + context[start-context_start:end-context_start] + '\033[0m' +
50
- context[end-context_start:]
51
- )
52
- results.append(highlighted_context)
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, html_text
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
- print("Extracting text from the image...")
73
- extracted_text = extract_text_with_gemini(image)
74
-
75
- st.subheader("Extracted Text:")
76
- st.write(extracted_text)
77
-
78
- # Search functionality
79
- search_keyword = st.text_input("Enter a keyword to search (or press Enter to exit)")
80
- if search_keyword:
81
- results, html_text = search_and_highlight(extracted_text, search_keyword)
82
-
83
- if results:
84
- st.subheader(f"Keyword '{search_keyword}' found in the extracted text:")
85
- for i, result in enumerate(results, 1):
86
- st.write(f"{i}. ...{result}...")
87
-
88
- # Display HTML with highlighted text
89
- st.markdown(f"<p>{html_text}</p>", unsafe_allow_html=True)
90
- else:
91
- st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
92
-
93
- st.write("OCR and search completed.")
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
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()