Spaces:
Sleeping
Sleeping
Update main.py
Browse files
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 |
-
#
|
| 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 |
-
@app.route('/
|
| 64 |
-
def
|
| 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 |
-
return
|
| 132 |
-
except Exception as e:
|
| 133 |
-
flash(f"Error
|
| 134 |
-
return redirect(request.referrer)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
#
|
| 150 |
-
return
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
if
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
except Exception as e:
|
| 208 |
-
flash(f"Error
|
| 209 |
-
|
| 210 |
-
return redirect(request.referrer)
|
| 211 |
-
|
| 212 |
-
#
|
| 213 |
-
@app.route('/
|
| 214 |
-
def
|
| 215 |
-
try:
|
| 216 |
-
json_folder = app.config['JSON_FOLDER']
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
#
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|