Код: Выделить всё
from flask import Flask, request, jsonify, send_from_directory
import openai
from openai import OpenAI
import os
app = Flask(__name__)
openai.api_key = 'sk-' # I added my key here.
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
@app.route('/analyze', methods=['POST'])
def analyze_cv():
if 'cv' not in request.files or 'job_description' not in request.files:
return jsonify({'error': 'CV and Job Description files are required'}), 400
cv = request.files['cv'].read()
job_description = request.files['job_description'].read()
try:
cv = cv.decode('utf-8')
except UnicodeDecodeError:
cv = cv.decode('latin-1')
try:
job_description = job_description.decode('utf-8')
except UnicodeDecodeError:
job_description = job_description.decode('latin-1')
prompt = f"Given the following job description:\n{job_description}\n\nAssess the suitability of the candidate based on the CV:\n{cv}\n\nScore the candidate from 1 to 10 and provide an explanation."
try:
client = OpenAI(api_key = os.environ.get("OPENAI_API_KEY"),)
openai.Completion.create
response = client.Completions.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=1024
)
score = int(response.choices[0].text.split()[0])
explanation = response.choices[0].text.split('\n')[1]
return jsonify({'score': score, 'explanation': explanation}), 200
except Exception as e:
print(e)
return jsonify({'error': 'An error occurred while analyzing the CV'}), 500
if __name__ == '__main__':
app.run(debug=True)
Код: Выделить всё
CV Analyzer
CV Analyzer
Analyze
function analyze() {
var cvFile = document.getElementById('cv').files[0];
var jobDescriptionFile = document.getElementById('job_description').files[0];
if (!cvFile || !jobDescriptionFile) {
alert('Please upload both CV and Job Description files.');
return;
}
var formData = new FormData();
formData.append('cv', cvFile);
formData.append('job_description', jobDescriptionFile);
fetch('/analyze', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('result').innerHTML = `
Score: ${data.score}
${data.explanation}
`;
})
.catch(error => console.error('Error:', error));
}
На консоли эта ошибка выскакивает снова и снова:
Код: Выделить всё
The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
127.0.0.1 - - \[04/May/2024 20:55:38\] "POST /analyze HTTP/1.1" 500 -
Я попробовал то, что было предложено некоторыми на SO. Но это просто не работает.
Подробнее здесь: https://stackoverflow.com/questions/784 ... openai-api