{ "cells": [ { "cell_type": "markdown", "id": "68e9310f-109d-4f30-b263-d1e6c058ee80", "metadata": {}, "source": [ "# Setup" ] }, { "cell_type": "code", "execution_count": 1, "id": "6805b3b5-782b-437c-82b3-9392abb5a599", "metadata": { "tags": [] }, "outputs": [], "source": [ "# %pip install -q -r requirements.txt" ] }, { "cell_type": "markdown", "id": "94f0fcdd-1653-440e-8ebc-9c33d931163a", "metadata": {}, "source": [ "## Config" ] }, { "cell_type": "code", "execution_count": 2, "id": "5d0bd22f-293e-4c15-9dfe-8070553f42b5", "metadata": { "tags": [] }, "outputs": [], "source": [ "INPUT_DATASET = 'derek-thomas/labeled-multiple-choice-explained-mistral-reasoning'\n", "REVISION = '536f3b8'\n", "OUTPUT_DATASET = 'derek-thomas/labeled-multiple-choice-explained-mistral-tokenized'" ] }, { "cell_type": "code", "execution_count": 3, "id": "a1fc7a29-6b60-446d-b708-012f897de6a9", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "85ca07b896804801b5ccd629341e965c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
formatted_questioncombined_factanswer_keytopicgpt3_5_reasoningquestion_textanswer_choicesmistral_reasoning
0what is satellite technology used for predicti...satellite technology is used for predicting wh...cTechnologya) Seconds and minutes: This option is incorre...What is satellite technology used for predicting?(a) Seconds and minutes (b) The strength and m...Incorrect answers and explanations:\\n\\n1. Elec...
1what does irradiating food do? (a) relieve pai...irradiated food improves food safety.cFood science(a) Relieve pain: This option is not correct b...What does irradiating food do?(a) Relieve pain (b) Enhance food's nutrients ...Sure, let's examine each answer and justify wh...
2what protects a mammal's skin? (a) fiber folli...fiber follicles protect mammal skinaBiologyb) Exfoliation: Exfoliation is the process of ...What protects a mammal's skin?(a) Fiber follicles (b) Exfoliation (c) Resist...Sure, let's go through each of the provided an...
3what do earthworms do when a segment breaks of...earthworms can regrow segments that break offbBiologya) Dies: This option is not correct because ea...What do earthworms do when a segment breaks off?(a) Dies (b) Regrows it (c) Reproduces (d) Sed...1. Reading the question carefully, we can see ...
4lightning can be bad for what? (a) the environ...lightning can be bad for the environment.aElectricityb) Rainstorms: Lightning is actually a natural...Lightning can be bad for what?(a) The environment (b) Rainstorms (c) Destruc...1. Food: While essential for the growth and he...
...........................
8408organisms that can cause infection do what? (a...organisms that can cause infection make humans...gBiologya) Bandaging open sores is not the correct ans...Organisms that can cause infection do what?(a) Bandage open sores (b) Keep flesh clean (c...1. Read the question and options carefully: Th...
8409fungi are living things that cannot make thei...fungi are living things that cannot make their...aBiologyb) Fungi are living things that can make their...Fungi are living things that cannot make their...(a) Food (b) Cells (c) Energy (d) Fruits (e) H...1. Read the question and options carefully: Th...
8410an overheated body can use water for: (a) meta...the evaporation of water from the skin cools t...gBiologya) Metabolic reaction: This option is incorrec...An overheated body can use water for:?(a) Metabolic reaction (b) Dehydrating (c) Rai...1. Read the question and options carefully: Th...
8411what is essential for cellular respiration for...plants are essential for cellular respiration ...fBiologya) Electrons are involved in cellular respirat...What is essential for cellular respiration for...(a) Electron (b) Glucose (c) Energy (d) Energy...1. First, let's read the question and options ...
8412what helps insulate and protect the body? (a) ...living cells in follicles help insulate and pr...bBiologya) H2O: Water is essential for life, but it do...What helps insulate and protect the body?(a) H2o (b) Living cells in follicles (c) Laye...1. Read the question and options carefully: Th...
\n", "

8413 rows × 8 columns

\n", "" ], "text/plain": [ " formatted_question \\\n", "0 what is satellite technology used for predicti... \n", "1 what does irradiating food do? (a) relieve pai... \n", "2 what protects a mammal's skin? (a) fiber folli... \n", "3 what do earthworms do when a segment breaks of... \n", "4 lightning can be bad for what? (a) the environ... \n", "... ... \n", "8408 organisms that can cause infection do what? (a... \n", "8409 fungi are living things that cannot make thei... \n", "8410 an overheated body can use water for: (a) meta... \n", "8411 what is essential for cellular respiration for... \n", "8412 what helps insulate and protect the body? (a) ... \n", "\n", " combined_fact answer_key \\\n", "0 satellite technology is used for predicting wh... c \n", "1 irradiated food improves food safety. c \n", "2 fiber follicles protect mammal skin a \n", "3 earthworms can regrow segments that break off b \n", "4 lightning can be bad for the environment. a \n", "... ... ... \n", "8408 organisms that can cause infection make humans... g \n", "8409 fungi are living things that cannot make their... a \n", "8410 the evaporation of water from the skin cools t... g \n", "8411 plants are essential for cellular respiration ... f \n", "8412 living cells in follicles help insulate and pr... b \n", "\n", " topic gpt3_5_reasoning \\\n", "0 Technology a) Seconds and minutes: This option is incorre... \n", "1 Food science (a) Relieve pain: This option is not correct b... \n", "2 Biology b) Exfoliation: Exfoliation is the process of ... \n", "3 Biology a) Dies: This option is not correct because ea... \n", "4 Electricity b) Rainstorms: Lightning is actually a natural... \n", "... ... ... \n", "8408 Biology a) Bandaging open sores is not the correct ans... \n", "8409 Biology b) Fungi are living things that can make their... \n", "8410 Biology a) Metabolic reaction: This option is incorrec... \n", "8411 Biology a) Electrons are involved in cellular respirat... \n", "8412 Biology a) H2O: Water is essential for life, but it do... \n", "\n", " question_text \\\n", "0 What is satellite technology used for predicting? \n", "1 What does irradiating food do? \n", "2 What protects a mammal's skin? \n", "3 What do earthworms do when a segment breaks off? \n", "4 Lightning can be bad for what? \n", "... ... \n", "8408 Organisms that can cause infection do what? \n", "8409 Fungi are living things that cannot make their... \n", "8410 An overheated body can use water for:? \n", "8411 What is essential for cellular respiration for... \n", "8412 What helps insulate and protect the body? \n", "\n", " answer_choices \\\n", "0 (a) Seconds and minutes (b) The strength and m... \n", "1 (a) Relieve pain (b) Enhance food's nutrients ... \n", "2 (a) Fiber follicles (b) Exfoliation (c) Resist... \n", "3 (a) Dies (b) Regrows it (c) Reproduces (d) Sed... \n", "4 (a) The environment (b) Rainstorms (c) Destruc... \n", "... ... \n", "8408 (a) Bandage open sores (b) Keep flesh clean (c... \n", "8409 (a) Food (b) Cells (c) Energy (d) Fruits (e) H... \n", "8410 (a) Metabolic reaction (b) Dehydrating (c) Rai... \n", "8411 (a) Electron (b) Glucose (c) Energy (d) Energy... \n", "8412 (a) H2o (b) Living cells in follicles (c) Laye... \n", "\n", " mistral_reasoning \n", "0 Incorrect answers and explanations:\\n\\n1. Elec... \n", "1 Sure, let's examine each answer and justify wh... \n", "2 Sure, let's go through each of the provided an... \n", "3 1. Reading the question carefully, we can see ... \n", "4 1. Food: While essential for the growth and he... \n", "... ... \n", "8408 1. Read the question and options carefully: Th... \n", "8409 1. Read the question and options carefully: Th... \n", "8410 1. Read the question and options carefully: Th... \n", "8411 1. First, let's read the question and options ... \n", "8412 1. Read the question and options carefully: Th... \n", "\n", "[8413 rows x 8 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load dataset from Hugging Face Hub\n", "dataset = load_dataset(INPUT_DATASET, split='train')\n", "\n", "# Convert to pandas dataframe\n", "df = dataset.to_pandas()\n", "print(f\"Before Cleaning: {len(df)} rows\")\n", "\n", "# Drop the __index_level_0__ column if it exists\n", "df.drop(columns=['mistral_reasoning_prompt'], errors='ignore', inplace=True)\n", "\n", "# Ensure all values in 'formatted_question' are strings\n", "df.rename(columns={\n", " 'explanation': 'gpt3_5_reasoning',\n", "}, inplace=True)\n", "df" ] }, { "cell_type": "markdown", "id": "2511bc04-f611-4dc7-b3ed-e477907b0200", "metadata": {}, "source": [ "## Create Prompts from Processed Data" ] }, { "cell_type": "markdown", "id": "d124c7cf-a369-46a9-94db-069894145959", "metadata": {}, "source": [ "We need to convert our sample into a format similar to below for each of the scenarios.\n", "\n", "```\n", "[\n", " {\"content\": user_content, \"role\": \"user\"},\n", " {\"content\": assistant_response, \"role\": \"assistant\"}\n", "]\n", "```\n", "\n", "We should include a helpful system_prompt with a general trivia prefix, and a suffix that contains instructions that fit each scenario.\n", "The `user_content` will have the Question and answer choices.\n", "The `assistant_response` should reflect the scenario. " ] }, { "cell_type": "code", "execution_count": 7, "id": "1c6554a6-4717-4bf0-ae51-102630d40fd7", "metadata": { "tags": [] }, "outputs": [], "source": [ "df['user_prompt'] = df.apply(lambda row: f\"Question: {row['question_text']}\\nAnswer Choices: {row['answer_choices']}\", axis=1)" ] }, { "cell_type": "markdown", "id": "1391b7e1-2462-41e3-b2f0-5a8f6c95859b", "metadata": {}, "source": [ "Here we need to create the structure of our conversation. Each system prompt should reflect the instructions we want, so we can start with a prefix and add in the specifics for each scenario." ] }, { "cell_type": "markdown", "id": "2c817304-9ecc-43d9-83b9-5bb8dd662797", "metadata": {}, "source": [ "### Reasoning Final Answer Structured Generation" ] }, { "cell_type": "code", "execution_count": 8, "id": "878727dc-4801-4376-be26-1cc601cb5f92", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INST] Answer the Question and include your Reasoning and the Final Answer in a json like: {\"Reasoning: \"...\", \"Final Answer\": \"x\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.\n", "Question: What is satellite technology used for predicting?\n", "Answer Choices: (a) Seconds and minutes (b) The strength and magnitude of an earthquake (c) What it's like outside each day (d) 70-75 degrees fahrenheit (e) Rapid changes occur (f) Dead-ends and false starts. (g) Snow, ice, and rock (h) Around 5 to 27 degrees celsius[/INST] {'Reasoning': \"a) Seconds and minutes: This option is incorrect because satellite technology is not used for predicting time intervals. Satellite technology is used for various purposes such as communication, navigation, and weather forecasting, but it is not used for predicting time intervals.\\n\\nb) The strength and magnitude of an earthquake: This option is incorrect because satellite technology is not used for predicting earthquakes. Earthquake prediction is a complex process that involves seismology and other scientific methods, but satellite technology is not one of them.\\n\\nd) 70-75 degrees Fahrenheit: This option is incorrect because satellite technology is not used for predicting specific temperature ranges. While satellite technology can provide temperature data, it is not used for predicting specific temperature ranges.\\n\\ne) Rapid changes occur: This option is too vague and does not provide enough information to determine whether it is correct or not. Satellite technology can be used to monitor changes in various environmental factors, but it is not used specifically for predicting rapid changes.\\n\\nf) Dead-ends and false starts: This option is incorrect because it is not related to satellite technology or any type of prediction.\\n\\ng) Snow, ice, and rock: This option is incorrect because it is too specific and does not cover the broad range of predictions that satellite technology can be used for. While satellite technology can be used to monitor snow, ice, and rock formations, it is not used exclusively for this purpose.\\n\\nh) Around 5 to 27 degrees Celsius: This option is incorrect because it is too specific and does not cover the broad range of temperature predictions that satellite technology can be used for. While satellite technology can provide temperature data, it is not used exclusively for predicting temperatures within a specific range.\\n\\nTherefore, the correct answer is c) what it's like outside each day, as satellite technology is commonly used for weather forecasting and predicting daily weather conditions.\", 'Final Answer': 'c'}\n", "\n", "\n", "\n", "[INST] Answer the Question and include your Reasoning and the Final Answer in a json like: {\"Reasoning: \"...\", \"Final Answer\": \"x\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.\n", "Question: What is satellite technology used for predicting?\n", "Answer Choices: (a) Seconds and minutes (b) The strength and magnitude of an earthquake (c) What it's like outside each day (d) 70-75 degrees fahrenheit (e) Rapid changes occur (f) Dead-ends and false starts. (g) Snow, ice, and rock (h) Around 5 to 27 degrees celsius[/INST] {'Reasoning': 'Incorrect answers and explanations:\\n\\n1. Electrical energy: Cells are not visible with electrical energy. Cells are physical structures and need to be observed visually, not through electrical means.\\n\\n2. By indirect light or Bright lights: Cells are so small that they are not visible to the naked eye under normal lighting conditions. Indirect light or bright lights are not a viable method to observe cells.\\n\\n3. Camera lenses: Camera lenses are used to focus and capture images, not to observe live objects. In order to observe cells, they must be viewed through a microscope which has a lens.\\n\\n4. Colors: Because cells are transparent, they do not have any specific color. They can appear as white or translucent when observed under a microscope.\\n\\n5. Telescope: A telescope is used for observing distant stars, galaxies, and other celestial objects, not for observing cells which are much smaller and earthbound.\\n\\n6. Rays or beams: Cells cannot be observed using rays or beams. Observation of cells is usually done via light microscopy.\\n\\n7. None of the above do not provide the means to observe cells, as they are not the tools or methods designed for cellular observation.\\n\\nThe correct answer (d) A microscope is the tool that allows us to see cells due to its magnifying capabilities making the cells visible to the naked eye effectively.', 'Final Answer': 'c'}\n" ] } ], "source": [ "# Define system prompt\n", "system_prompt_RFA = 'Answer the Question and include your Reasoning and the Final Answer in a json like: {\"Reasoning: \"...\", \"Final Answer\": \"x\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.'\n", "\n", "df['assistant_prompt_RFA_gpt3_5'] = df.apply(lambda row: {\"Reasoning\": row[\"gpt3_5_reasoning\"].strip(), \"Final Answer\": row[\"answer_key\"]}, axis=1)\n", "df['assistant_prompt_RFA_mistral'] = df.apply(lambda row: {\"Reasoning\": row[\"mistral_reasoning\"].strip(), \"Final Answer\": row[\"answer_key\"]}, axis=1)\n", "\n", "# Step 1: Create user prompt for both gpt3_5 and mistral\n", "df['user_prompt_RFA'] = df.apply(lambda row: {\n", " \"content\": system_prompt_RFA + '\\n' + row['user_prompt'],\n", " \"role\": \"user\"\n", "}, axis=1)\n", "\n", "# Step 2: Create conversation_RFA column using user_prompt_RFA\n", "df['conversation_RFA_gpt3_5'] = df.apply(lambda row: tokenizer.apply_chat_template([\n", " row['user_prompt_RFA'], # Use the precomputed user prompt\n", " {\"content\": row['assistant_prompt_RFA_gpt3_5'], \"role\": \"assistant\"}\n", "], tokenize=False), axis=1)\n", "\n", "df['conversation_RFA_mistral'] = df.apply(lambda row: tokenizer.apply_chat_template([\n", " row['user_prompt_RFA'], # Use the precomputed user prompt\n", " {\"content\": row['assistant_prompt_RFA_mistral'], \"role\": \"assistant\"}\n", "], tokenize=False), axis=1)\n", "\n", "df['user_prompt_RFA'] = df['user_prompt_RFA'].apply(lambda row: tokenizer.apply_chat_template([row], tokenize=False))\n", "\n", "df.drop(['assistant_prompt_RFA_gpt3_5', 'assistant_prompt_RFA_mistral'], inplace=True, axis=1)\n", "\n", "# Example output\n", "gpt3_5_example = df['conversation_RFA_gpt3_5'].iloc[0]\n", "mistral_example = df['conversation_RFA_mistral'].iloc[0]\n", "\n", "print(gpt3_5_example)\n", "print('\\n\\n')\n", "print(mistral_example)" ] }, { "cell_type": "markdown", "id": "a1150ae9-cc22-44c6-8a36-0e3d7c16111b", "metadata": {}, "source": [ "### Final Answer Reasoning Structured Generation" ] }, { "cell_type": "code", "execution_count": 9, "id": "c0234242-7389-4c28-ae85-78a82498d0ac", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INST] Answer the Question and include your Final Answer and the Reasoning in a json like: {\"Final Answer\": \"x\", \"Reasoning: \"...\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.\n", "Question: What is satellite technology used for predicting?\n", "Answer Choices: (a) Seconds and minutes (b) The strength and magnitude of an earthquake (c) What it's like outside each day (d) 70-75 degrees fahrenheit (e) Rapid changes occur (f) Dead-ends and false starts. (g) Snow, ice, and rock (h) Around 5 to 27 degrees celsius[/INST] {'Final Answer': 'c', 'Reasoning': \"a) Seconds and minutes: This option is incorrect because satellite technology is not used for predicting time intervals. Satellite technology is used for various purposes such as communication, navigation, and weather forecasting, but it is not used for predicting time intervals.\\n\\nb) The strength and magnitude of an earthquake: This option is incorrect because satellite technology is not used for predicting earthquakes. Earthquake prediction is a complex process that involves seismology and other scientific methods, but satellite technology is not one of them.\\n\\nd) 70-75 degrees Fahrenheit: This option is incorrect because satellite technology is not used for predicting specific temperature ranges. While satellite technology can provide temperature data, it is not used for predicting specific temperature ranges.\\n\\ne) Rapid changes occur: This option is too vague and does not provide enough information to determine whether it is correct or not. Satellite technology can be used to monitor changes in various environmental factors, but it is not used specifically for predicting rapid changes.\\n\\nf) Dead-ends and false starts: This option is incorrect because it is not related to satellite technology or any type of prediction.\\n\\ng) Snow, ice, and rock: This option is incorrect because it is too specific and does not cover the broad range of predictions that satellite technology can be used for. While satellite technology can be used to monitor snow, ice, and rock formations, it is not used exclusively for this purpose.\\n\\nh) Around 5 to 27 degrees Celsius: This option is incorrect because it is too specific and does not cover the broad range of temperature predictions that satellite technology can be used for. While satellite technology can provide temperature data, it is not used exclusively for predicting temperatures within a specific range.\\n\\nTherefore, the correct answer is c) what it's like outside each day, as satellite technology is commonly used for weather forecasting and predicting daily weather conditions.\"}\n", "\n", "\n", "\n", "[INST] Answer the Question and include your Final Answer and the Reasoning in a json like: {\"Final Answer\": \"x\", \"Reasoning: \"...\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.\n", "Question: What is satellite technology used for predicting?\n", "Answer Choices: (a) Seconds and minutes (b) The strength and magnitude of an earthquake (c) What it's like outside each day (d) 70-75 degrees fahrenheit (e) Rapid changes occur (f) Dead-ends and false starts. (g) Snow, ice, and rock (h) Around 5 to 27 degrees celsius[/INST] {'Final Answer': 'c', 'Reasoning': 'Incorrect answers and explanations:\\n\\n1. Electrical energy: Cells are not visible with electrical energy. Cells are physical structures and need to be observed visually, not through electrical means.\\n\\n2. By indirect light or Bright lights: Cells are so small that they are not visible to the naked eye under normal lighting conditions. Indirect light or bright lights are not a viable method to observe cells.\\n\\n3. Camera lenses: Camera lenses are used to focus and capture images, not to observe live objects. In order to observe cells, they must be viewed through a microscope which has a lens.\\n\\n4. Colors: Because cells are transparent, they do not have any specific color. They can appear as white or translucent when observed under a microscope.\\n\\n5. Telescope: A telescope is used for observing distant stars, galaxies, and other celestial objects, not for observing cells which are much smaller and earthbound.\\n\\n6. Rays or beams: Cells cannot be observed using rays or beams. Observation of cells is usually done via light microscopy.\\n\\n7. None of the above do not provide the means to observe cells, as they are not the tools or methods designed for cellular observation.\\n\\nThe correct answer (d) A microscope is the tool that allows us to see cells due to its magnifying capabilities making the cells visible to the naked eye effectively.'}\n" ] } ], "source": [ "system_prompt_FAR = 'Answer the Question and include your Final Answer and the Reasoning in a json like: {\"Final Answer\": \"x\", \"Reasoning: \"...\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.'\n", "\n", "df['assistant_prompt_FAR_gpt3_5'] = df.apply(lambda row: {\"Final Answer\": row[\"answer_key\"], \"Reasoning\": row[\"gpt3_5_reasoning\"].strip()}, axis=1)\n", "df['assistant_prompt_FAR_mistral'] = df.apply(lambda row: {\"Final Answer\": row[\"answer_key\"], \"Reasoning\": row[\"mistral_reasoning\"].strip()}, axis=1)\n", "\n", "# Step 1: Create user_prompt_FAR column\n", "df['user_prompt_FAR'] = df.apply(lambda row: {\n", " \"content\": system_prompt_FAR + '\\n' + row['user_prompt'],\n", " \"role\": \"user\"\n", "}, axis=1)\n", "\n", "# Step 2: Create conversation_FAR column using user_prompt_FAR\n", "df['conversation_FAR_gpt3_5'] = df.apply(lambda row: tokenizer.apply_chat_template([\n", " row['user_prompt_FAR'], # Use the precomputed user prompt\n", " {\"content\": row['assistant_prompt_FAR_gpt3_5'], \"role\": \"assistant\"}\n", "], tokenize=False), axis=1)\n", "\n", "df['conversation_FAR_mistral'] = df.apply(lambda row: tokenizer.apply_chat_template([\n", " row['user_prompt_FAR'], # Use the precomputed user prompt\n", " {\"content\": row['assistant_prompt_FAR_mistral'], \"role\": \"assistant\"}\n", "], tokenize=False), axis=1)\n", "\n", "df['user_prompt_FAR'] = df['user_prompt_FAR'].apply(lambda row: tokenizer.apply_chat_template([row], tokenize=False))\n", "\n", "df.drop(['assistant_prompt_FAR_gpt3_5', 'assistant_prompt_FAR_mistral'], inplace=True, axis=1)\n", "\n", "# Example output\n", "gpt3_5_example = df['conversation_FAR_gpt3_5'].iloc[0]\n", "mistral_example = df['conversation_FAR_mistral'].iloc[0]\n", "\n", "print(gpt3_5_example)\n", "print('\\n\\n')\n", "print(mistral_example)" ] }, { "cell_type": "markdown", "id": "2c9f0619-1467-4b4e-ad69-b5ca2c4e58a7", "metadata": {}, "source": [ "### Final Answer Structured Generation" ] }, { "cell_type": "code", "execution_count": 10, "id": "64dd3601-a40e-478d-97a5-5728005e5787", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INST] Answer the Question and include your Final Answer in a json like: {\"Final Answer\": \"x\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.\n", "Question: What is satellite technology used for predicting?\n", "Answer Choices: (a) Seconds and minutes (b) The strength and magnitude of an earthquake (c) What it's like outside each day (d) 70-75 degrees fahrenheit (e) Rapid changes occur (f) Dead-ends and false starts. (g) Snow, ice, and rock (h) Around 5 to 27 degrees celsius[/INST] {'Final Answer': 'c'}\n" ] } ], "source": [ "system_prompt_FA = 'Answer the Question and include your Final Answer in a json like: {\"Final Answer\": \"x\"} where x is a letter that corresponds to the answer choice which is a letter between a and h.'\n", "df['assistant_prompt_FA'] = df.apply(lambda row: {\"Final Answer\": row[\"answer_key\"]}, axis=1)\n", "\n", "# Step 1: Create user_prompt_FA column\n", "df['user_prompt_FA'] = df.apply(lambda row: {\n", " \"content\": system_prompt_FA + '\\n' + row['user_prompt'],\n", " \"role\": \"user\"\n", "}, axis=1)\n", "\n", "# Step 2: Create conversation_FA_R column using user_prompt_FA\n", "df['conversation_FA'] = df.apply(lambda row: tokenizer.apply_chat_template([\n", " row['user_prompt_FA'], # Use the precomputed user prompt\n", " {\"content\": row['assistant_prompt_FA'], \"role\": \"assistant\"}\n", " # {\"content\": json.dumps(row['assistant_prompt_FA']), \"role\": \"assistant\"}\n", "], tokenize=False), axis=1)\n", "\n", "\n", "df['user_prompt_FA'] = df['user_prompt_FA'].apply(lambda row: tokenizer.apply_chat_template([row], tokenize=False))\n", "\n", "df.drop(['assistant_prompt_FA'], inplace=True, axis=1)\n", "\n", "\n", "# Example output\n", "example = df['conversation_FA'].iloc[0]\n", "\n", "print(example)" ] }, { "cell_type": "markdown", "id": "ad51b99b-cf67-43d8-833c-5ec57d9d4613", "metadata": {}, "source": [ "### Cleanup" ] }, { "cell_type": "code", "execution_count": 11, "id": "69a687d5-35ab-4abb-8bb8-f975fa7be3f7", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "Index(['formatted_question', 'combined_fact', 'answer_key', 'topic',\n", " 'gpt3_5_reasoning', 'question_text', 'answer_choices',\n", " 'mistral_reasoning', 'user_prompt', 'user_prompt_RFA',\n", " 'conversation_RFA_gpt3_5', 'conversation_RFA_mistral',\n", " 'user_prompt_FAR', 'conversation_FAR_gpt3_5',\n", " 'conversation_FAR_mistral', 'user_prompt_FA', 'conversation_FA'],\n", " dtype='object')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "code", "execution_count": 12, "id": "6ec3b6b5-a359-4da8-98c8-4dae75b8a2d4", "metadata": { "tags": [] }, "outputs": [], "source": [ "df = df[['topic', 'question_text', 'answer_key', 'gpt3_5_reasoning', 'mistral_reasoning', 'answer_choices', 'user_prompt', \n", " 'user_prompt_RFA', 'conversation_RFA_gpt3_5', 'conversation_RFA_mistral',\n", " 'user_prompt_FAR', 'conversation_FAR_gpt3_5', 'conversation_FAR_mistral',\n", " 'user_prompt_FA', 'conversation_FA']]" ] }, { "cell_type": "code", "execution_count": 13, "id": "f9f3a5a8-e4ea-4ed9-9fc0-fdd1a19b1c92", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
topicquestion_textanswer_keygpt3_5_reasoningmistral_reasoninganswer_choicesuser_promptuser_prompt_RFAconversation_RFA_gpt3_5conversation_RFA_mistraluser_prompt_FARconversation_FAR_gpt3_5conversation_FAR_mistraluser_prompt_FAconversation_FA
0TechnologyWhat is satellite technology used for predicting?ca) Seconds and minutes: This option is incorre...Incorrect answers and explanations:\\n\\n1. Elec...(a) Seconds and minutes (b) The strength and m...Question: What is satellite technology used fo...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
1Food scienceWhat does irradiating food do?c(a) Relieve pain: This option is not correct b...Sure, let's examine each answer and justify wh...(a) Relieve pain (b) Enhance food's nutrients ...Question: What does irradiating food do?\\nAnsw...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
2BiologyWhat protects a mammal's skin?ab) Exfoliation: Exfoliation is the process of ...Sure, let's go through each of the provided an...(a) Fiber follicles (b) Exfoliation (c) Resist...Question: What protects a mammal's skin?\\nAnsw...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
3BiologyWhat do earthworms do when a segment breaks off?ba) Dies: This option is not correct because ea...1. Reading the question carefully, we can see ...(a) Dies (b) Regrows it (c) Reproduces (d) Sed...Question: What do earthworms do when a segment...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
4ElectricityLightning can be bad for what?ab) Rainstorms: Lightning is actually a natural...1. Food: While essential for the growth and he...(a) The environment (b) Rainstorms (c) Destruc...Question: Lightning can be bad for what?\\nAnsw...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
................................................
8408BiologyOrganisms that can cause infection do what?ga) Bandaging open sores is not the correct ans...1. Read the question and options carefully: Th...(a) Bandage open sores (b) Keep flesh clean (c...Question: Organisms that can cause infection d...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
8409BiologyFungi are living things that cannot make their...ab) Fungi are living things that can make their...1. Read the question and options carefully: Th...(a) Food (b) Cells (c) Energy (d) Fruits (e) H...Question: Fungi are living things that cannot ...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
8410BiologyAn overheated body can use water for:?ga) Metabolic reaction: This option is incorrec...1. Read the question and options carefully: Th...(a) Metabolic reaction (b) Dehydrating (c) Rai...Question: An overheated body can use water for...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
8411BiologyWhat is essential for cellular respiration for...fa) Electrons are involved in cellular respirat...1. First, let's read the question and options ...(a) Electron (b) Glucose (c) Energy (d) Energy...Question: What is essential for cellular respi...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
8412BiologyWhat helps insulate and protect the body?ba) H2O: Water is essential for life, but it do...1. Read the question and options carefully: Th...(a) H2o (b) Living cells in follicles (c) Laye...Question: What helps insulate and protect the ...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...<s>[INST] Answer the Question and include your...
\n", "

8413 rows × 15 columns

\n", "
" ], "text/plain": [ " topic question_text \\\n", "0 Technology What is satellite technology used for predicting? \n", "1 Food science What does irradiating food do? \n", "2 Biology What protects a mammal's skin? \n", "3 Biology What do earthworms do when a segment breaks off? \n", "4 Electricity Lightning can be bad for what? \n", "... ... ... \n", "8408 Biology Organisms that can cause infection do what? \n", "8409 Biology Fungi are living things that cannot make their... \n", "8410 Biology An overheated body can use water for:? \n", "8411 Biology What is essential for cellular respiration for... \n", "8412 Biology What helps insulate and protect the body? \n", "\n", " answer_key gpt3_5_reasoning \\\n", "0 c a) Seconds and minutes: This option is incorre... \n", "1 c (a) Relieve pain: This option is not correct b... \n", "2 a b) Exfoliation: Exfoliation is the process of ... \n", "3 b a) Dies: This option is not correct because ea... \n", "4 a b) Rainstorms: Lightning is actually a natural... \n", "... ... ... \n", "8408 g a) Bandaging open sores is not the correct ans... \n", "8409 a b) Fungi are living things that can make their... \n", "8410 g a) Metabolic reaction: This option is incorrec... \n", "8411 f a) Electrons are involved in cellular respirat... \n", "8412 b a) H2O: Water is essential for life, but it do... \n", "\n", " mistral_reasoning \\\n", "0 Incorrect answers and explanations:\\n\\n1. Elec... \n", "1 Sure, let's examine each answer and justify wh... \n", "2 Sure, let's go through each of the provided an... \n", "3 1. Reading the question carefully, we can see ... \n", "4 1. Food: While essential for the growth and he... \n", "... ... \n", "8408 1. Read the question and options carefully: Th... \n", "8409 1. Read the question and options carefully: Th... \n", "8410 1. Read the question and options carefully: Th... \n", "8411 1. First, let's read the question and options ... \n", "8412 1. Read the question and options carefully: Th... \n", "\n", " answer_choices \\\n", "0 (a) Seconds and minutes (b) The strength and m... \n", "1 (a) Relieve pain (b) Enhance food's nutrients ... \n", "2 (a) Fiber follicles (b) Exfoliation (c) Resist... \n", "3 (a) Dies (b) Regrows it (c) Reproduces (d) Sed... \n", "4 (a) The environment (b) Rainstorms (c) Destruc... \n", "... ... \n", "8408 (a) Bandage open sores (b) Keep flesh clean (c... \n", "8409 (a) Food (b) Cells (c) Energy (d) Fruits (e) H... \n", "8410 (a) Metabolic reaction (b) Dehydrating (c) Rai... \n", "8411 (a) Electron (b) Glucose (c) Energy (d) Energy... \n", "8412 (a) H2o (b) Living cells in follicles (c) Laye... \n", "\n", " user_prompt \\\n", "0 Question: What is satellite technology used fo... \n", "1 Question: What does irradiating food do?\\nAnsw... \n", "2 Question: What protects a mammal's skin?\\nAnsw... \n", "3 Question: What do earthworms do when a segment... \n", "4 Question: Lightning can be bad for what?\\nAnsw... \n", "... ... \n", "8408 Question: Organisms that can cause infection d... \n", "8409 Question: Fungi are living things that cannot ... \n", "8410 Question: An overheated body can use water for... \n", "8411 Question: What is essential for cellular respi... \n", "8412 Question: What helps insulate and protect the ... \n", "\n", " user_prompt_RFA \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " conversation_RFA_gpt3_5 \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " conversation_RFA_mistral \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " user_prompt_FAR \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " conversation_FAR_gpt3_5 \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " conversation_FAR_mistral \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " user_prompt_FA \\\n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", " conversation_FA \n", "0 [INST] Answer the Question and include your... \n", "1 [INST] Answer the Question and include your... \n", "2 [INST] Answer the Question and include your... \n", "3 [INST] Answer the Question and include your... \n", "4 [INST] Answer the Question and include your... \n", "... ... \n", "8408 [INST] Answer the Question and include your... \n", "8409 [INST] Answer the Question and include your... \n", "8410 [INST] Answer the Question and include your... \n", "8411 [INST] Answer the Question and include your... \n", "8412 [INST] Answer the Question and include your... \n", "\n", "[8413 rows x 15 columns]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "markdown", "id": "9fe52e77-0985-49d4-964e-7a17372d7007", "metadata": {}, "source": [ "## Explore Prompts\n", "Gradio can be really useful for quick inline apps. Here I want to make sure everything is as I expect.\n", "\n", "While the above print statements helped me see the format, the gradio app helps me explore a large volume of output easily. \n", "\n", "Note: Its tricky as I cant easily render newlines in strings. So be careful!" ] }, { "cell_type": "code", "execution_count": 14, "id": "a50d9d6c-18e6-476d-9a40-ed7a3f699477", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import json\n", "import gradio as gr\n", "\n", "# Gradio app to browse prompts with left and right buttons\n", "# index = 0\n", "\n", "# Functions to handle prompts\n", "def get_prompt(index, prompt_type):\n", " return df.iloc[index][prompt_type]\n", "\n", "def next_prompt(index, prompt_type):\n", " if index < len(df) - 1:\n", " index += 1\n", " return index, get_prompt(index, prompt_type)\n", "\n", "def previous_prompt(index, prompt_type):\n", " if index > 0:\n", " index -= 1\n", " return index, get_prompt(index, prompt_type)\n", "\n", "# Gradio App\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Prompt Browser\")\n", " with gr.Row():\n", " prompt_type_dropdown = gr.Dropdown(\n", " choices=['conversation_RFA_gpt3_5', 'conversation_RFA_mistral', 'conversation_FAR_gpt3_5', 'conversation_FAR_mistral', 'conversation_FA_gpt3_5', 'conversation_FA_mistral'],\n", " value='conversation_RFA_gpt3_5',\n", " label=\"Select Prompt Type\"\n", " )\n", " index_display = gr.Textbox(\"0\", label=\"Index\", interactive=False)\n", "\n", " prompt_display = gr.Textbox(value=df.iloc[0]['conversation_RFA_gpt3_5'], label=\"Prompt\")\n", " \n", " with gr.Row():\n", " prev_button = gr.Button(\"⬅️ Previous\")\n", " next_button = gr.Button(\"Next ➡️\")\n", " \n", " # State to hold the current index\n", " index_state = gr.State(value=0)\n", "\n", " # Button click events\n", " prev_button.click(\n", " fn=previous_prompt,\n", " inputs=[index_state, prompt_type_dropdown],\n", " outputs=[index_state, prompt_display]\n", " )\n", " next_button.click(\n", " fn=next_prompt,\n", " inputs=[index_state, prompt_type_dropdown],\n", " outputs=[index_state, prompt_display]\n", " )\n", "\n", " # Dropdown change event\n", " prompt_type_dropdown.change(\n", " fn=lambda index, prompt_type: get_prompt(index, prompt_type),\n", " inputs=[index_state, prompt_type_dropdown],\n", " outputs=prompt_display\n", " )\n", "\n", " # Update index display\n", " index_state.change(\n", " fn=lambda index: str(index),\n", " inputs=index_state,\n", " outputs=index_display\n", " )\n", "\n", "# Launch the app\n", "demo.launch(height=840)" ] }, { "cell_type": "markdown", "id": "3838daf0-8a3a-4513-ad2b-3589d60dfa3d", "metadata": {}, "source": [ "## Push Dataset to the Hub\n", "Its useful to get a train, test split, then we convert to `Dataset` and push to the hub. We also want to stratify on `'topic'`." ] }, { "cell_type": "code", "execution_count": 15, "id": "25f62e9b-09f8-4912-94fd-0ded680614b2", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import Dataset, DatasetDict\n", "from sklearn.model_selection import train_test_split\n", "\n", "# First split to create train and remaining (val + test)\n", "train_df, test_df = train_test_split(df, test_size=0.2, stratify=df['topic'], random_state=42)\n", "\n", "# Reset index to avoid index column in the Hugging Face Dataset\n", "train_df.reset_index(drop=True, inplace=True)\n", "test_df.reset_index(drop=True, inplace=True)\n", "\n", "# Convert each DataFrame to a Dataset object\n", "train_dataset = Dataset.from_pandas(train_df)\n", "test_dataset = Dataset.from_pandas(test_df)\n", "\n", "# Create a DatasetDict with the train, validation, and test datasets\n", "dataset_dict = DatasetDict({\n", " 'train': train_dataset,\n", " 'test': test_dataset\n", "})" ] }, { "cell_type": "code", "execution_count": 16, "id": "18a206c5-0e40-46b3-8dfb-20000789b6b5", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0e40274a780c4da0868362339d48e6de", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Uploading the dataset shards: 0%| | 0/1 [00:00