Small bugfix to not call OpenAI twice and File Reformat
This commit is contained in:
27
app.py
27
app.py
@@ -27,6 +27,7 @@ cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
|
|||||||
firebase_admin.initialize_app(cred)
|
firebase_admin.initialize_app(cred)
|
||||||
|
|
||||||
GRADING_TEMPERATURE = 0.1
|
GRADING_TEMPERATURE = 0.1
|
||||||
|
TIPS_TEMPERATURE = 0.2
|
||||||
GEN_QUESTION_TEMPERATURE = 0.9
|
GEN_QUESTION_TEMPERATURE = 0.9
|
||||||
GPT_3_5_TURBO = "gpt-3.5-turbo"
|
GPT_3_5_TURBO = "gpt-3.5-turbo"
|
||||||
GPT_3_5_TURBO_16K = "gpt-3.5-turbo-16k"
|
GPT_3_5_TURBO_16K = "gpt-3.5-turbo-16k"
|
||||||
@@ -38,6 +39,7 @@ FIREBASE_BUCKET = 'mti-ielts.appspot.com'
|
|||||||
AUDIO_FILES_PATH = 'download-audio/'
|
AUDIO_FILES_PATH = 'download-audio/'
|
||||||
FIREBASE_LISTENING_AUDIO_FILES_PATH = 'listening_recordings/'
|
FIREBASE_LISTENING_AUDIO_FILES_PATH = 'listening_recordings/'
|
||||||
|
|
||||||
|
|
||||||
@app.route('/listening_section_1', methods=['GET'])
|
@app.route('/listening_section_1', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_listening_section_1_question():
|
def get_listening_section_1_question():
|
||||||
@@ -45,11 +47,13 @@ def get_listening_section_1_question():
|
|||||||
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_1)
|
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_1)
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||||
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/listening_section_2', methods=['GET'])
|
@app.route('/listening_section_2', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_listening_section_2_question():
|
def get_listening_section_2_question():
|
||||||
@@ -58,7 +62,8 @@ def get_listening_section_2_question():
|
|||||||
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_2)
|
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_2)
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||||
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
|
||||||
# file_name = str(uuid.uuid4()) + ".mp3"
|
# file_name = str(uuid.uuid4()) + ".mp3"
|
||||||
# sound_file_path = AUDIO_FILES_PATH + file_name
|
# sound_file_path = AUDIO_FILES_PATH + file_name
|
||||||
@@ -70,6 +75,7 @@ def get_listening_section_2_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/listening_section_3', methods=['GET'])
|
@app.route('/listening_section_3', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_listening_section_3_question():
|
def get_listening_section_3_question():
|
||||||
@@ -78,7 +84,8 @@ def get_listening_section_3_question():
|
|||||||
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_3)
|
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_3)
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||||
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
|
||||||
# file_name = str(uuid.uuid4()) + ".mp3"
|
# file_name = str(uuid.uuid4()) + ".mp3"
|
||||||
# sound_file_path = AUDIO_FILES_PATH + file_name
|
# sound_file_path = AUDIO_FILES_PATH + file_name
|
||||||
@@ -90,6 +97,7 @@ def get_listening_section_3_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/listening_section_4', methods=['GET'])
|
@app.route('/listening_section_4', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_listening_section_4_question():
|
def get_listening_section_4_question():
|
||||||
@@ -98,7 +106,8 @@ def get_listening_section_4_question():
|
|||||||
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_4)
|
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_4)
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||||
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
|
||||||
|
GEN_QUESTION_TEMPERATURE)
|
||||||
|
|
||||||
# file_name = str(uuid.uuid4()) + ".mp3"
|
# file_name = str(uuid.uuid4()) + ".mp3"
|
||||||
# sound_file_path = AUDIO_FILES_PATH + file_name
|
# sound_file_path = AUDIO_FILES_PATH + file_name
|
||||||
@@ -110,6 +119,7 @@ def get_listening_section_4_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/writing_task1', methods=['POST'])
|
@app.route('/writing_task1', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def grade_writing_task_1():
|
def grade_writing_task_1():
|
||||||
@@ -126,6 +136,7 @@ def grade_writing_task_1():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/writing_task2', methods=['POST'])
|
@app.route('/writing_task2', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def grade_writing_task_2():
|
def grade_writing_task_2():
|
||||||
@@ -141,6 +152,7 @@ def grade_writing_task_2():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/fetch_tips', methods=['POST'])
|
@app.route('/fetch_tips', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def fetch_answer_tips():
|
def fetch_answer_tips():
|
||||||
@@ -153,7 +165,7 @@ def fetch_answer_tips():
|
|||||||
messages = get_question_tips(question, answer, correct_answer, context)
|
messages = get_question_tips(question, answer, correct_answer, context)
|
||||||
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
token_count = reduce(lambda count, item: count + count_tokens(item)['n_tokens'],
|
||||||
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
map(lambda x: x["content"], filter(lambda x: "content" in x, messages)), 0)
|
||||||
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
|
response = make_openai_call(GPT_3_5_TURBO, messages, token_count, None, TIPS_TEMPERATURE)
|
||||||
|
|
||||||
if isinstance(response, str):
|
if isinstance(response, str):
|
||||||
response = re.sub(r"^[a-zA-Z0-9_]+\:\s*", "", response)
|
response = re.sub(r"^[a-zA-Z0-9_]+\:\s*", "", response)
|
||||||
@@ -175,6 +187,7 @@ def get_writing_task_2_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/speaking_task_1', methods=['POST'])
|
@app.route('/speaking_task_1', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def grade_speaking_task_1():
|
def grade_speaking_task_1():
|
||||||
@@ -198,6 +211,7 @@ def grade_speaking_task_1():
|
|||||||
os.remove(sound_file_name)
|
os.remove(sound_file_name)
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/speaking_task_1', methods=['GET'])
|
@app.route('/speaking_task_1', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_speaking_task_1_question():
|
def get_speaking_task_1_question():
|
||||||
@@ -210,6 +224,7 @@ def get_speaking_task_1_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/speaking_task_2', methods=['POST'])
|
@app.route('/speaking_task_2', methods=['POST'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def grade_speaking_task_2():
|
def grade_speaking_task_2():
|
||||||
@@ -233,6 +248,7 @@ def grade_speaking_task_2():
|
|||||||
os.remove(sound_file_name)
|
os.remove(sound_file_name)
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/speaking_task_2', methods=['GET'])
|
@app.route('/speaking_task_2', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
def get_speaking_task_2_question():
|
def get_speaking_task_2_question():
|
||||||
@@ -245,5 +261,6 @@ def get_speaking_task_2_question():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
@@ -349,30 +349,30 @@ def get_question_gen_messages(question_type: QuestionType):
|
|||||||
def get_question_tips(question: str, answer: str, correct_answer: str, context: str = None):
|
def get_question_tips(question: str, answer: str, correct_answer: str, context: str = None):
|
||||||
messages = [
|
messages = [
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "user",
|
||||||
"content": "You are a IELTS exam program that analyzes incorrect answers to questions and gives tips to "
|
"content": "You are a IELTS exam program that analyzes incorrect answers to questions and gives tips to "
|
||||||
"help students understand why it was a wrong answer and explains how their thought process "
|
"help students understand why it was a wrong answer and gives helpful insight for the future. "
|
||||||
"should have been. The tip should refer to the context and question.",
|
"The tip should refer to the context and question.",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
if not (context is None or context == ""):
|
if not (context is None or context == ""):
|
||||||
messages.append({
|
messages.append({
|
||||||
"role": "system",
|
"role": "user",
|
||||||
"content": f"This is the context for the question: {context}",
|
"content": f"This is the context for the question: {context}",
|
||||||
})
|
})
|
||||||
|
|
||||||
messages.extend([
|
messages.extend([
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "user",
|
||||||
"content": f"This is the question: {question}",
|
"content": f"This is the question: {question}",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "user",
|
||||||
"content": f"This is the answer: {answer}",
|
"content": f"This is the answer: {answer}",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "system",
|
"role": "user",
|
||||||
"content": f"This is the correct answer: {correct_answer}",
|
"content": f"This is the correct answer: {correct_answer}",
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -53,7 +53,12 @@ def make_openai_call(model, messages, token_count, fields_to_check, temperature)
|
|||||||
frequency_penalty=float(FREQUENCY_PENALTY),
|
frequency_penalty=float(FREQUENCY_PENALTY),
|
||||||
messages=messages
|
messages=messages
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if fields_to_check is None:
|
||||||
|
return result["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
processed_response = process_response(result["choices"][0]["message"]["content"], fields_to_check[0])
|
processed_response = process_response(result["choices"][0]["message"]["content"], fields_to_check[0])
|
||||||
|
|
||||||
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
|
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
|
||||||
try_count = try_count + 1
|
try_count = try_count + 1
|
||||||
return make_openai_call(model, messages, token_count, fields_to_check, temperature)
|
return make_openai_call(model, messages, token_count, fields_to_check, temperature)
|
||||||
|
|||||||
Reference in New Issue
Block a user