File size: 9,136 Bytes
97132db
dbd7a64
97132db
af5935b
 
567a60e
099f191
af5935b
 
73884d7
 
 
e634f6b
73884d7
 
af5935b
97132db
 
 
da9874c
97132db
0a03a56
97132db
 
 
da9874c
97132db
 
 
 
 
 
c676e89
 
 
1acf205
37ffd75
1acf205
d2b54c3
 
 
 
 
 
 
97132db
 
d2b54c3
af5935b
97132db
 
3a6c535
97132db
098fbe3
454df45
745f0aa
098fbe3
745f0aa
098fbe3
f4a5454
098fbe3
745f0aa
 
098fbe3
745f0aa
098fbe3
 
 
 
 
 
 
 
745f0aa
098fbe3
745f0aa
098fbe3
 
 
 
 
 
 
f403784
098fbe3
8e56782
97132db
098fbe3
 
 
97132db
 
 
d2b54c3
97132db
6c1d851
d2b54c3
6c1d851
 
 
d2b54c3
6c1d851
d2b54c3
da9874c
af5935b
97132db
 
c48aa2f
fe47ff4
97132db
098fbe3
 
da9874c
098fbe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454df45
 
 
 
 
 
 
 
 
 
 
098fbe3
 
 
 
 
 
 
 
 
 
 
 
 
 
549159e
098fbe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da9874c
098fbe3
cdd897f
 
 
 
 
 
af5935b
97132db
098fbe3
da9874c
098fbe3
e9368dc
97132db
 
af5935b
97132db
 
d6174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97132db
af5935b
da9874c
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
# libraries
from flask import Flask, render_template, request, redirect, url_for, flash, session, send_from_directory, jsonify
import os
import logging
from utility.utils import extract_text_from_images, Data_Extractor, json_to_llm_str, process_extracted_text, process_resume_data
from backup.backup import NER_Model
from paddleocr import PaddleOCR

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    handlers=[
        logging.StreamHandler()  # Remove FileHandler and log only to the console
    ]
)

# Flask App
app = Flask(__name__)
app.secret_key = 'your_secret_key'

app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['RESULT_FOLDER'] = 'results/'

UPLOAD_FOLDER = 'static/uploads/'
RESULT_FOLDER = 'static/results/'

os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(RESULT_FOLDER, exist_ok=True)

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

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

# Set the PaddleOCR home directory to a writable location
os.environ['PADDLEOCR_HOME'] = '/tmp/.paddleocr' 

# Check if PaddleOCR home directory is writable
if not os.path.exists('/tmp/.paddleocr'):
    os.makedirs('/tmp/.paddleocr', exist_ok=True)
    logging.info("Created PaddleOCR home directory.")
else:
    logging.info("PaddleOCR home directory exists.")

@app.route('/')
def index():
    uploaded_files = session.get('uploaded_files', [])
    logging.info(f"Accessed index page, uploaded files: {uploaded_files}")
    return render_template('index.html', uploaded_files=uploaded_files)

@app.route('/process', methods=['GET','POST'])
def upload_file():
    try:
       
        # Check if the 'files' part exists in the request
        if 'files' not in request.files:
            print("No 'files' part in the request")
            logging.warning("No file part found in the request")
            return jsonify({'message': "No file part found in the request"})

        # Get all files from the request
        files = request.files.getlist('files')
        if not files or all(file.filename == '' for file in files):
            print("No files selected for upload")
            logging.warning("No files selected for upload")
            return jsonify({'message': "No files selected for upload"})

        uploaded_files = session.get('uploaded_files', [])
        for file in files:
            if file:
                filename = file.filename
                file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
                print(f"Saving file to: {file_path}")
                try:
                    # Save the file
                    file.save(file_path)
                    uploaded_files.append(filename)
                    logging.info(f"Uploaded file: {filename} at {file_path}")
                except Exception as save_error:
                    logging.error(f"Error saving file {filename}: {save_error}")
                    return jsonify({'message': f"Error saving file {filename}"}), 500

        session['uploaded_files'] = uploaded_files  # Store uploaded files in session
        logging.info(f"Files successfully uploaded: {uploaded_files}")
        return process_file(uploaded_files)

    except Exception as e:
        logging.error(f"An error occurred during file upload: {e}")
        return jsonify({'message': 'File upload failed'}), 500

