WebashalarForML commited on
Commit
9b278f5
·
verified ·
1 Parent(s): 4b2eaeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +236 -77
app.py CHANGED
@@ -50,10 +50,10 @@ ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg', 'json'}
50
  def allowed_file(filename):
51
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
52
 
 
53
  @app.route('/')
54
  def index():
55
  return render_template('upload.html')
56
-
57
  @app.route('/guide')
58
  def guide():
59
  return render_template('guide.html')
@@ -76,7 +76,6 @@ def text_preview():
76
  resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
77
  if not os.path.exists(resume_file_path):
78
  flash('Resume text not found', 'error')
79
- print('Resume text not found')
80
  return redirect(url_for('index'))
81
 
82
  with open(resume_file_path, 'r') as f:
@@ -84,84 +83,262 @@ def text_preview():
84
  return render_template('text.html', text=text)
85
  except Exception as e:
86
  flash(f"Error loading text preview: {str(e)}", 'error')
87
- print(f"Error loading text preview: {str(e)}")
88
  return redirect(url_for('index'))
89
 
90
- @app.route('/upload', methods=['GET', 'POST'])
 
91
  def upload_file():
92
  try:
93
- if request.method == 'POST':
94
- if 'file' not in request.files:
95
- flash('No file part', 'error')
96
- print('No file part to upload')
97
- return render_template('upload.html') # Avoid redirect loop
98
-
99
- file = request.files['file']
100
- if file.filename == '':
101
- flash('No selected file', 'error')
102
- print('No selected file to upload')
103
- return render_template('upload.html') # Avoid redirect loop
104
-
105
- if file and allowed_file(file.filename):
106
- filename = secure_filename(file.filename)
107
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
108
- if file_path:
109
- print("Folder got it", file_path)
110
- else:
111
- print("Folder not got it......................")
112
- try:
113
- file.save(file_path)
114
- print('File uploaded successfully', 'success')
115
- except Exception as e:
116
- print(f"Error saving file: {str(e)}", 'error')
117
- flash(f"Error saving file: {str(e)}", 'error')
118
- return render_template('upload.html')
119
-
120
- # Handle non-JSON files
121
- if not filename.lower().endswith('.json'):
122
- return process_other_files(file_path, filename)
123
- else:
124
- flash('JSON file uploaded successfully', 'success')
125
- print('JSON file uploaded successfully')
126
- return render_template('upload.html')
127
 
128
- flash('File type not allowed', 'error')
129
- print('File type not allowed')
130
- return render_template('upload.html')
 
 
 
 
 
 
131
 
132
- return render_template('upload.html')
 
 
 
 
133
  except Exception as e:
134
- print(f"Error----------: {str(e)}", 'error')
135
  flash(f"Error: {str(e)}", 'error')
136
- return render_template('upload.html')
 
137
 
 
138
  def process_other_files(file_path, filename):
139
  try:
140
  extracted_text, _ = extract_text_based_on_format(file_path)
141
  cleaned_text = preprocess_text(extracted_text)
142
 
 
143
  resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
144
- print("text file path",resume_file_path)
145
  with open(resume_file_path, 'w', encoding='utf-8') as f:
146
  f.write(cleaned_text)
147
 
148
  session['uploaded_file'] = filename
149
- print("save in txt file")
150
  return render_template('text.html', text=cleaned_text)
151
  except Exception as e:
152
  flash(f"Error processing file {filename}: {str(e)}", 'error')
153
- print(f"Error processing file {filename}: {str(e)}")
154
  return redirect(request.referrer)
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  @app.route('/download', methods=['GET'])
157
  def download_file():
158
  try:
159
  return send_from_directory(app.config['DATA_FOLDER'], 'resume_text.txt', as_attachment=True)
160
  except Exception as e:
161
  flash(f"Error downloading file: {str(e)}", 'error')
162
- print(f"Error downloading file: {str(e)}")
163
  return redirect(request.referrer)
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  @app.route('/download_model', methods=['GET'])
166
  def download_latest_model():
167
  try:
@@ -170,50 +347,32 @@ def download_latest_model():
170
 
171
  if not model_files:
172
  flash('No model files found', 'error')
173
- print('No model files found')
174
  return redirect(request.referrer)
175
 
 
176
  latest_model_file = sorted(model_files, reverse=True)[0]
 
 
177
  model_path = os.path.join(models_dir, latest_model_file)
178
-
179
  if not os.path.exists(model_path):
180
  flash('Model file not found on the server', 'error')
