{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "DEPRECATION: Loading egg at c:\\programdata\\anaconda3\\lib\\site-packages\\vboxapi-1.0-py3.11.egg is deprecated. pip 23.3 will enforce this behaviour change. A possible replacement is to use pip for package installation..\n" ] } ], "source": [ "%pip install --upgrade --quiet langchain-core langchain-community langchain-openai python-dotenv gradio" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Requirement already satisfied: python-dotenv==1.0.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (1.0.0)\n", "Requirement already satisfied: gradio in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (4.27.0)\n", "Requirement already satisfied: aiofiles<24.0,>=22.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (22.1.0)\n", "Requirement already satisfied: altair<6.0,>=4.2.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (5.3.0)\n", "Requirement already satisfied: fastapi in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.110.2)\n", "Requirement already satisfied: ffmpy in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.3.2)\n", "Requirement already satisfied: gradio-client==0.15.1 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.15.1)\n", "Requirement already satisfied: httpx>=0.24.1 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.27.0)\n", "Requirement already satisfied: huggingface-hub>=0.19.3 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.22.2)\n", "Requirement already satisfied: importlib-resources<7.0,>=1.3 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (6.4.0)\n", "Requirement already satisfied: jinja2<4.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (3.1.2)\n", "Requirement already satisfied: markupsafe~=2.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (2.1.1)\n", "Requirement already satisfied: matplotlib~=3.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (3.7.2)\n", "Requirement already satisfied: numpy~=1.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (1.24.3)\n", "Requirement already satisfied: orjson~=3.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (3.10.1)\n", "Requirement already satisfied: packaging in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (23.2)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (2.0.3)\n", "Requirement already satisfied: pillow<11.0,>=8.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (10.0.1)\n", "Requirement already satisfied: pydantic>=2.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (2.7.1)\n", "Requirement already satisfied: pydub in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: python-multipart>=0.0.9 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.0.9)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from gradio) (6.0)\n", "Requirement already satisfied: ruff>=0.2.2 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.4.1)\n", "Requirement already satisfied: semantic-version~=2.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (2.10.0)\n", "Requirement already satisfied: tomlkit==0.12.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.12.0)\n", "Requirement already satisfied: typer<1.0,>=0.12 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.12.3)\n", "Requirement already satisfied: typing-extensions~=4.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (4.11.0)\n", "Requirement already satisfied: urllib3~=2.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (2.2.1)\n", "Requirement already satisfied: uvicorn>=0.14.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio) (0.29.0)\n", "Requirement already satisfied: fsspec in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio-client==0.15.1->gradio) (2024.3.1)\n", "Requirement already satisfied: websockets<12.0,>=10.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from gradio-client==0.15.1->gradio) (11.0.3)\n", "Requirement already satisfied: jsonschema>=3.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from altair<6.0,>=4.2.0->gradio) (4.17.3)\n", "Requirement already satisfied: toolz in c:\\programdata\\anaconda3\\lib\\site-packages (from altair<6.0,>=4.2.0->gradio) (0.12.0)\n", "Requirement already satisfied: anyio in c:\\programdata\\anaconda3\\lib\\site-packages (from httpx>=0.24.1->gradio) (3.5.0)\n", "Requirement already satisfied: certifi in c:\\programdata\\anaconda3\\lib\\site-packages (from httpx>=0.24.1->gradio) (2023.11.17)\n", "Requirement already satisfied: httpcore==1.* in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from httpx>=0.24.1->gradio) (1.0.5)\n", "Requirement already satisfied: idna in c:\\programdata\\anaconda3\\lib\\site-packages (from httpx>=0.24.1->gradio) (3.4)\n", "Requirement already satisfied: sniffio in c:\\programdata\\anaconda3\\lib\\site-packages (from httpx>=0.24.1->gradio) (1.2.0)\n", "Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n", "Requirement already satisfied: filelock in c:\\programdata\\anaconda3\\lib\\site-packages (from huggingface-hub>=0.19.3->gradio) (3.9.0)\n", "Requirement already satisfied: requests in c:\\programdata\\anaconda3\\lib\\site-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from huggingface-hub>=0.19.3->gradio) (4.65.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.0.5)\n", "Requirement already satisfied: cycler>=0.10 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (0.11.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (4.25.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.4.4)\n", "Requirement already satisfied: pyparsing<3.1,>=2.3.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (3.0.9)\n", "Requirement already satisfied: python-dateutil>=2.7 in c:\\programdata\\anaconda3\\lib\\site-packages (from matplotlib~=3.0->gradio) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n", "Requirement already satisfied: tzdata>=2022.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n", "Requirement already satisfied: annotated-types>=0.4.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from pydantic>=2.0->gradio) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.18.2 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from pydantic>=2.0->gradio) (2.18.2)\n", "Requirement already satisfied: click>=8.0.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from typer<1.0,>=0.12->gradio) (8.0.4)\n", "Requirement already satisfied: shellingham>=1.3.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n", "Requirement already satisfied: rich>=10.11.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n", "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from fastapi->gradio) (0.37.2)\n", "Requirement already satisfied: colorama in c:\\programdata\\anaconda3\\lib\\site-packages (from click>=8.0.0->typer<1.0,>=0.12->gradio) (0.4.6)\n", "Requirement already satisfied: attrs>=17.4.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (22.1.0)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.0)\n", "Requirement already satisfied: six>=1.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.2.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\\users\\logis\\appdata\\roaming\\python\\python311\\site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.14.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\lib\\site-packages (from requests->huggingface-hub>=0.19.3->gradio) (2.0.4)\n", "Requirement already satisfied: mdurl~=0.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "DEPRECATION: Loading egg at c:\\programdata\\anaconda3\\lib\\site-packages\\vboxapi-1.0-py3.11.egg is deprecated. pip 23.3 will enforce this behaviour change. A possible replacement is to use pip for package installation..\n" ] } ], "source": [ "%pip install \\\n", " python-dotenv==1.0.0 \\\n", " gradio" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "\n", "import os\n", "from dotenv import load_dotenv, find_dotenv\n", "\n", "_ = load_dotenv(find_dotenv()) # read local .env file" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import ChatOpenAI\n", "\n", "model = ChatOpenAI(model=\"gpt-4\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "template = \"\"\"\n", "You are a proffesional IELTS essay evaluator.\n", "Your task is to evaluate an essay text that I provided below according to the International English Language Testing System (IELTS) format. Ensure the essay meets the requirements of an IELTS essay in terms of structure, length, coherence, and language proficiency. Then, grade the essay based on the IELTS scoring system and provide detailed feedback on each criterion.\n", "More detailed evaluation instructions and the essay text are provided below:\n", "\n", "\n", "### EVALUATION INSTRUCTIONS:\n", "1. Check if the essay conforms to the standard IELTS essay format, including introduction, body paragraphs, and conclusion.\n", "Ensure the essay is of appropriate length (minimum 250 words).\n", "Evaluate whether the essay addresses the given topic comprehensively and logically.\n", "\n", "- Task Response (TR): Evaluate how well the essay addresses the task, presents a clear position, and supports it with relevant examples and arguments.\n", "- Coherence and Cohesion (CC): Assess the organization of ideas, logical progression, and coherence between paragraphs and sentences.\n", "- Lexical Resource (LR): Examine the range and accuracy of vocabulary used, including the ability to paraphrase and express ideas precisely.\n", "- Grammatical Range and Accuracy (GRA): Evaluate the variety and accuracy of grammatical structures used, including sentence structure and punctuation.\n", "\n", "2. Provide specific feedback for each criterion, highlighting strengths and areas for improvement.\n", "Offer advice on how the essay can be enhanced to achieve a higher score in each category.\n", "Suggest alternative vocabulary, sentence structures, or argumentative strategies where applicable.\n", "\n", "3. Strictly follow the response structure below, do not say something after you give the scores and feedback. If you have nothing to say, leave it blank. If the text is completely not related to an essay (e.g it is just one sentence) you may not follow the structure and point out the issue.\n", "\n", "\n", "### RESPONSE STRUCTURE\n", "Use only the following answer structure and do not include any personal opinions or biases in the evaluation:\n", "\"Band X.X\n", "Task Response: X\n", "Coherence and Cohesion: X\n", "Lexical Resource: X\n", "Grammatical Range and Accuracy: X\n", "Detailed Feedback: [Provide detailed feedback here]\"\n", "\n", "\n", "### EXAMPLE RESPONSE:\n", "\"\n", "Band 7.5\n", "Task Response: 7\n", "Coherence and Cohesion: 8\n", "Lexical Resource: 8\n", "Grammatical Range and Accuracy: 7\n", "The test taker presents a clear position at the outset and explores some ideas to support this. An\n", "alternative position is also considered, but rejected. This is a strong response, but there is rather\n", "too much emphasis on technology: other aspects of the proposition could also be considered,\n", "e.g. less physical work and more sedentary work, greater reliance on cars meaning less\n", "exercise, aging populations in some countries leading to more complex health issues. Ideas are\n", "organised logically and there is a clear progression throughout the response, with good use of\n", "cohesive devices and logical paragraphing. The response could perhaps be improved by\n", "breaking down paragraphs 2 and 3. There is a wide range of vocabulary with good use of less\n", "common items as well as evidence of higher level features, such as ‘softening’, e.g. ‘They tend\n", "to’, ‘This appears to be’, and ‘might disagree’. Errors in spelling and word formation are rare.\n", "There is also a variety of complex structures with frequent error-free sentences, though some\n", "errors do occur and there is some overuse of rather short sentence forms.\n", "\"\n", "\n", "### ESSAY TEXT:\n", "{essay_text}\n", "\"\"\"\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "essay_text = \"\"\"\n", "Recently, there have been a lot of discussions about health and whether it is going to\n", "improve or not. In my opinion, I think that people will become unhealthier in the future than\n", "they are now.\n", "There are many reasons that support the idea of people becoming unhealthy in the future.\n", "Firstly, one reason is that of food. People tend to eat more fast food nowadays. They tend to\n", "treat themselves with sweets and chocolate whenever they want. This appears to be\n", "because people are busier now than they used to be. So, people don’t have a chance to\n", "cook or even learn the art of cookery. Also, having a lot of unhealthy food can lead to obesity\n", "and it could be a serious issue in the future. Another reason is that technology is developing\n", "everyday. Young people enjoy buying new gadgets and the latest devices. This has a\n", "negative impact on their health, especially when they enjoy video games. Spending long\n", "hours looking at a screen can lead to bad eyesight and obesity as well. Yet another reason\n", "is that laziness is a big issue. Different forms of exercise might disappear in the future\n", "because people don’t like sports. Also, people prefer spending most of their time on the\n", "internet and the internet is growing every single day.\n", "Other people might disagree and say that health will improve in the future. They believe\n", "that new sports and new ways to exercise will appear in the future. However, I don’t think it\n", "can happen since the majority of people spend less time outdoors. Moreover, other people\n", "believe that technology will try and help people improve their health. For example, there\n", "have been some games released on the Wii console that makes people exercise but\n", "technology is developing more in a negative way. For instance, many phone industries are\n", "developing new applications everyday and today’s generation likes to follow every trend.\n", "This prevents people to go outside to exercise. They like to spend more time on the internet\n", "downloading new programmes or reading gossips about celebraties. This affects people’s\n", "health badly.\n", "In conclusion, I believe that people’s health is affected negatively by fast food, technology\n", "and sports and it will be a problem in the future.\"\"\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "prompt = ChatPromptTemplate.from_template(template)\n", "output_parser = StrOutputParser()\n", "\n", "chain = prompt | model | output_parser\n", "\n", "result = chain.invoke({\"essay_text\": essay_text})" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Band 6.5\n", "Task Response: 6\n", "Coherence and Cohesion: 7\n", "Lexical Resource: 6\n", "Grammatical Range and Accuracy: 7\n", "Detailed Feedback: The essay does follow the standard IELTS format, with an introduction, body paragraphs, and conclusion, and it addresses the task relatively well. The author presents a clear position and supports it with some relevant examples. However, the argument could be more comprehensive and logically structured. The essay also slightly lacks depth in its exploration of the topic. \n", "\n", "The organization of ideas and logical progression is quite good, with each paragraph discussing a different aspect of the argument. However, some points are not developed fully and transition between ideas could be smoother. \n", "\n", "The range of vocabulary is adequate but could be more varied and precise. There are a few instances of awkward phrasing and incorrect word usage, such as \"celebraties\" instead of \"celebrities\". The author could benefit from using more academic language and formal expressions. \n", "\n", "The grammatical range is good, with a variety of sentence structures employed. However, there are a few minor errors and inaccuracies. The author could improve their score by proofreading their work for grammar and spelling mistakes and by using more complex, varied sentence structures. It would also be beneficial to avoid repetitions and aim for a more academic writing style. \n", "\n", "Overall, the essay is a satisfactory response to the task, but there is room for improvement in all areas. The author should work on developing their argument more fully, improving the flow of ideas, expanding their vocabulary, and polishing their grammar.\n" ] } ], "source": [ "print(result)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7867\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "