SVashishta1 commited on
Commit
b2a58db
Β·
1 Parent(s): 5facdeb
Files changed (1) hide show
  1. app.py +52 -18
app.py CHANGED
@@ -208,14 +208,26 @@ def process_file_upload(files):
208
  cursor.execute(f"SELECT COUNT(*) FROM {table_name};")
209
  row_count = cursor.fetchone()[0]
210
 
 
 
 
 
211
  conn.close()
212
 
213
- file_info.append(f"CSV data loaded into table: {table_name}")
214
- file_info.append(f"Columns: {', '.join(columns)}")
215
- file_info.append(f"Rows: {row_count}")
 
 
 
 
 
 
 
 
216
 
217
  except Exception as e:
218
- file_info.append(f"Error loading CSV {file_name}: {str(e)}")
219
 
220
  else:
221
  # Process PDF or other document types
@@ -229,9 +241,12 @@ def process_file_upload(files):
229
  "table_name": None
230
  }
231
 
232
- file_info.append(f"{result['message']} ({result['chunks']} chunks)")
 
 
 
233
  except Exception as e:
234
- file_info.append(f"Error processing document {file_name}: {str(e)}")
235
 
236
  return "\n".join(file_info)
237
 
@@ -280,15 +295,9 @@ def load_csv_to_sqlite(file_path, conn, table_name):
280
 
281
  def list_documents():
282
  """List all indexed documents"""
283
- docs = document_assistant.get_all_documents()
284
- if not docs:
285
- return "No documents indexed yet"
286
-
287
- doc_list = []
288
- for doc in docs:
289
- doc_list.append(f"{doc['filename']} (ID: {doc['id']})")
290
 
291
- # Also list CSV tables
292
  try:
293
  conn = sqlite3.connect(DB_PATH)
294
  cursor = conn.cursor()
@@ -296,7 +305,7 @@ def list_documents():
296
  tables = cursor.fetchall()
297
 
298
  if tables:
299
- doc_list.append("\nCSV data tables:")
300
  for table in tables:
301
  # Get column info
302
  cursor.execute(f"PRAGMA table_info({table[0]});")
@@ -306,13 +315,38 @@ def list_documents():
306
  cursor.execute(f"SELECT COUNT(*) FROM {table[0]};")
307
  row_count = cursor.fetchone()[0]
308
 
309
- doc_list.append(f"- {table[0]} ({row_count} rows, {len(columns)} columns)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
 
311
  conn.close()
312
  except Exception as e:
313
- doc_list.append(f"Error listing CSV tables: {str(e)}")
 
 
 
 
 
 
 
 
 
 
314
 
315
- return "\n".join(doc_list)
316
 
317
  def clear_context():
318
  """Clear the current context and chat history"""
 
208
  cursor.execute(f"SELECT COUNT(*) FROM {table_name};")
209
  row_count = cursor.fetchone()[0]
210
 
211
+ # Get sample of data
212
+ cursor.execute(f"SELECT * FROM {table_name} LIMIT 5;")
213
+ sample_rows = cursor.fetchall()
214
+
215
  conn.close()
216
 
217
+ file_info.append("βœ… CSV File Successfully Loaded")
218
+ file_info.append(f"πŸ“Š Table Name: {table_name}")
219
+ file_info.append(f"πŸ“ˆ Total Rows: {row_count:,}")
220
+ file_info.append(f"\nπŸ“‹ Columns:")
221
+ for col in columns:
222
+ file_info.append(f" β€’ {col}")
223
+
224
+ if sample_rows:
225
+ file_info.append("\nπŸ” Sample Data (first 5 rows):")
226
+ sample_df = pd.DataFrame(sample_rows, columns=[col.split(' ')[0] for col in columns])
227
+ file_info.append(f"```\n{sample_df.to_string()}\n```")
228
 
229
  except Exception as e:
230
+ file_info.append(f"❌ Error loading CSV {file_name}: {str(e)}")
231
 
232
  else:
233
  # Process PDF or other document types
 
241
  "table_name": None
242
  }
243
 
244
+ file_info.append("βœ… Document Successfully Processed")
245
+ file_info.append(f"πŸ“„ File: {file_name}")
246
+ file_info.append(f"πŸ“š Chunks: {result['chunks']}")
247
+ file_info.append(result['message'])
248
  except Exception as e:
249
+ file_info.append(f"❌ Error processing document {file_name}: {str(e)}")
250
 
251
  return "\n".join(file_info)
252
 
 
295
 
296
  def list_documents():
297
  """List all indexed documents"""
298
+ info_list = []
 
 
 
 
 
 
299
 
300
+ # Check for CSV data
301
  try:
302
  conn = sqlite3.connect(DB_PATH)
303
  cursor = conn.cursor()
 
305
  tables = cursor.fetchall()
306
 
307
  if tables:
308
+ info_list.append("πŸ“Š CSV Data Tables:")
309
  for table in tables:
310
  # Get column info
311
  cursor.execute(f"PRAGMA table_info({table[0]});")
 
315
  cursor.execute(f"SELECT COUNT(*) FROM {table[0]};")
316
  row_count = cursor.fetchone()[0]
317
 
318
+ # Get sample of unique values for some interesting columns
319
+ sample_info = []
320
+ for col in ['vendor_id', 'rate_code', 'payment_type']:
321
+ if col in columns:
322
+ cursor.execute(f"SELECT DISTINCT {col} FROM {table[0]} LIMIT 5;")
323
+ unique_vals = [str(row[0]) for row in cursor.fetchall()]
324
+ if unique_vals:
325
+ sample_info.append(f"{col}: {', '.join(unique_vals)}")
326
+
327
+ info_list.append(f"\nπŸ”Ή Table: {table[0]}")
328
+ info_list.append(f" - Rows: {row_count:,}")
329
+ info_list.append(f" - Columns: {len(columns)}")
330
+ if sample_info:
331
+ info_list.append(" - Sample values:")
332
+ for info in sample_info:
333
+ info_list.append(f" β€’ {info}")
334
 
335
  conn.close()
336
  except Exception as e:
337
+ info_list.append(f"Error accessing CSV data: {str(e)}")
338
+
339
+ # Check for indexed documents
340
+ docs = document_assistant.get_all_documents()
341
+ if docs:
342
+ info_list.append("\nπŸ“‘ Indexed Documents:")
343
+ for doc in docs:
344
+ info_list.append(f"- {doc['filename']} (ID: {doc['id']})")
345
+
346
+ if not info_list:
347
+ return "No data or documents loaded yet"
348
 
349
+ return "\n".join(info_list)
350
 
351
  def clear_context():
352
  """Clear the current context and chat history"""