How to Build a Cover Letter Generator App using Hugging Face Transformers and with OpenAI?



 

This blog guides you to construct the app using two ways: Hugging Face Transformers & OpenAI API

To begin, let us see a quick introduction to text generation and in the next section we will gradually implement the application using Hugging Face Transformers & OpenAI API.

Text Generation:

Text generation involves the use of algorithms and language models to process input data and create output text. The process includes training AI models on extensive text datasets to grasp patterns, grammar rules, and contextual information. These models, exemplified by GPT and Google’s PaLM, leverage deep learning techniques like neural networks to comprehend sentence structures and produce coherent and contextually appropriate text.

At its core, text generation relies on language models that have undergone pre-training on large volumes of internet text. These models, armed with learned knowledge, can then generate new text based on provided prompts or conditions.

In the text generation process, the AI model starts with a seed input, like a sentence or keyword, and employs its acquired knowledge to predict the most likely next words or phrases. The model continues generating text, maintaining context and coherence, until it reaches a specified length or meets a predetermined condition.

Cover Letter Generation using Hugging Face Transformers – Pipelines.

Now let us dive into the process of building the application with Hugging Face Transformer — Pipelines.

Step 1: Install Required Packages

First, make sure you have the required packages installed. You can install them using the following commands:

pip install streamlit transformers
pip install PyPDF2

Step 2: Create the App Script (app.py)

Create a file named app.py.


import streamlit as st
from transformers import pipeline
from PyPDF2 import PdfReader

# Load the text generation model from Hugging Face
generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')

st.markdown("""
# AI-Powered Cover Letter Generator

Generate a cover letter. All you need to do is:
1. Upload your resume or copy your resume/experiences
2. Paste a relevant job description
3. Input some other relevant user/job data
"""
)

# radio for upload or copy paste option
res_format = st.radio(
"Do you want to upload or paste your resume/key experience",
('Upload', 'Paste'))

if res_format == 'Upload':
# upload_resume
res_file = st.file_uploader(' Upload your resume in pdf format')
if res_file:
# Read the content of the PDF file
pdf_reader = PdfReader(res_file)
res_text = ""
for page in pdf_reader.pages:
res_text += page.extract_text()
else:
# use the pasted contents instead
res_text = st.text_input('Pasted resume elements')

with st.form('input_form'):
# other inputs
job_desc = st.text_input('Pasted job description')
user_name = st.text_input('Your name')
company = st.text_input('Company name')
manager = st.text_input('Hiring manager')
role = st.text_input('Job title/role')
referral = st.text_input('How did you find out about this opportunity?')

# submit button
submitted = st.form_submit_button("Generate Cover Letter")

# if the form is submitted, generate the cover letter
if submitted:
# Generate cover letter using Hugging Face model
prompt = f"""
You will need to generate a cover letter based on specific resume and a job description.

My resume text: {res_text}
The job description is: {job_desc}
The candidate's name to include on the cover letter: {user_name}
The job title/role: {role}
The hiring manager is: {manager}
How you heard about the opportunity: {referral}
The company to which you are generating the cover letter for: {company}
The cover letter should have three content paragraphs

In the first paragraph, focus on the following: you will convey who you are, what position you are interested in, and where you heard about it, and summarize what you have to offer based on the above resume.

In the second paragraph, focus on why the candidate is a great fit, drawing parallels between the experience included in the resume and the qualifications on the job description.

In the 3RD PARAGRAPH: Conclusion
Restate your interest in the organization and/or job and summarize what you have to offer and thank the reader for their time and consideration.

Note that contact information may be found in the included resume text, and use and/or summarize specific resume context for the letter.

Use {user_name} as the candidate.

Generate a specific cover letter based on the above. Generate the response and include appropriate spacing between the paragraph text
"""

response_out = generator(prompt, max_length=2000, num_return_sequences=1)[0]['generated_text']
st.write(response_out)

# include an option to download a txt file
st.download_button('Download the cover_letter', response_out)

Step 3: Understand the Code

Let’s break down the code and understand each section:

Import Libraries:

  • streamlit: Used for building the web app.
  • transformers.pipeline: Provides an interface to Hugging Face's model for text generation.
  • PyPDF2.PdfReader: Used for reading the content of PDF files.
  • Initialize the Text Generation Model: pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B'): Loads the text generation model from Hugging Face.
  • Build the Streamlit App Interface: The app has a title and a brief description at the beginning.
  • Choose Resume Input Method: Users can either upload a PDF resume or paste the text.
  • Handle Resume Input: If the user uploads a PDF, the app reads the text from the PDF. If the user pastes the text, it takes the input from the text input field.
  • Collect Other Input Data: Collect additional information such as job description, user name, company, manager, role, and referral.
  • Generate Cover Letter: When the user submits the form, the app generates a cover letter using the Hugging Face model.
  • Display Cover Letter: The generated cover letter is displayed on the app.

