Add question db insert.
This commit is contained in:
@@ -2,6 +2,10 @@ from enum import Enum
|
||||
|
||||
|
||||
class QuestionType(Enum):
|
||||
LISTENING_SECTION_1 = "Listening Section 1"
|
||||
LISTENING_SECTION_2 = "Listening Section 2"
|
||||
LISTENING_SECTION_3 = "Listening Section 3"
|
||||
LISTENING_SECTION_4 = "Listening Section 4"
|
||||
WRITING_TASK_1 = "Writing Task 1"
|
||||
WRITING_TASK_2 = "Writing Task 2"
|
||||
SPEAKING_1 = "Speaking Task Part 1"
|
||||
@@ -172,7 +176,93 @@ def get_grading_messages(question_type: QuestionType, question: str, answer: str
|
||||
|
||||
|
||||
def get_question_gen_messages(question_type: QuestionType):
|
||||
if QuestionType.WRITING_TASK_2 == question_type:
|
||||
if QuestionType.LISTENING_SECTION_1 == question_type:
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "You are a IELTS program that generates questions for the exams.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Provide me with a transcript similar to the ones in ielts exam Listening Section 1. "
|
||||
"Create an engaging transcript simulating a conversation related to a unique type of service "
|
||||
"that requires getting the customer's details. Make sure to include specific details "
|
||||
"and descriptions to bring"
|
||||
"the scenario to life. After the transcript, please "
|
||||
"generate a 'form like' fill in the blanks exercise with 6 form fields (ex: name, date of birth)"
|
||||
" to fill related to the customer's details. Finally, "
|
||||
"provide the answers for the exercise. The response must be a json following this format: "
|
||||
"{ 'type': '<type of registration (ex: hotel, gym, english course, etc)>', "
|
||||
"'transcript': '<transcript of just the conversation about a registration of some sort, "
|
||||
"identify the person talking in each speech line>', "
|
||||
"'exercise': { 'form field': { '1': '<form field 1>', '2': '<form field 2>', "
|
||||
"'3': '<form field 3>', '4': '<form field 4>', "
|
||||
"'5': '<form field 5>', '6': '<form field 5>' }, "
|
||||
"'answers': {'1': '<answer to fill blank space in form field 1>', '2': '<answer to fill blank "
|
||||
"space in form field 2>', '3': '<answer to fill blank space in form field 3>', "
|
||||
"'4': '<answer to fill blank space in form field 4>', '5': '<answer to fill blank space in form field 5>',"
|
||||
" '6': '<answer to fill blank space in form field 6>'}}}",
|
||||
}
|
||||
]
|
||||
elif QuestionType.LISTENING_SECTION_2 == question_type:
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "You are a IELTS program that generates questions for the exams.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Provide me with a transcript similar to the ones in ielts exam Listening section 2. After the transcript, please "
|
||||
"generate a fill in the blanks exercise with 6 statements related to the text content. Finally, "
|
||||
"provide the answers for the exercise. The response must be a json following this format: "
|
||||
"{ 'transcript': 'transcript about some subject', 'exercise': { 'statements': { '1': 'statement 1 "
|
||||
"with a blank space to fill', '2': 'statement 2 with a blank space to fill', '3': 'statement 3 with a "
|
||||
"blank space to fill', '4': 'statement 4 with a blank space to fill', '5': 'statement 5 with a blank "
|
||||
"space to fill', '6': 'statement 6 with a blank space to fill' }, "
|
||||
"'answers': {'1': 'answer to fill blank space in statement 1', '2': 'answer to fill blank "
|
||||
"space in statement 2', '3': 'answer to fill blank space in statement 3', "
|
||||
"'4': 'answer to fill blank space in statement 4', '5': 'answer to fill blank space in statement 5',"
|
||||
" '6': 'answer to fill blank space in statement 6'}}}",
|
||||
}
|
||||
]
|
||||
elif QuestionType.LISTENING_SECTION_3 == question_type:
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "You are a IELTS program that generates questions for the exams.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Provide me with a transcript similar to the ones in ielts exam Listening section 3. After the transcript, please "
|
||||
"generate 4 multiple choice questions related to the text content. Finally, "
|
||||
"provide the answers for the exercise. The response must be a json following this format: "
|
||||
"{ 'transcript': 'generated transcript similar to the ones in ielts exam Listening section 3', "
|
||||
"'exercise': { 'questions': [ { 'question': "
|
||||
"'question 1', 'options': ['option 1', 'option 2', 'option 3', 'option 4'], 'answer': 1}, "
|
||||
"{'question': 'question 2', 'options': ['option 1', 'option 2', 'option 3', 'option 4'], "
|
||||
"'answer': 3}, {'question': 'question 3', 'options': ['option 1', 'option 2', 'option 3', "
|
||||
"'option 4'], 'answer': 0}, {'question': 'question 4', 'options': ['option 1', 'option 2', "
|
||||
"'option 3', 'option 4'], 'answer': 2}]}}",
|
||||
}
|
||||
]
|
||||
elif QuestionType.LISTENING_SECTION_4 == question_type:
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "You are a IELTS program that generates questions for the exams.",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Provide me with a transcript similar to the ones in ielts exam Listening section 4. After the transcript, please "
|
||||
"generate 4 completion-type questions related to the text content to complete with 1 word. Finally, "
|
||||
"provide the answers for the exercise. The response must be a json following this format: "
|
||||
"{ 'transcript': 'generated transcript similar to the ones in ielts exam Listening section 4', "
|
||||
"'exercise': [ { 'question': 'question 1', 'answer': 'answer 1'}, "
|
||||
"{'question': 'question 2', 'answer': 'answer 2'}, {'question': 'question 3', 'answer': 'answer 3'}, "
|
||||
"{'question': 'question 4', 'answer': 'answer 4'}]}",
|
||||
}
|
||||
]
|
||||
elif QuestionType.WRITING_TASK_2 == question_type:
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
|
||||
@@ -9,7 +9,18 @@ def download_firebase_file(bucket_name, source_blob_name, destination_file_name)
|
||||
print(f"File downloaded to {destination_file_name}")
|
||||
return destination_file_name
|
||||
|
||||
|
||||
def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
|
||||
# Uploads a file to Firebase Storage.
|
||||
storage_client = storage.Client()
|
||||
bucket = storage_client.bucket(bucket_name)
|
||||
try:
|
||||
blob = bucket.blob(destination_blob_name)
|
||||
blob.upload_from_filename(source_file_name)
|
||||
print(f"File uploaded to {destination_blob_name}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print("Error uploading file to Google Cloud Storage:", e)
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,22 +16,25 @@ FREQUENCY_PENALTY = 0.5
|
||||
TRY_LIMIT = 1
|
||||
|
||||
try_count = 0
|
||||
def process_response(input_string):
|
||||
def process_response(input_string, quotation_check_field):
|
||||
if '{' in input_string:
|
||||
try:
|
||||
# Find the index of the first occurrence of '{'
|
||||
index = input_string.index('{')
|
||||
# Extract everything after the first '{' (inclusive)
|
||||
result = input_string[index:]
|
||||
if re.search(r"'" + quotation_check_field + "':\s*'(.*?)'", result):
|
||||
parsed_string = result.replace("\"", "\\\"")
|
||||
pattern = r"(?<!\w)'|'(?!\w)"
|
||||
parsed_string = re.sub(pattern, '"', parsed_string)
|
||||
parsed_string = parsed_string.replace("\\\"", "'")
|
||||
parsed_string = parsed_string.replace("\n\n", " ")
|
||||
|
||||
parsed_string = result.replace("\"", "\\\"")
|
||||
pattern = r"(?<!\w)'|'(?!\w)"
|
||||
parsed_string = re.sub(pattern, '"', parsed_string)
|
||||
parsed_string = parsed_string.replace("\\\"", "'")
|
||||
parsed_string = parsed_string.replace("\n\n", " ")
|
||||
|
||||
json_obj = json.loads(parsed_string)
|
||||
return json_obj
|
||||
json_obj = json.loads(parsed_string)
|
||||
return json_obj
|
||||
else:
|
||||
json_obj = json.loads(result)
|
||||
return json_obj
|
||||
except json.JSONDecodeError:
|
||||
print("Invalid JSON string!")
|
||||
else:
|
||||
@@ -40,20 +43,20 @@ def process_response(input_string):
|
||||
def check_fields(obj, fields):
|
||||
return all(field in obj for field in fields)
|
||||
|
||||
def make_openai_call(messages, token_count, fields_to_check, temperature):
|
||||
def make_openai_call(model, messages, token_count, fields_to_check, temperature):
|
||||
global try_count
|
||||
result = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo",
|
||||
model=model,
|
||||
max_tokens=int(MAX_TOKENS - token_count - 300),
|
||||
temperature=float(temperature),
|
||||
top_p=float(TOP_P),
|
||||
frequency_penalty=float(FREQUENCY_PENALTY),
|
||||
messages=messages
|
||||
)
|
||||
processed_response = process_response(result["choices"][0]["message"]["content"])
|
||||
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:
|
||||
try_count = try_count + 1
|
||||
return make_openai_call(messages, token_count, fields_to_check, temperature)
|
||||
return make_openai_call(model, messages, token_count, fields_to_check, temperature)
|
||||
elif try_count >= TRY_LIMIT:
|
||||
try_count = 0
|
||||
return result["choices"][0]["message"]["content"]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import whisper
|
||||
import os
|
||||
import gtts
|
||||
from playsound import playsound
|
||||
|
||||
def speech_to_text(file_path):
|
||||
if os.path.exists(file_path):
|
||||
@@ -8,4 +10,8 @@ def speech_to_text(file_path):
|
||||
return result["text"]
|
||||
else:
|
||||
print("File not found:", file_path)
|
||||
raise Exception("File " + file_path + " not found.")
|
||||
raise Exception("File " + file_path + " not found.")
|
||||
|
||||
def text_to_speech(text: str, file_name: str):
|
||||
tts = gtts.gTTS(text)
|
||||
tts.save(file_name)
|
||||
Reference in New Issue
Block a user