WebashalarForML commited on
Commit
ccf6f64
·
verified ·
1 Parent(s): c1e0ac3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -115
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
- """Display the index page with the uploaded file information."""
34
- uploaded_file = session.get('uploaded_file', None)
35
- return render_template('index.html', uploaded_file=uploaded_file)
36
-
37
- @app.route('/upload_and_process', methods=['POST'])
38
- def upload_and_process():
39
- """Handle file upload and process the file."""
40
- if 'file' not in request.files or request.files['file'].filename == '':
41
- flash('No file selected for upload.')
42
- return redirect(request.url)
43
-
44
- file = request.files['file']
45
-
46
- # Check if the file is allowed
47
- if file and allowed_file(file.filename):
48
- filename = secure_filename(file.filename)
49
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
50
- file.save(file_path)
51
- logging.debug(f"File uploaded: {filename}")
52
- session['uploaded_file'] = filename
53
-
54
- # Process the file after uploading
55
- try:
56
- parsed_data = process_resume_data(file_path)
57
- if not parsed_data or 'error' in parsed_data:
58
- flash('An error occurred during file processing.')
59
- return redirect(url_for('index'))
60
-
61
- session['processed_data'] = parsed_data
62
- flash('File uploaded and data processed successfully.')
63
- return redirect(url_for('result'))
64
-
65
- except Exception as e:
66
- logging.error(f"File processing error: {str(e)}")
67
- flash('An error occurred while processing the file.')
68
- return handle_file_processing_error()
69
- else:
70
- return handle_invalid_file_type()
71
-
72
- @app.route('/remove_file', methods=['POST'])
73
- def remove_file():
74
- """Remove the uploaded file and reset the session."""
75
- uploaded_file = session.get('uploaded_file')
76
- if uploaded_file:
77
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
78
- if os.path.exists(file_path):
79
- os.remove(file_path)
80
- session.pop('uploaded_file', None)
81
- flash('File successfully removed.')
82
- else:
83
- flash('No file to remove.')
84
- return redirect(url_for('index'))
85
-
86
- @app.route('/reset_upload')
87
- def reset_upload():
88
- """Reset the uploaded file and the processed data."""
89
- uploaded_file = session.get('uploaded_file')
90
- if uploaded_file:
91
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
92
- if os.path.exists(file_path):
93
- os.remove(file_path)
94
- session.pop('uploaded_file', None)
95
-
96
- session.pop('processed_data', None)
97
- flash('File and data reset. You can upload a new file.')
98
- return redirect(url_for('index'))
99
-
100
- @app.route('/result')
101
- def result():
102
- """Display the processed data result."""
103
- processed_data = session.get('processed_data', None)
104
- if not processed_data:
105
- flash('No data to display. Please upload and process a file.')
106
- return redirect(url_for('index'))
107
- return render_template('result.html', parsed_data=processed_data)
108
-
109
- if __name__ == '__main__':
110
- # For Windows development
111
- if platform.system() == "Windows":
112
- app.run(debug=True)
113
- # For Linux or production with Waitress
114
- else:
115
- serve(app, host="0.0.0.0", port=7860)
 
 
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)