WebashalarForML commited on
Commit
0bae2dd
·
verified ·
1 Parent(s): 13477ad

Update main.py

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