|
from flask import Flask, request, redirect, flash, session, render_template, url_for |
|
import os |
|
import json |
|
import logging |
|
from werkzeug.utils import secure_filename |
|
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error |
|
from utils.spacy import Parser_from_model |
|
from utils.mistral import process_resume_data |
|
import platform |
|
from waitress import serve |
|
|
|
|
|
app = Flask(__name__) |
|
app.secret_key = 'your_secret_key' |
|
|
|
app.config['UPLOAD_FOLDER'] = 'uploads/' |
|
UPLOAD_FOLDER = 'uploads/' |
|
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
|
|
|
if not os.path.exists(app.config['UPLOAD_FOLDER']): |
|
os.makedirs(app.config['UPLOAD_FOLDER']) |
|
|
|
|
|
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'} |
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
|
|
app.register_error_handler(404, page_not_found) |
|
app.register_error_handler(500, internal_server_error) |
|
|
|
def allowed_file(filename): |
|
"""Check if the file has an allowed extension.""" |
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS |
|
|
|
@app.route('/') |
|
def index(): |
|
"""Display the index page with the uploaded file information.""" |
|
uploaded_file = session.get('uploaded_file', None) |
|
return render_template('index.html', uploaded_file=uploaded_file) |
|
|
|
@app.route('/upload_and_process', methods=['POST']) |
|
def upload_and_process(): |
|
"""Handle file upload and process the file.""" |
|
if 'file' not in request.files or request.files['file'].filename == '': |
|
flash('No file selected for upload.') |
|
return redirect(request.url) |
|
|
|
file = request.files['file'] |
|
|
|
|
|
if file and allowed_file(file.filename): |
|
filename = secure_filename(file.filename) |
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) |
|
file.save(file_path) |
|
print(f"file path --->{file_path}") |
|
logging.debug(f"File uploaded: {filename}") |
|
session['uploaded_file'] = filename |
|
|
|
file_url = f"/uploads/{filename}" |
|
file_extension = filename.rsplit('.', 1)[1].lower() |
|
print(f"File URL: {file_url}, File Extension: {file_extension}") |
|
session['file_url'] = file_url |
|
session['file_extension'] = file_extension |
|
|
|
|
|
try: |
|
parsed_data = process_resume_data(file_path) |
|
if not parsed_data or 'error' in parsed_data: |
|
flash('An error occurred during file processing.') |
|
return redirect(url_for('index')) |
|
|
|
session['processed_data'] = parsed_data |
|
flash('File uploaded and data processed successfully.') |
|
return redirect(url_for('result')) |
|
|
|
except Exception as e: |
|
logging.error(f"File processing error: {str(e)}") |
|
flash('An error occurred while processing the file.') |
|
return handle_file_processing_error() |
|
else: |
|
return handle_invalid_file_type() |
|
|
|
@app.route('/remove_file', methods=['POST']) |
|
def remove_file(): |
|
"""Remove the uploaded file and reset the session.""" |
|
uploaded_file = session.get('uploaded_file') |
|
if uploaded_file: |
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file) |
|
if os.path.exists(file_path): |
|
os.remove(file_path) |
|
session.pop('uploaded_file', None) |
|
flash('File successfully removed.') |
|
else: |
|
flash('No file to remove.') |
|
return redirect(url_for('index')) |
|
|
|
@app.route('/reset_upload') |
|
def reset_upload(): |
|
"""Reset the uploaded file and the processed data.""" |
|
uploaded_file = session.get('uploaded_file') |
|
if uploaded_file: |
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file) |
|
if os.path.exists(file_path): |
|
os.remove(file_path) |
|
session.pop('uploaded_file', None) |
|
|
|
session.pop('processed_data', None) |
|
flash('File and data reset. You can upload a new file.') |
|
return redirect(url_for('index')) |
|
|
|
@app.route('/result') |
|
def result(): |
|
"""Display the processed data result.""" |
|
processed_data = session.get('processed_data', None) |
|
file_url = session.get('file_url', None) |
|
file_extension = session.get('file_extension', None) |
|
uploaded_file = session.get('uploaded_file', None) |
|
if not processed_data: |
|
flash('No data to display. Please upload and process a file.') |
|
return redirect(url_for('index')) |
|
return render_template('result.html', parsed_data=processed_data, file_url=file_url, file_extension=file_extension, uploaded_file=uploaded_file) |
|
|
|
if __name__ == '__main__': |
|
|
|
if platform.system() == "Windows": |
|
app.run(debug=True) |
|
|
|
else: |
|
serve(app, host="0.0.0.0", port=7860) |
|
|