{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pHzbx3WTkGcl", "outputId": "2da56fff-5007-42d1-d924-4b0bc2ec08e6" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting gradientai\n", " Downloading gradientai-1.11.0-py3-none-any.whl (375 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m375.5/375.5 kB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting aenum>=3.1.11 (from gradientai)\n", " Downloading aenum-3.1.15-py3-none-any.whl (137 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m137.6/137.6 kB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting pydantic<2.0.0,>=1.10.5 (from gradientai)\n", " Downloading pydantic-1.10.15-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from gradientai) (2.8.2)\n", "Requirement already satisfied: urllib3>=1.25.3 in /usr/local/lib/python3.10/dist-packages (from gradientai) (2.0.7)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<2.0.0,>=1.10.5->gradientai) (4.11.0)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->gradientai) (1.16.0)\n", "Installing collected packages: aenum, pydantic, gradientai\n", " Attempting uninstall: pydantic\n", " Found existing installation: pydantic 2.7.1\n", " Uninstalling pydantic-2.7.1:\n", " Successfully uninstalled pydantic-2.7.1\n", "Successfully installed aenum-3.1.15 gradientai-1.11.0 pydantic-1.10.15\n" ] } ], "source": [ "!pip install gradientai --upgrade" ] }, { "cell_type": "code", "source": [ "import os\n", "import pandas as pd\n", "os.environ['GRADIENT_WORKSPACE_ID']='9d0447f2-fcd4-4177-9145-9f019fd59f1e_workspace'\n", "os.environ['GRADIENT_ACCESS_TOKEN']='cPErsUMgadGMbzeq8z8W36eJn7UA0Uob'" ], "metadata": { "id": "XJfF9GXCkM1f" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/CS-5302/CS-5302-Project-Group-15/main/Datasets/testing/combined_df.csv\")\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 424 }, "id": "1hAAan9ikNdE", "outputId": "b84b6caf-1bc0-4aca-b4f5-3cc47e17293b" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " prompts \\\n", "0 I have Fever, Fatigue, Difficulty Breathing. W... \n", "1 I have Cough, Fatigue. What disease do i have? \n", "2 I have Cough, Fatigue. What disease do i have? \n", "3 I have Fever, Cough, Difficulty Breathing. Wha... \n", "4 I have Fever, Cough, Difficulty Breathing. Wha... \n", "... ... \n", "1344 i have leg pain, neck pain, paresthesia, leg c... \n", "1345 i have nasal congestion, white discharge from ... \n", "1346 i have cough, white discharge from eye, dimini... \n", "1347 i have diminished hearing, headache, facial pa... \n", "1348 i have diminished hearing, facial pain. what d... \n", "\n", " results \n", "0 You have Influenza. \n", "1 You don't have a disease. \n", "2 You don't have a disease. \n", "3 You have Asthma. \n", "4 You have Asthma. \n", "... ... \n", "1344 you have spondylolisthesis. \n", "1345 you have conjunctivitis due to virus. \n", "1346 you have conjunctivitis due to virus. \n", "1347 you have open wound of the nose. \n", "1348 you have open wound of the nose. \n", "\n", "[1349 rows x 2 columns]" ], "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", "
promptsresults
0I have Fever, Fatigue, Difficulty Breathing. W...You have Influenza.
1I have Cough, Fatigue. What disease do i have?You don't have a disease.
2I have Cough, Fatigue. What disease do i have?You don't have a disease.
3I have Fever, Cough, Difficulty Breathing. Wha...You have Asthma.
4I have Fever, Cough, Difficulty Breathing. Wha...You have Asthma.
.........
1344i have leg pain, neck pain, paresthesia, leg c...you have spondylolisthesis.
1345i have nasal congestion, white discharge from ...you have conjunctivitis due to virus.
1346i have cough, white discharge from eye, dimini...you have conjunctivitis due to virus.
1347i have diminished hearing, headache, facial pa...you have open wound of the nose.
1348i have diminished hearing, facial pain. what d...you have open wound of the nose.
\n", "

1349 rows × 2 columns

\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df", "summary": "{\n \"name\": \"df\",\n \"rows\": 1349,\n \"fields\": [\n {\n \"column\": \"prompts\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1052,\n \"samples\": [\n \"i have sharp chest pain, abusing alcohol, sharp abdominal pain, vomiting, diarrhea, back pain, burning abdominal pain, side pain, lower body pain, upper abdominal pain. what disease do i have?\",\n \"i have wrist pain, hand or finger swelling, arm pain, knee pain, foot or toe pain, ankle pain, shoulder pain. what disease do i have?\",\n \"i have sharp chest pain, leg pain, sharp abdominal pain, vomiting, lower abdominal pain, low back pain. what disease do i have?\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"results\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 455,\n \"samples\": [\n \"you have temporary or benign blood in urine.\",\n \"You have Hypoglycemia.\",\n \"you have sebaceous cyst.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 3 } ] }, { "cell_type": "code", "source": [ "\n", "BATCH_SIZE = 100\n", "NUM_EPOCHS = 1\n" ], "metadata": { "id": "0_f3sqBtpXpQ" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "def create_model_adapter(gradient):\n", " base_model = gradient.get_base_model(base_model_slug=\"nous-hermes2\")\n", " new_model_adapter = base_model.create_model_adapter(\n", " name=\"meta/llama-2-7b:73001d654114dad81ec65da3b834e2f691af1e1526453189b7bf36fb3f32d0f9\"\n", " )\n", " print(f\"Created model adapter with id {new_model_adapter.id}\")\n", " return new_model_adapter" ], "metadata": { "id": "bW9Qin9MpcRU" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "def fine_tune_in_batches(df, gradient, batch_size, num_epochs):\n", " new_model_adapter = create_model_adapter(gradient)\n", "\n", " # Split the DataFrame into batches\n", " batches = [df[i:i + batch_size] for i in range(0, len(df), batch_size)]\n", "\n", " # Iterate over batches and perform fine-tuning\n", " for batch_index, batch in enumerate(batches):\n", " fine_tuning_samples = []\n", " for _, row in batch.iterrows():\n", " fine_tuning_samples.append({\n", " \"inputs\": f\"### Instruction: {row['prompts']}\",\n", " \"targets\": f\"### Response: {row['results']}\"\n", " })\n", "\n", " # Fine-tune for the given number of epochs\n", " for epoch in range(num_epochs):\n", " print(f\"Fine-tuning batch {batch_index + 1} (epoch {epoch + 1})\")\n", " new_model_adapter.fine_tune(samples=fine_tuning_samples)\n", "\n", " return new_model_adapter" ], "metadata": { "id": "XfDSZVfspe9c" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from gradientai import Gradient\n", "\n", "def main():\n", " # Initialize the Gradient API\n", " gradient = Gradient()\n", "\n", "\n", " # Fine-tune in batches and retain the final model adapter\n", " model_adapter = fine_tune_in_batches(df, gradient, BATCH_SIZE, NUM_EPOCHS)\n", "\n", " # Test the model after fine-tuning\n", " sample_query = f\"### Instruction: {df['prompts'][0]} \\n\\n### Response:\"\n", " completion = model_adapter.complete(query=sample_query, max_generated_token_count=100).generated_output\n", " print(f\"Generated (after fine-tuning): {completion}\")\n", "\n", " # Clean up the model adapter\n", " model_adapter.delete()\n", " gradient.close()\n", "\n", "if __name__ == \"__main__\":\n", " main()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RzHdyX0nkPtl", "outputId": "b5ae54fd-6246-414b-cffb-5421a5e8c8a5" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Created model adapter with id 9aebddd0-336b-4b61-8910-d8be7ef38f43_model_adapter\n", "Fine-tuning batch 1 (epoch 1)\n", "Fine-tuning batch 2 (epoch 1)\n", "Fine-tuning batch 3 (epoch 1)\n", "Fine-tuning batch 4 (epoch 1)\n", "Fine-tuning batch 5 (epoch 1)\n", "Fine-tuning batch 6 (epoch 1)\n", "Fine-tuning batch 7 (epoch 1)\n", "Fine-tuning batch 8 (epoch 1)\n", "Fine-tuning batch 9 (epoch 1)\n", "Fine-tuning batch 10 (epoch 1)\n", "Fine-tuning batch 11 (epoch 1)\n", "Fine-tuning batch 12 (epoch 1)\n", "Fine-tuning batch 13 (epoch 1)\n", "Fine-tuning batch 14 (epoch 1)\n", "Generated (after fine-tuning): You may have Pneumonia.\n" ] } ] }, { "cell_type": "code", "source": [ "sample_query = f\"### Instruction: {df['prompts'][0]} \\n\\n### Response:\"\n", "sample_query" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "LLfG-8dG3gF3", "outputId": "bd7192a7-6a07-4262-eaf6-fb62a5b278c6" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'### Instruction: I have Fever, Fatigue, Difficulty Breathing. What disease do i have? \\n\\n### Response:'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 20 } ] } ] }