PRIYANSHUDHAKED commited on
Commit
a9c96c1
·
verified ·
1 Parent(s): d85cb4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
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
- from IPython.display import HTML, display
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
- 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()
 
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()