Spaces:
Sleeping
Sleeping
WebashalarForML
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -88,32 +88,50 @@ def text_preview():
|
|
88 |
return redirect(url_for('index'))
|
89 |
|
90 |
# API for uploading Resume files
|
91 |
-
@app.route('/upload',methods=['GET', 'POST'])
|
92 |
def upload_file():
|
93 |
try:
|
94 |
-
if
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
return process_other_files(file_path, filename)
|
111 |
|
112 |
-
flash('File type not allowed', 'error')
|
113 |
except Exception as e:
|
|
|
114 |
flash(f"Error: {str(e)}", 'error')
|
115 |
-
|
116 |
-
|
117 |
|
118 |
# Process non-JSON files, extract text and save to 'resume_text.txt'
|
119 |
def process_other_files(file_path, filename):
|
|
|
88 |
return redirect(url_for('index'))
|
89 |
|
90 |
# API for uploading Resume files
|
91 |
+
@app.route('/upload', methods=['GET', 'POST'])
|
92 |
def upload_file():
|
93 |
try:
|
94 |
+
# Check if the request is a POST method, meaning a file is being uploaded
|
95 |
+
if request.method == 'POST':
|
96 |
+
# Check if the 'file' part is in the request (no file selected case)
|
97 |
+
if 'file' not in request.files:
|
98 |
+
# Flash an error message and render the same page without redirecting (prevents redirect loop)
|
99 |
+
flash('No file part', 'error')
|
100 |
+
return render_template('upload.html') # Render the upload page directly
|
101 |
+
|
102 |
+
file = request.files['file']
|
103 |
+
# Check if a file was selected by the user
|
104 |
+
if file.filename == '':
|
105 |
+
# Flash an error message and render the same page without redirecting (prevents redirect loop)
|
106 |
+
flash('No selected file', 'error')
|
107 |
+
return render_template('upload.html') # Render the upload page directly
|
108 |
+
|
109 |
+
# Check if the uploaded file has an allowed extension
|
110 |
+
if file and allowed_file(file.filename):
|
111 |
+
filename = secure_filename(file.filename)
|
112 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
113 |
+
file.save(file_path)
|
114 |
+
|
115 |
+
# Handle non-JSON files and process accordingly
|
116 |
+
if not filename.lower().endswith('.json'):
|
117 |
+
return process_other_files(file_path, filename) # Return after processing non-JSON files
|
118 |
+
else:
|
119 |
+
# Flash success message for JSON files and render the upload page
|
120 |
+
flash('JSON file uploaded successfully', 'success')
|
121 |
+
return render_template('upload.html') # Return the same page with success
|
122 |
+
|
123 |
+
# Flash an error if the file type is not allowed
|
124 |
+
flash('File type not allowed', 'error')
|
125 |
+
return render_template('upload.html') # Return the same page with the error message
|
126 |
|
127 |
+
# Render the upload page for GET requests (when the page is loaded initially)
|
128 |
+
return render_template('upload.html')
|
|
|
129 |
|
|
|
130 |
except Exception as e:
|
131 |
+
# Handle any exceptions that occur, flash the error message, and render the same page
|
132 |
flash(f"Error: {str(e)}", 'error')
|
133 |
+
return render_template('upload.html') # Return the same page with the error
|
134 |
+
|
135 |
|
136 |
# Process non-JSON files, extract text and save to 'resume_text.txt'
|
137 |
def process_other_files(file_path, filename):
|