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") # https://chat.openai.com/share/195c4549-f67c-4353-be44-65c4d5ad5b05 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 people’s 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 world’s health." # 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) 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." 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." 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'}" 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']}" 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_1', " \ "'question_2', 'question_3']}" 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 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" ] 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.' } } }" gen_reading_passage_1 = "Generate an extensive text for IELTS Reading Passage 1, of at least 1500 words, on the topic " \ "of " + random.choice(topics) + ". The passage should offer a substantial amount of " \ "information, analysis, or narrative " \ "relevant to the chosen subject matter. This text passage aims to serve as the primary reading " \ "section of an IELTS test, providing an in-depth and comprehensive exploration of the topic." \ "Provide your response in this json format: {'title': 'title of the text', 'text': 'generated text'}" gen_reading_passage_2 = "Generate an extensive text for IELTS Reading Passage 2, of at least 1500 words, on the topic " \ "of " + random.choice(topics) + ". The passage should offer a substantial amount of " \ "information, analysis, or narrative " \ "relevant to the chosen subject matter. This text passage aims to serve as the primary reading " \ "section of an IELTS test, providing an in-depth and comprehensive exploration of the topic." \ "Provide your response in this json format: {'title': 'title of the text', 'text': 'generated text'}" gen_reading_passage_3 = "Generate an extensive text for IELTS Reading Passage 2, of at least 1500 words, on the topic " \ "of " + random.choice(topics) + ". The passage should offer a substantial amount of " \ "information, analysis, or narrative " \ "relevant to the chosen subject matter. This text passage aims to serve as the primary reading " \ "section of an IELTS test, providing an in-depth and comprehensive exploration of the topic." \ "Provide your response in this json format: {'title': 'title of the text', 'text': 'generated text'}" sample_text = "In recent years, there has been a significant shift towards renewable energy sources as societies " \ "strive to reduce their dependence on fossil fuels and combat the effects of climate change. Renewable " \ "energy, derived from sources such as sunlight, wind, and water, offers a promising alternative to " \ "traditional energy generation methods.\\nSolar power, harnessed from the sun's rays, has gained immense " \ "popularity. Photovoltaic cells, commonly known as solar panels, capture sunlight and convert it into " \ "electricity. This technology has become more efficient and affordable, contributing to a growing number " \ "of solar installations on rooftops and solar farms. Solar energy not only reduces greenhouse gas " \ "emissions but also grants individuals and businesses the ability to generate their own power.\\nSimilarly, " \ "wind energy has emerged as a reliable and clean source of electricity. Wind turbines, often found in " \ "wind farms, harness the kinetic energy of moving air to turn blades and generate electricity. These " \ "structures are strategically placed in areas with consistent wind patterns, contributing to the overall " \ "energy grid. Wind power has the advantage of scalability, with both small and large installations being " \ "feasible options.\\nHydropower, generated from flowing water in rivers and dams, is another vital renewable " \ "energy source. The force of water is used to turn turbines, which then produce electricity. While hydropower " \ "has a long history, advancements in technology have led to more efficient and environmentally friendly " \ "designs. However, the construction of dams for hydropower can have significant ecological and social " \ "impacts, requiring careful consideration.\\nGeothermal energy, originating from the Earth's heat, is " \ "also gaining attention. This energy source is harnessed through underground steam and hot water reservoirs, " \ "which are tapped for electricity generation and heating. Geothermal power plants have a relatively small " \ "environmental footprint and provide a consistent source of energy, making them a viable option in regions " \ "with suitable geological conditions.\\nIn conclusion, the transition to renewable energy sources marks a " \ "pivotal step towards sustainable and environmentally conscious energy generation. Solar, wind, hydropower, " \ "and geothermal energy offer diverse options that can collectively contribute to reducing carbon emissions " \ "and combating climate change. As technology continues to evolve and economies adapt, the potential for " \ "widespread adoption of renewable energy remains promising." gen_multiple_choice_for_text = "Generate 10 multiple choice questions for this text: '" + sample_text + "'\n" \ "Parse those questions to this format: 'questions': [{'id': '9', 'options': [{'id': 'A', 'text': " \ "'Economic benefits'}, {'id': 'B', 'text': 'Government regulations'}, {'id': 'C', 'text': " \ "'Concerns about climate change'}, {'id': 'D', 'text': 'Technological advancement'}], " \ "'prompt': 'What is the main reason for the shift towards renewable energy sources?', " \ "'solution': 'C', 'variant': 'text'}]" mc_questions = "questions: 1. What is the main reason for the shift towards renewable energy sources? - A. Economic benefits - B. Government regulations - C. Concerns about climate change - D. Technological advancement 2. Which of the following is NOT a renewable energy source mentioned in the text? - A. Solar power - B. Coal - C. Wind energy - D. Hydropower 3. What is the term used to describe the conversion of sunlight into electricity? - A. Solar power - B. Photovoltaic cells - C. Wind turbines - D. Hydropower 4. Which of the following is a benefit of solar energy? - A. Reduced greenhouse gas emissions - B. Increased dependence on fossil fuels - C. Higher energy costs - D. Limited availability 5. How do wind turbines generate electricity? - A. By harnessing the sun's rays - B. By using the force of water - C. By capturing wind - D. By tapping into underground reservoirs 6. What is the advantage of wind power over other renewable energy sources? - A. It is more affordable - B. It is more scalable - C. It has a smaller environmental footprint - D. It is easier to construct 7. Which renewable energy source has a long history but has recently seen advancements in technology? - A. Solar energy - B. Hydropower - C. Wind energy - D. Geothermal energy 8. What is the main drawback of constructing dams for hydropower? - A. Limited availability of suitable locations - B. High cost of construction - C. Negative impacts on the environment - D. Technological limitations 9. What is the source of geothermal energy? - A. The sun's rays - B. Moving air - C. Flowing water - D. The Earth's heat 10. How does geothermal energy differ from other renewable energy sources mentioned in the text? - A. It has a smaller environmental footprint - B. It is more expensive to harness - C. It is not affected by geographical conditions - D. It provides a consistent source of energy." parse_mc_questions = "Parse this '" + mc_questions + "' into this json format: 'questions': [{'id': '9', 'options': [{'id': 'A', 'text': " \ "'Economic benefits'}, {'id': 'B', 'text': 'Government regulations'}, {'id': 'C', 'text': " \ "'Concerns about climate change'}, {'id': 'D', 'text': 'Technological advancement'}], " \ "'prompt': 'What is the main reason for the shift towards renewable energy sources?', " \ "'solution': 'C', 'variant': 'text'}]" token_count = count_tokens(parse_mc_questions)["n_tokens"] response = openai.Completion.create( model="gpt-3.5-turbo-instruct", prompt=parse_mc_questions, max_tokens=int(4097 - token_count - 300), temperature=0.7 ) # processed = process_response(response, "comment") processed = process_response(response["choices"][0]["text"], "transcript") print(processed)