rajsecrets0 commited on
Commit
752af65
·
verified ·
1 Parent(s): 0d1227c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -42
app.py CHANGED
@@ -7,35 +7,24 @@ import fitz # PyMuPDF
7
  import time
8
  import os
9
 
10
- # Configuration
11
- GEMINI_API_KEY = st.secrets["GEMINI_API_KEY"]
12
- GEMINI_MODEL = "gemini-2.0-flash" # Verify with Gemini API documentation
13
  DOCUMENT_TYPES = ["Land Records", "Caste Certificates", "Property Registrations"]
14
 
15
- # Define a class to mimic Streamlit's UploadedFile object for the example image
16
- class ExampleFile:
17
- def __init__(self, file_path):
18
- self.name = os.path.basename(file_path)
19
- self.type = "image/jpeg"
20
- with open(file_path, "rb") as f:
21
- self._buffer = BytesIO(f.read())
22
-
23
- def getvalue(self):
24
- return self._buffer.getvalue()
25
-
26
- # Initialize session state
27
  def initialize_session_state():
28
  if "processed_doc" not in st.session_state:
29
  st.session_state.processed_doc = None
30
  if "current_file" not in st.session_state:
31
  st.session_state.current_file = None
 
 
32
 
33
  # Reset session state
34
  def reset_session_state():
35
  st.session_state.processed_doc = None
36
  st.session_state.current_file = None
37
 
38
- # Encode file to base64 for Gemini API
39
  def encode_file(file):
40
  try:
41
  file_content = file.getvalue()
@@ -46,23 +35,27 @@ def encode_file(file):
46
 
47
  # Query Gemini API
48
  def query_gemini(prompt, image_b64):
 
 
 
 
49
  try:
50
  headers = {
51
- "Authorization": f"Bearer {GEMINI_API_KEY}",
52
  "Content-Type": "application/json"
53
  }
54
  payload = {
55
- "model": GEMINI_MODEL,
56
  "prompt": prompt,
57
  "image": image_b64
58
  }
59
  response = requests.post(
60
- "https://api.gemini.com/v1/analyze", # Replace with actual Gemini API endpoint
61
  headers=headers,
62
  json=payload
63
  )
64
  response.raise_for_status()
65
- return response.json().get("result", "")
66
  except Exception as e:
67
  st.error(f"Gemini API error: {str(e)}")
68
  return None
@@ -71,7 +64,6 @@ def query_gemini(prompt, image_b64):
71
  def process_document(file):
72
  try:
73
  with st.spinner("Analyzing document..."):
74
- # Encode file to base64
75
  image_b64 = encode_file(file)
76
  if not image_b64:
77
  return
@@ -84,12 +76,6 @@ def process_document(file):
84
  st.session_state.doc_preview = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
85
  elif file.type.startswith('image/'):
86
  st.session_state.doc_preview = Image.open(BytesIO(file.getvalue()))
87
- elif file.type == "text/plain":
88
- text = file.getvalue().decode('utf-8')
89
- img = Image.new('RGB', (800, 600), color=(73, 109, 137))
90
- d = ImageDraw.Draw(img)
91
- d.text((10, 10), text, fill=(255, 255, 0))
92
- st.session_state.doc_preview = img
93
  else:
94
  st.error("Unsupported file format")
95
  return
@@ -109,7 +95,7 @@ def process_document(file):
109
  details = query_gemini(extract_prompt, image_b64)
110
 
111
  # Verify authenticity
112
- verify_prompt = "Analyze this document for signs of tampering or forgery. Check for: inconsistent fonts, alignment issues, suspicious modifications. Provide verification conclusion."
113
  verification = query_gemini(verify_prompt, image_b64)
114
 
