WebashalarForML commited on
Commit
ed60e50
·
verified ·
1 Parent(s): 68553c5

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +70 -157
Dockerfile CHANGED
@@ -1,157 +1,70 @@
1
- # libraries
2
- from flask import Flask, render_template, request, redirect, url_for, flash, session, send_from_directory
3
- import os
4
- import logging
5
- from utility.utils import extract_text_from_images, Data_Extractor, json_to_llm_str, process_extracted_text, process_resume_data
6
- from backup.backup import NER_Model
7
- from paddleocr import PaddleOCR
8
-
9
- # Configure logging
10
- logging.basicConfig(
11
- level=logging.INFO,
12
- handlers=[
13
- logging.StreamHandler() # Remove FileHandler and log only to the console
14
- ]
15
- )
16
-
17
- # Flask App
18
- app = Flask(__name__)
19
- app.secret_key = 'your_secret_key'
20
- app.config['UPLOAD_FOLDER'] = 'uploads/'
21
- app.config['RESULT_FOLDER'] = 'uploads/'
22
-
23
- UPLOAD_FOLDER = 'static/uploads/'
24
- RESULT_FOLDER = 'static/results/'
25
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
26
- os.makedirs(RESULT_FOLDER, exist_ok=True)
27
-
28
- if not os.path.exists(app.config['UPLOAD_FOLDER']):
29
- os.makedirs(app.config['UPLOAD_FOLDER'])
30
-
31
- if not os.path.exists(app.config['RESULT_FOLDER']):
32
- os.makedirs(app.config['RESULT_FOLDER'])
33
-
34
- # Set the PaddleOCR home directory to a writable location
35
- os.environ['PADDLEOCR_HOME'] = '/tmp/.paddleocr'
36
-
37
- # Check if PaddleOCR home directory is writable
38
- if not os.path.exists('/tmp/.paddleocr'):
39
- os.makedirs('/tmp/.paddleocr', exist_ok=True)
40
- logging.info("Created PaddleOCR home directory.")
41
- else:
42
- logging.info("PaddleOCR home directory exists.")
43
-
44
- @app.route('/')
45
- def index():
46
- uploaded_files = session.get('uploaded_files', [])
47
- logging.info(f"Accessed index page, uploaded files: {uploaded_files}")
48
- return render_template('index.html', uploaded_files=uploaded_files)
49
-
50
- @app.route('/upload', methods=['POST'])
51
- def upload_file():
52
- if 'files' not in request.files:
53
- flash('No file part')
54
- logging.warning("No file part found in the request")
55
- return redirect(request.url)
56
-
57
- files = request.files.getlist('files')
58
- if not files or all(file.filename == '' for file in files):
59
- flash('No selected files')
60
- logging.warning("No files selected for upload")
61
- return redirect(request.url)
62
-
63
- uploaded_files = session.get('uploaded_files', [])
64
- for file in files:
65
- if file:
66
- filename = file.filename
67
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
68
- file.save(file_path)
69
- uploaded_files.append(filename)
70
- logging.info(f"Uploaded file: {filename} at {file_path}")
71
-
72
- session['uploaded_files'] = uploaded_files
73
- flash('Files successfully uploaded')
74
- logging.info(f"Files successfully uploaded: {uploaded_files}")
75
- return redirect(url_for('index'))
76
-
77
- @app.route('/remove_file')
78
- def remove_file():
79
- uploaded_files = session.get('uploaded_files', [])
80
- for filename in uploaded_files:
81
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
82
- if os.path.exists(file_path):
83
- os.remove(file_path)
84
- logging.info(f"Removed file: {filename}")
85
- else:
86
- logging.warning(f"File not found for removal: {file_path}") # More specific log
87
-
88
- session.pop('uploaded_files', None)
89
- flash('Files successfully removed')
90
- logging.info("All uploaded files removed")
91
- return redirect(url_for('index'))
92
-
93
- @app.route('/process', methods=['POST'])
94
- def process_file():
95
- uploaded_files = session.get('uploaded_files', [])
96
- if not uploaded_files:
97
- flash('No files selected for processing')
98
- logging.warning("No files selected for processing")
99
- return redirect(url_for('index'))
100
-
101
- file_paths = [os.path.join(app.config['UPLOAD_FOLDER'], filename) for filename in uploaded_files]
102
- logging.info(f"Processing files: {file_paths}")
103
-
104
- extracted_text = {}
105
- processed_Img = {}
106
-
107
- try:
108
- extracted_text, processed_Img = extract_text_from_images(file_paths, RESULT_FOLDER)
109
- logging.info(f"Extracted text: {extracted_text}")
110
- logging.info(f"Processed images: {processed_Img}")
111
-
112
- llmText = json_to_llm_str(extracted_text)
113
- logging.info(f"LLM text: {llmText}")
114
-
115
- LLMdata = Data_Extractor(llmText)
116
- logging.info(f"LLM data: {LLMdata}")
117
-
118
- except Exception as e:
119
- logging.error(f"Error during LLM processing: {e}")
120
- logging.info("Running backup model...")
121
-
122
- LLMdata = {}
123
-
124
- if extracted_text:
125
- text = json_to_llm_str(extracted_text)
126
- LLMdata = NER_Model(text)
127
- logging.info(f"NER model data: {LLMdata}")
128
- else:
129
- logging.warning("No extracted text available for backup model")
130
-
131
- cont_data = process_extracted_text(extracted_text)
132
- logging.info(f"Contextual data: {cont_data}")
133
-
134
- processed_data = process_resume_data(LLMdata, cont_data, extracted_text)
135
- logging.info(f"Processed data: {processed_data}")
136
-
137
- session['processed_data'] = processed_data
138
- session['processed_Img'] = processed_Img
139
- flash('Data processed and analyzed successfully')
140
- logging.info("Data processed and analyzed successfully")
141
- return redirect(url_for('result'))
142
-
143
- @app.route('/result')
144
- def result():
145
- processed_data = session.get('processed_data', {})
146
- processed_Img = session.get('processed_Img', {})
147
- logging.info(f"Displaying results: Data - {processed_data}, Images - {processed_Img}")
148
- return render_template('result.html', data=processed_data, Img=processed_Img)
149
-
150
- @app.route('/uploads/<filename>')
151
- def uploaded_file(filename):
152
- logging.info(f"Serving file: {filename}")
153
- return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
154
-
155
- if __name__ == '__main__':
156
- logging.info("Starting Flask app")
157
- app.run(debug=True)
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set environment variables for Python
5
+ ENV PYTHONDONTWRITEBYTECODE 1
6
+ ENV PYTHONUNBUFFERED 1
7
+
8
+ # Set environment variables for Hugging Face cache to /tmp (which is writable)
9
+ ENV TRANSFORMERS_CACHE=/tmp/huggingface
10
+ ENV HF_HOME=/tmp/huggingface
11
+
12
+ # Ensure permissions are correct for /tmp/huggingface (optional, but just in case)
13
+ RUN mkdir -p /tmp/huggingface && chmod -R 777 /tmp/huggingface
14
+
15
+ # Create cache directories for Hugging Face and Matplotlib
16
+ RUN mkdir -p /app/.cache/huggingface /app/.cache/matplotlib
17
+
18
+ # Set environment variables for Hugging Face and Matplotlib cache
19
+ ENV TRANSFORMERS_CACHE=/app/.cache/huggingface
20
+ ENV HF_HOME=/app/.cache/huggingface
21
+ ENV MPLCONFIGDIR=/app/.cache/matplotlib
22
+
23
+ # Adding permission for the cache
24
+ RUN chmod -R 777 /app/.cache
25
+
26
+ # Give write permissions to the /app directory
27
+ RUN chmod -R 777 /app
28
+
29
+ # Create /app/logs directory and set permissions for logging
30
+ RUN mkdir -p /app/logs && chmod -R 777 /app/logs
31
+
32
+ # Set the working directory
33
+ WORKDIR /app
34
+
35
+ # Install system dependencies, including libgomp
36
+ RUN apt-get update && apt-get install -y \
37
+ libgl1-mesa-glx \
38
+ libgomp1 \
39
+ libglib2.0-0 \
40
+ && rm -rf /var/lib/apt/lists/*
41
+
42
+ # Set environment variables for Matplotlib and Hugging Face cache directories
43
+ ENV MPLCONFIGDIR=/tmp/matplotlib
44
+ ENV TRANSFORMERS_CACHE=/tmp/transformers_cache
45
+
46
+ # Copy the requirements file into the container at /app
47
+ COPY requirements.txt /app/
48
+
49
+ # Install any needed packages specified in requirements.txt
50
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
51
+
52
+ # Create directories for session storage, uploads, and cache
53
+ RUN mkdir -p /app/flask_sessions /app/uploads /tmp/matplotlib /tmp/transformers_cache && chmod -R 777 /app/flask_sessions /app/uploads /tmp/matplotlib /tmp/transformers_cache
54
+
55
+ # Set permission for the Paddle OCR
56
+ ENV PADDLEOCR_MODEL_DIR=/tmp/.paddleocr
57
+ RUN mkdir -p /tmp/.paddleocr && chmod -R 777 /tmp/.paddleocr
58
+
59
+ # Copy the rest of the application code to /app
60
+ COPY . /app/
61
+
62
+ # Expose the port that the app runs on
63
+ EXPOSE 7860
64
+
65
+ # Set environment variables for Flask
66
+ ENV FLASK_APP=app.py
67
+ ENV FLASK_ENV=production
68
+
69
+ # Command to run the Flask app using Gunicorn with 1 worker
70
+ CMD ["gunicorn", "--workers=1", "--bind=0.0.0.0:7860", "--timeout=120", "app:app"]