File size: 17,955 Bytes
0bae2dd
 
 
 
 
 
 
 
 
017b3e2
0bae2dd
c105677
c9a0d37
 
c105677
 
0bae2dd
9a45dad
 
 
 
8b83b0a
0bae2dd
7dde793
 
 
 
 
0bae2dd
9a45dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bae2dd
 
 
 
 
 
 
9b278f5
0bae2dd
 
d41ca54
0bae2dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b278f5
 
0bae2dd
 
9b278f5
 
 
49341e5
9b278f5
 
 
 
 
 
 
 
 
49341e5
9b278f5
 
 
 
 
0bae2dd
 
9b278f5
 
0bae2dd
9b278f5
0bae2dd
 
 
 
 
9b278f5
0bae2dd
9b278f5
0bae2dd
 
 
 
 
 
 
 
 
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bae2dd
 
 
 
 
 
 
 
9b278f5
 
 
 
 
 
 
 
 
 
 
 
017b3e2
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017b3e2
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3da744
9b278f5
 
 
 
 
 
 
 
 
d3da744
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017b3e2
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017b3e2
9b278f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e340908
0bae2dd
 
 
 
 
 
 
 
 
 
9b278f5
0bae2dd
9b278f5
 
0bae2dd
9b278f5
0bae2dd
 
 
9b278f5
 
0bae2dd
9b278f5
0bae2dd
 
9b278f5
 
0bae2dd
9b278f5
0bae2dd
 
 
e340908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b278f5
9a45dad
8b83b0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a45dad
0922542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bae2dd
7dde793
017b3e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
64
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import os
import json
from flask import Flask, request, render_template, redirect, url_for, session, flash, send_from_directory, send_file
from werkzeug.utils import secure_filename
from utils.file_to_text import extract_text_based_on_format, preprocess_text
from utils.anoter_to_json import process_uploaded_json
from utils.json_to_spacy import convert_json_to_spacy
from utils.model import train_model
import zipfile
import shutil


app = Flask(__name__)
app.secret_key = 'your_secret_key'
os.umask(0o000)

# Folder paths
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['JSON_FOLDER'] = 'JSON/'
app.config['DATA_FOLDER'] = 'data/'
app.config['MODELS_FOLDER'] = 'Models/'
app.config['MAIN_FOLDER'] = os.getcwd() 

# Creating Folders if not exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['JSON_FOLDER'], exist_ok=True)
os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
os.makedirs(app.config['MODELS_FOLDER'], exist_ok=True)

# Verify and check once again if the folders are created or not
if not os.path.exists(app.config['UPLOAD_FOLDER']):
    os.makedirs(app.config['UPLOAD_FOLDER'])

if not os.path.exists(app.config['JSON_FOLDER']):
    os.makedirs(app.config['JSON_FOLDER'])

if not os.path.exists(app.config['DATA_FOLDER']):
    os.makedirs(app.config['DATA_FOLDER'])

if not os.path.exists(app.config['MODELS_FOLDER']):
    os.makedirs(app.config['MODELS_FOLDER'])

# Create the empty file
file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
with open(file_path, 'w') as file:
    pass

# Allowed file extensions
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg', 'json'}

# Function to check file extensions
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# HTML render routes (modify to fit your structure)
@app.route('/')
def index():
    return render_template('upload.html')
@app.route('/guide')
def guide():
    return render_template('guide.html')

@app.route('/ner_preview', methods=['GET'])
def ner_preview():
    return render_template('anoter.html')

@app.route('/json', methods=['GET'])
def json_file():
    return render_template('savejson.html')

@app.route('/spacy', methods=['GET'])
def spacy_file():
    return render_template('saveSpacy.html')

@app.route('/text_preview', methods=['GET'])
def text_preview():
    try:
        resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
        if not os.path.exists(resume_file_path):
            flash('Resume text not found', 'error')
            return redirect(url_for('index'))
        
        with open(resume_file_path, 'r') as f:
            text = f.read()
        return render_template('text.html', text=text)
    except Exception as e:
        flash(f"Error loading text preview: {str(e)}", 'error')
        return redirect(url_for('index'))

