Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,21 +5,19 @@ import io
|
|
| 5 |
from docx import Document
|
| 6 |
import os
|
| 7 |
import pymupdf # Corrected import for PyMuPDF
|
| 8 |
-
|
| 9 |
# For PDF generation
|
| 10 |
from reportlab.pdfgen import canvas
|
| 11 |
from reportlab.lib.pagesizes import letter
|
| 12 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 13 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
|
| 14 |
|
| 15 |
# Initialize Hugging Face Inference Client with Meta-Llama-3.1-8B-Instruct
|
| 16 |
client = InferenceClient(
|
| 17 |
-
model="meta-llama/Meta-Llama-3
|
| 18 |
-
token=os.getenv("HF_TOKEN")
|
| 19 |
-
)
|
| 20 |
|
| 21 |
# Function to extract text from PDF
|
| 22 |
-
|
| 23 |
def extract_text_from_pdf(pdf_file):
|
| 24 |
try:
|
| 25 |
pdf_document = pymupdf.open(pdf_file)
|
|
@@ -41,11 +39,9 @@ def extract_text_from_docx(docx_file):
|
|
| 41 |
def parse_cv(file, job_description):
|
| 42 |
if file is None:
|
| 43 |
return "Please upload a CV file.", ""
|
| 44 |
-
|
| 45 |
try:
|
| 46 |
file_path = file.name
|
| 47 |
file_ext = os.path.splitext(file_path)[1].lower()
|
| 48 |
-
|
| 49 |
if file_ext == ".pdf":
|
| 50 |
extracted_text = extract_text_from_pdf(file_path)
|
| 51 |
elif file_ext == ".docx":
|
|
@@ -54,17 +50,14 @@ def parse_cv(file, job_description):
|
|
| 54 |
return "Unsupported file format. Please upload a PDF or DOCX file.", ""
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error reading file: {e}", ""
|
| 57 |
-
|
| 58 |
if extracted_text.startswith("Error"):
|
| 59 |
return extracted_text, "Error during text extraction. Please check the file."
|
| 60 |
-
|
| 61 |
prompt = (
|
| 62 |
f"Analyze the CV against the job description. Provide a summary, assessment, "
|
| 63 |
f"and a score 0-10.\n\n"
|
| 64 |
f"Job Description:\n{job_description}\n\n"
|
| 65 |
f"Candidate CV:\n{extracted_text}\n"
|
| 66 |
)
|
| 67 |
-
|
| 68 |
try:
|
| 69 |
analysis = client.text_generation(prompt, max_new_tokens=512)
|
| 70 |
return extracted_text, f"--- Analysis Report ---\n{analysis}"
|
|
@@ -75,11 +68,64 @@ def parse_cv(file, job_description):
|
|
| 75 |
def toggle_download_button(analysis_report):
|
| 76 |
return gr.update(interactive=bool(analysis_report.strip()), visible=bool(analysis_report.strip()))
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# Build the Gradio UI
|
| 79 |
demo = gr.Blocks()
|
| 80 |
with demo:
|
| 81 |
gr.Markdown("## AI-powered CV Analyzer, Optimizer, and Chatbot")
|
| 82 |
-
|
| 83 |
with gr.Tab("Chatbot"):
|
| 84 |
chat_interface = gr.ChatInterface(
|
| 85 |
lambda message, history: client.chat_completion(
|
|
@@ -89,7 +135,6 @@ with demo:
|
|
| 89 |
chatbot=gr.Chatbot(label="Chatbot", type="messages"),
|
| 90 |
textbox=gr.Textbox(placeholder="Enter your message here...", label="Message"),
|
| 91 |
)
|
| 92 |
-
|
| 93 |
with gr.Tab("CV Analyzer"):
|
| 94 |
gr.Markdown("### Upload your CV and provide the job description")
|
| 95 |
file_input = gr.File(label="Upload CV", file_types=[".pdf", ".docx"])
|
|
@@ -99,19 +144,18 @@ with demo:
|
|
| 99 |
download_pdf_button = gr.Button("Download Analysis as PDF", visible=False, interactive=False)
|
| 100 |
pdf_file = gr.File(label="Download PDF", interactive=False)
|
| 101 |
analyze_button = gr.Button("Analyze CV")
|
| 102 |
-
|
| 103 |
analyze_button.click(parse_cv, [file_input, job_desc_input], [extracted_text, analysis_output])
|
| 104 |
analyze_button.click(toggle_download_button, [analysis_output], [download_pdf_button])
|
| 105 |
download_pdf_button.click(create_pdf_report, [analysis_output], [pdf_file])
|
| 106 |
-
|
| 107 |
with gr.Tab("CV Optimizer"):
|
| 108 |
gr.Markdown("### Upload your Resume and Enter Job Title")
|
| 109 |
-
resume_file = gr.File(label="Upload Resume (PDF or Word)")
|
| 110 |
job_title_input = gr.Textbox(label="Job Title", lines=1)
|
| 111 |
optimized_resume_output = gr.Textbox(label="Optimized Resume", lines=20)
|
| 112 |
optimize_button = gr.Button("Optimize Resume")
|
| 113 |
-
|
| 114 |
optimize_button.click(process_resume, [resume_file, job_title_input], [optimized_resume_output])
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
-
demo.queue().launch()
|
|
|
|
| 5 |
from docx import Document
|
| 6 |
import os
|
| 7 |
import pymupdf # Corrected import for PyMuPDF
|
|
|
|
| 8 |
# For PDF generation
|
| 9 |
from reportlab.pdfgen import canvas
|
| 10 |
from reportlab.lib.pagesizes import letter
|
| 11 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 12 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 13 |
+
from reportlab.lib import colors
|
| 14 |
|
| 15 |
# Initialize Hugging Face Inference Client with Meta-Llama-3.1-8B-Instruct
|
| 16 |
client = InferenceClient(
|
| 17 |
+
model="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 18 |
+
token=os.getenv("HF_TOKEN"))
|
|
|
|
| 19 |
|
| 20 |
# Function to extract text from PDF
|
|
|
|
| 21 |
def extract_text_from_pdf(pdf_file):
|
| 22 |
try:
|
| 23 |
pdf_document = pymupdf.open(pdf_file)
|
|
|
|
| 39 |
def parse_cv(file, job_description):
|
| 40 |
if file is None:
|
| 41 |
return "Please upload a CV file.", ""
|
|
|
|
| 42 |
try:
|
| 43 |
file_path = file.name
|
| 44 |
file_ext = os.path.splitext(file_path)[1].lower()
|
|
|
|
| 45 |
if file_ext == ".pdf":
|
| 46 |
extracted_text = extract_text_from_pdf(file_path)
|
| 47 |
elif file_ext == ".docx":
|
|
|
|
| 50 |
return "Unsupported file format. Please upload a PDF or DOCX file.", ""
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error reading file: {e}", ""
|
|
|
|
| 53 |
if extracted_text.startswith("Error"):
|
| 54 |
return extracted_text, "Error during text extraction. Please check the file."
|
|
|
|
| 55 |
prompt = (
|
| 56 |
f"Analyze the CV against the job description. Provide a summary, assessment, "
|
| 57 |
f"and a score 0-10.\n\n"
|
| 58 |
f"Job Description:\n{job_description}\n\n"
|
| 59 |
f"Candidate CV:\n{extracted_text}\n"
|
| 60 |
)
|
|
|
|
| 61 |
try:
|
| 62 |
analysis = client.text_generation(prompt, max_new_tokens=512)
|
| 63 |
return extracted_text, f"--- Analysis Report ---\n{analysis}"
|
|
|
|
| 68 |
def toggle_download_button(analysis_report):
|
| 69 |
return gr.update(interactive=bool(analysis_report.strip()), visible=bool(analysis_report.strip()))
|
| 70 |
|
| 71 |
+
# Function to create PDF report
|
| 72 |
+
def create_pdf_report(report_text):
|
| 73 |
+
if not report_text.strip():
|
| 74 |
+
report_text = "No analysis report to convert."
|
| 75 |
+
|
| 76 |
+
pdf_buffer = io.BytesIO()
|
| 77 |
+
doc = SimpleDocTemplate(pdf_buffer, pagesize=letter)
|
| 78 |
+
styles = getSampleStyleSheet()
|
| 79 |
+
Story = []
|
| 80 |
+
|
| 81 |
+
title = Paragraph("<b>Analysis Report</b>", styles['Title'])
|
| 82 |
+
Story.append(title)
|
| 83 |
+
Story.append(Spacer(1, 12))
|
| 84 |
+
|
| 85 |
+
report_paragraph = Paragraph(report_text.replace("\n", "<br/>"), styles['BodyText'])
|
| 86 |
+
Story.append(report_paragraph)
|
| 87 |
+
|
| 88 |
+
doc.build(Story)
|
| 89 |
+
pdf_buffer.seek(0)
|
| 90 |
+
return (pdf_buffer.getvalue(), "analysis_report.pdf")
|
| 91 |
+
|
| 92 |
+
def process_resume(resume_file, job_title):
|
| 93 |
+
"""
|
| 94 |
+
Processes the uploaded resume, optimizes it for the given job title using the LLM,
|
| 95 |
+
and returns the optimized resume content.
|
| 96 |
+
"""
|
| 97 |
+
if resume_file is None:
|
| 98 |
+
return "Please upload a resume file."
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
file_path = resume_file.name
|
| 102 |
+
file_ext = os.path.splitext(file_path)[1].lower()
|
| 103 |
+
|
| 104 |
+
if file_ext == ".pdf":
|
| 105 |
+
resume_text = extract_text_from_pdf(file_path)
|
| 106 |
+
elif file_ext == ".docx":
|
| 107 |
+
resume_text = extract_text_from_docx(file_path)
|
| 108 |
+
else:
|
| 109 |
+
return "Unsupported file format. Please upload a PDF or DOCX file."
|
| 110 |
+
|
| 111 |
+
if resume_text.startswith("Error"):
|
| 112 |
+
return resume_text
|
| 113 |
+
|
| 114 |
+
prompt = (
|
| 115 |
+
f"Optimize the following resume for the job title: {job_title}.\n"
|
| 116 |
+
f"Include relevant skills, experience, and keywords related to the job title.\n\n"
|
| 117 |
+
f"Resume:\n{resume_text}\n"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
optimized_resume = client.text_generation(prompt, max_new_tokens=1024)
|
| 121 |
+
return optimized_resume
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return f"Error processing resume: {e}"
|
| 125 |
# Build the Gradio UI
|
| 126 |
demo = gr.Blocks()
|
| 127 |
with demo:
|
| 128 |
gr.Markdown("## AI-powered CV Analyzer, Optimizer, and Chatbot")
|
|
|
|
| 129 |
with gr.Tab("Chatbot"):
|
| 130 |
chat_interface = gr.ChatInterface(
|
| 131 |
lambda message, history: client.chat_completion(
|
|
|
|
| 135 |
chatbot=gr.Chatbot(label="Chatbot", type="messages"),
|
| 136 |
textbox=gr.Textbox(placeholder="Enter your message here...", label="Message"),
|
| 137 |
)
|
|
|
|
| 138 |
with gr.Tab("CV Analyzer"):
|
| 139 |
gr.Markdown("### Upload your CV and provide the job description")
|
| 140 |
file_input = gr.File(label="Upload CV", file_types=[".pdf", ".docx"])
|
|
|
|
| 144 |
download_pdf_button = gr.Button("Download Analysis as PDF", visible=False, interactive=False)
|
| 145 |
pdf_file = gr.File(label="Download PDF", interactive=False)
|
| 146 |
analyze_button = gr.Button("Analyze CV")
|
| 147 |
+
|
| 148 |
analyze_button.click(parse_cv, [file_input, job_desc_input], [extracted_text, analysis_output])
|
| 149 |
analyze_button.click(toggle_download_button, [analysis_output], [download_pdf_button])
|
| 150 |
download_pdf_button.click(create_pdf_report, [analysis_output], [pdf_file])
|
|
|
|
| 151 |
with gr.Tab("CV Optimizer"):
|
| 152 |
gr.Markdown("### Upload your Resume and Enter Job Title")
|
| 153 |
+
resume_file = gr.File(label="Upload Resume (PDF or Word)", file_types=[".pdf", ".docx"])
|
| 154 |
job_title_input = gr.Textbox(label="Job Title", lines=1)
|
| 155 |
optimized_resume_output = gr.Textbox(label="Optimized Resume", lines=20)
|
| 156 |
optimize_button = gr.Button("Optimize Resume")
|
| 157 |
+
|
| 158 |
optimize_button.click(process_resume, [resume_file, job_title_input], [optimized_resume_output])
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
+
demo.queue().launch()
|