Sankalp Srivastava commited on
Commit
7459ced
·
1 Parent(s): f646c84

Added error handling

Browse files
Dict_Results.txt CHANGED
The diff for this file is too large to render. See raw diff
 
Matching_rows_Format.txt CHANGED
The diff for this file is too large to render. See raw diff
 
ResultsAfterClassification.txt CHANGED
The diff for this file is too large to render. See raw diff
 
app.py CHANGED
@@ -26,9 +26,13 @@ results = get_results.main(searchquery)
26
 
27
  #st.dataframe(results)
28
 
29
- df = pd.DataFrame(results)
30
-
31
- dict_results = df.to_dict('records')
 
 
 
 
32
 
33
  st.button("Generate/View results after text-classification")
34
 
 
26
 
27
  #st.dataframe(results)
28
 
29
+ if results:
30
+ df = pd.DataFrame(results)
31
+ dict_results = df.to_dict('records')
32
+ else:
33
+ st.warning("No results found. This may be because the database is not properly configured or no matching data exists.")
34
+ df = pd.DataFrame() # Empty DataFrame
35
+ dict_results = []
36
 
37
  st.button("Generate/View results after text-classification")
38
 
get_results.py CHANGED
@@ -133,8 +133,14 @@ def main(shortcode):
133
  # Check for documents that are already present
134
  list_of_docs_not_present = insert_data.check_for_already_present(lst)
135
 
136
- # Identify documents that are already present
137
- list_of_docs_already_present = [docid for docid in lst.keys() if docid not in list_of_docs_not_present]
 
 
 
 
 
 
138
 
139
  # Get text for new documents that are not already present
140
  lst_new_data = get_text_for_new_docs(list_of_docs_not_present, shortcode, lst)
 
133
  # Check for documents that are already present
134
  list_of_docs_not_present = insert_data.check_for_already_present(lst)
135
 
136
+ # Handle case where database check failed
137
+ if list_of_docs_not_present is None:
138
+ print("Error: Could not check database for existing documents. Treating all documents as new.")
139
+ list_of_docs_not_present = list(lst.keys())
140
+ list_of_docs_already_present = []
141
+ else:
142
+ # Identify documents that are already present
143
+ list_of_docs_already_present = [docid for docid in lst.keys() if docid not in list_of_docs_not_present]
144
 
145
  # Get text for new documents that are not already present
146
  lst_new_data = get_text_for_new_docs(list_of_docs_not_present, shortcode, lst)
insert_data.py CHANGED
@@ -92,7 +92,11 @@ def create_task(conn, task):
92
  try:
93
  # Using a context manager to handle the cursor
94
  with conn.cursor() as cur:
95
- cur.execute(sql_task, task)
 
 
 
 
96
  cur.execute(sql_results, task)
97
  conn.commit()
98
  except Exception as e:
@@ -115,7 +119,12 @@ def retrieve_text(conn, query):
115
 
116
  # Execute the query to fetch rows from the table
117
  cursor = conn.cursor()
118
- cursor.execute(sql_query)
 
 
 
 
 
119
 
120
  # Fetch all rows from the cursor
121
  rows = cursor.fetchall()
@@ -204,7 +213,11 @@ def add_stored_results(conn, lst):
204
  try:
205
  with conn.cursor() as cur:
206
  for docid in lst:
207
- cur.execute(sql, (docid,))
 
 
 
 
208
  conn.commit()
209
  print("Stored data transferred to tasks.")
210
  return cur.rowcount # Returns the total number of rows affected by the last execute call
@@ -244,10 +257,14 @@ def find_matching_text(column_value):
244
  def delete_sql_records(conn):
245
  delete_records = "DELETE FROM tasks"
246
 
247
- cur=conn.cursor()
248
- cur.execute(delete_records)
249
-
250
- print("Deleted records")
 
 
 
 
251
 
252
  def add_classified_results(dict_of_results, searchquery):
253
  conn = create_connection()
 
92
  try:
93
  # Using a context manager to handle the cursor
94
  with conn.cursor() as cur:
95
+ try:
96
+ cur.execute(sql_task, task)
97
+ except psycopg.errors.UndefinedTable:
98
+ print("Warning: 'tasks' table does not exist. Skipping tasks table insertion.")
99
+
100
  cur.execute(sql_results, task)
101
  conn.commit()
102
  except Exception as e:
 
119
 
120
  # Execute the query to fetch rows from the table
121
  cursor = conn.cursor()
122
+ try:
123
+ cursor.execute(sql_query)
124
+ except psycopg.errors.UndefinedTable:
125
+ print("Warning: 'tasks' table does not exist. No data to retrieve.")
126
+ cursor.close()
127
+ return [] # Return empty list if table doesn't exist
128
 
129
  # Fetch all rows from the cursor
130
  rows = cursor.fetchall()
 
213
  try:
214
  with conn.cursor() as cur:
215
  for docid in lst:
216
+ try:
217
+ cur.execute(sql, (docid,))
218
+ except psycopg.errors.UndefinedTable:
219
+ print("Warning: 'tasks' table does not exist. Cannot transfer data.")
220
+ return 0
221
  conn.commit()
222
  print("Stored data transferred to tasks.")
223
  return cur.rowcount # Returns the total number of rows affected by the last execute call
 
257
  def delete_sql_records(conn):
258
  delete_records = "DELETE FROM tasks"
259
 
260
+ cur = conn.cursor()
261
+ try:
262
+ cur.execute(delete_records)
263
+ print("Deleted records")
264
+ except psycopg.errors.UndefinedTable:
265
+ print("Warning: 'tasks' table does not exist. Skipping deletion.")
266
+ except Exception as e:
267
+ print(f"Error deleting records: {e}")
268
 
269
  def add_classified_results(dict_of_results, searchquery):
270
  conn = create_connection()