WebashalarForML commited on
Commit
824f4e9
1 Parent(s): a25ce68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -48
app.py CHANGED
@@ -72,7 +72,7 @@ def upload_file():
72
  session['uploaded_files'] = uploaded_files
73
  flash('Files successfully uploaded')
74
  logging.info(f"Files successfully uploaded: {uploaded_files}")
75
- return process_file()
76
 
77
  @app.route('/remove_file',methods=['POST'])
78
  def remove_file():
@@ -108,56 +108,71 @@ def reset_upload():
108
  flash('No file to remove.')
109
  return redirect(url_for('index'))
110
 
111
- @app.route('/process', methods=['GET','POST'])
112
- def process_file():
113
- uploaded_files = session.get('uploaded_files', [])
114
- if not uploaded_files:
115
- flash('No files selected for processing')
116
- logging.warning("No files selected for processing")
117
- return redirect(url_for('index'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
- file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
120
- logging.info(f"Processing files: {file_paths}")
 
121
 
122
- extracted_text = {}
123
- processed_Img = {}
124
 
125
- try:
126
- extracted_text, processed_Img = extract_text_from_images(file_paths)
127
- logging.info(f"Extracted text: {extracted_text}")
128
- logging.info(f"Processed images: {processed_Img}")
129
-
130
- llmText = json_to_llm_str(extracted_text)
131
- logging.info(f"LLM text: {llmText}")
132
-
133
- LLMdata = Data_Extractor(llmText)
134
- logging.info(f"LLM data: {LLMdata}")
135
-
136
- except Exception as e:
137
- logging.error(f"Error during LLM processing: {e}")
138
- logging.info("Running backup model...")
139
-
140
- LLMdata = {}
141
- extracted_text, processed_Img = extract_text_from_images(file_paths)
142
- logging.info(f"Extracted text(Backup): {extracted_text}")
143
- logging.info(f"Processed images(Backup): {processed_Img}")
144
- if extracted_text:
145
- text = json_to_llm_str(extracted_text)
146
- LLMdata = NER_Model(text)
147
- logging.info(f"NER model data: {LLMdata}")
148
- else:
149
- logging.warning("No extracted text available for backup model")
150
-
151
- cont_data = process_extracted_text(extracted_text)
152
- logging.info(f"Contextual data: {cont_data}")
153
-
154
- processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
155
- logging.info(f"Processed data: {processed_data}")
156
-
157
- session['processed_data'] = processed_data
158
- session['processed_Img'] = processed_Img
159
- flash('Data processed and analyzed successfully')
160
- logging.info("Data processed and analyzed successfully")
161
  return redirect(url_for('result'))
162
 
163
  @app.route('/result')
 
72
  session['uploaded_files'] = uploaded_files
73
  flash('Files successfully uploaded')
74
  logging.info(f"Files successfully uploaded: {uploaded_files}")
75
+ return process_file(uploaded_files)
76
 
77
  @app.route('/remove_file',methods=['POST'])
78
  def remove_file():
 
108
  flash('No file to remove.')
109
  return redirect(url_for('index'))
110
 
111
+ @app.route('/process_file/<filename>', methods=['GET', 'POST'])
112
+ def process_file(filename):
113
+ try:
114
+ uploaded_files = session.get('uploaded_files', [])
115
+ if not uploaded_files:
116
+ print('No files selected for processing')
117
+ logging.warning("No files selected for processing")
118
+ return redirect(url_for('index'))
119
+
120
+ # Joining the base and the requested path
121
+ file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
122
+ logging.info(f"Processing files: {file_paths}")
123
+
124
+ extracted_text = {}
125
+ processed_Img = {}
126
+
127
+ # Try to process using the main model (Mistral 7b)
128
+ try:
129
+ extracted_text, processed_Img = extract_text_from_images(file_paths)
130
+ logging.info(f"Extracted text: {extracted_text}")
131
+ logging.info(f"Processed images: {processed_Img}")
132
+
133
+ #run the model code only if the text is extracted.
134
+ if extracted_text:
135
+ llmText = json_to_llm_str(extracted_text)
136
+ logging.info(f"LLM text: {llmText}")
137
+
138
+ #run the model code only if the text is extracted.
139
+ LLMdata = Data_Extractor(llmText)
140
+ print("Json Output from model------------>",LLMdata)
141
+ logging.info(f"LLM data: {LLMdata}")
142
+ else:
143
+ raise ('The text is not detected in the OCR')
144
+
145
+ except Exception as model_error:
146
+ logging.error(f"Error during LLM processing: {model_error}")
147
+ logging.info("Running backup model...")
148
+
149
+ # Use backup model in case of errors
150
+ LLMdata = {}
151
+ extracted_text, processed_Img = extract_text_from_images(file_paths)
152
+ logging.info(f"Extracted text (Backup): {extracted_text}")
153
+ logging.info(f"Processed images (Backup): {processed_Img}")
154
+
155
+ if extracted_text:
156
+ text = json_to_llm_str(extracted_text)
157
+ LLMdata = NER_Model(text)
158
+ print("Json Output from model------------>",LLMdata)
159
+ logging.info(f"NER model data: {LLMdata}")
160
+ else:
161
+ logging.warning("No extracted text available for backup model")
162
 
163
+ # Process extracted text and structure the output
164
+ cont_data = process_extracted_text(extracted_text)
165
+ logging.info(f"Contextual data: {cont_data}")
166
 
167
+ processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
168
+ logging.info(f"Processed data: {processed_data}")
169
 
170
+ # Save data in session for later use
171
+ session['processed_data'] = processed_data
172
+ session['processed_Img'] = processed_Img
173
+
174
+ print('Data processed and analyzed successfully')
175
+ logging.info("Data processed and analyzed successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  return redirect(url_for('result'))
177
 
178
  @app.route('/result')