-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathexample_app.py
196 lines (159 loc) · 5.7 KB
/
example_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
app.py - Cover Letter Generator
"""
# Standard library imports
import os
import random
# Third-party imports
import streamlit as st
from openai import OpenAI
# Local imports
from samples import resumes, job_descriptions
from utils import check_secrets
from guardrails import openai_moderation, sentinel
if check_secrets():
st.error("Please set the required environment variables.")
st.stop()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
THRESHOLD_LG = 0.5
THRESHOLD_PG = 0.5
THRESHOLD_OT = 0.5
# Initialize the OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY)
def main():
"""
Main function to run the Cover Letter Generator app.
"""
st.title("✍️ Cover Letter Generator")
# Input fields
job_description = st.text_area(
"Enter the job description:",
height=200,
placeholder="Paste the job description here...",
value=st.session_state.get("job_description", ""),
)
resume = st.text_area(
"Enter your resume:",
height=200,
placeholder="Paste your resume or key experiences here...",
value=st.session_state.get("resume", ""),
)
# style = st.text_input(
# "Writing Style (Optional)",
# placeholder="e.g., professional, enthusiastic, concise",
# )
style = st.selectbox(
"Writing Style (Optional)",
options = ["professional", "enthusiatic", "concise"]
# placeholder="e.g., professional, enthusiastic, concise",
)
# Load example button
load_example = st.button("📋 Load Examples")
if load_example:
example_job = random.choice(job_descriptions)
example_resume = random.choice(resumes)
st.session_state["job_description"] = example_job
st.session_state["resume"] = example_resume
st.rerun()
generate_btn = st.button("📝 Generate Cover Letter")
if generate_btn and job_description and resume:
with st.spinner("Generating your cover letter..."):
if input_guardrail(job_description, resume):
st.warning("Please revise your inputs.")
else:
# Only continue if _score is less than threshold
st.divider()
st.subheader("Your Cover Letter")
cover_letter_box = st.empty()
generate_cover_letter(
job_description, resume, style, additional_info, cover_letter_box
)
def input_guardrail(job_description: str, resume: str) -> bool:
"""
Parameters:
- job_description (str): The text of the job description to validate.
- resume (str): The text of the resume to validate.
Returns:
- bool: `True` if either input fails any of the guardrails, otherwise `False`.
"""
# Perform moderation checks
jd_moderation = openai_moderation(text=job_description)
r_moderation = openai_moderation(text=resume)
# Apply sentinel filters
jd_response = sentinel(
text=job_description, filters=["lionguard", "promptguard"], detail="scores"
)
resume_response = sentinel(
text=resume, filters=["lionguard", "promptguard"], detail="scores"
)
# Extract scores for job description
jd_lg_binary_score = jd_response["lionguard"]["binary"]["score"]
jd_pg_score = jd_response["promptguard"]["jailbreak"]
# Extract scores for resume
r_lg_binary_score = resume_response["lionguard"]["binary"]["score"]
r_pg_score = resume_response["promptguard"]["jailbreak"]
# Evaluate conditions
if jd_moderation or r_moderation:
return True
st.toast("Passed OpenAI Moderation")
if jd_lg_binary_score > THRESHOLD_LG or r_lg_binary_score > THRESHOLD_LG:
return True
st.toast("Pass LionGuard check")
if jd_pg_score > THRESHOLD_PG or r_pg_score > THRESHOLD_PG:
return True
st.toast("Pass PromptGuard check")
return False
def generate_cover_letter(
job_description: str, resume: str, style: str, additional_info: str, text_box
) -> None:
"""
Generate a cover letter based on the job description and resume.
Parameters:
- job_description (str): The job posting description
- resume (str): The applicant's resume or relevant experiences
- style (str): Preferred writing style
- additional_info (str): Any additional information to consider
Returns:
- str: The generated cover letter
"""
system_prompt = f"""
You will receive:
- A Job Description
- A Resume
- (Optionally) Additional Information
Please write a cover letter based on above info.
Style: {style if style else 'professional tone'}
""".strip()
resume_ot_response = sentinel(
text=job_description,
filters=["off-topic"],
system_prompt=system_prompt,
)["off-topic"]["off-topic"]
jd_ot_response = sentinel(
text=resume,
filters=["off-topic"],
system_prompt=system_prompt,
)["off-topic"]["off-topic"]
if resume_ot_response > THRESHOLD_OT or jd_ot_response > THRESHOLD_OT:
st.warning("Please revise your inputs.")
st.stop()
st.toast("Passed off-topic check")
stream = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": f"Job Description:\n{job_description}\n\nResume:\n{resume}\n\nAdditional Information:\n{additional_info}",
},
],
stream=True,
)
text = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
text += chunk.choices[0].delta.content
text_box.empty()
text_box.info(text)
if __name__ == "__main__":
main()