如何使用Hugging Face Transformers和OpenAI构建求职信生成器应用程序?

2023年11月29日 由 alex 发表 284 0

首先,让我们快速了解一下文本生成的简介。


文本生成:


文本生成涉及使用算法和语言模型处理输入数据并创建输出文本。这个过程包括对大量文本数据集培训人工智能模型,以把握模式、语法规则和上下文信息。这些模型,如 GPT 和谷歌的 PaLM 所示,利用深度学习技术例如神经网络来理解句子结构,并产生连贯且语境恰当的文本。


文本生成的核心依赖于经过预训练的语言模型,这些模型在大量互联网文本上进行训练。这些模型配备了学习到的知识,然后可以根据提供的提示或条件生成新文本。


11


在文本生成过程中,人工智能模型从一个种子输入开始,比如一个句子或关键词,并利用其获得的知识来预测最可能的下一个单词或短语。模型继续生成文本,保持上下文和连贯性,直到它达到一个指定的长度或满足一个预定条件。


使用 Hugging Face Transformers - Pipelines 生成求职信


现在,让我们深入了解使用Hugging Face转换器——管道构建应用程序的过程。


第1步:安装所需包


首先,确保你已经安装了所需的包。你可以使用以下命令来安装它们:


pip install streamlit transformers
pip install PyPDF2


第2步:创建应用程序脚本 (app.py)


创建一个名为 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)


第3步: 理解代码


我们来分解代码,并理解每个部分:


导入库:


  • streamlit: 用于构建web应用程序。
  • transformers.pipeline: 提供了一个接口,用于Hugging Face的文本生成模型。
  • PyPDF2.PdfReader:用于读取PDF文件的内容。
  • 初始化文本生成模型:pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B'): 从Hugging Face加载文本生成模型。
  • 构建Streamlit应用界面: 应用程序在开始有一个标题和简短的描述。
  • 选择简历输入方式: 用户可以上传PDF简历或粘贴文本。
  • 处理简历输入: 如果用户上传了PDF,应用程序将从PDF读取文本。如果用户粘贴了文本,将从文本输入字段中取得输入。
  • 收集其他输入数据: 收集其他信息,如职位描述、用户名、公司、经理、角色和推荐人。
  • 生成求职信: 当用户提交表格时,应用程序使用Hugging Face模型生成一封求职信。
  • 展示求职信: 在应用程序上显示生成的求职信。


第4步: 在本地运行Streamlit应用程序


打开你的终端,导航到包含app.py的目录,并运行以下命令:


streamlit run app.pypy


这将启动一个本地服务器,你可以在指定的地址(通常是 http://localhost:8501)通过网页浏览器查看你的应用程序。


第5步:测试应用程序


  • 上传一个PDF格式的简历或粘贴文本。
  • 输入工作描述和其他相关信息。
  • 点击“生成求职信”按钮。
  • 在应用程序上查看生成的求职信。


第6步:定制和扩展


根据你的需求,可以进一步自定义应用程序。你可以添加更多样式、改进用户界面或扩展功能。此外,你也可以探索Hugging Face提供的用于文本生成的其它模型。


使用OpenAPI密钥生成求职信


在本分步指南中,我们将讲解使用Streamlit和OpenAI的GPT-3.5家族,特别是gpt-3.5-turbo模型,创建一个AI驱动的求职信生成器的过程。这个应用程序将允许用户上传他们的简历,粘贴相关的工作描述,输入额外的细节,并生成一个量身定制的求职信。


先决条件


确保你已经安装了以下内容:


  • Python
  • Pip
  • Streamlit
  • OpenAI Python库
  • PyPDF2


pip install streamlit openai PyPDF2PyPDF2


步骤1:获取OpenAI API密钥


注册OpenAI GPT-3.5 API并获取你的API密钥。


步骤2:设置Streamlit应用


创建一个新的Python脚本(例如,app.py)并导入必要的库:


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
""")


步骤3:允许简历输入


允许用户上传他们的简历或将其粘贴到应用程序中。使用PyPDF2从上传的PDF文件中提取文本。


res_format = st.radio(
    "Do you want to upload or paste your resume/key experience","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')


步骤 4:收集额外信息


创建一个表单以收集编写求职信所需的额外信息。


with st.form('input_form'):.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")


步骤 5:使用 OpenAI ChatCompletion


当表单提交后,使用 OpenAI 的 gpt-3.5-turbo 模型进行聊天内容的自动补全。


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)


步骤6:运行你的 Streamlit 应用


在终端中导航到包含你脚本的目录,并运行:


streamlit run app.pypy


在浏览器中访问显示的URL以进入应用程序。


输出


12


填写提示栏位:


13


回应:


14


结论


你已经成功地利用两种技术(OpenAI API和hugging Face Transformers)建立了一个AI驱动的求职信生成器。请随意根据你的需求进一步自定义和增强这个应用程序。你可以在像Streamlit Sharing或Heroku这样的平台上部署它,使其可以被其他人访问。


文章来源:https://medium.com/ai-in-plain-english/how-to-build-a-cover-letter-generator-app-using-hugging-face-transformers-and-with-openai-264a2fd74336
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消