File size: 4,333 Bytes
be28faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
116
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

# Initialize the Flask application
app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads'

# Allowed file extensions
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}

# Configure logging
logging.basicConfig(level=logging.DEBUG)

# Error handlers
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']
    
    # Check if the file is allowed
    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)
        logging.debug(f"File uploaded: {filename}")
        session['uploaded_file'] = filename

        # Process the file after uploading
        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)
    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)

if __name__ == '__main__':
    # For Windows development
    if platform.system() == "Windows":
        app.run(debug=True)
    # For Linux or production with Waitress
    else:
        serve(app, host="0.0.0.0", port=7860)