Endpoint generate reading kinda working.
This commit is contained in:
186
app.py
186
app.py
@@ -1,25 +1,23 @@
|
||||
import random
|
||||
|
||||
from flask import Flask, request
|
||||
from flask_jwt_extended import JWTManager, jwt_required
|
||||
from functools import reduce
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, firestore
|
||||
from helper.api_messages import QuestionType, get_grading_messages, get_question_gen_messages, get_question_tips, \
|
||||
get_speaking_grading_messages
|
||||
from helper.api_messages import *
|
||||
from helper.constants import *
|
||||
from helper.exercises import *
|
||||
from helper.file_helper import delete_files_older_than_one_day
|
||||
from helper.firebase_helper import download_firebase_file, upload_file_firebase, upload_file_firebase_get_url, \
|
||||
save_to_db
|
||||
from helper.firebase_helper import *
|
||||
from helper.heygen_api import create_video
|
||||
from helper.speech_to_text_helper import speech_to_text, text_to_speech, has_words, has_10_words
|
||||
from helper.speech_to_text_helper import *
|
||||
from helper.token_counter import count_tokens
|
||||
from helper.openai_interface import make_openai_call
|
||||
from helper.openai_interface import make_openai_call, make_openai_instruct_call
|
||||
import os
|
||||
import uuid
|
||||
import re
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from templates.question_templates import getListening1Template, getListening2Template, getSpeaking1Template, \
|
||||
getSpeaking2Template, getSpeaking3Template
|
||||
from templates.question_templates import *
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -32,22 +30,6 @@ jwt = JWTManager(app)
|
||||
cred = credentials.Certificate(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
|
||||
firebase_admin.initialize_app(cred)
|
||||
|
||||
GRADING_TEMPERATURE = 0.1
|
||||
TIPS_TEMPERATURE = 0.2
|
||||
GEN_QUESTION_TEMPERATURE = 0.9
|
||||
GPT_3_5_TURBO = "gpt-3.5-turbo"
|
||||
GPT_3_5_TURBO_16K = "gpt-3.5-turbo-16k"
|
||||
GRADING_FIELDS = ['comment', 'overall', 'task_response']
|
||||
GEN_FIELDS = ['question']
|
||||
LISTENING_GEN_FIELDS = ['transcript', 'exercise']
|
||||
|
||||
FIREBASE_BUCKET = 'mti-ielts.appspot.com'
|
||||
AUDIO_FILES_PATH = 'download-audio/'
|
||||
FIREBASE_LISTENING_AUDIO_FILES_PATH = 'listening_recordings/'
|
||||
|
||||
VIDEO_FILES_PATH = 'download-video/'
|
||||
FIREBASE_SPEAKING_VIDEO_FILES_PATH = 'speaking_videos/'
|
||||
|
||||
@app.route('/listening_section_1', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_listening_section_1_question():
|
||||
@@ -61,6 +43,7 @@ def get_listening_section_1_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_listening_section_1', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_listening_section_1_question():
|
||||
@@ -97,6 +80,7 @@ def get_listening_section_2_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_listening_section_2', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_listening_section_2_question():
|
||||
@@ -132,6 +116,7 @@ def get_listening_section_3_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_listening_section_3', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_listening_section_3_question():
|
||||
@@ -167,6 +152,7 @@ def get_listening_section_4_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_listening_section_4', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_listening_section_4_question():
|
||||
@@ -216,6 +202,24 @@ def grade_writing_task_1():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/writing_task1_general', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_writing_task_1_general_question():
|
||||
try:
|
||||
gen_wt1_question = "Craft a prompt for an IELTS Writing Task 1 General Training exercise that instructs the " \
|
||||
"student to compose a letter. The prompt should present a specific scenario or situation, " \
|
||||
"requiring the student to provide information, advice, or instructions within the letter."
|
||||
token_count = count_tokens(gen_wt1_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_wt1_question, token_count, None,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
return {
|
||||
"question": response.strip()
|
||||
}
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_writing_task_1', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_writing_task_1_question():
|
||||
@@ -259,18 +263,26 @@ def grade_writing_task_2():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
@app.route('/writing_task2', methods=['GET'])
|
||||
|
||||
@app.route('/writing_task2_general', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_writing_task_2_question():
|
||||
def get_writing_task_2_general_question():
|
||||
try:
|
||||
messages = get_question_gen_messages(QuestionType.WRITING_TASK_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, messages, token_count, GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
||||
return response
|
||||
gen_wt2_question = "Craft a comprehensive question for IELTS Writing Task 2 General Training that directs the candidate " \
|
||||
"to delve into an in-depth analysis of contrasting perspectives on a specific topic. The candidate " \
|
||||
"should be asked to discuss the strengths and weaknesses of both viewpoints, provide evidence or " \
|
||||
"examples, and present a well-rounded argument before concluding with their personal opinion on the " \
|
||||
"subject."
|
||||
token_count = count_tokens(gen_wt2_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_wt2_question, token_count, None,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
return {
|
||||
"question": response.strip()
|
||||
}
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_writing_task_2', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_writing_task_2_question():
|
||||
@@ -325,14 +337,19 @@ def grade_speaking_task_1():
|
||||
@jwt_required()
|
||||
def get_speaking_task_1_question():
|
||||
try:
|
||||
messages = get_question_gen_messages(QuestionType.SPEAKING_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, messages, token_count, GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
||||
gen_sp1_question = "Craft a thought-provoking question for IELTS Speaking Part 1 that encourages candidates to delve deeply " \
|
||||
"into personal experiences, preferences, or insights on diverse topics. Instruct the candidate to offer " \
|
||||
"not only detailed descriptions but also provide nuanced explanations, examples, or anecdotes to enrich " \
|
||||
"their response." \
|
||||
"Provide your response in this json format: {'topic': 'topic','question': 'question'}"
|
||||
token_count = count_tokens(gen_sp1_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_sp1_question, token_count, GEN_FIELDS,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_speaking_task_1', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_speaking_task_1_question():
|
||||
@@ -380,6 +397,7 @@ def save_speaking_task_1_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/speaking_task_2', methods=['POST'])
|
||||
@jwt_required()
|
||||
def grade_speaking_task_2():
|
||||
@@ -419,14 +437,20 @@ def grade_speaking_task_2():
|
||||
@jwt_required()
|
||||
def get_speaking_task_2_question():
|
||||
try:
|
||||
messages = get_question_gen_messages(QuestionType.SPEAKING_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, messages, token_count, GEN_FIELDS, GEN_QUESTION_TEMPERATURE)
|
||||
gen_sp2_question = "Create a question for IELTS Speaking Part 2 that encourages candidates to narrate a personal experience " \
|
||||
"or story related to a randomly selected topic. Include 3 prompts that guide the candidate to describe " \
|
||||
"specific aspects of the experience, such as details about the situation, their actions, and the " \
|
||||
"reasons it left a lasting impression." \
|
||||
"Provide your response in this json format: {'topic': 'topic','question': 'question', " \
|
||||
"'prompts': ['prompt_1', 'prompt_2', 'prompt_3']}"
|
||||
token_count = count_tokens(gen_sp2_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_sp2_question, token_count, GEN_FIELDS,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/save_speaking_task_2', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_speaking_task_2_question():
|
||||
@@ -474,6 +498,27 @@ def save_speaking_task_2_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/speaking_task_3', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_speaking_task_3_question():
|
||||
try:
|
||||
gen_sp3_question = "Formulate a set of 3 questions for IELTS Speaking Part 3 that encourage candidates to engage in a " \
|
||||
"meaningful discussion on a particular topic. Provide inquiries, ensuring " \
|
||||
"they explore various aspects, perspectives, and implications related to the topic." \
|
||||
"Provide your response in this json format: {'topic': 'topic','questions': ['question', " \
|
||||
"'question', 'question']}"
|
||||
token_count = count_tokens(gen_sp3_question)["n_tokens"]
|
||||
response = make_openai_instruct_call(GPT_3_5_TURBO_INSTRUCT, gen_sp3_question, token_count, GEN_FIELDS,
|
||||
GEN_QUESTION_TEMPERATURE)
|
||||
# Remove the numbers from the questions only if the string starts with a number
|
||||
response["questions"] = [re.sub(r"^\d+\.\s*", "", question) if re.match(r"^\d+\.", question) else question for
|
||||
question in response["questions"]]
|
||||
return response
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/speaking_task_3', methods=['POST'])
|
||||
@jwt_required()
|
||||
def grade_speaking_task_3():
|
||||
@@ -508,6 +553,7 @@ def grade_speaking_task_3():
|
||||
except Exception as e:
|
||||
return str(e), 400
|
||||
|
||||
|
||||
@app.route('/save_speaking_task_3', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_speaking_task_3_question():
|
||||
@@ -555,6 +601,61 @@ def save_speaking_task_3_question():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/reading_passage_1', methods=['GET'])
|
||||
@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))
|
||||
|
||||
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)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"text": {
|
||||
"content": passage["text"],
|
||||
"title": passage["title"]
|
||||
},
|
||||
}
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/reading_passage_1', methods=['POST'])
|
||||
@jwt_required()
|
||||
def save_reading_passage_1_question():
|
||||
try:
|
||||
# data = request.get_json()
|
||||
# question = data.get('question')
|
||||
question = getListening1Template()
|
||||
file_name = str(uuid.uuid4()) + ".mp3"
|
||||
sound_file_path = AUDIO_FILES_PATH + file_name
|
||||
firebase_file_path = FIREBASE_LISTENING_AUDIO_FILES_PATH + file_name
|
||||
# TODO it's the conversation audio, still work to do on text-to-speech
|
||||
text_to_speech(question["audio"]["conversation"], sound_file_path)
|
||||
file_url = upload_file_firebase_get_url(FIREBASE_BUCKET, firebase_file_path, sound_file_path)
|
||||
question["audio"]["source"] = file_url
|
||||
if save_to_db("listening", question):
|
||||
return question
|
||||
else:
|
||||
raise Exception("Failed to save question: " + question)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
@app.route('/fetch_tips', methods=['POST'])
|
||||
@jwt_required()
|
||||
def fetch_answer_tips():
|
||||
@@ -576,5 +677,6 @@ def fetch_answer_tips():
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user