Spaces:
No application file
No application file
import os | |
from flask import Flask, request, jsonify | |
import openai | |
app = Flask(__name__) | |
# Load your OpenAI API key | |
# - Either from environment variables or from a .env file using dotenv | |
openai.api_key = os.environ.get("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") | |
def generate_cover_letter(): | |
# Example JSON payload: | |
# { | |
# "position": "Data Scientist", | |
# "company": "BigCorp", | |
# "skills": "Machine Learning, Python, R", | |
# "experience_years": 5, | |
# "candidate_name": "John Doe" | |
# } | |
data = request.json | |
position = data.get("position", "Software Engineer") | |
company = data.get("company", "SomeCompany") | |
skills = data.get("skills", "Python, Data Analysis") | |
experience_years = data.get("experience_years", 3) | |
candidate_name = data.get("candidate_name", "Candidate") | |
# Construct a prompt (prompt engineering) to guide the model | |
prompt = f""" | |
You are a professional cover letter writing assistant. | |
The user is applying for the position of {position} at {company}. | |
They have {experience_years} years of experience in {skills}. | |
Their name is {candidate_name}. | |
Please write a compelling, professional cover letter in the first person | |
that highlights relevant skills, experience, and achievements. | |
Ensure the letter is clear, concise, and well-structured. | |
""" | |
# Call the OpenAI API | |
try: | |
response = openai.ChatCompletion.create( | |
model="gpt-4" | |
messages=[ | |
{"role": "system", "content": "You are an expert cover letter writer."}, | |
{"role": "user", "content": prompt} | |
], | |
temperature=0.7, | |
max_tokens=600 | |
) | |
generated_text = response["choices"][0]["message"]["content"].strip() | |
return jsonify({ | |
"cover_letter": generated_text | |
}), 200 | |
except Exception as e: | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == "__main__": | |
# Run Flask app | |
app.run(host="0.0.0.0", port=5000, debug=True) | |