CosmickVisions commited on
Commit
07dd073
·
verified ·
1 Parent(s): 193673a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -12,7 +12,7 @@ import av
12
  from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration
13
  import cv2
14
  from typing import List, Union
15
- from google.cloud import documentai_v1 as documentai
16
  import pandas as pd
17
  from google.cloud import bigquery
18
  from google.cloud.exceptions import NotFound
@@ -243,18 +243,21 @@ def analyze_document(file_content, processor_id, location="us"):
243
  client = documentai.DocumentProcessorServiceClient(credentials=credentials)
244
 
245
  # The full resource name of the processor
246
- name = f"projects/{credentials.project_id}/locations/{location}/processors/{processor_id}"
247
 
248
- # Create document object
249
- document = documentai.Document(
250
- content=file_content,
251
- mime_type="application/pdf" # Adjust based on input type
252
- )
 
 
 
253
 
254
- # Configure the process request
255
  request = documentai.ProcessRequest(
256
- name=name,
257
- document=document
258
  )
259
 
260
  # Process the document
@@ -274,24 +277,32 @@ def analyze_document(file_content, processor_id, location="us"):
274
  for page in document.pages:
275
  for table in page.tables:
276
  table_data = []
277
- # Get header row
278
  headers = []
279
- for cell in table.header_rows[0].cells:
280
- headers.append(text[cell.layout.text_anchor.text_segments[0].start_index:
281
- cell.layout.text_anchor.text_segments[0].end_index])
 
 
 
 
282
 
283
  # Get data rows
284
  for row in table.body_rows:
285
  row_data = []
286
  for cell in row.cells:
287
- if len(cell.layout.text_anchor.text_segments) > 0:
288
- cell_text = text[cell.layout.text_anchor.text_segments[0].start_index:
289
- cell.layout.text_anchor.text_segments[0].end_index]
290
  row_data.append(cell_text)
291
  else:
292
  row_data.append("")
293
  table_data.append(row_data)
294
 
 
 
 
 
295
  tables.append({"headers": headers, "data": table_data})
296
 
297
  return text, entities, tables
@@ -335,6 +346,9 @@ def upload_csv_to_bigquery(file, dataset_id, table_id, append=False):
335
  # Create client
336
  bq_client = bigquery.Client(credentials=credentials, project=credentials.project_id)
337
 
 
 
 
338
  # Create a temporary file
339
  with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as temp_file:
340
  temp_file.write(file.getvalue())
@@ -352,10 +366,6 @@ def upload_csv_to_bigquery(file, dataset_id, table_id, append=False):
352
  else:
353
  job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
354
 
355
- # Create table reference
356
- dataset_ref = bq_client.dataset(dataset_id)
357
- table_ref = dataset_ref.table(table_id)
358
-
359
  # Load the file
360
  with open(temp_file_path, "rb") as source_file:
361
  job = bq_client.load_table_from_file(
 
12
  from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration
13
  import cv2
14
  from typing import List, Union
15
+ from google.cloud import documentai
16
  import pandas as pd
17
  from google.cloud import bigquery
18
  from google.cloud.exceptions import NotFound
 
243
  client = documentai.DocumentProcessorServiceClient(credentials=credentials)
244
 
245
  # The full resource name of the processor
246
+ processor_name = f"projects/{credentials.project_id}/locations/{location}/processors/{processor_id}"
247
 
248
+ # Determine the mime type based on input file type
249
+ if file_content[:4] == b'%PDF':
250
+ mime_type = "application/pdf"
251
+ else: # Default to image for other types
252
+ mime_type = "image/jpeg"
253
+
254
+ # Create the request
255
+ raw_document = documentai.RawDocument(content=file_content, mime_type=mime_type)
256
 
257
+ # Updated API request format
258
  request = documentai.ProcessRequest(
259
+ name=processor_name,
260
+ raw_document=raw_document
261
  )
262
 
263
  # Process the document
 
277
  for page in document.pages:
278
  for table in page.tables:
279
  table_data = []
280
+ # Get header row if available
281
  headers = []
282
+ if hasattr(table, 'header_rows') and table.header_rows:
283
+ for cell in table.header_rows[0].cells:
284
+ if cell.layout.text_anchor.text_segments:
285
+ segment = cell.layout.text_anchor.text_segments[0]
286
+ headers.append(text[segment.start_index:segment.end_index])
287
+ else:
288
+ headers.append("")
289
 
290
  # Get data rows
291
  for row in table.body_rows:
292
  row_data = []
293
  for cell in row.cells:
294
+ if cell.layout.text_anchor.text_segments:
295
+ segment = cell.layout.text_anchor.text_segments[0]
296
+ cell_text = text[segment.start_index:segment.end_index]
297
  row_data.append(cell_text)
298
  else:
299
  row_data.append("")
300
  table_data.append(row_data)
301
 
302
+ # If no header found, create generic column names
303
+ if not headers and table_data:
304
+ headers = [f"Column_{i+1}" for i in range(len(table_data[0]))]
305
+
306
  tables.append({"headers": headers, "data": table_data})
307
 
308
  return text, entities, tables
 
346
  # Create client
347
  bq_client = bigquery.Client(credentials=credentials, project=credentials.project_id)
348
 
349
+ # First, ensure dataset and table exist
350
+ table_ref = create_bigquery_table(dataset_id, table_id)
351
+
352
  # Create a temporary file
353
  with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as temp_file:
354
  temp_file.write(file.getvalue())
 
366
  else:
367
  job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
368
 
 
 
 
 
369
  # Load the file
370
  with open(temp_file_path, "rb") as source_file:
371
  job = bq_client.load_table_from_file(