181
- print('Model file not found on the server')
182
  return redirect(request.referrer)
183
-
 
184
  zip_filename = os.path.join(models_dir, f"{latest_model_file}.zip")
185
-
186
  with zipfile.ZipFile(zip_filename, 'w') as zipf:
187
  zipf.write(model_path, os.path.basename(model_path))
188
-
 
189
  return send_file(zip_filename, as_attachment=True)
 
190
  except Exception as e:
191
  flash(f"Error while downloading the model: {str(e)}", 'error')
192
- print(f"Error while downloading the model: {str(e)}")
193
  return redirect(request.referrer)
194
 
195
- @app.route('/remove_file', methods=['POST'])
196
- def remove_file():
197
- try:
198
- file_to_remove = session.get('uploaded_file', None)
199
- if file_to_remove:
200
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], file_to_remove)
201
- if os.path.exists(file_path):
202
- os.remove(file_path)
203
- flash(f"{file_to_remove} removed successfully.", "success")
204
- print(f"{file_to_remove} removed successfully.")
205
- session.pop('uploaded_file', None)
206
- else:
207
- flash("File not found.", "error")
208
- print("File not found.")
209
- else:
210
- flash("No file selected for removal.", "error")
211
- print("No file selected for removal.")
212
- except Exception as e:
213
- flash(f"Error while removing file: {str(e)}", "error")
214
- print(f"Error while removing file: {str(e)}", "error")
215
- return redirect(url_for('index'))
216
-
217
  # Route to serve uploaded files
