sanjaymalladi commited on
Commit
0ae087f
·
1 Parent(s): 77014ce

changes made

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -15,7 +15,7 @@ import numpy as np
15
  import joblib
16
  import base64
17
  from werkzeug.utils import secure_filename
18
-
19
  # HTML template with embedded JavaScript
20
  HTML_TEMPLATE = """
21
  <!DOCTYPE html>
@@ -333,26 +333,25 @@ def batch_process():
333
  files = request.files.getlist('files[]')
334
  user_id = request.form.get('user_id')
335
 
336
- file_paths = []
337
- for file in files:
338
- if file.filename.endswith('.pdf'):
339
- temp_path = f"temp_{file.filename}"
340
- file.save(temp_path)
341
- file_paths.append(temp_path)
342
-
343
- try:
344
- results = processor.process_batch(file_paths, user_id)
345
- except Exception as e:
346
- return jsonify({'error': str(e)}), 500
347
- finally:
348
- # Clean up temporary files
349
- for path in file_paths:
350
- try:
351
- os.remove(path)
352
- except:
353
- pass
354
 
355
- return jsonify(results)
356
 
357
  if __name__ == '__main__':
358
  app.run(host='0.0.0.0', port=7860, debug=True)
 
15
  import joblib
16
  import base64
17
  from werkzeug.utils import secure_filename
18
+ import tempfile
19
  # HTML template with embedded JavaScript
20
  HTML_TEMPLATE = """
21
  <!DOCTYPE html>
 
333
  files = request.files.getlist('files[]')
334
  user_id = request.form.get('user_id')
335
 
336
+ # Create a temporary directory
337
+ with tempfile.TemporaryDirectory() as temp_dir:
338
+ file_paths = []
339
+ for file in files:
340
+ if file.filename.endswith('.pdf'):
341
+ # Create a secure filename
342
+ secure_name = secure_filename(file.filename)
343
+ # Create full path in temporary directory
344
+ temp_path = os.path.join(temp_dir, secure_name)
345
+ file.save(temp_path)
346
+ file_paths.append(temp_path)
347
+
348
+ try:
349
+ results = processor.process_batch(file_paths, user_id)
350
+ except Exception as e:
351
+ return jsonify({'error': str(e)}), 500
352
+ # No need to manually clean up - TemporaryDirectory does it automatically
 
353
 
354
+ return jsonify(results)
355
 
356
  if __name__ == '__main__':
357
  app.run(host='0.0.0.0', port=7860, debug=True)