File size: 5,268 Bytes
deaecdf
ccf6f64
 
 
 
 
 
 
 
 
 
 
 
 
82f7a53
a321f36
712a0f2
82f7a53
 
 
 
 
ccf6f64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a95151f
ccf6f64
 
 
1c08ab9
a321f36
1c08ab9
a321f36
 
 
ccf6f64
 
 
 
 
 
 
ede5128
ccf6f64
ede5128
ccf6f64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a321f36
 
1cdd9f1
ede5128
ccf6f64
 
 
ede5128
ccf6f64
deaecdf
 
 
 
 
ccf6f64
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask, request, redirect, flash, session, render_template, url_for, send_from_directory
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/'
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 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)
        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

        # 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'))

            print("file path of files---->",file_path)
            session['processed_data'] = parsed_data
            session['file_path'] = file_path
            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)
    file_path = session.get('file_path', 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, file_path=file_path)

# Route to serve uploaded files
@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

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)