resumeMagic / app.py
pktpaulie's picture
Update app.py
ba120b3 verified
raw
history blame
27.5 kB
# -*- coding: utf-8 -*-
import streamlit as st
import os
import pandas as pd
import matplotlib.pyplot as plt
from resume_generation_gemini_pro import generate_gemini
from similarity_score_refined import similarity_main
from pdf2image import convert_from_path, convert_from_bytes
from docx import Document
import subprocess
import shutil
import io
from io import BytesIO
import tempfile
from PIL import Image, ImageDraw, ImageFont
import PyPDF2
from docx2pdf import convert
import pdfplumber
import docx
import numpy as np
import pypandoc
# Create temporary directories
temp_dir = tempfile.mkdtemp()
import subprocess
# Helper function to save uploaded files temporarily and return their paths
def save_uploaded_file(content):
if hasattr(content, 'name'): # Check if it's a file-like object
file_path = os.path.join("/tmp", content.name)
with open(file_path, "wb") as f:
f.write(content.read())
else: # It's a string (the file contents)
file_path = os.path.join("/tmp", "temp_upload")
with open(file_path, "w") as f:
f.write(str(content))
return file_path
def save_docx_as_pdf0(input_path, output_path='output.pdf'):
if input_path.lower().endswith('.docx'):
try:
# Convert .docx to .pdf using pypandoc
pypandoc.convert_file(input_path, 'pdf', outputfile=output_path)
if not os.path.exists(output_path):
raise FileNotFoundError("Conversion failed; output PDF not found.")
except Exception as e:
st.error(f"Failed to convert DOCX to PDF: {str(e)}")
elif input_path.lower().endswith('.pdf'):
shutil.copy(input_path, output_path)
else:
raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
# def save_uploaded_file(uploaded_file):
# file_path = os.path.join("/tmp", uploaded_file.name)
# with open(file_path, "wb") as f:
# f.write(uploaded_file.getbuffer())
# return file_path
# Custom CSS for styling
st.markdown("""
<style>
.main {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
}
h1, h2 {
color: #4B7BE5;
text-align: center;
}
.stContainer {
# background-color: #000000;
display: flex;
justify-content: center;
align-items: center;
# max-width: 100%;
height: 30%;
width: 45%;
}
.logo-container {
# background-color: black;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
# max-width: 100%;
}
.logo-container img {
max-width: 60%;
height: 40%;
}
.stButton>button {
# background-color: #4B7BE5;
# color: white;
# font-size: 18px;
appearance: none;
background-color: transparent;
border: 0.125em solid #1A1A1A;
border-radius: 0.9375em;
box-sizing: border-box;
color: #3B3B3B;
cursor: pointer;
display: inline-block;
font-family: Roobert,-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
font-size: 16px;
font-weight: 600;
line-height: normal;
margin: 0;
min-height: 3.75em;
min-width: 0;
outline: none;
padding: 1em 2.3em;
text-align: center;
text-decoration: none;
transition: all 300ms cubic-bezier(.23, 1, 0.32, 1);
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
will-change: transform;
}
.stButton>button:hover {
color: #fff;
background-color: #1A1A1A;
box-shadow: rgba(0, 0, 0, 0.25) 0 8px 15px;
transform: translateY(-2px);
border: none !important;
}
/* From Uiverse.io by e-coders */
# .stButton>btn:disabled {
# pointer-events: none;
# }
.stButton>:active, focus {
box-shadow: none;
transform: translateY(0);
}
</style>
""", unsafe_allow_html=True)
# Add ResumeMagic Logo
# st.image("logo.jpeg", use_container_width=True)
st.markdown('<div class="logo-container"></div>', unsafe_allow_html=True)
st.image("logo.jpeg", width=80)
st.markdown('</div>', unsafe_allow_html=True)
# Title and Description
st.title("Resume Tailoring with Google Generative AI")
st.markdown("### Upload your resume and job description to check similarity and generate a tailored resume.")
# Two columns for file uploaders
col1, col2 = st.columns(2)
with col1:
uploaded_resume = st.file_uploader("Upload Current Resume (.docx or .pdf)", type=["docx", "pdf"], key="resume")
with col2:
uploaded_job_description = st.file_uploader("Upload Job Description (.docx or .pdf)", type=["docx", "pdf"], key="job_description")
def get_score(resume_path, job_description_path):
similarity_score = similarity_main(resume_path, job_description_path)
if isinstance(similarity_score, str) and '%' in similarity_score:
similarity_score = float(similarity_score.replace('%', ''))
# Display messages based on score range
if similarity_score < 50:
st.markdown('<p style="color: red; font-weight: bold;">Low chance, skills gap identified!</p>', unsafe_allow_html=True)
pie_colors = ['#FF4B4B', '#E5E5E5']
elif 50 <= similarity_score < 70:
st.markdown('<p style="color: red; font-weight: bold;">Good chance but you can improve further!</p>', unsafe_allow_html=True)
pie_colors = ['#FFC107', '#E5E5E5']
else:
st.markdown('<p style="color: green; font-weight: bold;">Excellent! You can submit your CV.</p>', unsafe_allow_html=True)
pie_colors = ['#4CAF50', '#E5E5E5']
return similarity_score, pie_colors
def display_score(similarity, colors):
# Display Score as a Pie Chart
st.markdown(f"### Resume - Job Match: {int(similarity_score)}%")
# Pie chart to show similarity
fig, ax = plt.subplots()
# ax.pie([similarity_score, 100 - similarity_score], labels=['Match', 'Difference'], autopct='%1.1f%%', startangle=140, colors=['#4B7BE5', '#E5E5E5'])
ax.pie([similarity_score, 100 - similarity_score], labels=['Match', 'Difference'], autopct='%1.1f%%', startangle=140, colors=pie_colors)
ax.axis('equal')
st.pyplot(fig)
def save_file(file_name):
if hasattr(uploaded_file, 'name'):
file_path = os.path.join("/tmp", uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.read())
else:
file_path = os.path.join("/tmp", "temp_upload")
with open(file_path, "w") as f:
f.write(uploaded_file)
return file_path
import tempfile
# Function to save a file from BytesIO to a temporary file
def save_bytes_to_tempfile(bytes_data, suffix):
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
temp_file.write(bytes_data)
return temp_file.name
def save_bytes_as_pdf(docx_bytes, output_path='output.pdf'):
# Create a temporary directory
with tempfile.TemporaryDirectory() as tmp_dir:
# Write the DOCX bytes to a temporary file
temp_file = os.path.join(tmp_dir, 'temp.docx')
with open(temp_file, 'wb') as f:
f.write(docx_bytes)
# Convert the temporary DOCX to PDF
pdf_path = os.path.join(tmp_dir, 'output.pdf')
convert(temp_file, pdf_path)
# Copy the PDF to the desired output location
with open(output_path, 'wb') as f:
with open(pdf_path, 'rb') as src_f:
f.write(src_f.read())
# Clean up the temporary directory
os.remove(output_path)
def save_docx_as_pdf(input_path, output_path='output.pdf'):
if input_path.lower().endswith('.docx'):
try:
# Convert .docx to .pdf using LibreOffice
subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', input_path, '--outdir', os.path.dirname(output_path)], check=True)
if not os.path.exists(output_path):
raise FileNotFoundError("Conversion failed; output PDF not found.")
except (FileNotFoundError, subprocess.CalledProcessError):
st.error("Failed to convert DOCX to PDF. Please check LibreOffice installation.")
elif input_path.lower().endswith('.pdf'):
shutil.copy(input_path, output_path)
else:
raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
# def save_docx_as_pdf(input_path, output_path='output.pdf'):
# if input_path.lower().endswith('.docx'):
# try:
# # Attempt to use unoconv
# subprocess.run(['unoconv', '-o', output_path, input_path])
# if not os.path.exists(output_path):
# raise FileNotFoundError("unoconv failed to convert DOCX to PDF")
# except FileNotFoundError:
# # Fallback to text-based display
# with open(output_path, 'w') as f:
# f.write(input_path.read())
# elif input_path.lower().endswith('.pdf'):
# shutil.copy(input_path, output_path)
# else:
# raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
def save_docx_as_pdf6(input_path, output_path='output.pdf'):
if input_path.lower().endswith('.docx'):
try:
# Convert .docx to .pdf using docx2pdf
convert(input_path, output_path)
if not os.path.exists(output_path):
raise FileNotFoundError("Conversion failed; output PDF not found.")
except Exception as e:
st.error(f"Failed to convert DOCX to PDF: {str(e)}")
elif input_path.lower().endswith('.pdf'):
shutil.copy(input_path, output_path)
else:
raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
# Install docx2pdf if not already installed
# try:
# import docx2pdf
# except ImportError:
# !pip install docx2pdf
# def save_docx_as_pdf(input_path, output_path='output.pdf'):
# if input_path.lower().endswith('.docx'):
# from docx2pdf import convert
# convert(input_path, output_path)
# elif input_path.lower().endswith('.pdf'):
# shutil.copy(input_path, output_path)
# else:
# raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
def save_docx_as_pdf2(doc_content, output_path='output.pdf'):
# Save document content as a .docx file
temp_doc_path = 'temp.docx'
doc = Document()
doc.add_paragraph(doc_content)
doc.save(temp_doc_path)
# Convert .docx to PDF
from docx2pdf import convert
convert(temp_doc_path, output_path)
os.remove(temp_doc_path)
# subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', temp_doc_path, '--outdir', os.path.dirname(output_path)])
def save_docx_as_pdf1(input_path, output_path='output.pdf'):
if input_path.lower().endswith('.docx'):
from docx2pdf import convert
convert(input_path, output_path)
elif input_path.lower().endswith('.pdf'):
shutil.copy(input_path, output_path)
else:
raise ValueError("Unsupported file format. Please upload a .docx or .pdf file.")
# if uploaded_resume.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
# save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
# display_doc_as_image('uploaded_resume.pdf')
def display_pdf_page1(pdf_path):
try:
# Open PDF file
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
# Extract text from the first page
page = reader.pages[0]
x_object = page.extract_text()
# Convert text to image (using PIL)
img = Image.new('RGB', (800, 1000))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 20)
# Draw text on the image
draw.text((10, 10), x_object[:500], fill=(255, 255, 255), font=font)
# Display the image
display(img)
except Exception as e:
st.error(f"Failed to display image: {str(e)}")
def display_pdf_pages_as_images(pdf_path):
try:
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages):
st.markdown(f"### Page {i + 1}")
# Convert the page to an image
image = page.to_image()
# Render the image using Streamlit
# st.image(image.original, use_column_width=True)
st.image(image.original, use_container_width=False)
except Exception as e:
st.error(f"Failed to display PDF as image: {str(e)}")
def display_pdf_page(pdf_path, page_number=1):
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
page = reader.pages[page_number]
x_object = page.extract_text()
# Convert text to image (using PIL)
img = Image.new('RGB', (800, 1000))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 20)
# Draw text on the image
draw.text((10, 10), x_object[:500], fill=(255, 255, 255), font=font)
# Display the image
display(img)
def display_doc_as_image2(pdf_path):
# try:
# images = convert_from_path(pdf_path, size=800)
# display(Image(filename=images[0].filename))
# except Exception as e:
# st.error(f"Failed to display image: {str(e)}")
iframe_code = f"""
<iframe src="{pdf_path}" width="100%" height="600px"></iframe>
"""
st.markdown(iframe_code, unsafe_allow_html=True)
# try:
# # Convert PDF to images
# images = []
# for i in range(1): # Assuming only one page
# command = [
# "pdf2image", "-j", "--png",
# "-f", str(i+1),
# "-l", str(i+1),
# pdf_path
# ]
# output = subprocess.check_output(command)
# # Decode the base64 image data
# img_data = base64.b64decode(output.split()[0])
# img = Image.open(BytesIO(img_data))
# images.append(img)
# # Display the first page
# display(images[0])
# except Exception as e:
# st.error(f"Failed to display image: {str(e)}")
def display_doc_as_image3(pdf_path):
try:
# Convert PDF to images
images = []
for i in range(1): # Assuming only one page
img = Image.open(BytesIO(open(pdf_path, 'rb').read()))
images.append(img)
# Display the first page
display(images[0])
except Exception as e:
st.error(f"Failed to display image: {str(e)}")
# try:
# images = convert_from_path(pdf_path, size=800)
# display(Image.fromarray(images[0]))
# except Exception as e:
# st.error(f"Failed to display image: {str(e)}")
def display_doc_as_image1(pdf_url):
st.components.v1.iframe(pdf_url, width=800, height=600)
# try:
# img = Image.open(pdf_path)
# st.image(img)
# except Exception as e:
# st.error(f"Failed to display image: {str(e)}")
def display_doc_as_image(file_path):
"""Display document with multiple fallback options"""
try:
# First attempt: Use pdf2image with explicit poppler path
try:
from pdf2image import convert_from_path
# Try common poppler paths on Linux
poppler_paths = [
'/usr/bin/pdftoppm', # Common Linux path
'/opt/homebrew/bin/pdftoppm', # Mac Homebrew path
'poppler-utils' # Package name
]
for poppler_path in poppler_paths:
try:
images = convert_from_path(
file_path,
size=800,
poppler_path=os.path.dirname(poppler_path) if os.path.isfile(poppler_path) else None
)
st.image(images[0])
return
except Exception:
continue
# If no poppler path worked, try without specifying path
images = convert_from_path(file_path, size=800)
st.image(images[0])
except Exception as e:
# Second attempt: Use Streamlit's native PDF display
try:
with open(file_path, "rb") as pdf_file:
pdf_bytes = pdf_file.read()
st.pdf(pdf_bytes)
return
except Exception as pdf_error:
st.error(f"Error displaying PDF: {str(pdf_error)}")
# Third attempt: If it's a DOCX file, try to extract text
if file_path.lower().endswith('.docx'):
try:
doc = Document(file_path)
text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
st.text(text)
return
except Exception as docx_error:
st.error(f"Error displaying DOCX: {str(docx_error)}")
except Exception as main_error:
st.error(f"Error in display function: {str(main_error)}")
st.info("Displaying file path for debugging: " + file_path)
# Last resort: Try to read and display raw bytes
try:
with open(file_path, 'rb') as f:
bytes_data = f.read()
if file_path.lower().endswith('.pdf'):
st.pdf(bytes_data)
else:
st.text("File content preview not available")
except Exception as final_error:
st.error(f"Final fallback failed: {str(final_error)}")
def display_doc_as_image5(pdf_path):
poppler_path = 'usr/bin'
images = convert_from_path(pdf_path, poppler_path=poppler_path)
for img in images:
buf = BytesIO()
img.save(buf, format="PNG")
st.image(buf)
# from IPython.display import display, Image
# images = convert_from_bytes(open(pdf_path, 'rb').read(), size=800)
# display(images[0])
# Process if files are uploaded
if uploaded_resume and uploaded_job_description:
# Save files
resume_path = save_uploaded_file(uploaded_resume)
job_description_path = save_uploaded_file(uploaded_job_description)
# Similarity Score Section
st.markdown("---")
# st.subheader("Check Job Match")
if st.button("Resume-JD Matching"):
with st.spinner("Computing Match"):
similarity_score, pie_colors = get_score(resume_path, job_description_path)
display_score(similarity_score, pie_colors)
#Autoscroll
st.markdown("""
<script>
window.scrollTo(0, document.body.scrollHeight);
</script>
""", unsafe_allow_html=True)
# Generate Tailored Resume Section
st.markdown("---")
# st.subheader("Tailor Resume")
# iframe_code = f"""
# <iframe src="{resume_path}" width="100%" height="600px"></iframe>
# """
# st.markdown(iframe_code, unsafe_allow_html=True)
# st.components.v1.iframe(resume_path, width=800, height=600)
if st.button("Tailor Resume"):
with st.spinner("Generating resume..."):
generated_resume, new_resume_path = generate_gemini(resume_path, job_description_path)
# resume_path = save_uploaded_file(generated_resume)
# st.markdown("Generated Tailored Resume:")
# st.write(generated_resume)
#Autoscroll
st.markdown("""
<script>
window.scrollTo(0, document.body.scrollHeight);
</script>
""", unsafe_allow_html=True)
# with col1:
# st.markdown("### Uploaded Resume:")
# # if resume_path.endswith('.docx'):
# # save_docx_as_pdf(uploaded_resume.getvalue().decode('utf-8'), 'uploaded_resume.pdf')
# # if uploaded_resume.type == "application/pdf":
# # display_doc_as_image(resume_path)
# # else:
# # save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
# display_doc_as_image(uploaded_resume)
# with st.spinner("Computing Match"):
# similarity_score, pie_colors = get_score(resume_path, job_description_path)
# display_score(similarity_score, pie_colors)
if generated_resume is not None:
doc = Document()
doc.add_paragraph(generated_resume) # Add the generated content to the document
# Save the generated document as a .docx file in memory
resume_bytes = BytesIO()
doc.save(resume_bytes)
resume_bytes.seek(0)
# Save the .docx to a temporary file
gen_docx_path = save_bytes_to_tempfile(resume_bytes.getvalue(), 'docx')
# Convert the generated .docx to a .pdf
# gen_pdf_path = '/tmp/tailored_resume.pdf'
gen_pdf_path = save_uploaded_file(gen_docx_path)
save_docx_as_pdf0(gen_docx_path, gen_pdf_path)
# save_bytes_as_pdf(gen_docx_path, gen_pdf_path)
# save_docx_as_pdf2(gen_docx_path, gen_pdf_path)
# Display uploaded and generated resumes side-by-side
col1, col2 = st.columns(2)
with col1:
st.markdown("### Uploaded Resume:")
save_docx_as_pdf(resume_path, '/tmp/uploaded_resume.pdf')
# display_doc_as_image1('/tmp/uploaded_resume.pdf')
display_pdf_pages_as_images(resume_path)
with col2:
st.markdown("### Tailored Resume:")
# display_doc_as_image2(gen_pdf_path)
display_pdf_pages_as_images(gen_pdf_path)
st.download_button(
label="Generated Resume (PDF)",
data=open(gen_pdf_path, 'rb').read(),
file_name="generated_resume.pdf",
mime="application/pdf"
)
st.download_button(
label="Generated Resume (Word)",
data=resume_bytes,
file_name="tailored_resume.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
# from io import BytesIO
# doc = Document()
# with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as temp_doc:
# temp_doc_path = temp_doc.name
# doc.save(temp_doc_path)
# Convert the DOCX to PDF and display
# pdf_path = '/tmp/generated_resume.pdf'
# save_docx_as_pdf(temp_doc_path, pdf_path)
# # Now pass the path to any function that expects a file path
# pdf_path = '/tmp/generated_resume.pdf'
# save_bytes_as_pdf(temp_doc_path, pdf_path)
# # Display the generated PDF or handle further processing
# display_doc_as_image(pdf_path)
# with tempfile.NamedTemporaryFile(suffix='.docx') as temp_doc:
# doc = Document()
# doc.add_paragraph(generated_resume)
# # doc.save(temp_doc.name)
# # resume_bytes = BytesIO()
# # doc.save(resume_bytes)
# # resume_bytes.seek(0)
# # Convert DOCX to PDF
# # pdf_path = f"temp_{os.path.basename(temp_doc.name)}.pdf"
# # convert(temp_doc.name, pdf_path)
# # save_docx_as_pdf(temp_doc.name, pdf_path)
# pdf_path = '/tmp/generated_resume.docx'
# doc.save(pdf_path)
# # pdf_path = save_uploaded_file(resume_bytes)
# save_docx_as_pdf(generated_resume, pdf_path)
# # Display resumes side by side
# col1, col2 = st.columns(2)
# with col1:
# st.write("Uploaded Resume:")
# if resume_path:
# display_doc_as_image(resume_path)
# else:
# st.warning("No resume file found")
# with col2:
# st.write("Generated Resume:")
# if pdf_path:
# display_doc_as_image(pdf_path)
# else:
# st.warning("No generated resume file found")
# Allow users to download both PDFs
# st.download_button(
# label="Download Uploaded Resume",
# data=resume_bytes,
# file_name="uploaded_resume.pdf",
# mime="application/pdf"
# )
# doc = Document()
# doc.add_paragraph(generated_resume)
# resume_bytes = BytesIO()
# doc.save(resume_bytes)
# resume_bytes.seek(0)
# gen_resume_path = save_uploaded_file(resume_bytes)
# # uploaded_resume_path = save_uploaded_file(resume)
# col1, col2 = st.columns(2)
# with col1:
# save_docx_as_pdf(resume_path, 'uploaded_resume.pdf')
# display_doc_as_image('uploaded_resume.pdf')
# with col2:
# st.markdown("### Tailored Resume:")
# save_docx_as_pdf(gen_resume_path, 'tailored_resume.pdf')
# display_doc_as_image('tailored_resume.pdf')
# st.download_button(
# label="Download Resume",
# data=resume_bytes,
# file_name="tailored_resume.docx",
# mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
# )
else:
st.warning("Please upload both the resume and job description files.")