Spaces:
Build error
Build error
import streamlit as st | |
import os | |
from dotenv import load_dotenv | |
from groq import Groq | |
import PyPDF2 | |
# Load environment variables | |
load_dotenv() | |
# Initialize Groq client | |
client = Groq( | |
api_key=os.getenv("GROQ_API_KEY"), | |
) | |
def extract_text_from_pdf(file): | |
reader = PyPDF2.PdfReader(file) | |
text = '' | |
for page_num in range(len(reader.pages)): | |
page = reader.pages[page_num] | |
text += page.extract_text() | |
return text | |
def analyze_job_description(job_description): | |
prompt = f"Here is a job description:\n\n{job_description}\n\nPlease extract the key requirements and skills needed for this position." | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": prompt, | |
} | |
], | |
model="llama3-8b-8192", | |
) | |
return chat_completion.choices[0].message.content | |
def get_resume_feedback(content, job_requirements): | |
prompt = f"Here is the content of the resume:\n\n{content}\n\nBased on the following job requirements and skills:\n\n{job_requirements}\n\nPlease provide suggestions for improving this resume, including keyword optimization and ATS compatibility checks." | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": prompt, | |
} | |
], | |
model="llama3-8b-8192", | |
) | |
return chat_completion.choices[0].message.content | |
def score_resume_against_job_description(resume_text, job_description_text): | |
resume_words = set(resume_text.lower().split()) | |
job_description_words = set(job_description_text.lower().split()) | |
common_words = resume_words.intersection(job_description_words) | |
score = len(common_words) / len(job_description_words) * 100 | |
return score | |
def chat_with_llm(resume_text, job_description_text, user_question): | |
prompt = f"Here is the content of the resume:\n\n{resume_text}\n\nHere is the job description:\n\n{job_description_text}\n\nQuestion: {user_question}\n\nAnswer:" | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": prompt, | |
} | |
], | |
model="llama3-8b-8192", | |
) | |
return chat_completion.choices[0].message.content | |
def main(): | |
st.title("Resume Upgrader with ATS, Job Description Matching, and Chat Capabilities") | |
# Upload resume files | |
uploaded_resumes = st.file_uploader("Upload your resume (PDF)", type=["pdf"], accept_multiple_files=True) | |
# Initialize job_description_text to avoid UnboundLocalError | |
job_description_text = None | |
# Upload or input job description | |
job_description_option = st.radio("How would you like to provide the job description?", ("Upload PDF", "Enter Text")) | |
if job_description_option == "Upload PDF": | |
job_description_file = st.file_uploader("Upload job description (PDF)", type=["pdf"]) | |
if job_description_file: | |
job_description_text = extract_text_from_pdf(job_description_file) | |
else: | |
job_description_text = st.text_area("Enter job description") | |
if uploaded_resumes and job_description_text: | |
st.write("Uploaded Resumes:") | |
for uploaded_file in uploaded_resumes: | |
st.write(uploaded_file.name) | |
if job_description_option == "Upload PDF": | |
st.write("Uploaded Job Description:") | |
st.write(job_description_file.name) | |
else: | |
st.write("Entered Job Description:") | |
# Extract text from all uploaded resumes | |
combined_resume_text = "" | |
for uploaded_file in uploaded_resumes: | |
combined_resume_text += extract_text_from_pdf(uploaded_file) + "\n\n" | |
st.write("All documents uploaded and text extracted successfully!") | |
# Analyze the job description | |
job_requirements = analyze_job_description(job_description_text) | |
st.write("Extracted Job Requirements and Skills:") | |
st.write(job_requirements) | |
# Initialize session state for resume feedback history | |
if "resume_feedback" not in st.session_state: | |
st.session_state.resume_feedback = [] | |
# Provide options for the user | |
option = st.selectbox("What would you like to do?", ("Get Feedback and ATS Check", "Get Score", "Chat")) | |
if option == "Get Feedback and ATS Check": | |
if st.button("Get Feedback"): | |
feedback = get_resume_feedback(combined_resume_text, job_requirements) | |
st.session_state.resume_feedback.append((feedback, None, None)) | |
elif option == "Get Score": | |
if st.button("Get Score"): | |
score = score_resume_against_job_description(combined_resume_text, job_description_text) | |
st.session_state.resume_feedback.append((None, None, score)) | |
elif option == "Chat": | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
user_question = st.text_input("Enter your question:") | |
if st.button("Ask"): | |
if user_question: | |
response = chat_with_llm(combined_resume_text, job_description_text, user_question) | |
st.session_state.chat_history.append((user_question, response)) | |
user_question = "" # Clear input field | |
if st.session_state.chat_history: | |
st.subheader("Chat History") | |
for i, (q, a) in enumerate(st.session_state.chat_history): | |
st.write(f"**Q{i+1}:** {q}") | |
st.write(f"**A{i+1}:** {a}") | |
# Display feedback history | |
if st.session_state.resume_feedback: | |
st.subheader("Resume Improvement Suggestions and Scores") | |
for i, (feedback, ats_feedback, score) in enumerate(st.session_state.resume_feedback): | |
if feedback: | |
st.write(f"**Feedback for Resume {i+1}:**") | |
st.write(feedback) | |
if score is not None: | |
st.write(f"**Match Score for Resume {i+1}:** {score:.2f}%") | |
if __name__ == "__main__": | |
main() | |