# API for uploading Resume files
@app.route('/upload',methods=['GET', 'POST'])
def upload_file():
    try:
        if 'file' not in request.files:
            flash('No file part', 'error')
            return redirect(request.url)

        file = request.files['file']
        if file.filename == '':
            flash('No selected file', 'error')
            return redirect(request.url)

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            file.save(file_path)

            # Handle text extraction for non-JSON files
            if not filename.lower().endswith('.json'):
                return process_other_files(file_path, filename)

        flash('File type not allowed', 'error')
    except Exception as e:
        flash(f"Error: {str(e)}", 'error')
    
    return redirect(request.url)

# Process non-JSON files, extract text and save to 'resume_text.txt'
def process_other_files(file_path, filename):
    try:
        extracted_text, _ = extract_text_based_on_format(file_path)
        cleaned_text = preprocess_text(extracted_text)

        os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
        resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')

        with open(resume_file_path, 'w', encoding='utf-8') as f:
            f.write(cleaned_text)

        session['uploaded_file'] = filename
        return render_template('text.html', text=cleaned_text)
    except Exception as e:
        flash(f"Error processing file {filename}: {str(e)}", 'error')
        return redirect(request.referrer)

# API to handle the text editing and saving
@app.route('/edit_text', methods=['POST'])
def edit_text():
    try:
        # Get the edited text from the form
        edited_text = request.form['edited_text']

        # Save the edited text back to 'resume_text.txt'
        resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
        with open(resume_file_path, 'w', encoding='utf-8') as f:
            f.write(edited_text)

        flash('Text edited successfully', 'success')
        # Pass the edited text back to the template
        return render_template('text.html', text=edited_text)
    except Exception as e:
        flash(f"Error saving edited text: {str(e)}", 'error')
        return redirect(request.referrer)
    
# API for downloading the 'resume_text.txt' file
@app.route('/download', methods=['GET'])
def download_file():
    try:
        return send_from_directory(app.config['DATA_FOLDER'], 'resume_text.txt', as_attachment=True)
    except Exception as e:
        flash(f"Error downloading file: {str(e)}", 'error')
        return redirect(request.referrer)

@app.route('/save_and_download', methods=['POST'])
def save_and_download():
    try:
        # Get the edited text from the form
        edited_text = request.form['edited_text']

        # Save the edited text back to 'resume_text.txt'
        resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt')
        with open(resume_file_path, 'w', encoding='utf-8') as f:
            f.write(edited_text)

        # flash('Text edited successfully', 'success')
        print(f"Saved instace: {app.config['DATA_FOLDER']}/resume_text.txt")
        # Now send the file as a download
        return send_from_directory(app.config['DATA_FOLDER'], 'resume_text.txt', as_attachment=True)

    except Exception as e:
        flash(f"Error saving and downloading file: {str(e)}", 'error')
        return redirect(request.referrer)


# API for uploading and processing JSON files
@app.route('/upload_json', methods=['POST'])
def upload_json_file():
    try:
        if 'file' not in request.files:
            flash('No file part', 'error')
            return redirect(request.url)

        file = request.files['file']
        if file.filename == '':
            flash('No selected file', 'error')
            return redirect(request.url)

        if file and file.filename.lower().endswith('.json'):
            filename = secure_filename(file.filename)
            json_path = os.path.join(app.config['JSON_FOLDER'], filename)
            os.makedirs(app.config['JSON_FOLDER'], exist_ok=True)
            file.save(json_path)
            session['uploaded_json'] = filename
            flash(f'JSON file {filename} uploaded successfully')
            print(f"Saved instace for JSon: {app.config['JSON_FOLDER']}/{filename}")
        else:
            flash('File type not allowed', 'error')
    except Exception as e:
        flash(f"Error: {str(e)}", 'error')

    return redirect(request.referrer)

# Process uploaded JSON file and save formatted data
@app.route('/process_json', methods=['GET'])
def process_json_file():
    try:
        json_folder = app.config['JSON_FOLDER']
        json_files = os.listdir(json_folder)

        if not json_files:
            flash('No JSON files found in the folder', 'error')
            return redirect(request.referrer)

        filename = json_files[0]  # Modify logic if needed to handle multiple files
        json_path = os.path.join(json_folder, filename)

        if not os.path.exists(json_path):
            flash(f'JSON file {filename} not found', 'error')
            return redirect(request.referrer)

        process_uploaded_json(json_path)
        os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
        processed_file_path = os.path.join(app.config['DATA_FOLDER'], f'Processed_{filename}')
        
        flash(f'JSON file {filename} processed successfully')
    except Exception as e:
        flash(f"Error processing JSON file: {str(e)}", 'error')

    return redirect(request.referrer)

# API for removing uploaded JSON files
@app.route('/remove_json', methods=['POST'])
def remove_all_json_files():
    try:
        json_folder = app.config['JSON_FOLDER']
        for filename in os.listdir(json_folder):
            file_path = os.path.join(json_folder, filename)
            if os.path.isfile(file_path):
                os.remove(file_path)
            session.pop('uploaded_json', None)

        flash('All JSON files removed successfully')
    except Exception as e:
        flash(f"Error removing files: {str(e)}", 'error')

    return redirect(request.referrer)

# API for removing non-JSON files
@app.route('/remove', methods=['POST'])
def remove_file():
    try:
        upload_folder = app.config['UPLOAD_FOLDER']
        
        # Check if the folder exists
        if os.path.exists(upload_folder):
            # Loop through all files in the upload folder and remove them
            for filename in os.listdir(upload_folder):
                file_path = os.path.join(upload_folder, filename)
                
                # Check if it is a file and remove it
                if os.path.isfile(file_path):
                    os.remove(file_path)
            
            # Clear session data related to uploaded files
            session.pop('uploaded_file', None)
            flash('All files removed successfully')
        else:
            flash(f"Upload folder does not exist", 'error')

    except Exception as e:
        flash(f"Error removing files: {str(e)}", 'error')

    return redirect(url_for('index'))


@app.route('/to_sapcy', methods=['POST'])
def to_sapcy():
    try:
        # Path to the JSON file
        json_file_path = 'data/Json_Data.json'
        # Convert the JSON file to a .spacy file
        spacy_file_path = 'data/Spacy_data.spacy'
        
        # Call the conversion function
        convert_json_to_spacy(json_file_path, spacy_file_path)
     
        flash('Model training data converted successfully', 'success')
    except Exception as e:
        flash(f"Error during conversion: {str(e)}", 'error')

    return redirect(request.referrer)

@app.route('/train_model_endpoint', methods=['POST'])
def train_model_endpoint():
    try:
        # Get the number of epochs and model version from the request
        epochs = int(request.form.get('epochs', 10))  # Default to 10 if not provided
        version = request.form.get('model_version', 'v1')  # Default to 'v1' if not provided
        
        # Call the training function with user-defined parameters
        model_path = f"./Models/ner_model_{version}"
        train_model(epochs, model_path)
        print(f"Saved instace for Model: app//Models/ner_model_{version}")
        flash('Model training completed successfully', 'success')
    except Exception as e:
        flash(f"Error during training: {str(e)}", 'error')

    return redirect(url_for('index'))

# API for removing all files from specific folders
@app.route('/remove_files', methods=['POST'])
def remove_files():
    try:
        # Define folders to clear
        folders_to_clear = [app.config['UPLOAD_FOLDER'], app.config['JSON_FOLDER'], app.config['MODELS_FOLDER'] ]

        for folder_path in folders_to_clear:
            # Remove all files from the specified folder
            for filename in os.listdir(folder_path):
                file_path = os.path.join(folder_path, filename)
                if os.path.isfile(file_path):
                    os.remove(file_path)

        # Clear session variables related to the removed folders
        session.pop('uploaded_file', None)
        session.pop('uploaded_json', None)

        flash('All files removed from folder successfully')
    except Exception as e:
        flash(f"Error removing files: {str(e)}", 'error')

    return redirect(url_for('index'))

