OptiHire / app.py
AdithyaSNair's picture
Update app.py
a1320e5 verified
raw
history blame
4.19 kB
import streamlit as st
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate
import fitz # PyMuPDF
import requests
from bs4 import BeautifulSoup
# Initialize the LLM with your Groq API key
llm = ChatGroq(
temperature=0,
groq_api_key='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', # Replace with your Groq API key
model_name="llama-3.1-70b-versatile"
)
# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_file):
text = ""
with fitz.open(stream=pdf_file.read(), filetype="pdf") as doc:
for page in doc:
text += page.get_text()
return text
# Function to extract the job description from a given URL
def extract_job_description(job_link):
try:
response = requests.get(job_link)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
job_description = soup.get_text(separator='\n')
return job_description.strip()
except Exception as e:
st.error(f"Error fetching job description: {e}")
return ""
# Function to extract requirements from the job description using ChatGroq
def extract_requirements(job_description):
prompt_text = f"""
The following is a job description:
{job_description}
Extract the list of job requirements, qualifications, and skills from the job description. Provide them as a numbered list.
Requirements:
"""
prompt = PromptTemplate.from_template(prompt_text)
chain = prompt | llm
response = chain.invoke({})
requirements = response.content.strip()
return requirements
# Function to generate an email using ChatGroq
def generate_email(job_description, requirements, resume_text):
prompt_text = f"""
Given the following job description:
{job_description}
And the following extracted requirements:
{requirements}
And the following resume text:
{resume_text}
Write a cold email as Adithya S Nair, a recent graduate in Computer Science with a focus on Artificial Intelligence and Machine Learning. Highlight your relevant skills and experiences from the resume, emphasizing how you, as a fresher, can bring value to the client’s company. Mention key projects, internships, and any leadership experiences that align with the job description and requirements. Ensure the email is concise and professional.
Email:
"""
prompt = PromptTemplate.from_template(prompt_text)
chain = prompt | llm
response = chain.invoke({})
email_text = response.content.strip()
return email_text
# Streamlit App
def main():
st.title("Automated Email Generator")
st.write("""
This application generates a personalized email based on a job posting and your resume.
""")
# Input fields
job_link = st.text_input("Enter the job link:")
uploaded_file = st.file_uploader("Upload your resume (PDF format):", type="pdf")
if st.button("Generate Email"):
if not job_link:
st.error("Please enter a job link.")
return
if not uploaded_file:
st.error("Please upload your resume.")
return
with st.spinner("Processing..."):
# Extract job description
job_description = extract_job_description(job_link)
if not job_description:
st.error("Failed to extract job description.")
return
# Extract requirements
requirements = extract_requirements(job_description)
if not requirements:
st.error("Failed to extract requirements.")
return
# Extract resume text
resume_text = extract_text_from_pdf(uploaded_file)
if not resume_text:
st.error("Failed to extract text from resume.")
return
# Generate email
email_text = generate_email(job_description, requirements, resume_text)
if email_text:
st.subheader("Generated Email:")
st.write(email_text)
else:
st.error("Failed to generate email.")
if __name__ == "__main__":
main()