Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,32 +55,47 @@ def index():
|
|
| 55 |
|
| 56 |
@app.route('/upload', methods=['POST'])
|
| 57 |
def upload_file():
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
flash('
|
| 66 |
-
|
| 67 |
-
return redirect(request.url)
|
| 68 |
|
| 69 |
-
uploaded_files = session.get('uploaded_files', [])
|
| 70 |
-
for file in files:
|
| 71 |
-
if file:
|
| 72 |
-
filename = file.filename
|
| 73 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 74 |
-
file.save(file_path)
|
| 75 |
-
uploaded_files.append(filename)
|
| 76 |
-
logging.info(f"Uploaded file: {filename} at {file_path}")
|
| 77 |
-
|
| 78 |
-
session['uploaded_files'] = uploaded_files
|
| 79 |
-
flash('Files successfully uploaded')
|
| 80 |
-
logging.info(f"Files successfully uploaded: {uploaded_files}")
|
| 81 |
-
return jsonify({'filename':filename,
|
| 82 |
-
'filepath':file_path
|
| 83 |
-
})
|
| 84 |
|
| 85 |
@app.route('/remove_file')
|
| 86 |
def remove_file():
|
|
@@ -102,62 +117,71 @@ from flask import jsonify
|
|
| 102 |
|
| 103 |
@app.route('/process/<filename>', methods=['GET', 'POST'])
|
| 104 |
def process_file(filename):
|
| 105 |
-
uploaded_files = session.get('uploaded_files', [])
|
| 106 |
-
if not uploaded_files:
|
| 107 |
-
flash('No files selected for processing')
|
| 108 |
-
logging.warning("No files selected for processing")
|
| 109 |
-
return redirect(url_for('index'))
|
| 110 |
-
|
| 111 |
-
BASE_DIR = 'uploads/'
|
| 112 |
-
|
| 113 |
-
# Joining the base and the requested path
|
| 114 |
-
#file_paths = os.path.join(BASE_DIR, filename)
|
| 115 |
-
|
| 116 |
-
file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
|
| 117 |
-
logging.info(f"Processing files: {file_paths}")
|
| 118 |
-
|
| 119 |
-
extracted_text = {}
|
| 120 |
-
processed_Img = {}
|
| 121 |
-
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
-
logging.error(f"
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
LLMdata = {}
|
| 138 |
-
extracted_text, processed_Img = extract_text_from_images(file_paths)
|
| 139 |
-
logging.info(f"Extracted text(Backup): {extracted_text}")
|
| 140 |
-
logging.info(f"Processed images(Backup): {processed_Img}")
|
| 141 |
-
if extracted_text:
|
| 142 |
-
text = json_to_llm_str(extracted_text)
|
| 143 |
-
LLMdata = NER_Model(text)
|
| 144 |
-
logging.info(f"NER model data: {LLMdata}")
|
| 145 |
-
else:
|
| 146 |
-
logging.warning("No extracted text available for backup model")
|
| 147 |
-
|
| 148 |
-
cont_data = process_extracted_text(extracted_text)
|
| 149 |
-
logging.info(f"Contextual data: {cont_data}")
|
| 150 |
-
|
| 151 |
-
processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
|
| 152 |
-
logging.info(f"Processed data: {processed_data}")
|
| 153 |
|
| 154 |
-
session['processed_data'] = processed_data
|
| 155 |
-
session['processed_Img'] = processed_Img
|
| 156 |
-
flash('Data processed and analyzed successfully')
|
| 157 |
-
logging.info("Data processed and analyzed successfully")
|
| 158 |
-
return jsonify({'process_data': [processed_data],
|
| 159 |
-
'process_image': processed_Img
|
| 160 |
-
})
|
| 161 |
|
| 162 |
@app.route('/result')
|
| 163 |
def result():
|
|
|
|
| 55 |
|
| 56 |
@app.route('/upload', methods=['POST'])
|
| 57 |
def upload_file():
|
| 58 |
+
try:
|
| 59 |
+
# Check if 'files' part is in the request
|
| 60 |
+
if 'files' not in request.files:
|
| 61 |
+
flash('No file part')
|
| 62 |
+
logging.warning("No file part found in the request")
|
| 63 |
+
return redirect(request.url)
|
| 64 |
+
|
| 65 |
+
files = request.files.getlist('files') # Get all files from the request
|
| 66 |
+
if not files or all(file.filename == '' for file in files):
|
| 67 |
+
flash('No selected files')
|
| 68 |
+
logging.warning("No files selected for upload")
|
| 69 |
+
return jsonify({'message': "No files selected for upload"})
|
| 70 |
+
|
| 71 |
+
uploaded_files = session.get('uploaded_files', [])
|
| 72 |
+
for file in files:
|
| 73 |
+
if file:
|
| 74 |
+
filename = file.filename
|
| 75 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 76 |
+
|
| 77 |
+
# Save the file
|
| 78 |
+
try:
|
| 79 |
+
file.save(file_path)
|
| 80 |
+
uploaded_files.append(filename)
|
| 81 |
+
logging.info(f"Uploaded file: {filename} at {file_path}")
|
| 82 |
+
except Exception as save_error:
|
| 83 |
+
logging.error(f"Error saving file {filename}: {save_error}")
|
| 84 |
+
return jsonify({'message': f"Error saving file {filename}"}), 500
|
| 85 |
+
|
| 86 |
+
session['uploaded_files'] = uploaded_files # Store the uploaded files in session
|
| 87 |
+
flash('Files successfully uploaded')
|
| 88 |
+
logging.info(f"Files successfully uploaded: {uploaded_files}")
|
| 89 |
+
return jsonify({
|
| 90 |
+
'message': 'Files successfully uploaded',
|
| 91 |
+
'uploaded_files': uploaded_files
|
| 92 |
+
})
|
| 93 |
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logging.error(f"An error occurred during file upload: {e}")
|
| 96 |
+
flash('An error occurred during file upload')
|
| 97 |
+
return jsonify({'message': 'File upload failed'}), 500
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
@app.route('/remove_file')
|
| 101 |
def remove_file():
|
|
|
|
| 117 |
|
| 118 |
@app.route('/process/<filename>', methods=['GET', 'POST'])
|
| 119 |
def process_file(filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
try:
|
| 121 |
+
uploaded_files = session.get('uploaded_files', [])
|
| 122 |
+
if not uploaded_files:
|
| 123 |
+
flash('No files selected for processing')
|
| 124 |
+
logging.warning("No files selected for processing")
|
| 125 |
+
return redirect(url_for('index'))
|
| 126 |
+
|
| 127 |
+
BASE_DIR = 'uploads/'
|
| 128 |
+
|
| 129 |
+
# Joining the base and the requested path
|
| 130 |
+
file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
|
| 131 |
+
logging.info(f"Processing files: {file_paths}")
|
| 132 |
+
|
| 133 |
+
extracted_text = {}
|
| 134 |
+
processed_Img = {}
|
| 135 |
+
|
| 136 |
+
# Try to process using the main model (Mistral 7b)
|
| 137 |
+
try:
|
| 138 |
+
extracted_text, processed_Img = extract_text_from_images(file_paths)
|
| 139 |
+
logging.info(f"Extracted text: {extracted_text}")
|
| 140 |
+
logging.info(f"Processed images: {processed_Img}")
|
| 141 |
+
|
| 142 |
+
llmText = json_to_llm_str(extracted_text)
|
| 143 |
+
logging.info(f"LLM text: {llmText}")
|
| 144 |
+
|
| 145 |
+
LLMdata = Data_Extractor(llmText)
|
| 146 |
+
logging.info(f"LLM data: {LLMdata}")
|
| 147 |
+
|
| 148 |
+
except Exception as model_error:
|
| 149 |
+
logging.error(f"Error during LLM processing: {model_error}")
|
| 150 |
+
logging.info("Running backup model...")
|
| 151 |
+
|
| 152 |
+
# Use backup model in case of errors
|
| 153 |
+
LLMdata = {}
|
| 154 |
+
extracted_text, processed_Img = extract_text_from_images(file_paths)
|
| 155 |
+
logging.info(f"Extracted text (Backup): {extracted_text}")
|
| 156 |
+
logging.info(f"Processed images (Backup): {processed_Img}")
|
| 157 |
+
|
| 158 |
+
if extracted_text:
|
| 159 |
+
text = json_to_llm_str(extracted_text)
|
| 160 |
+
LLMdata = NER_Model(text)
|
| 161 |
+
logging.info(f"NER model data: {LLMdata}")
|
| 162 |
+
else:
|
| 163 |
+
logging.warning("No extracted text available for backup model")
|
| 164 |
+
|
| 165 |
+
# Process extracted text and structure the output
|
| 166 |
+
cont_data = process_extracted_text(extracted_text)
|
| 167 |
+
logging.info(f"Contextual data: {cont_data}")
|
| 168 |
+
|
| 169 |
+
processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
|
| 170 |
+
logging.info(f"Processed data: {processed_data}")
|
| 171 |
+
|
| 172 |
+
# Save data in session for later use
|
| 173 |
+
session['processed_data'] = processed_data
|
| 174 |
+
session['processed_Img'] = processed_Img
|
| 175 |
+
|
| 176 |
+
flash('Data processed and analyzed successfully')
|
| 177 |
+
logging.info("Data processed and analyzed successfully")
|
| 178 |
+
return jsonify({'process_data': [processed_data], 'process_image': processed_Img})
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
+
logging.error(f"An unexpected error occurred during file processing: {e}")
|
| 182 |
+
flash('An error occurred during file processing')
|
| 183 |
+
return jsonify({'message': 'File processing failed'}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
@app.route('/result')
|
| 187 |
def result():
|