# API for downloading the latest trained model
'''
@app.route('/download_model', methods=['GET'])
def download_latest_model():
    try:
        models_dir = app.config['MODELS_FOLDER']
        model_files = os.listdir(models_dir)

        if not model_files:
            flash('No model files found', 'error')
            return redirect(request.referrer)

        # Sort model files and get the latest one
        latest_model_file = sorted(model_files, reverse=True)[0]
        
        # Full path to the latest model file
        model_path = os.path.join(models_dir, latest_model_file)
        
        if not os.path.exists(model_path):
            flash('Model file not found on the server', 'error')
            return redirect(request.referrer)
        
        # Create a zip file with the model
        zip_filename = os.path.join(models_dir, f"{latest_model_file}.zip")
        
        with zipfile.ZipFile(zip_filename, 'w') as zipf:
            zipf.write(model_path, os.path.basename(model_path))
        
        # Send the zip file as a download
        return send_file(zip_filename, as_attachment=True)
    
    except Exception as e:
        flash(f"Error while downloading the model: {str(e)}", 'error')
        return redirect(request.referrer)
'''
@app.route('/download_model', methods=['GET'])
def download_latest_model():
    try:
        models_dir = app.config['MODELS_FOLDER']
        model_files = os.listdir(models_dir)

        if not model_files:
            flash('No model files found', 'error')
            return redirect(request.referrer)

        # Sort model files and get the latest one
        latest_model_file = sorted(model_files, reverse=True)[0]
        
        # Full path to the latest model file
        model_path = os.path.join(models_dir, latest_model_file)
        
        if not os.path.exists(model_path):
            flash('Model file not found on the server', 'error')
            return redirect(request.referrer)
        
        # Create a zip file with the model
        zip_filename = os.path.join(models_dir, f"{latest_model_file}.zip")
        
        # Create a zip file that includes the whole directory structure of the model
        with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
            # If the model is a directory, zip the whole directory
            if os.path.isdir(model_path):
                for foldername, subfolders, filenames in os.walk(model_path):
                    for filename in filenames:
                        file_path = os.path.join(foldername, filename)
                        # Write the file, preserving the directory structure
                        zipf.write(file_path, os.path.relpath(file_path, models_dir))
            else:
                # If it's just a single file, zip it
                zipf.write(model_path, os.path.basename(model_path))
        
        # Send the zip file as a download
        return send_file(zip_filename, as_attachment=True)

    except Exception as e:
        flash(f"Error while downloading the model: {str(e)}", 'error')
        return redirect(request.referrer)
        
# Route to serve uploaded files
@app.route('/<foldername>/zip', methods=['GET'])
def zip_folder(foldername):
    # Construct the folder path
    folder_path = os.path.join(app.config['MAIN_FOLDER'], foldername)

    # Check if the folder exists
    if not os.path.exists(folder_path) or not os.path.isdir(folder_path):
        abort(404, description="Folder not found")

    # Define the zip file name and path
    zip_filename = f"{foldername}.zip"
    zip_path = os.path.join(app.config['MAIN_FOLDER'], zip_filename)

    # Create a zip file of the folder
    shutil.make_archive(zip_path[:-4], 'zip', folder_path)

    # Send the zipped file to the client
    return send_from_directory(app.config['MAIN_FOLDER'], zip_filename, as_attachment=True)

@app.route('/upload_data_file', methods=['GET', 'POST'])
def upload_data_file():
    try:
        if request.method == 'POST':
            if 'file' not in request.files:
                flash('No file part', 'error')
                return redirect(request.url)

            file = request.files['file']
            if file.filename == '':
                flash('No selected file', 'error')
                return redirect(request.url)

            filename = secure_filename(file.filename)
            file_path = os.path.join(app.config['DATA_FOLDER'], filename)
            file.save(file_path)

            flash(f'File {filename} uploaded/overwritten successfully!', 'success')
        return redirect(url_for('show_data_files'))

    except Exception as e:
        flash(f"Error: {str(e)}", 'error')
        return redirect(request.referrer)
    
@app.route('/data_files', methods=['GET'])
def show_data_files():
    try:
        files = os.listdir(app.config['DATA_FOLDER'])
        return render_template('data_files.html', files=files)

    except Exception as e:
        flash(f"Error retrieving files: {str(e)}", 'error')
        return redirect(url_for('index'))

@app.route('/download/<filename>', methods=['GET'])
def download_files(filename):
    try:
        file_path = os.path.join(app.config['DATA_FOLDER'], filename)
        return send_file(file_path, as_attachment=True)

    except Exception as e:
        flash(f"Error downloading {filename}: {str(e)}", 'error')
        return redirect(url_for('show_data_files'))

if __name__ == '__main__':
    app.run(debug=True)