Spaces:
Sleeping
Sleeping
Upload 5 files
Browse filesFiles changed (5)
- BeckUp.pyAdded+177
- app.pyAdded+115
- app_error.logAdded+1
- readmeAdded+146
- requirements.txtAdded+148
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177from flask import Flask, request, redirect, flash, session, render_template, url_for
import os
import json
from werkzeug.utils import secure_filename
import logging
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error
from utils.spacy import Parser_from_model
from utils.mistral import process_resume_data
import platform
from waitress import serve
if platform.system() == "Windows":
app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads'
# else:
# # For Hugging Face Spaces or other Linux environments
# if __name__ != "__main__":
# serve(app, host="0.0.0.0", port=7860)
# Error handlers
app.register_error_handler(404, page_not_found)
app.register_error_handler(500, internal_server_error)
# Allowed extensions
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
uploaded_file = session.get('uploaded_file', None)
return render_template('index.html', uploaded_file=uploaded_file)
# @app.route('/upload', methods=['POST'])
# def upload_file():
# if 'file' not in request.files:
# flash('No file part')
# return redirect(request.url)
# file = request.files['file']
# if file.filename == '':
# flash('No selected file')
# return redirect(request.url)
# if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# logging.debug(f"File uploaded: {filename}")
# session['uploaded_file'] = filename
# flash('File successfully uploaded')
# return redirect(url_for('index'))
# else:
# return handle_invalid_file_type()
# def process_file():
# selected_file = session.get('uploaded_file')
# if not selected_file:
# flash('No file selected for processing')
# return redirect(url_for('index'))
# file_path = os.path.join(app.config['UPLOAD_FOLDER'], selected_file)
# if not os.path.exists(file_path):
# return handle_file_not_found()
# parsed_data = process_resume_data(file_path)
# if not parsed_data or 'error' in parsed_data:
# return handle_file_processing_error()
# session['processed_data'] = parsed_data
# flash('Data processed successfully')
# return redirect(url_for('result'))
@app.route('/upload_and_process', methods=['POST', 'GET'])
def upload_and_process():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
logging.debug(f"File uploaded: {filename}")
session['uploaded_file'] = filename
# Process the file after uploading
parsed_data = process_resume_data(file_path)
if not parsed_data or 'error' in parsed_data:
return handle_file_processing_error()
session['processed_data'] = parsed_data
flash('File uploaded and data processed successfully')
return redirect(url_for('result'))
else:
return handle_invalid_file_type()
@app.route('/remove_file')
def remove_file():
uploaded_file = session.get('uploaded_file')
if uploaded_file:
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file))
session.pop('uploaded_file', None)
flash('File successfully removed')
return redirect(url_for('index'))
@app.route('/reset_upload')
def reset_upload():
uploaded_file = session.get('uploaded_file')
if uploaded_file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
if os.path.exists(file_path):
os.remove(file_path)
session.pop('uploaded_file', None)
session.pop('processed_data', None)
flash('File reset. You can upload a new file.')
return redirect(url_for('index'))
# @app.route('/process', methods=['GET', 'POST'])
# def process_file():
# selected_file = session.get('uploaded_file')
# if not selected_file:
# flash('No file selected for processing')
# return redirect(url_for('index'))
# file_path = os.path.join(app.config['UPLOAD_FOLDER'], selected_file)
# if not os.path.exists(file_path):
# return handle_file_not_found()
# parsed_data = process_resume_data(file_path)
# if not parsed_data or 'error' in parsed_data:
# return handle_file_processing_error()
# session['processed_data'] = parsed_data
# flash('Data processed successfully')
# return redirect(url_for('result'))
@app.route('/loading')
def loading():
selected_file = session.get('uploaded_file')
if not selected_file:
flash('No file selected for processing')
return redirect(url_for('index'))
file_path = os.path.join(app.config['UPLOAD_FOLDER'], selected_file)
parsed_data = process_resume_data(file_path)
if parsed_data and 'error' not in parsed_data:
session['processed_data'] = json.loads(parsed_data)
flash('Data processed successfully')
return redirect(url_for('result'))
else:
return handle_file_processing_error()
@app.route('/result')
def result():
processed_data = session.get('processed_data', None)
if not processed_data:
flash('No data to display. Please upload and process a file.')
return redirect(url_for('index'))
return render_template('result.html', parsed_data=processed_data)
if __name__ == '__main__':
app.run(debug=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115from flask import Flask, request, redirect, flash, session, render_template, url_for
import os
import json
import logging
from werkzeug.utils import secure_filename
from utils.error import handle_file_not_found, handle_invalid_file_type, handle_file_processing_error, page_not_found, internal_server_error
from utils.spacy import Parser_from_model
from utils.mistral import process_resume_data
import platform
from waitress import serve
# Initialize the Flask application
app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads'
# Allowed file extensions
ALLOWED_EXTENSIONS = {'pdf', 'docx', 'rsf', 'odt', 'png', 'jpg', 'jpeg'}
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Error handlers
app.register_error_handler(404, page_not_found)
app.register_error_handler(500, internal_server_error)
def allowed_file(filename):
"""Check if the file has an allowed extension."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
"""Display the index page with the uploaded file information."""
uploaded_file = session.get('uploaded_file', None)
return render_template('index.html', uploaded_file=uploaded_file)
@app.route('/upload_and_process', methods=['POST'])
def upload_and_process():
"""Handle file upload and process the file."""
if 'file' not in request.files or request.files['file'].filename == '':
flash('No file selected for upload.')
return redirect(request.url)
file = request.files['file']
# Check if the file is allowed
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
logging.debug(f"File uploaded: {filename}")
session['uploaded_file'] = filename
# Process the file after uploading
try:
parsed_data = process_resume_data(file_path)
if not parsed_data or 'error' in parsed_data:
flash('An error occurred during file processing.')
return redirect(url_for('index'))
session['processed_data'] = parsed_data
flash('File uploaded and data processed successfully.')
return redirect(url_for('result'))
except Exception as e:
logging.error(f"File processing error: {str(e)}")
flash('An error occurred while processing the file.')
return handle_file_processing_error()
else:
return handle_invalid_file_type()
@app.route('/remove_file', methods=['POST'])
def remove_file():
"""Remove the uploaded file and reset the session."""
uploaded_file = session.get('uploaded_file')
if uploaded_file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
if os.path.exists(file_path):
os.remove(file_path)
session.pop('uploaded_file', None)
flash('File successfully removed.')
else:
flash('No file to remove.')
return redirect(url_for('index'))
@app.route('/reset_upload')
def reset_upload():
"""Reset the uploaded file and the processed data."""
uploaded_file = session.get('uploaded_file')
if uploaded_file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file)
if os.path.exists(file_path):
os.remove(file_path)
session.pop('uploaded_file', None)
session.pop('processed_data', None)
flash('File and data reset. You can upload a new file.')
return redirect(url_for('index'))
@app.route('/result')
def result():
"""Display the processed data result."""
processed_data = session.get('processed_data', None)
if not processed_data:
flash('No data to display. Please upload and process a file.')
return redirect(url_for('index'))
return render_template('result.html', parsed_data=processed_data)
if __name__ == '__main__':
# For Windows development
if platform.system() == "Windows":
app.run(debug=True)
# For Linux or production with Waitress
else:
serve(app, host="0.0.0.0", port=7860)12024-09-28 13:31:17,959 ERROR: 404 Error: http://127.0.0.1:5000/favicon.ico123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146_\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\_
_\\----------- **Resume Parser** ----------\\_
_\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\_
# Overview:
This project is a comprehensive Resume Parsing tool built using Python,
integrating the Mistral-Nemo-Instruct-2407 model for primary parsing.
If Mistral fails or encounters issues,
the system falls back to a custom-trained spaCy model to ensure continued functionality.
The tool is wrapped with a Flask API and has a user interface built using HTML and CSS.
# Installation Guide:
1. Create and Activate a Virtual Environment
python -m venv venv
source venv/bin/activate # For Linux/Mac
# or
venv\Scripts\activate # For Windows
# NOTE: If the virtual environment (venv) is already created, you can skip the creation step and just activate.
- For Linux/Mac:
source venv/bin/activate
- For Windows:
venv\Scripts\activate
2. Install Required Libraries
pip install -r requirements.txt
# Ensure the following dependencies are included:
- Flask
- spaCy
- huggingface_hub
- PyMuPDF
- python-docx
- Tesseract-OCR (for image-based parsing)
3. Set up Hugging Face Token
- Add your Hugging Face token to the .env file as:
HF_TOKEN=<your_huggingface_token>
# File Structure Overview:
Mistral_With_Spacy/
β
βββ Spacy_Models/
β βββ ner_model_05_3 # Pretrained spaCy model directory for resume parsing
β
βββ templates/
β βββ index.html # UI for file upload
β βββ result.html # Display parsed results in structured JSON
β
βββ uploads/ # Directory for uploaded resume files
β
βββ utils/
β βββ mistral.py # Code for calling Mistral API and handling responses
β βββ spacy.py # spaCy fallback model for parsing resumes
β βββ error.py # Error handling utilities
β βββ fileTotext.py # Functions to extract text from different file formats (PDF, DOCX, etc.)
β
βββ venv/ # Virtual environment
β
βββ .env # Environment variables file (contains Hugging Face token)
β
βββ main.py # Flask app handling API routes for uploading and processing resumes
β
βββ requirements.txt # Dependencies required for the project
# Program Overview:
# Mistral Integration (utils/mistral.py)
- Mistral API Calls: Uses Hugging Faceβs Mistral-Nemo-Instruct-2407 model to parse resumes.
- Personal and Professional Extraction: Two functions extract personal and professional information in structured JSON format.
- Fallback Mechanism: If Mistral fails, spaCy NER model is used as a fallback.
# SpaCy Integration (utils/spacy.py)
- Custom Trained Model: Uses a spaCy model (ner_model_05_3) trained specifically for resume parsing.
- Named Entity Recognition: Extracts key information like Name, Email, Contact, Location, Skills, Experience, etc., from resumes.
- Validation: Includes validation for extracted emails and contacts.
# File Conversion (utils/fileTotext.py)
- Text Extraction: Handles different resume formats (PDF, DOCX, ODT, RSF, and images like PNG, JPG, JPEG) and extracts text for further processing.
- PDF Files: Uses PyMuPDF to extract text and, if necessary, Tesseract-OCR for image-based PDF content.
- DOCX Files: Uses `python-docx` to extract structured text from Word documents.
- ODT Files: Uses `odfpy` to extract text from ODT (OpenDocument) files.
- RSF Files: Reads plain text from RSF files.
- Images (PNG, JPG, JPEG): Uses Tesseract-OCR to extract text from image-based resumes.
- Hyperlink Extraction: Extracts hyperlinks from PDF files, capturing any embedded URLs during the parsing process.
# Error Handling (utils/error.py)
- Handles API response errors, file format errors, and ensures smooth fallbacks without crashing the app.
# Flask API (main.py)
Endpoints:
- /upload for uploading resumes.
- Displays parsed results in JSON format on the results page.
- UI: Simple interface for uploading resumes and viewing the parsing results.
# Tree map of your program:
main.py
βββ Handles API side
βββ File upload/remove
βββ Process resumes
βββ Show result
utils
βββ fileTotext.py
β βββ Converts files to text
β βββ PDF
β βββ DOCX
β βββ RTF
β βββ ODT
β βββ PNG
β βββ JPG
β βββ JPEG
βββ mistral.py
β βββ Mistral API Calls
β β βββ Uses Mistral-Nemo-Instruct-2407 model
β βββ Personal and Professional Extraction
β β βββ Extracts personal information
β β βββ Extracts professional information
β βββ Fallback Mechanism
β βββ Uses spaCy NER model if Mistral fails
βββ spacy.py
βββ Custom Trained Model
β βββ Uses spaCy model (ner_model_05_3)
βββ Named Entity Recognition
β βββ Extracts key information (Name, Email, Contact, etc.)
βββ Validation
βββ Validates emails and contacts
# References:
- [Flask Documentation](https://flask.palletsprojects.com/)
- [spaCy Documentation](https://spacy.io/usage)
- [Hugging Face Hub API](https://huggingface.co/docs/huggingface_hub/index)
- [PyMuPDF (MuPDF) Documentation](https://pymupdf.readthedocs.io/en/latest/)
- [python-docx Documentation](https://python-docx.readthedocs.io/en/latest/)
- [Tesseract OCR Documentation](https://github.com/tesseract-ocr/tesseract)
- [Virtual Environments in Python](https://docs.python.org/3/tutorial/venv.html)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148alembic==1.13.2
amqp==5.2.0
annotated-types==0.7.0
attrs==24.2.0
bcrypt==4.2.0
blinker==1.8.2
blis==0.7.11
cachetools==5.5.0
catalogue==2.0.10
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
click==8.1.7
cloudpathlib==0.19.0
colorama==0.4.6
confection==0.1.5
croniter==3.0.3
cryptography==43.0.1
cymem==2.0.8
debtcollector==3.0.0
decorator==5.1.1
defusedxml==0.7.1
dnspython==2.6.1
dogpile.cache==1.3.3
eventlet==0.37.0
fasteners==0.19
filelock==3.16.0
Flask==3.0.3
fsspec==2024.9.0
futurist==3.0.0
greenlet==3.1.0
huggingface-hub==0.24.7
idna==3.8
importlib_metadata==8.5.0
iso8601==2.1.0
itsdangerous==2.2.0
Jinja2==3.1.4
jsonschema==4.23.0
jsonschema-specifications==2023.12.1
keystoneauth1==5.8.0
keystonemiddleware==10.7.1
kombu==5.4.1
langcodes==3.4.0
language_data==1.2.0
logutils==0.3.5
lxml==5.3.0
Mako==1.3.5
marisa-trie==1.2.0
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
mistral==18.0.1
mistral-lib==3.0.0
msgpack==1.1.0
murmurhash==1.0.10
netaddr==1.3.0
netifaces==0.11.0
networkx==3.3
numpy==1.26.4
odfpy==1.4.1
os-service-types==1.7.0
oslo.cache==3.8.0
oslo.concurrency==6.1.0
oslo.config==9.6.0
oslo.context==5.6.0
oslo.db==16.0.0
oslo.i18n==6.4.0
oslo.log==6.1.2
oslo.messaging==14.9.0
oslo.metrics==0.9.0
oslo.middleware==6.2.0
oslo.policy==4.4.0
oslo.serialization==5.5.0
oslo.service==3.5.0
oslo.utils==7.3.0
osprofiler==4.2.0
packaging==24.1
paramiko==3.4.1
Paste==3.10.1
PasteDeploy==3.1.0
pbr==6.1.0
pdf2image==1.17.0
pecan==1.5.1
pillow==10.4.0
ply==3.11
preshed==3.0.9
prettytable==3.11.0
prometheus_client==0.20.0
pycadf==3.1.1
pycparser==2.22
pydantic==2.9.1
pydantic_core==2.23.3
Pygments==2.18.0
PyJWT==2.9.0
PyMuPDF==1.24.10
PyMuPDFb==1.24.10
PyNaCl==1.5.0
pyparsing==3.1.4
pytesseract==0.3.13
python-dateutil==2.9.0.post0
python-docx==1.1.2
python-dotenv==1.0.1
python-keystoneclient==5.5.0
pytz==2024.2
PyYAML==6.0.2
referencing==0.35.1
repoze.lru==0.7
requests==2.32.3
rfc3986==2.0.0
rich==13.8.1
Routes==2.5.1
rpds-py==0.20.0
setuptools==74.1.2
shellingham==1.5.4
simplegeneric==0.8.1
six==1.16.0
smart-open==7.0.4
spacy==3.8.0
spacy-legacy==3.0.12
spacy-loggers==1.0.5
SQLAlchemy==2.0.34
srsly==2.4.8
statsd==4.0.1
stevedore==5.3.0
tenacity==9.0.0
testresources==2.0.1
testscenarios==0.5.0
testtools==2.7.2
thinc==8.2.5
tooz==6.3.0
tqdm==4.66.5
typer==0.12.5
typing_extensions==4.12.2
tzdata==2024.1
urllib3==2.2.3
vine==5.1.0
voluptuous==0.15.2
waitress==3.0.0
wasabi==1.1.3
wcwidth==0.2.13
weasel==0.4.1
WebOb==1.8.8
Werkzeug==3.0.4
wrapt==1.16.0
WSME==0.12.1
yappi==1.6.0
yaql==3.0.0
zipp==3.20.1BeckUp.pyAdded+177
app.pyAdded+115
app_error.logAdded+1
readmeAdded+146
requirements.txtAdded+148