Spaces:
Build error
Build error
sanjaymalladi
commited on
Commit
·
9ad6883
1
Parent(s):
62bc58d
fixed few errors
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ from sklearn.naive_bayes import MultinomialNB
|
|
| 14 |
import numpy as np
|
| 15 |
import joblib
|
| 16 |
import base64
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# HTML template with embedded JavaScript
|
| 19 |
HTML_TEMPLATE = """
|
|
@@ -318,6 +320,7 @@ class EnhancedDocProcessor:
|
|
| 318 |
return results
|
| 319 |
|
| 320 |
app = Flask(__name__)
|
|
|
|
| 321 |
processor = EnhancedDocProcessor()
|
| 322 |
|
| 323 |
@app.route('/')
|
|
@@ -333,14 +336,21 @@ def batch_process():
|
|
| 333 |
user_id = request.form.get('user_id')
|
| 334 |
|
| 335 |
file_paths = []
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
file.save(temp_path)
|
| 340 |
-
file_paths.append(temp_path)
|
| 341 |
-
|
| 342 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
results = processor.process_batch(file_paths, user_id)
|
|
|
|
|
|
|
| 344 |
except Exception as e:
|
| 345 |
return jsonify({'error': str(e)}), 500
|
| 346 |
finally:
|
|
@@ -350,7 +360,8 @@ def batch_process():
|
|
| 350 |
os.remove(path)
|
| 351 |
except:
|
| 352 |
pass
|
| 353 |
-
|
|
|
|
| 354 |
return jsonify(results)
|
| 355 |
|
| 356 |
if __name__ == '__main__':
|
|
|
|
| 14 |
import numpy as np
|
| 15 |
import joblib
|
| 16 |
import base64
|
| 17 |
+
from werkzeug.utils import secure_filename
|
| 18 |
+
from flask_sqlalchemy import SQLAlchemy
|
| 19 |
|
| 20 |
# HTML template with embedded JavaScript
|
| 21 |
HTML_TEMPLATE = """
|
|
|
|
| 320 |
return results
|
| 321 |
|
| 322 |
app = Flask(__name__)
|
| 323 |
+
db = SQLAlchemy(app)
|
| 324 |
processor = EnhancedDocProcessor()
|
| 325 |
|
| 326 |
@app.route('/')
|
|
|
|
| 336 |
user_id = request.form.get('user_id')
|
| 337 |
|
| 338 |
file_paths = []
|
| 339 |
+
temp_dir = '/tmp/batch_process'
|
| 340 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 341 |
+
|
|
|
|
|
|
|
|
|
|
| 342 |
try:
|
| 343 |
+
for file in files:
|
| 344 |
+
if file.filename.endswith('.pdf'):
|
| 345 |
+
filename = secure_filename(file.filename)
|
| 346 |
+
temp_path = os.path.join(temp_dir, filename)
|
| 347 |
+
file.save(temp_path)
|
| 348 |
+
file_paths.append(temp_path)
|
| 349 |
+
|
| 350 |
+
processor = EnhancedDocProcessor(db, app.config['UPLOAD_FOLDER'], app.config['TEMP_FOLDER'])
|
| 351 |
results = processor.process_batch(file_paths, user_id)
|
| 352 |
+
except PermissionError:
|
| 353 |
+
return jsonify({'error': 'Permission denied. Please check your permissions.'}), 403
|
| 354 |
except Exception as e:
|
| 355 |
return jsonify({'error': str(e)}), 500
|
| 356 |
finally:
|
|
|
|
| 360 |
os.remove(path)
|
| 361 |
except:
|
| 362 |
pass
|
| 363 |
+
shutil.rmtree(temp_dir)
|
| 364 |
+
|
| 365 |
return jsonify(results)
|
| 366 |
|
| 367 |
if __name__ == '__main__':
|