115
  st.session_state.processed_doc = {
@@ -134,24 +120,20 @@ def main():
134
  # Sidebar Controls
135
  with st.sidebar:
136
  st.header("Document Controls")
137
-
 
 
 
138
  uploaded_file = st.file_uploader(
139
  "Upload Document",
140
- type=["pdf", "jpg", "jpeg", "png", "txt"],
141
  key="uploaded_file"
142
  )
143
 
144
- if st.button("Use Example Image"):
145
- example_file_path = "Caste-Certificate.jpg"
146
- st.session_state.current_file = ExampleFile(example_file_path)
147
- elif uploaded_file:
148
- st.session_state.current_file = uploaded_file
149
- else:
150
- st.session_state.current_file = None
151
-
152
  if st.button("Process Document"):
153
- if st.session_state.current_file:
154
- process_document(st.session_state.current_file)
 
155
  else:
156
  st.error("Please select a document to process.")
157
 
@@ -181,7 +163,7 @@ def main():
181
  st.subheader("Verification Analysis")
182
  st.markdown(st.session_state.processed_doc['verification'])
183
  else:
184
- st.info("Please select a document and click 'Process Document' to begin verification analysis.")
185
 
186
  if __name__ == "__main__":
187
- main()
 
7
  import time
8
  import os
9
 
10
+ # Document categories
 
 
11
  DOCUMENT_TYPES = ["Land Records", "Caste Certificates", "Property Registrations"]
12
 
13
+ # Set up session state
 
 
 
 
 
 
 
 
 
 
 
14
  def initialize_session_state():
15
  if "processed_doc" not in st.session_state:
16
  st.session_state.processed_doc = None
17
  if "current_file" not in st.session_state:
18
  st.session_state.current_file = None
19
+ if "gemini_api_key" not in st.session_state:
20
+ st.session_state.gemini_api_key = None
21
 
22
  # Reset session state
23
  def reset_session_state():
24
  st.session_state.processed_doc = None
25
  st.session_state.current_file = None
26
 
27
+ # Encode file to base64
28
  def encode_file(file):
29
  try:
30
  file_content = file.getvalue()
 
35
 
36
  # Query Gemini API
37
  def query_gemini(prompt, image_b64):
38
+ if not st.session_state.gemini_api_key:
39
+ st.error("API Key is missing. Please enter your Gemini API Key.")
40
+ return None
41
+
42
  try:
43
  headers = {
44
+ "Authorization": f"Bearer {st.session_state.gemini_api_key}",
45
  "Content-Type": "application/json"
46
  }
47
  payload = {
48
+ "model": "gemini-1.5-pro", # Change based on the latest API model
49
  "prompt": prompt,
50
  "image": image_b64
51
  }
52
  response = requests.post(
53
+ "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent", # Corrected URL
54
  headers=headers,
55
  json=payload
56
  )
57
  response.raise_for_status()
58
+ return response.json().get("result", "No response received.")
59
  except Exception as e:
60
  st.error(f"Gemini API error: {str(e)}")
61
  return None
 
64
  def process_document(file):
65
  try:
66
  with st.spinner("Analyzing document..."):
 
67
  image_b64 = encode_file(file)
68
  if not image_b64:
69
  return
 
76
  st.session_state.doc_preview = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
77
  elif file.type.startswith('image/'):
78
  st.session_state.doc_preview = Image.open(BytesIO(file.getvalue()))
 
 
 
 
 
 
79
  else:
80
  st.error("Unsupported file format")
81
  return
 
95
  details = query_gemini(extract_prompt, image_b64)
96
 
97
  # Verify authenticity
98
+ verify_prompt = "Analyze this document for signs of tampering or forgery. Provide verification results."
99
  verification = query_gemini(verify_prompt, image_b64)
100
 
101
  st.session_state.processed_doc = {
 
120
  # Sidebar Controls
121
  with st.sidebar:
122
  st.header("Document Controls")
123
+
124
+ # API Key input
125
+ st.session_state.gemini_api_key = st.text_input("Enter Gemini API Key", type="password")
126
+
127
  uploaded_file = st.file_uploader(
128
  "Upload Document",
129
+ type=["pdf", "jpg", "jpeg", "png"],
130
  key="uploaded_file"
131
  )
132
 
 
 
 
 
 
 
 
 
133
  if st.button("Process Document"):
134
+ if uploaded_file:
135
+ st.session_state.current_file = uploaded_file
136
+ process_document(uploaded_file)
137
  else:
138
  st.error("Please select a document to process.")
139
 
 
163
  st.subheader("Verification Analysis")
164
  st.markdown(st.session_state.processed_doc['verification'])
165
  else:
166
+ st.info("Please upload a document and click 'Process Document' to start verification.")
167
 
168
  if __name__ == "__main__":
169
+ main()