Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,116 @@
|
|
| 1 |
-
from flask import Flask, request, redirect, flash, session, render_template, url_for
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import logging
|
| 5 |
-
from werkzeug.utils import secure_filename
|
| 6 |
-
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error
|
| 7 |
-
from utils.spacy import Parser_from_model
|
| 8 |
-
from utils.mistral import process_resume_data
|
| 9 |
-
import platform
|
| 10 |
-
from waitress import serve
|
| 11 |
-
|
| 12 |
-
# Initialize the Flask application
|
| 13 |
-
app = Flask(__name__)
|
| 14 |
-
app.secret_key = 'your_secret_key'
|
| 15 |
-
app.config['UPLOAD_FOLDER'] = 'uploads'
|
| 16 |
-
|
| 17 |
-
# Allowed file extensions
|
| 18 |
-
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}
|
| 19 |
-
|
| 20 |
-
# Configure logging
|
| 21 |
-
logging.basicConfig(level=logging.DEBUG)
|
| 22 |
-
|
| 23 |
-
# Error handlers
|
| 24 |
-
app.register_error_handler(404, page_not_found)
|
| 25 |
-
app.register_error_handler(500, internal_server_error)
|
| 26 |
-
|
| 27 |
-
def allowed_file(filename):
|
| 28 |
-
"""Check if the file has an allowed extension."""
|
| 29 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 30 |
-
|
| 31 |
-
@app.route('/')
|
| 32 |
-
def index():
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if file
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, flash, session, render_template, url_for
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import logging
|
| 5 |
+
from werkzeug.utils import secure_filename
|
| 6 |
+
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error
|
| 7 |
+
from utils.spacy import Parser_from_model
|
| 8 |
+
from utils.mistral import process_resume_data
|
| 9 |
+
import platform
|
| 10 |
+
from waitress import serve
|
| 11 |
+
|
| 12 |
+
# Initialize the Flask application
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
app.secret_key = 'your_secret_key'
|
| 15 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
| 16 |
+
|
| 17 |
+
# Allowed file extensions
|
| 18 |
+
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}
|
| 19 |
+
|
| 20 |
+
# Configure logging
|
| 21 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 22 |
+
|
| 23 |
+
# Error handlers
|
| 24 |
+
app.register_error_handler(404, page_not_found)
|
| 25 |
+
app.register_error_handler(500, internal_server_error)
|
| 26 |
+
|
| 27 |
+
def allowed_file(filename):
|
| 28 |
+
"""Check if the file has an allowed extension."""
|
| 29 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 30 |
+
|
| 31 |
+
@app.route('/')
|
| 32 |
+
def index():
|
| 33 |
+
session.clear()
|
| 34 |
+
"""Display the index page with the uploaded file information."""
|
| 35 |
+
uploaded_file = session.get('uploaded_file', None)
|
| 36 |
+
return render_template('index.html', uploaded_file=uploaded_file)
|
| 37 |
+
|
| 38 |
+
@app.route('/upload_and_process', methods=['POST'])
|
| 39 |
+
def upload_and_process():
|
| 40 |
+
"""Handle file upload and process the file."""
|
| 41 |
+
if 'file' not in request.files or request.files['file'].filename == '':
|
| 42 |
+
flash('No file selected for upload.')
|
| 43 |
+
return redirect(request.url)
|
| 44 |
+
|
| 45 |
+
file = request.files['file']
|
| 46 |
+
|
| 47 |
+
# Check if the file is allowed
|
| 48 |
+
if file and allowed_file(file.filename):
|
| 49 |
+
filename = secure_filename(file.filename)
|
| 50 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 51 |
+
file.save(file_path)
|
| 52 |
+
logging.debug(f"File uploaded: {filename}")
|
| 53 |
+
session['uploaded_file'] = filename
|
| 54 |
+
|
| 55 |
+
# Process the file after uploading
|
| 56 |
+
try:
|
| 57 |
+
parsed_data = process_resume_data(file_path)
|
| 58 |
+
if not parsed_data or 'error' in parsed_data:
|
| 59 |
+
flash('An error occurred during file processing.')
|
| 60 |
+
return redirect(url_for('index'))
|
| 61 |
+
|
| 62 |
+
session['processed_data'] = parsed_data
|
| 63 |
+
flash('File uploaded and data processed successfully.')
|
| 64 |
+
return redirect(url_for('result'))
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logging.error(f"File processing error: {str(e)}")
|
| 68 |
+
flash('An error occurred while processing the file.')
|
| 69 |
+
return handle_file_processing_error()
|
| 70 |
+
else:
|
| 71 |
+
return handle_invalid_file_type()
|
| 72 |
+
|
| 73 |
+
@app.route('/remove_file', methods=['POST'])
|
| 74 |
+
def remove_file():
|
| 75 |
+
"""Remove the uploaded file and reset the session."""
|
| 76 |
+
uploaded_file = session.get('uploaded_file')
|
| 77 |
+
if uploaded_file:
|
| 78 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
|
| 79 |
+
if os.path.exists(file_path):
|
| 80 |
+
os.remove(file_path)
|
| 81 |
+
session.pop('uploaded_file', None)
|
| 82 |
+
flash('File successfully removed.')
|
| 83 |
+
else:
|
| 84 |
+
flash('No file to remove.')
|
| 85 |
+
return redirect(url_for('index'))
|
| 86 |
+
|
| 87 |
+
@app.route('/reset_upload')
|
| 88 |
+
def reset_upload():
|
| 89 |
+
"""Reset the uploaded file and the processed data."""
|
| 90 |
+
uploaded_file = session.get('uploaded_file')
|
| 91 |
+
if uploaded_file:
|
| 92 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
|
| 93 |
+
if os.path.exists(file_path):
|
| 94 |
+
os.remove(file_path)
|
| 95 |
+
session.pop('uploaded_file', None)
|
| 96 |
+
|
| 97 |
+
session.pop('processed_data', None)
|
| 98 |
+
flash('File and data reset. You can upload a new file.')
|
| 99 |
+
return redirect(url_for('index'))
|
| 100 |
+
|
| 101 |
+
@app.route('/result')
|
| 102 |
+
def result():
|
| 103 |
+
"""Display the processed data result."""
|
| 104 |
+
processed_data = session.get('processed_data', None)
|
| 105 |
+
if not processed_data:
|
| 106 |
+
flash('No data to display. Please upload and process a file.')
|
| 107 |
+
return redirect(url_for('index'))
|
| 108 |
+
return render_template('result.html', parsed_data=processed_data)
|
| 109 |
+
|
| 110 |
+
if __name__ == '__main__':
|
| 111 |
+
# For Windows development
|
| 112 |
+
if platform.system() == "Windows":
|
| 113 |
+
app.run(debug=True)
|
| 114 |
+
# For Linux or production with Waitress
|
| 115 |
+
else:
|
| 116 |
+
serve(app, host="0.0.0.0", port=7860)
|