Generate questions endpoints working for all.

This commit is contained in:
Cristiano Ferreira
2023-11-12 23:40:24 +00:00
parent 274252bf92
commit 695d9b589a
7 changed files with 2143 additions and 312 deletions

178
app.py
View File

@@ -30,16 +30,32 @@ jwt = JWTManager(app)
cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
firebase_admin.initialize_app(cred)
@app.route('/listening_section_1', methods=['GET'])
@jwt_required()
def get_listening_section_1_question():
try:
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_1)
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)
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
GEN_QUESTION_TEMPERATURE)
return response
delete_files_older_than_one_day(AUDIO_FILES_PATH)
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(two_people_scenarios))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(LISTENING_EXERCISE_TYPES, 1)
number_of_exercises_q = divide_number_into_parts(TOTAL_LISTENING_SECTION_1_EXERCISES, len(req_exercises))
unprocessed_conversation, processed_conversation = generate_listening_1_conversation(topic)
print("Generated conversation: " + str(processed_conversation))
start_id = 1
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises, number_of_exercises_q,
start_id)
return {
"exercises": exercises,
"text": processed_conversation
}
except Exception as e:
return str(e)
@@ -71,12 +87,24 @@ def save_listening_section_1_question():
def get_listening_section_2_question():
try:
delete_files_older_than_one_day(AUDIO_FILES_PATH)
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_2)
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)
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
GEN_QUESTION_TEMPERATURE)
return response
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(social_monologue_contexts))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(LISTENING_EXERCISE_TYPES, 2)
number_of_exercises_q = divide_number_into_parts(TOTAL_LISTENING_SECTION_2_EXERCISES, len(req_exercises))
monologue = generate_listening_2_monologue(topic)
print("Generated monologue: " + str(monologue))
start_id = 11
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
return {
"exercises": exercises,
"text": monologue
}
except Exception as e:
return str(e)
@@ -107,12 +135,26 @@ def save_listening_section_2_question():
def get_listening_section_3_question():
try:
delete_files_older_than_one_day(AUDIO_FILES_PATH)
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_3)
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)
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
GEN_QUESTION_TEMPERATURE)
return response
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(four_people_scenarios))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(LISTENING_EXERCISE_TYPES, 1)
number_of_exercises_q = divide_number_into_parts(TOTAL_LISTENING_SECTION_3_EXERCISES, len(req_exercises))
unprocessed_conversation, processed_conversation = generate_listening_3_conversation(topic)
print("Generated conversation: " + str(processed_conversation))
start_id = 21
exercises = generate_listening_conversation_exercises(unprocessed_conversation, req_exercises, number_of_exercises_q,
start_id)
return {
"exercises": exercises,
"text": processed_conversation
}
except Exception as e:
return str(e)
@@ -143,12 +185,24 @@ def save_listening_section_3_question():
def get_listening_section_4_question():
try:
delete_files_older_than_one_day(AUDIO_FILES_PATH)
messages = get_question_gen_messages(QuestionType.LISTENING_SECTION_4)
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)
response = make_openai_call(GPT_3_5_TURBO_16K, messages, token_count, LISTENING_GEN_FIELDS,
GEN_QUESTION_TEMPERATURE)
return response
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(academic_subjects))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(LISTENING_EXERCISE_TYPES, 2)
number_of_exercises_q = divide_number_into_parts(TOTAL_LISTENING_SECTION_4_EXERCISES, len(req_exercises))
monologue = generate_listening_4_monologue(topic)
print("Generated monologue: " + str(monologue))
start_id = 31
exercises = generate_listening_monologue_exercises(monologue, req_exercises, number_of_exercises_q, start_id)
return {
"exercises": exercises,
"text": monologue
}
except Exception as e:
return str(e)
@@ -606,29 +660,25 @@ def save_speaking_task_3_question():
@jwt_required()
def get_reading_passage_1_question():
try:
TOTAL_EXERCISES = 13
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(topics))
req_exercises = request.args.getlist('exercises')
number_of_exercises_q = divide_number_into_parts(TOTAL_EXERCISES, len(req_exercises))
if (len(req_exercises) == 0):
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_1_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_1, topic)
exercises = []
for req_exercise in req_exercises:
if (req_exercise == "multiple_choice"):
mc_question = gen_multiple_choice_exercise(passage["text"], number_of_exercises_q.get())
exercises.append(mc_question)
exercises = fix_exercise_ids(exercises)
print("Generated passage: " + str(passage))
start_id = 1
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return {
"exercises": exercises,
"text": {
"content": passage["text"],
"title": passage["title"]
},
}
}
except Exception as e:
return str(e)
@@ -656,6 +706,62 @@ def save_reading_passage_1_question():
return str(e)
@app.route('/reading_passage_2', methods=['GET'])
@jwt_required()
def get_reading_passage_2_question():
try:
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(topics))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_2_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_2, topic)
print("Generated passage: " + str(passage))
start_id = 14
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return {
"exercises": exercises,
"text": {
"content": passage["text"],
"title": passage["title"]
}
}
except Exception as e:
return str(e)
@app.route('/reading_passage_3', methods=['GET'])
@jwt_required()
def get_reading_passage_3_question():
try:
# Extract parameters from the URL query string
topic = request.args.get('topic', default=random.choice(topics))
req_exercises = request.args.getlist('exercises')
if (len(req_exercises) == 0):
req_exercises = random.sample(READING_EXERCISE_TYPES, 2)
number_of_exercises_q = divide_number_into_parts(TOTAL_READING_PASSAGE_3_EXERCISES, len(req_exercises))
passage = generate_reading_passage(QuestionType.READING_PASSAGE_3, topic)
print("Generated passage: " + str(passage))
start_id = 27
exercises = generate_reading_exercises(passage["text"], req_exercises, number_of_exercises_q, start_id)
return {
"exercises": exercises,
"text": {
"content": passage["text"],
"title": passage["title"]
}
}
except Exception as e:
return str(e)
@app.route('/fetch_tips', methods=['POST'])
@jwt_required()
def fetch_answer_tips():