-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_utils.py
More file actions
150 lines (127 loc) · 5.37 KB
/
Copy pathopenai_utils.py
File metadata and controls
150 lines (127 loc) · 5.37 KB
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
import openai
import streamlit as st
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
# Get API key from environment variable
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
def parse_resume_text(text):
try:
# Use OpenAI to extract structured information from resume
client = openai.OpenAI(api_key=OPENAI_API_KEY)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a resume parser. Extract and format the information exactly as requested."},
{"role": "user", "content": f"""Please extract and format the following information from this resume:
Full Name:
[Extract full name]
Contact Information:
[Extract email, phone, location]
Technical Skills:
- [Skill 1]
- [Skill 2]
- [Skill 3]
- [Skill 4]
- so on...
Notable Projects/Achievements (top 3):
- [Project 1]
- [Project 2]
- [Project 3]
Resume text:
{text}"""}
]
)
parsed_info = response.choices[0].message.content.strip()
# Initialize default values
name = "Unknown Name"
contact = "No contact information provided"
skills = []
projects = []
# Parse sections more robustly
sections = parsed_info.split('\n\n')
for section in sections:
section = section.strip()
if section.startswith('Full Name:'):
name = section.replace('Full Name:', '').strip()
elif section.startswith('Contact Information:'):
contact = section.replace('Contact Information:', '').strip()
elif section.startswith('Technical Skills'):
skills = [s.strip().replace('- ', '') for s in section.split('\n')[1:] if s.strip()]
elif section.startswith('Notable Projects'):
projects = [p.strip().replace('- ', '') for p in section.split('\n')[1:] if p.strip()]
# Ensure we have at least some default values
if not skills:
skills = ["Technical skill not found"]
if not projects:
projects = ["Project details not found"]
# Create the structured output
return {
'name': name or "Unknown Name",
'contact': contact or "No contact information provided",
'skills': skills[:4],
# Return all skills found, with a minimum of 1 skill
'skills': skills if len(skills) > 0 else ["Technical skill not found"],
'projects': projects[:3] # Keep top 3 projects
}
except Exception as e:
st.error(f"Error parsing resume: {str(e)}")
st.write("Debug info:", parsed_info) # Add debug information
# Return default information instead of None
return {
'name': "Unknown Name",
'contact': "No contact information provided",
'skills': ["Technical skill not specified"],
'projects': ["Project details not specified"]
}
def analyze_resume_ats(resume_text, job_description):
try:
client = openai.OpenAI(api_key=OPENAI_API_KEY)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an ATS optimization expert. Analyze resume compatibility with job requirements."},
{"role": "user", "content": f"""Analyze this resume against the job description and provide JSON formatted output with:
1. ats_score: number between 0-100
2. missing_keywords: array of important missing keywords
3. skills_to_highlight: array of existing skills to emphasize
4. improvement_suggestions: array of specific suggestions
5. key_job_requirements: array of critical job requirements
Resume:
{resume_text}
Job Description:
{job_description}"""}
]
)
return response.choices[0].message.content
except Exception as e:
st.error(f"Error analyzing resume: {str(e)}")
return None
def optimize_resume(resume_text, job_description, ats_analysis):
try:
client = openai.OpenAI(api_key=OPENAI_API_KEY)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an expert resume writer. Optimize resumes for ATS compatibility."},
{"role": "user", "content": f"""Optimize this resume based on the ATS analysis and job description.
Use these specific insights to improve the resume:
ATS Analysis:
{ats_analysis}
Focus on:
1. Incorporating missing keywords naturally
2. Emphasizing suggested skills to highlight
3. Implementing the specific improvement suggestions
4. Addressing key job requirements
5. Maintaining a clear, ATS-friendly format
Original Resume:
{resume_text}
Job Description:
{job_description}"""}
]
)
return response.choices[0].message.content
except Exception as e:
st.error(f"Error optimizing resume: {str(e)}")
return None