Step 4: Run the Streamlit App Locally

Open your terminal, and navigate to the directory containing app.py, and run the following command:

streamlit run app.py

This will launch a local server, and you can view your app in a web browser at the specified address (usually http://localhost:8501).

Step 5: Test the App

  • Upload a PDF resume or paste text.
  • Input the job description and other relevant information.
  • Click the “Generate Cover Letter” button.
  • View the generated cover letter on the app.

Step 6: Customize and Extend

Feel free to customize the app further based on your requirements. You can add more styling, improve the UI, or extend the functionality. Additionally, you may explore other models provided by Hugging Face for text generation.

Cover Letter Generation using OpenAPI Key

In this step-by-step guide, we will walk through the process of creating an AI-powered cover letter generator using Streamlit and OpenAI’s GPT-3.5 family, specifically the gpt-3.5-turbo model. This application will allow users to upload their resumes, paste relevant job descriptions, input additional details, and generate a tailored cover letter.

Prerequisites

Make sure you have the following installed:

  • Python
  • Pip
  • Streamlit
  • OpenAI Python library
  • PyPDF2
pip install streamlit openai PyPDF2

Step 1: Obtain OpenAI API Key

  1. Sign up for the OpenAI GPT-3.5 API and obtain your API key.

Step 2: Set Up Streamlit App

Create a new Python script (e.g., app.py) and import the necessary libraries:

import streamlit as st
import openai as ai
from PyPDF2 import PdfReader
# Set your OpenAI API key
ai.api_key = "YOUR_OPENAI_API_KEY"
# Streamlit app title and description
st.markdown("""
# AI-Powered Cover Letter Generator
Generate a cover letter. All you need to do is:
1. Upload your resume or copy your resume/experiences
2. Paste a relevant job description
3. Input some other relevant user/job data
""")

Step 3: Allow Resume Input

Enable users to upload their resume or paste it into the app. Use PyPDF2 to extract text from uploaded PDFs.

res_format = st.radio(
"Do you want to upload or paste your resume/key experience",
('Upload', 'Paste'))
if res_format == 'Upload':
res_file = st.file_uploader(' Upload your resume in pdf format')
if res_file:
pdf_reader = PdfReader(res_file)
res_text = ""
for page in pdf_reader.pages:
res_text += page.extract_text()
else:
res_text = st.text_input('Pasted resume elements')

Step 4: Collect Additional Input

Create a form to gather additional information required for crafting the cover letter.

with st.form('input_form'):
job_desc = st.text_input('Pasted job description')
user_name = st.text_input('Your name')
company = st.text_input('Company name')
manager = st.text_input('Hiring manager')
role = st.text_input('Job title/role')
referral = st.text_input('How did you find out about this opportunity?')
ai_temp = st.number_input('AI Temperature (0.0-1.0) Input how creative the API can be', value=0.99)
submitted = st.form_submit_button("Generate Cover Letter")

Step 5: Use OpenAI ChatCompletion

When the form is submitted, utilize OpenAI’s gpt-3.5-turbo model for chat completion.

if submitted:
completion = ai.ChatCompletion.create(
model="gpt-3.5-turbo",
temperature=ai_temp,
messages=[
{"role": "user", "content": f"You will need to generate a cover letter based on specific resume and a job description"},
{"role": "user", "content": f"My resume text: {res_text}"},
# ... (include other user messages)
]
)
response_out = completion['choices'][0]['message']['content']
st.write(response_out)
# Include an option to download a txt file
st.download_button('Download the cover_letter', response_out)

Step 6: Run Your Streamlit App

Navigate to the directory containing your script in the terminal and run:

streamlit run app.py

Visit the displayed URL in your browser to access the app.

Output

Filling the prompt fields:

Response:

Conclusion

Congratulations! You’ve successfully built an AI-powered cover letter generator using both techniques (OpenAI API and hugging Face Transformers). Feel free to customize and enhance the app further based on your needs. You can deploy it on platforms like Streamlit Sharing or Heroku to make it accessible to others.

Refer: https://ai-coverletter-generator.streamlit.app/

Original Post>