Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970# Use an official Python runtime as a parent imageFROM python:3.9-slim# Set environment variables for PythonENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1# Set environment variables for Hugging Face cache to /tmp (which is writable)ENV TRANSFORMERS_CACHE=/tmp/huggingfaceENV HF_HOME=/tmp/huggingface# Ensure permissions are correct for /tmp/huggingface (optional, but just in case)RUN mkdir -p /tmp/huggingface && chmod -R 777 /tmp/huggingface# Create cache directories for Hugging Face and MatplotlibRUN mkdir -p /app/.cache/huggingface /app/.cache/matplotlib# Set environment variables for Hugging Face and Matplotlib cacheENV TRANSFORMERS_CACHE=/app/.cache/huggingfaceENV HF_HOME=/app/.cache/huggingfaceENV MPLCONFIGDIR=/app/.cache/matplotlib# Adding permission for the cacheRUN chmod -R 777 /app/.cache# Give write permissions to the /app directoryRUN chmod -R 777 /app# Create /app/logs directory and set permissions for loggingRUN mkdir -p /app/logs && chmod -R 777 /app/logs# Set the working directoryWORKDIR /app# Install system dependencies, including libgompRUN apt-get update && apt-get install -y \libgl1-mesa-glx \libgomp1 \libglib2.0-0 \&& rm -rf /var/lib/apt/lists/*# Set environment variables for Matplotlib and Hugging Face cache directoriesENV MPLCONFIGDIR=/tmp/matplotlibENV TRANSFORMERS_CACHE=/tmp/transformers_cache# Copy the requirements file into the container at /appCOPY requirements.txt /app/# Install any needed packages specified in requirements.txtRUN pip install --no-cache-dir --upgrade -r requirements.txt# Create directories for session storage, uploads, and cacheRUN 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# Set permission for the Paddle OCRENV PADDLEOCR_MODEL_DIR=/tmp/.paddleocrRUN mkdir -p /tmp/.paddleocr && chmod -R 777 /tmp/.paddleocr# Copy the rest of the application code to /appCOPY . /app/# Expose the port that the app runs onEXPOSE 7860# Set environment variables for FlaskENV FLASK_APP=app.pyENV FLASK_ENV=production# Command to run the Flask app using Gunicorn with 1 workerCMD ["gunicorn", "--workers=1", "--bind=0.0.0.0:7860", "--timeout=120", "app:app"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157# librariesfrom flask import Flask, render_template, request, redirect, url_for, flash, session, send_from_directoryimport osimport loggingfrom utility.utils import extract_text_from_images, Data_Extractor, json_to_llm_str, process_extracted_text, process_resume_datafrom backup.backup import NER_Modelfrom paddleocr import PaddleOCR# Configure logginglogging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler() # Remove FileHandler and log only to the console])# Flask Appapp = Flask(__name__)app.secret_key = 'your_secret_key'app.config['UPLOAD_FOLDER'] = 'uploads/'app.config['RESULT_FOLDER'] = 'uploads/'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 locationos.environ['PADDLEOCR_HOME'] = '/tmp/.paddleocr'# Check if PaddleOCR home directory is writableif 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('/upload', methods=['POST'])def upload_file():if 'files' not in request.files:flash('No file part')logging.warning("No file part found in the request")return redirect(request.url)files = request.files.getlist('files')if not files or all(file.filename == '' for file in files):flash('No selected files')logging.warning("No files selected for upload")return redirect(request.url)uploaded_files = session.get('uploaded_files', [])for file in files:if file:filename = file.filenamefile_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)file.save(file_path)uploaded_files.append(filename)logging.info(f"Uploaded file: {filename} at {file_path}")session['uploaded_files'] = uploaded_filesflash('Files successfully uploaded')logging.info(f"Files successfully uploaded: {uploaded_files}")return redirect(url_for('index'))@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 logsession.pop('uploaded_files', None)flash('Files successfully removed')logging.info("All uploaded files removed")return redirect(url_for('index'))@app.route('/process', methods=['POST'])def process_file():uploaded_files = session.get('uploaded_files', [])if not uploaded_files:flash('No files selected for processing')logging.warning("No files selected for processing")return redirect(url_for('index'))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:extracted_text, processed_Img = extract_text_from_images(file_paths, RESULT_FOLDER)logging.info(f"Extracted text: {extracted_text}")logging.info(f"Processed images: {processed_Img}")llmText = json_to_llm_str(extracted_text)logging.info(f"LLM text: {llmText}")LLMdata = Data_Extractor(llmText)logging.info(f"LLM data: {LLMdata}")except Exception as e:logging.error(f"Error during LLM processing: {e}")logging.info("Running backup model...")LLMdata = {}if extracted_text:text = json_to_llm_str(extracted_text)LLMdata = NER_Model(text)logging.info(f"NER model data: {LLMdata}")else:logging.warning("No extracted text available for backup model")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}")session['processed_data'] = processed_datasession['processed_Img'] = processed_Imgflash('Data processed and analyzed successfully')logging.info("Data processed and analyzed successfully")return redirect(url_for('result'))@app.route('/result')def result():processed_data = session.get('processed_data', {})processed_Img = session.get('processed_Img', {})logging.info(f"Displaying results: Data - {processed_data}, Images - {processed_Img}")return render_template('result.html', data=processed_data, Img=processed_Img)@app.route('/uploads/<filename>')def uploaded_file(filename):logging.info(f"Serving file: {filename}")return send_from_directory(app.config['UPLOAD_FOLDER'], filename)if __name__ == '__main__':logging.info("Starting Flask app")app.run(debug=True)
Dockerfile+157-70