-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
66 lines (53 loc) · 2.5 KB
/
main.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
import os
import openai
import time
import json
# Read the config file containing API key and additional prompt options
with open('config.json', 'r') as f:
config = json.load(f)
# Function to generate a cover letter given experience and job advertisement
def generate_cover_letter(experience, job_ad):
additional_prompt_options = config['additional_prompt_options']
content = ("This is the content of my resume: " + experience +
"Write a cover letter for the following job advert: " +
job_ad + additional_prompt_options)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": content}]
)
return completion.choices[0].message.content
# Main function to process job advertisements and generate cover letters
def main():
# Set the API key for OpenAI
openai.api_key = config['api-key']
# Define the directories for job advertisements and output
ADVERTS_DIR = "Adverts/"
OUTPUT_DIR = "outputs/"
# Read the user's experience from a text file
with open('resume/experience.txt', encoding="utf8") as f:
experience = f.read()
# Start the timer to measure the time taken to process all job ads
start_time = time.time()
# Create a list of job advertisement filenames in the Adverts directory
job_ads = [(count, filename) for count, filename in enumerate(os.listdir(ADVERTS_DIR), start=1)]
# Iterate through each job advertisement and generate a cover letter
for count, filename in job_ads:
if filename == '.DS_Store':
continue
print(f"Current Job [{count}]: {filename}")
# Read the content of the job advertisement
with open(os.path.join(ADVERTS_DIR, filename)) as f:
job_ad = f.read()
# Generate a cover letter using the experience and job advertisement
cover_letter = generate_cover_letter(experience, job_ad)
# Save the generated cover letter in the output directory
filename = filename.split('.')[0]
print(filename)
with open(os.path.join(OUTPUT_DIR, f'{filename}_Output.txt'), 'w') as file:
file.write(cover_letter)
# Calculate and display the elapsed time taken to process all job ads
elapsed_time = round(time.time() - start_time, 2)
print(f"\nCompleted all jobs [{count}] in {elapsed_time} seconds.")
# Run the main function
if __name__ == "__main__":
main()