Update app.py
Browse files
app.py
CHANGED
|
@@ -31,16 +31,17 @@ def initialize_ner_pipeline():
|
|
| 31 |
# Step 3: Load custom resume generation model (.h5 file)
|
| 32 |
def load_resume_model():
|
| 33 |
try:
|
| 34 |
-
return load_model("
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error loading resume generation model: {e}")
|
| 37 |
return None
|
| 38 |
|
| 39 |
# Function to extract text from a PDF file
|
| 40 |
-
def extract_text_from_pdf(
|
| 41 |
text = ""
|
| 42 |
try:
|
| 43 |
-
|
|
|
|
| 44 |
for page_num in range(len(pdf_document)):
|
| 45 |
page = pdf_document.load_page(page_num)
|
| 46 |
text += page.get_text()
|
|
@@ -49,83 +50,66 @@ def extract_text_from_pdf(pdf_file_path):
|
|
| 49 |
return text
|
| 50 |
|
| 51 |
# Function to extract text from a Word document
|
| 52 |
-
def extract_text_from_word(
|
| 53 |
text = ""
|
| 54 |
try:
|
| 55 |
-
|
|
|
|
| 56 |
for paragraph in doc.paragraphs:
|
| 57 |
text += paragraph.text + "\n"
|
| 58 |
except Exception as e:
|
| 59 |
return f"Error extracting text from Word document: {e}"
|
| 60 |
return text
|
| 61 |
|
| 62 |
-
# Function to
|
| 63 |
-
def
|
| 64 |
-
cleaned_entities = []
|
| 65 |
-
for entity in ner_results:
|
| 66 |
-
entity_text = entity['word']
|
| 67 |
-
entity_type = entity['entity']
|
| 68 |
-
|
| 69 |
-
# Filter out short or irrelevant tokens (e.g., single characters, punctuation)
|
| 70 |
-
if len(entity_text) > 2 and entity_text.isalpha(): # Only keep meaningful words
|
| 71 |
-
cleaned_entities.append((entity_text, entity_type))
|
| 72 |
-
|
| 73 |
-
return cleaned_entities
|
| 74 |
-
|
| 75 |
-
# Function to use the custom .h5 resume model for enhancements
|
| 76 |
-
def generate_resume_with_model(resume_text, job_title, resume_model):
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
return enhanced_resume[0]
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
-
return "Error generating enhanced resume using the custom model."
|
| 86 |
|
| 87 |
-
# Function to
|
| 88 |
-
def
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if entity_type == "I-ORG":
|
| 97 |
-
enhanced_text += f"Consider emphasizing your experience at {entity_text}.\n"
|
| 98 |
-
|
| 99 |
-
llama_input = f"Enhance this resume for the job title: {job_title}. Current content: {model_enhanced_resume}"
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
llama_response = client.text_generation(prompt=llama_input, max_new_tokens=1000)
|
| 103 |
-
print("LLaMA Response:", llama_response) # Debug output
|
| 104 |
-
llama_enhanced = llama_response.get("generated_text", "No suggestions generated.")
|
| 105 |
-
except Exception as llama_error:
|
| 106 |
-
print(f"Error in LLaMA text generation: {llama_error}")
|
| 107 |
-
llama_enhanced = "Error generating suggestions from LLaMA model. Please try again later."
|
| 108 |
-
|
| 109 |
-
return enhanced_text + "\nRefined Suggestions:\n" + llama_enhanced
|
| 110 |
-
|
| 111 |
except Exception as e:
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
# Function to process the resume and job title inputs
|
| 116 |
def process_resume(file, job_title, client, resume_model):
|
| 117 |
try:
|
| 118 |
file_name = file.name
|
|
|
|
|
|
|
| 119 |
if file_name.endswith(".pdf"):
|
| 120 |
-
|
|
|
|
| 121 |
elif file_name.endswith(".docx"):
|
| 122 |
-
|
|
|
|
| 123 |
else:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
return f"Error processing resume: {e}"
|
| 131 |
|
|
@@ -141,11 +125,10 @@ interface = gr.Interface(
|
|
| 141 |
gr.File(label="Upload your resume (Word or PDF)"),
|
| 142 |
gr.Textbox(lines=1, placeholder="Enter the job title...", label="Job Title"),
|
| 143 |
],
|
| 144 |
-
outputs=gr.Textbox(label="
|
| 145 |
title="Resume Enhancement Tool",
|
| 146 |
description="Upload your resume and specify a job title to optimize your resume for that position."
|
| 147 |
)
|
| 148 |
|
| 149 |
-
|
| 150 |
# Launch the Gradio app
|
| 151 |
interface.launch(share=True)
|
|
|
|
| 31 |
# Step 3: Load custom resume generation model (.h5 file)
|
| 32 |
def load_resume_model():
|
| 33 |
try:
|
| 34 |
+
return load_model("resume_generator_model.h5")
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error loading resume generation model: {e}")
|
| 37 |
return None
|
| 38 |
|
| 39 |
# Function to extract text from a PDF file
|
| 40 |
+
def extract_text_from_pdf(pdf_binary_content):
|
| 41 |
text = ""
|
| 42 |
try:
|
| 43 |
+
# Open the PDF from binary content
|
| 44 |
+
pdf_document = fitz.open(stream=pdf_binary_content, filetype="pdf")
|
| 45 |
for page_num in range(len(pdf_document)):
|
| 46 |
page = pdf_document.load_page(page_num)
|
| 47 |
text += page.get_text()
|
|
|
|
| 50 |
return text
|
| 51 |
|
| 52 |
# Function to extract text from a Word document
|
| 53 |
+
def extract_text_from_word(docx_binary_content):
|
| 54 |
text = ""
|
| 55 |
try:
|
| 56 |
+
# Open the DOCX file from binary content
|
| 57 |
+
doc = Document(docx_binary_content)
|
| 58 |
for paragraph in doc.paragraphs:
|
| 59 |
text += paragraph.text + "\n"
|
| 60 |
except Exception as e:
|
| 61 |
return f"Error extracting text from Word document: {e}"
|
| 62 |
return text
|
| 63 |
|
| 64 |
+
# Function to optimize resume based on job title using the .h5 model
|
| 65 |
+
def enhance_with_custom_model(resume_text, resume_model):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
+
# Assuming the .h5 model takes text input and generates enhancement suggestions
|
| 68 |
+
predictions = resume_model.predict([resume_text])
|
| 69 |
+
enhancements = " ".join(predictions) # Convert predictions to text (adjust as needed)
|
| 70 |
+
return enhancements
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
+
return f"Error using the custom .h5 model: {e}"
|
|
|
|
| 73 |
|
| 74 |
+
# Function to optimize resume based on job title
|
| 75 |
+
def optimize_resume(resume_text, job_title, client):
|
| 76 |
+
prompt = f"Optimize the following resume for the job title '{job_title}':\n\n{resume_text}"
|
| 77 |
+
responses = []
|
| 78 |
try:
|
| 79 |
+
for message in client.chat_completion(
|
| 80 |
+
messages=[{"role": "user", "content": prompt}],
|
| 81 |
+
max_tokens=1000,
|
| 82 |
+
stream=True,
|
| 83 |
+
):
|
| 84 |
+
responses.append(message.choices[0].delta.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
return f"Error during model inference: {e}"
|
| 87 |
+
|
| 88 |
+
return ''.join(responses)
|
| 89 |
|
| 90 |
# Function to process the resume and job title inputs
|
| 91 |
def process_resume(file, job_title, client, resume_model):
|
| 92 |
try:
|
| 93 |
file_name = file.name
|
| 94 |
+
file_content = file.read()
|
| 95 |
+
|
| 96 |
if file_name.endswith(".pdf"):
|
| 97 |
+
# Extract text if the file is a PDF
|
| 98 |
+
resume_text = extract_text_from_pdf(file_content)
|
| 99 |
elif file_name.endswith(".docx"):
|
| 100 |
+
# Extract text if the file is a Word document
|
| 101 |
+
resume_text = extract_text_from_word(file_content)
|
| 102 |
else:
|
| 103 |
+
# Assume the file is a text file and read it directly
|
| 104 |
+
resume_text = file_content.decode("utf-8")
|
| 105 |
+
|
| 106 |
+
# Step 1: Use the custom .h5 model to enhance the resume
|
| 107 |
+
enhanced_resume = enhance_with_custom_model(resume_text, resume_model)
|
| 108 |
|
| 109 |
+
# Step 2: Optimize the enhanced resume using the LLaMA model
|
| 110 |
+
optimized_resume = optimize_resume(enhanced_resume, job_title, client)
|
| 111 |
+
|
| 112 |
+
return optimized_resume
|
| 113 |
except Exception as e:
|
| 114 |
return f"Error processing resume: {e}"
|
| 115 |
|
|
|
|
| 125 |
gr.File(label="Upload your resume (Word or PDF)"),
|
| 126 |
gr.Textbox(lines=1, placeholder="Enter the job title...", label="Job Title"),
|
| 127 |
],
|
| 128 |
+
outputs=gr.Textbox(label="Optimized Resume", lines=20),
|
| 129 |
title="Resume Enhancement Tool",
|
| 130 |
description="Upload your resume and specify a job title to optimize your resume for that position."
|
| 131 |
)
|
| 132 |
|
|
|
|
| 133 |
# Launch the Gradio app
|
| 134 |
interface.launch(share=True)
|