Spaces:
Sleeping
Sleeping
| 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 | |
| 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/' | |
| # 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 | |
| def index(): | |
| return render_template('upload.html') | |
| def guide(): | |
| return render_template('guide.html') | |
| def ner_preview(): | |
| return render_template('anoter.html') | |
| def json_file(): | |
| return render_template('savejson.html') | |
| def spacy_file(): | |
| return render_template('saveSpacy.html') | |
| 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') | |
| print('Resume text not found') | |
| 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') | |
| print(f"Error loading text preview: {str(e)}") | |
| return redirect(url_for('index')) | |
| def upload_file(): | |
| try: | |
| if request.method == 'POST': | |
| if 'file' not in request.files: | |
| flash('No file part', 'error') | |
| print('No file part to upload') | |
| return render_template('upload.html') # Avoid redirect loop | |
| file = request.files['file'] | |
| if file.filename == '': | |
| flash('No selected file', 'error') | |
| print('No selected file to upload') | |
| return render_template('upload.html') # Avoid redirect loop | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
| if file_path: | |
| print("Folder got it", file_path) | |
| else: | |
| print("Folder not got it......................") | |
| try: | |
| file.save(file_path) | |
| print('File uploaded successfully', 'success') | |
| except Exception as e: | |
| print(f"Error saving file: {str(e)}", 'error') | |
| flash(f"Error saving file: {str(e)}", 'error') | |
| return render_template('upload.html') | |
| # Handle non-JSON files | |
| if not filename.lower().endswith('.json'): | |
| return process_other_files(file_path, filename) | |
| else: | |
| flash('JSON file uploaded successfully', 'success') | |
| print('JSON file uploaded successfully') | |
| return render_template('upload.html') | |
| flash('File type not allowed', 'error') | |
| print('File type not allowed') | |
| return render_template('upload.html') | |
| return render_template('upload.html') | |
| except Exception as e: | |
| print(f"Error----------: {str(e)}", 'error') | |
| flash(f"Error: {str(e)}", 'error') | |
| return render_template('upload.html') | |
| def process_other_files(file_path, filename): | |
| try: | |
| extracted_text, _ = extract_text_based_on_format(file_path) | |
| cleaned_text = preprocess_text(extracted_text) | |
| resume_file_path = os.path.join(app.config['DATA_FOLDER'], 'resume_text.txt') | |
| print("text file path",resume_file_path) | |
| with open(resume_file_path, 'w', encoding='utf-8') as f: | |
| f.write(cleaned_text) | |
| session['uploaded_file'] = filename | |
| print("save in txt file") | |
| return render_template('text.html', text=cleaned_text) | |
| except Exception as e: | |
| flash(f"Error processing file {filename}: {str(e)}", 'error') | |
| print(f"Error processing file {filename}: {str(e)}") | |
| return redirect(request.referrer) | |
| 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') | |
| print(f"Error downloading file: {str(e)}") | |
| return redirect(request.referrer) | |
| 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') | |
| print('No model files found') | |
| return redirect(request.referrer) | |
| latest_model_file = sorted(model_files, reverse=True)[0] | |
| 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') | |
| print('Model file not found on the server') | |
| return redirect(request.referrer) | |
| 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)) | |
| return send_file(zip_filename, as_attachment=True) | |
| except Exception as e: | |
| flash(f"Error while downloading the model: {str(e)}", 'error') | |
| print(f"Error while downloading the model: {str(e)}") | |
| return redirect(request.referrer) | |
| def remove_file(): | |
| try: | |
| file_to_remove = session.get('uploaded_file', None) | |
| if file_to_remove: | |
| file_path = os.path.join(app.config['UPLOAD_FOLDER'], file_to_remove) | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| flash(f"{file_to_remove} removed successfully.", "success") | |
| print(f"{file_to_remove} removed successfully.") | |
| session.pop('uploaded_file', None) | |
| else: | |
| flash("File not found.", "error") | |
| print("File not found.") | |
| else: | |
| flash("No file selected for removal.", "error") | |
| print("No file selected for removal.") | |
| except Exception as e: | |
| flash(f"Error while removing file: {str(e)}", "error") | |
| print(f"Error while removing file: {str(e)}", "error") | |
| return redirect(url_for('upload_file')) | |
| # Route to serve uploaded files | |
| ''' | |
| @app.route('/uploads/<filename>') | |
| def uploaded_file(filename): | |
| return send_from_directory(app.config['UPLOAD_FOLDER'], filename) | |
| ''' | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |