Add new writing question on generate questions

This commit is contained in:
Cristiano Ferreira
2023-09-27 22:24:55 +01:00
parent 51085619ee
commit d6b75de856
2 changed files with 198 additions and 90 deletions

View File

@@ -1,102 +1,174 @@
import random
from functools import reduce
import openai
import os
from dotenv import load_dotenv
from app import GRADING_FIELDS, GRADING_TEMPERATURE
from helper.api_messages import get_grading_messages, QuestionType
from helper.openai_interface import make_openai_call, process_response
from helper.token_counter import count_tokens
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
question = "The average standard of people's health is likely to be lower in the future than it is now. To what extent do " \
"you agree or disagree with this statement?"
answer = "I completely disagree with the written statement. I believe that most of the people in the world have more information " \
"about their health and also about how they can improve their healthy conditions. Nowadays, information about " \
"how harmful is to smoke for our bodies can be seen in many packets of cigars. This is a clear example how " \
"things can change from our recent past. There is a clear trend in the diminishing of smokers and if this " \
"continues it will have a positive impact in our health. On the other hand, the alimentation habbits are " \
"changing all over the world and this can affect peoples health. However every one can choose what to eat " \
"every day. Mostly everybody, from developed societies, know the importance of having a healthy diet. Advances " \
"such as the information showed in the menus of fast food restaurants will help people to have a clever choice " \
"before they choose what to eat. Another important issue that I would like to mention is how medicine is changing. " \
"There are new discovers and treatments almost every week and that is an inequivoque sintom of how things are " \
"changing in order to improve the worlds health."
def generate_summarizer(
max_tokens,
temperature,
top_p,
frequency_penalty,
question_type,
question,
answer
):
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
max_tokens=int(max_tokens),
temperature=float(temperature),
top_p=float(top_p),
frequency_penalty=float(frequency_penalty),
messages=
[
{
"role": "user",
"content": "You are a IELTS examiner.",
},
{
"role": "user",
"content": f"The question you have to grade is of type {question_type} and is the following: {question}",
},
{
"role": "user",
"content": "Please provide a JSON object response with the overall grade and breakdown grades, "
"formatted as follows: {'overall': 7.0, 'task_response': {'Task Achievement': 8.0, "
"'Coherence and Cohesion': 6.5, 'Lexical Resource': 7.5, 'Grammatical Range and Accuracy': "
"6.0}}",
},
{
"role": "user",
"content": "Don't give explanations for the grades, just provide the json with the grades.",
},
{
"role": "user",
"content": f"Evaluate this answer according to ielts grading system: {answer}",
},
],
)
return res["choices"][0]["message"]["content"]
# messages = get_grading_messages(QuestionType.WRITING_TASK_2, question, answer)
# 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-4", messages, token_count, GRADING_FIELDS, GRADING_TEMPERATURE)
grade_wt2_message = "You are a IELTS examiner. " \
"The question you have to grade is of type Writing Task 2 and is the following:" + question + \
"Assess this answer according to the IELTS grading system. It is mandatory for you to provide your response" \
" with the overall grade and breakdown grades, with just the following json format: {'comment': 'Comment about " \
"answer quality will go here', 'overall': 7.0, 'task_response': {'Task Achievement': 8.0, " \
"'Coherence and Cohesion': 6.5, 'Lexical Resource': 7.5, 'Grammatical Range and Accuracy': 6.0}} " \
"Evaluate this answer according to ielts grading system: " + answer
import streamlit as st
topics = [
"Art and Creativity",
"History of Ancient Civilizations",
"Environmental Conservation",
"Space Exploration",
"Artificial Intelligence",
"Climate Change",
"World Religions",
"The Human Brain",
"Renewable Energy",
"Cultural Diversity",
"Modern Technology Trends",
"Women's Rights",
"Sustainable Agriculture",
"Globalization",
"Natural Disasters",
"Cybersecurity",
"Philosophy of Ethics",
"Robotics",
"Health and Wellness",
"Literature and Classics",
"World Geography",
"Music and Its Influence",
"Human Rights",
"Social Media Impact",
"Food Sustainability",
"Economics and Markets",
"Human Evolution",
"Political Systems",
"Mental Health Awareness",
"Quantum Physics",
"Biodiversity",
"Education Reform",
"Animal Rights",
"The Industrial Revolution",
"Future of Work",
"Film and Cinema",
"Genetic Engineering",
"Ancient Mythology",
"Climate Policy",
"Space Travel",
"Renewable Energy Sources",
"Cultural Heritage Preservation",
"Modern Art Movements",
"Immigration Issues",
"Sustainable Transportation",
"The History of Medicine",
"Artificial Neural Networks",
"Climate Adaptation",
"Philosophy of Existence",
"Augmented Reality",
"Yoga and Meditation",
"Literary Genres",
"World Oceans",
"Gender Equality",
"Social Networking",
"Sustainable Fashion",
"International Trade",
"Prehistoric Era",
"Democracy and Governance",
"Postcolonial Literature",
"Geopolitics",
"Psychology and Behavior",
"Nanotechnology",
"Endangered Species",
"Education Technology",
"Renaissance Art",
"Renewable Energy Policy",
"Cultural Festivals",
"Modern Architecture",
"Climate Resilience",
"Artificial Life",
"Fitness and Nutrition",
"Classic Literature Adaptations",
"World History Wars",
"Ethical Dilemmas",
"Internet of Things (IoT)",
"Meditation Practices",
"Literary Symbolism",
"Marine Conservation",
"Social Justice Movements",
"Sustainable Tourism",
"International Finance",
"Ancient Philosophy",
"Cold War Era",
"Behavioral Economics",
"Space Colonization",
"Clean Energy Initiatives",
"Cultural Exchange",
"Modern Sculpture",
"Climate Mitigation",
"Artificial Intelligence Ethics",
"Mindfulness",
"Literary Criticism",
"Wildlife Conservation",
"Political Activism",
"Renewable Energy Innovations",
"History of Mathematics",
"Human-Computer Interaction",
"Global Health",
"Cultural Appropriation"
]
# Set the application title
st.title("GPT-3.5 IELTS Examiner")
gen_listening2_message = "You are an IELTS program designed to assist with language learning. Your task is to provide a detailed " \
"transcript of a monologue on the subject of " + random.choice(topics) + ". Ensure that the transcript is comprehensive " \
"and covers the topic " \
"thoroughly.After the transcript, you should generate a fill in the blanks " \
"exercise with six statements related to the content of the monologue. The blank spaces in the exercise should " \
"be identified by {{number}}. Finally, provide the answers for the exercise." \
"Response Format (JSON): { 'transcript': 'Transcript of the monologue on a specific subject with a minimum of 500 words.', '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 the blank space in statement 1.', " \
"'2': 'Answer to fill the blank space in statement 2.', '3': 'Answer to fill the blank space in statement 3.'," \
"'4': 'Answer to fill the blank space in statement 4.', '5': 'Answer to fill the blank space in statement 5.'," \
"'6': 'Answer to fill the blank space in statement 6.' } } }"
# qt_col, q_col = st.columns(2)
token_count = count_tokens(gen_listening2_message)["n_tokens"]
# Selection box to select the question type
# with qt_col:
question_type = st.selectbox(
"What is the question type?",
(
"Listening",
"Reading",
"Writing Task 1",
"Writing Task 2",
"Speaking Part 1",
"Speaking Part 2"
),
)
response = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
prompt=gen_listening2_message,
max_tokens=int(4097 - token_count - 300),
temperature=0.7
)["choices"][0]["text"]
# Provide the input area for question to be answered
# with q_col:
question = st.text_area("Enter the question:", height=100)
# processed = process_response(response, "comment")
processed = process_response(response, "transcript")
print(processed)
# Provide the input area for text to be summarized
answer = st.text_area("Enter the answer:", height=100)
# Initiate two columns for section to be side-by-side
# col1, col2 = st.columns(2)
# Slider to control the model hyperparameter
# with col1:
token = st.slider("Token", min_value=0.0, max_value=2000.0, value=1000.0, step=1.0)
temp = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7, step=0.01)
top_p = st.slider("Top_p", min_value=0.0, max_value=1.0, value=0.9, step=0.01)
f_pen = st.slider("Frequency Penalty", min_value=-1.0, max_value=1.0, value=0.5, step=0.01)
# Showing the current parameter used for the model
# with col2:
with st.expander("Current Parameter"):
st.write("Current Token :", token)
st.write("Current Temperature :", temp)
st.write("Current Nucleus Sampling :", top_p)
st.write("Current Frequency Penalty :", f_pen)
# Creating button for execute the text summarization
if st.button("Grade"):
st.write(generate_summarizer(token, temp, top_p, f_pen, question_type, question, answer))