@app.route('/remove_file')
def remove_file():
    uploaded_files = session.get('uploaded_files', [])
    for filename in uploaded_files:
        file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        if os.path.exists(file_path):
            os.remove(file_path)
            logging.info(f"Removed file: {filename}")
        else:
            logging.warning(f"File not found for removal: {file_path}")  # More specific log

    session.pop('uploaded_files', None)  
    print('Files successfully removed')
    logging.info("All uploaded files removed")
    return redirect(url_for('index'))

@app.route('/process_file/<filename>', methods=['GET', 'POST'])
def process_file(filename):
    try:
        uploaded_files = session.get('uploaded_files', [])
        if not uploaded_files:
            print('No files selected for processing')
            logging.warning("No files selected for processing")
            return redirect(url_for('index'))

        # Joining the base and the requested path
        file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
        logging.info(f"Processing files: {file_paths}")

        extracted_text = {}
        processed_Img = {}

        # Try to process using the main model (Mistral 7b)
        try:
            extracted_text, processed_Img = extract_text_from_images(file_paths)
            logging.info(f"Extracted text: {extracted_text}")
            logging.info(f"Processed images: {processed_Img}")

            #run the model code only if the text is extracted.
            if extracted_text:
                llmText = json_to_llm_str(extracted_text)
                logging.info(f"LLM text: {llmText}")

                #run the model code only if the text is extracted.
                LLMdata = Data_Extractor(llmText)
                print("Json Output from model------------>",LLMdata)
                logging.info(f"LLM data: {LLMdata}")
            else:
                raise ('The text is not detected in the OCR')

        except Exception as model_error:
            logging.error(f"Error during LLM processing: {model_error}")
            logging.info("Running backup model...")

            # Use backup model in case of errors
            LLMdata = {}
            extracted_text, processed_Img = extract_text_from_images(file_paths)
            logging.info(f"Extracted text (Backup): {extracted_text}")
            logging.info(f"Processed images (Backup): {processed_Img}")
            
            if extracted_text:
                text = json_to_llm_str(extracted_text)
                LLMdata = NER_Model(text)
                print("Json Output from model------------>",LLMdata)
                logging.info(f"NER model data: {LLMdata}")
            else:
                logging.warning("No extracted text available for backup model")

        # Process extracted text and structure the output
        cont_data = process_extracted_text(extracted_text)
        logging.info(f"Contextual data: {cont_data}")

        processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
        logging.info(f"Processed data: {processed_data}")

        # Save data in session for later use
        session['processed_data'] = processed_data
        session['processed_Img'] = processed_Img

        print('Data processed and analyzed successfully')
        logging.info("Data processed and analyzed successfully")
        return jsonify({
        'data': [processed_data], 
        'process_image': processed_Img, 
        'success': True, 
        'message': 'Data processed and analyzed successfully'
    })

    except Exception as e:
        logging.error(f"An unexpected error occurred during file processing: {e}")
        print('An error occurred during file processing')
        return jsonify({'message': 'File processing failed'}), 500

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    logging.info(f"Serving file: {filename}")
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

@app.route('/clear_folders', methods=['DELETE'])
def clear_folders():
    try:
        # Function to clear all files in a given folder
        def clear_folder(folder_path):
            if os.path.exists(folder_path):
                for filename in os.listdir(folder_path):
                    file_path = os.path.join(folder_path, filename)
                    try:
                        if os.path.isfile(file_path):
                            os.remove(file_path)
                            logging.info(f"Deleted file: {file_path}")
                        else:
                            logging.warning(f"{file_path} is not a file, skipping.")
                    except Exception as e:
                        logging.error(f"Error deleting file {file_path}: {e}")
                        return jsonify({'message': f"Error deleting file {file_path}"}), 500
            else:
                logging.warning(f"Folder {folder_path} does not exist.")
                return jsonify({'message': f"Folder {folder_path} does not exist"}), 404

        # Clear both the upload and result folders
        clear_folder(app.config['UPLOAD_FOLDER'])
        clear_folder(app.config['RESULT_FOLDER'])

        logging.info("Both upload and result folders cleared successfully.")
        return jsonify({'message': 'Both upload and result folders cleared successfully'}), 200

    except Exception as e:
        logging.error(f"An unexpected error occurred while clearing folders: {e}")
        return jsonify({'message': 'Failed to clear folders'}), 500

if __name__ == '__main__':
    logging.info("Starting Flask app")
    app.run(debug=True)