hmfayyaz commited on
Commit
3faab6f
·
unverified ·
1 Parent(s): 225b038

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -28,14 +28,15 @@ def process_pdf_file(uploaded_file, use_gpu=False, airline="flydubai"):
28
  """
29
  extractor = PassportExtractor(use_gpu=use_gpu)
30
  results = []
31
-
32
- # Save uploaded file to temporary location
33
- suffix = os.path.splitext(uploaded_file.name)[1]
34
- with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
35
- tmp.write(uploaded_file.getvalue())
36
- tmp_path = tmp.name
37
 
38
  try:
 
 
 
 
 
 
39
  # Extract passport data from PDF with airline-specific formatting
40
  results = extractor.process_pdf(tmp_path, airline=airline.lower(), progress_callback=None)
41
  if not results:
@@ -47,12 +48,17 @@ def process_pdf_file(uploaded_file, use_gpu=False, airline="flydubai"):
47
 
48
  except Exception as e:
49
  print(f"❌ Error processing {uploaded_file.name}: {str(e)}")
 
 
50
  results = []
51
 
52
  finally:
53
  # Clean up temp file
54
- if os.path.exists(tmp_path):
55
- os.remove(tmp_path)
 
 
 
56
 
57
  return results
58
 
@@ -153,9 +159,16 @@ def main():
153
 
154
  except Exception as e:
155
  st.error(f"❌ Error processing {file.name}: {str(e)}")
 
 
156
 
157
  finally:
158
- os.remove(tmp_path)
 
 
 
 
 
159
 
160
  # Update main progress bar
161
  main_progress_bar.progress((i + 1) / len(uploaded_files))
 
28
  """
29
  extractor = PassportExtractor(use_gpu=use_gpu)
30
  results = []
31
+ tmp_path = None
 
 
 
 
 
32
 
33
  try:
34
+ # Save uploaded file to temporary location
35
+ suffix = os.path.splitext(uploaded_file.name)[1]
36
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
37
+ tmp.write(uploaded_file.getvalue())
38
+ tmp_path = tmp.name
39
+
40
  # Extract passport data from PDF with airline-specific formatting
41
  results = extractor.process_pdf(tmp_path, airline=airline.lower(), progress_callback=None)
42
  if not results:
 
48
 
49
  except Exception as e:
50
  print(f"❌ Error processing {uploaded_file.name}: {str(e)}")
51
+ import traceback
52
+ print(f"Debug info: {traceback.format_exc()}")
53
  results = []
54
 
55
  finally:
56
  # Clean up temp file
57
+ if tmp_path and os.path.exists(tmp_path):
58
+ try:
59
+ os.remove(tmp_path)
60
+ except Exception as cleanup_error:
61
+ print(f"Warning: Could not clean up temp file: {cleanup_error}")
62
 
63
  return results
64
 
 
159
 
160
  except Exception as e:
161
  st.error(f"❌ Error processing {file.name}: {str(e)}")
162
+ import traceback
163
+ st.error(f"Debug info: {traceback.format_exc()}")
164
 
165
  finally:
166
+ # Safely remove temp file
167
+ try:
168
+ if os.path.exists(tmp_path):
169
+ os.remove(tmp_path)
170
+ except Exception as cleanup_error:
171
+ st.warning(f"Warning: Could not clean up temp file: {cleanup_error}")
172
 
173
  # Update main progress bar
174
  main_progress_bar.progress((i + 1) / len(uploaded_files))