218
  '''
219
  @app.route('/uploads/<filename>')
 
50
  def allowed_file(filename):
51
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
52
 
53
+ # HTML render routes (modify to fit your structure)
54
  @app.route('/')
55
  def index():
56
  return render_template('upload.html')
 
57
  @app.route('/guide')
58
  def guide():
59
  return render_template('guide.html')
 
76
  resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
77
  if not os.path.exists(resume_file_path):
78
  flash('Resume text not found', 'error')
 
79
  return redirect(url_for('index'))
80
 
81
  with open(resume_file_path, 'r') as f:
 
83
  return render_template('text.html', text=text)
84
  except Exception as e:
85
  flash(f"Error loading text preview: {str(e)}", 'error')
 
86
  return redirect(url_for('index'))
87
 
88
+ # API for uploading Resume files
89
+ @app.route('/upload',methods=['GET', 'POST'])
90
  def upload_file():
91
  try:
92
+ if 'file' not in request.files:
93
+ flash('No file part', 'error')
94
+ return redirect(request.url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ file = request.files['file']
97
+ if file.filename == '':
98
+ flash('No selected file', 'error')
99
+ return redirect(request.url)
100
+
101
+ if file and allowed_file(file.filename):
102
+ filename = secure_filename(file.filename)
103
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
104
+ file.save(file_path)
105
 
106
+ # Handle text extraction for non-JSON files
107
+ if not filename.lower().endswith('.json'):
108
+ return process_other_files(file_path, filename)
109
+
110
+ flash('File type not allowed', 'error')
111
  except Exception as e:
 
112
  flash(f"Error: {str(e)}", 'error')
113
+
114
+ return redirect(request.url)
115
 
116
+ # Process non-JSON files, extract text and save to 'resume_text.txt'
117
  def process_other_files(file_path, filename):
118
  try:
119
  extracted_text, _ = extract_text_based_on_format(file_path)
120
  cleaned_text = preprocess_text(extracted_text)
121
 
122
+ os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
123
  resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
124
+
125
  with open(resume_file_path, 'w', encoding='utf-8') as f:
126
  f.write(cleaned_text)
127
 
128
  session['uploaded_file'] = filename
 
129
  return render_template('text.html', text=cleaned_text)
130
  except Exception as e:
131
  flash(f"Error processing file {filename}: {str(e)}", 'error')
 
132
  return redirect(request.referrer)
133
 
134
+ # API to handle the text editing and saving
135
+ @app.route('/edit_text', methods=['POST'])
136
+ def edit_text():
137
+ try:
138
+ # Get the edited text from the form
139
+ edited_text = request.form['edited_text']
140
+
141
+ # Save the edited text back to 'resume_text.txt'
142
+ resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
143
+ with open(resume_file_path, 'w', encoding='utf-8') as f:
144
+ f.write(edited_text)
145
+
146
+ flash('Text edited successfully', 'success')
147
+ # Pass the edited text back to the template
148
+ return render_template('text.html', text=edited_text)
149
+ except Exception as e:
150
+ flash(f"Error saving edited text: {str(e)}", 'error')
151
+ return redirect(request.referrer)
152
+
153
+ # API for downloading the 'resume_text.txt' file
154
  @app.route('/download', methods=['GET'])
155
  def download_file():
156
  try:
157
  return send_from_directory(app.config['DATA_FOLDER'], 'resume_text.txt', as_attachment=True)
158
  except Exception as e:
159
  flash(f"Error downloading file: {str(e)}", 'error')
 
160
  return redirect(request.referrer)
161
 
162
+ @app.route('/save_and_download', methods=['POST'])
163
+ def save_and_download():
164
+ try:
165
+ # Get the edited text from the form
166
+ edited_text = request.form['edited_text']
167
+
168
+ # Save the edited text back to 'resume_text.txt'
169
+ resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
170
+ with open(resume_file_path, 'w', encoding='utf-8') as f:
171
+ f.write(edited_text)
172
+
173
+ # flash('Text edited successfully', 'success')
174
+
175
+ # Now send the file as a download
176
+ return send_from_directory(app.config['DATA_FOLDER'], 'resume_text.txt', as_attachment=True)
177
+
178
+ except Exception as e:
179
+ flash(f"Error saving and downloading file: {str(e)}", 'error')
180
+ return redirect(request.referrer)
181
+
182
+
183
+ # API for uploading and processing JSON files
184
+ @app.route('/upload_json', methods=['POST'])
185
+ def upload_json_file():
186
+ try:
187
+ if 'file' not in request.files:
188
+ flash('No file part', 'error')
189
+ return redirect(request.url)
190
+
191
+ file = request.files['file']
192
+ if file.filename == '':
193
+ flash('No selected file', 'error')
194
+ return redirect(request.url)
195
+
196
+ if file and file.filename.lower().endswith('.json'):
197
+ filename = secure_filename(file.filename)
198
+ json_path = os.path.join(app.config['JSON_FOLDER'], filename)
199
+ os.makedirs(app.config['JSON_FOLDER'], exist_ok=True)
200
+ file.save(json_path)
201
+ session['uploaded_json'] = filename
202
+ flash(f'JSON file {filename} uploaded successfully')
203
+ else:
204
+ flash('File type not allowed', 'error')
205
+ except Exception as e:
206
+ flash(f"Error: {str(e)}", 'error')
207
+
208
+ return redirect(request.referrer)
209
+
210
+ # Process uploaded JSON file and save formatted data
211
+ @app.route('/process_json', methods=['GET'])
212
+ def process_json_file():
213
+ try:
214
+ json_folder = app.config['JSON_FOLDER']
215
+ json_files = os.listdir(json_folder)
216
+
217
+ if not json_files:
218
+ flash('No JSON files found in the folder', 'error')
219
+ return redirect(request.referrer)
220
+
221
+ filename = json_files[0] # Modify logic if needed to handle multiple files
222
+ json_path = os.path.join(json_folder, filename)
223
+
224
+ if not os.path.exists(json_path):
225
+ flash(f'JSON file {filename} not found', 'error')
226
+ return redirect(request.referrer)
227
+
228
+ process_uploaded_json(json_path)
229
+ os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
230
+ processed_file_path = os.path.join(app.config['DATA_FOLDER'], f'Processed_{filename}')
231
+
232
+ flash(f'JSON file {filename} processed successfully')
233
+ except Exception as e:
234
+ flash(f"Error processing JSON file: {str(e)}", 'error')
235
+
236
+ return redirect(request.referrer)
237
+
238
+ # API for removing uploaded JSON files
239
+ @app.route('/remove_json', methods=['POST'])
240
+ def remove_all_json_files():
241
+ try:
242
+ json_folder = app.config['JSON_FOLDER']
243
+ for filename in os.listdir(json_folder):
244
+ file_path = os.path.join(json_folder, filename)
245
+ if os.path.isfile(file_path):
246
+ os.remove(file_path)
247
+ session.pop('uploaded_json', None)
248
+
249
+ flash('All JSON files removed successfully')
250
+ except Exception as e:
251
+ flash(f"Error removing files: {str(e)}", 'error')
252
+
253
+ return redirect(request.referrer)
254
+
255
+ # API for removing non-JSON files
256
+ @app.route('/remove', methods=['POST'])
257
+ def remove_file():
258
+ try:
259
+ upload_folder = app.config['UPLOAD_FOLDER']
260
+
261
+ # Check if the folder exists
262
+ if os.path.exists(upload_folder):
263
+ # Loop through all files in the upload folder and remove them
264
+ for filename in os.listdir(upload_folder):
265
+ file_path = os.path.join(upload_folder, filename)
266
+
267
+ # Check if it is a file and remove it
268
+ if os.path.isfile(file_path):
269
+ os.remove(file_path)
270
+
271
+ # Clear session data related to uploaded files
272
+ session.pop('uploaded_file', None)
273
+ flash('All files removed successfully')
274
+ else:
275
+ flash(f"Upload folder does not exist", 'error')
276
+
277
+ except Exception as e:
278
+ flash(f"Error removing files: {str(e)}", 'error')
279
+
280
+ return redirect(url_for('index'))
281
+
282
+
283
+ @app.route('/to_sapcy', methods=['POST'])
284
+ def to_sapcy():
285
+ try:
286
+ # Path to the JSON file
287
+ json_file_path = 'data/Json_Data.json'
288
+ # Convert the JSON file to a .spacy file
289
+ spacy_file_path = 'data/Spacy_data.spacy'
290
+
291
+ # Call the conversion function
292
+ convert_json_to_spacy(json_file_path, spacy_file_path)
293
+
294
+ flash('Model training data converted successfully', 'success')
295
+ except Exception as e:
296
+ flash(f"Error during conversion: {str(e)}", 'error')
297
+
298
+ return redirect(request.referrer)
299
+
300
+ @app.route('/train_model_endpoint', methods=['POST'])
301
+ def train_model_endpoint():
302
+ try:
303
+ # Get the number of epochs and model version from the request
304
+ epochs = int(request.form.get('epochs', 10)) # Default to 10 if not provided
305
+ version = request.form.get('model_version', 'v1') # Default to 'v1' if not provided
306
+
307
+ # Call the training function with user-defined parameters
308
+ model_path = f"./Models/ner_model_{version}"
309
+ train_model(epochs, model_path)
310
+
311
+ flash('Model training completed successfully', 'success')
312
+ except Exception as e:
313
+ flash(f"Error during training: {str(e)}", 'error')
314
+
315
+ return redirect(url_for('index'))
316
+
317
+ # API for removing all files from specific folders
318
+ @app.route('/remove_files', methods=['POST'])
319
+ def remove_files():
320
+ try:
321
+ # Define folders to clear
322
+ folders_to_clear = [app.config['UPLOAD_FOLDER'], app.config['JSON_FOLDER'], app.config['MODELS_FOLDER'] ]
323
+
324
+ for folder_path in folders_to_clear:
325
+ # Remove all files from the specified folder
326
+ for filename in os.listdir(folder_path):
327
+ file_path = os.path.join(folder_path, filename)
328
+ if os.path.isfile(file_path):
329
+ os.remove(file_path)
330
+
331
+ # Clear session variables related to the removed folders
332
+ session.pop('uploaded_file', None)
333
+ session.pop('uploaded_json', None)
334
+
335
+ flash('All files removed from folder successfully')
336
+ except Exception as e:
337
+ flash(f"Error removing files: {str(e)}", 'error')
338
+
339
+ return redirect(url_for('index'))
340
+
341
+ # API for downloading the latest trained model
342
  @app.route('/download_model', methods=['GET'])
343
  def download_latest_model():
344
  try:
 
347
 
348
  if not model_files:
349
  flash('No model files found', 'error')
 
350
  return redirect(request.referrer)
351
 
352
+ # Sort model files and get the latest one
353
  latest_model_file = sorted(model_files, reverse=True)[0]
354
+
355
+ # Full path to the latest model file
356
  model_path = os.path.join(models_dir, latest_model_file)
357
+
358
  if not os.path.exists(model_path):
359
  flash('Model file not found on the server', 'error')
 
360
  return redirect(request.referrer)
361
+
362
+ # Create a zip file with the model
363
  zip_filename = os.path.join(models_dir, f"{latest_model_file}.zip")
364
+
365
  with zipfile.ZipFile(zip_filename, 'w') as zipf:
366
  zipf.write(model_path, os.path.basename(model_path))
367
+
368
+ # Send the zip file as a download
369
  return send_file(zip_filename, as_attachment=True)
370
+
371
  except Exception as e:
372
  flash(f"Error while downloading the model: {str(e)}", 'error')
 
373
  return redirect(request.referrer)
374
 
375
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  # Route to serve uploaded files
377
  '''
378
  @app.route('/uploads/<filename>')