{ "cells": [ { "cell_type": "markdown", "id": "a2996243-a398-4e7f-86e6-b1fbb3d0b38f", "metadata": {}, "source": [ "# Building a Gradio Frontend " ] }, { "cell_type": "markdown", "id": "27359fd5-fc05-40aa-b636-97dc340aa77e", "metadata": {}, "source": [ "## Installing packages" ] }, { "cell_type": "code", "execution_count": 1, "id": "c6d2ba96-e517-4126-b09a-a063155f9596", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: keras_nlp in /opt/conda/lib/python3.10/site-packages (0.17.0)\n", "Requirement already satisfied: kaggle in /opt/conda/lib/python3.10/site-packages (1.6.17)\n", "Requirement already satisfied: gradio in /opt/conda/lib/python3.10/site-packages (5.4.0)\n", "Requirement already satisfied: tensorflow in /opt/conda/lib/python3.10/site-packages (2.18.0)\n", "Requirement already satisfied: keras-hub==0.17.0 in /opt/conda/lib/python3.10/site-packages (from keras_nlp) (0.17.0)\n", "Requirement already satisfied: absl-py in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (1.4.0)\n", "Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (1.26.4)\n", "Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (24.1)\n", "Requirement already satisfied: regex in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (2024.9.11)\n", "Requirement already satisfied: rich in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (13.9.2)\n", "Requirement already satisfied: kagglehub in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (0.3.3)\n", "Requirement already satisfied: tensorflow-text in /opt/conda/lib/python3.10/site-packages (from keras-hub==0.17.0->keras_nlp) (2.18.0)\n", "Requirement already satisfied: six>=1.10 in /opt/conda/lib/python3.10/site-packages (from kaggle) (1.16.0)\n", "Requirement already satisfied: certifi>=2023.7.22 in /opt/conda/lib/python3.10/site-packages (from kaggle) (2024.8.30)\n", "Requirement already satisfied: python-dateutil in /opt/conda/lib/python3.10/site-packages (from kaggle) (2.9.0.post0)\n", "Requirement already satisfied: requests in /opt/conda/lib/python3.10/site-packages (from kaggle) (2.32.3)\n", "Requirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (from kaggle) (4.66.5)\n", "Requirement already satisfied: python-slugify in /opt/conda/lib/python3.10/site-packages (from kaggle) (8.0.4)\n", "Requirement already satisfied: urllib3 in /opt/conda/lib/python3.10/site-packages (from kaggle) (1.26.20)\n", "Requirement already satisfied: bleach in /opt/conda/lib/python3.10/site-packages (from kaggle) (6.1.0)\n", "Requirement already satisfied: aiofiles<24.0,>=22.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (23.2.1)\n", "Requirement already satisfied: anyio<5.0,>=3.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (3.7.1)\n", "Requirement already satisfied: fastapi<1.0,>=0.115.2 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.115.4)\n", "Requirement already satisfied: ffmpy in /opt/conda/lib/python3.10/site-packages (from gradio) (0.4.0)\n", "Requirement already satisfied: gradio-client==1.4.2 in /opt/conda/lib/python3.10/site-packages (from gradio) (1.4.2)\n", "Requirement already satisfied: httpx>=0.24.1 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.27.2)\n", "Requirement already satisfied: huggingface-hub>=0.25.1 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.26.2)\n", "Requirement already satisfied: jinja2<4.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (3.1.4)\n", "Requirement already satisfied: markupsafe~=2.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (2.1.5)\n", "Requirement already satisfied: orjson~=3.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (3.10.10)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (2.2.3)\n", "Requirement already satisfied: pillow<12.0,>=8.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (10.4.0)\n", "Requirement already satisfied: pydantic>=2.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (2.9.2)\n", "Requirement already satisfied: pydub in /opt/conda/lib/python3.10/site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: python-multipart==0.0.12 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.0.12)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (6.0.2)\n", "Requirement already satisfied: ruff>=0.2.2 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.7.1)\n", "Requirement already satisfied: safehttpx<1.0,>=0.1.1 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.1.1)\n", "Requirement already satisfied: semantic-version~=2.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (2.10.0)\n", "Requirement already satisfied: starlette<1.0,>=0.40.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.41.2)\n", "Requirement already satisfied: tomlkit==0.12.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.12.0)\n", "Requirement already satisfied: typer<1.0,>=0.12 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.12.5)\n", "Requirement already satisfied: typing-extensions~=4.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (4.12.2)\n", "Requirement already satisfied: uvicorn>=0.14.0 in /opt/conda/lib/python3.10/site-packages (from gradio) (0.31.1)\n", "Requirement already satisfied: fsspec in /opt/conda/lib/python3.10/site-packages (from gradio-client==1.4.2->gradio) (2024.9.0)\n", "Requirement already satisfied: websockets<13.0,>=10.0 in /opt/conda/lib/python3.10/site-packages (from gradio-client==1.4.2->gradio) (12.0)\n", "Requirement already satisfied: astunparse>=1.6.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (1.6.3)\n", "Requirement already satisfied: flatbuffers>=24.3.25 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (24.3.25)\n", "Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (0.6.0)\n", "Requirement already satisfied: google-pasta>=0.1.1 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (0.2.0)\n", "Requirement already satisfied: libclang>=13.0.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (18.1.1)\n", "Requirement already satisfied: opt-einsum>=2.3.2 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (3.4.0)\n", "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<6.0.0dev,>=3.20.3 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (3.20.3)\n", "Requirement already satisfied: setuptools in /opt/conda/lib/python3.10/site-packages (from tensorflow) (74.1.2)\n", "Requirement already satisfied: termcolor>=1.1.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (2.5.0)\n", "Requirement already satisfied: wrapt>=1.11.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (1.16.0)\n", "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (1.66.2)\n", "Requirement already satisfied: tensorboard<2.19,>=2.18 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (2.18.0)\n", "Requirement already satisfied: keras>=3.5.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (3.6.0)\n", "Requirement already satisfied: h5py>=3.11.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (3.12.1)\n", "Requirement already satisfied: ml-dtypes<0.5.0,>=0.4.0 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (0.4.1)\n", "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /opt/conda/lib/python3.10/site-packages (from tensorflow) (0.37.1)\n", "Requirement already satisfied: idna>=2.8 in /opt/conda/lib/python3.10/site-packages (from anyio<5.0,>=3.0->gradio) (3.10)\n", "Requirement already satisfied: sniffio>=1.1 in /opt/conda/lib/python3.10/site-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n", "Requirement already satisfied: exceptiongroup in /opt/conda/lib/python3.10/site-packages (from anyio<5.0,>=3.0->gradio) (1.2.2)\n", "Requirement already satisfied: wheel<1.0,>=0.23.0 in /opt/conda/lib/python3.10/site-packages (from astunparse>=1.6.0->tensorflow) (0.44.0)\n", "Requirement already satisfied: httpcore==1.* in /opt/conda/lib/python3.10/site-packages (from httpx>=0.24.1->gradio) (1.0.6)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /opt/conda/lib/python3.10/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n", "Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from huggingface-hub>=0.25.1->gradio) (3.16.1)\n", "Requirement already satisfied: namex in /opt/conda/lib/python3.10/site-packages (from keras>=3.5.0->tensorflow) (0.0.8)\n", "Requirement already satisfied: optree in /opt/conda/lib/python3.10/site-packages (from keras>=3.5.0->tensorflow) (0.13.0)\n", "Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas<3.0,>=1.0->gradio) (2024.2)\n", "Requirement already satisfied: tzdata>=2022.7 in /opt/conda/lib/python3.10/site-packages (from pandas<3.0,>=1.0->gradio) (2024.2)\n", "Requirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.10/site-packages (from pydantic>=2.0->gradio) (0.7.0)\n", "Requirement already satisfied: pydantic-core==2.23.4 in /opt/conda/lib/python3.10/site-packages (from pydantic>=2.0->gradio) (2.23.4)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests->kaggle) (3.3.2)\n", "Requirement already satisfied: markdown>=2.6.8 in /opt/conda/lib/python3.10/site-packages (from tensorboard<2.19,>=2.18->tensorflow) (3.7)\n", "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /opt/conda/lib/python3.10/site-packages (from tensorboard<2.19,>=2.18->tensorflow) (0.7.2)\n", "Requirement already satisfied: werkzeug>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from tensorboard<2.19,>=2.18->tensorflow) (3.0.6)\n", "Requirement already satisfied: click>=8.0.0 in /opt/conda/lib/python3.10/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n", "Requirement already satisfied: shellingham>=1.3.0 in /opt/conda/lib/python3.10/site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n", "Requirement already satisfied: webencodings in /opt/conda/lib/python3.10/site-packages (from bleach->kaggle) (0.5.1)\n", "Requirement already satisfied: text-unidecode>=1.3 in /opt/conda/lib/python3.10/site-packages (from python-slugify->kaggle) (1.3)\n", "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/conda/lib/python3.10/site-packages (from rich->keras-hub==0.17.0->keras_nlp) (3.0.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/conda/lib/python3.10/site-packages (from rich->keras-hub==0.17.0->keras_nlp) (2.18.0)\n", "Requirement already satisfied: mdurl~=0.1 in /opt/conda/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich->keras-hub==0.17.0->keras_nlp) (0.1.2)\n" ] } ], "source": [ "!pip install keras_nlp kaggle gradio tensorflow" ] }, { "cell_type": "code", "execution_count": 2, "id": "e8bd9ce4-a2b9-4778-9081-9a0f2cf666f4", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-10-31 19:29:35.482316: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-10-31 19:29:35.844009: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-10-31 19:29:36.148747: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "E0000 00:00:1730402976.395556 5425 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "E0000 00:00:1730402976.465622 5425 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "2024-10-31 19:29:37.078974: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" ] } ], "source": [ "import keras_nlp\n", "import os\n", "import tensorflow as tf" ] }, { "cell_type": "markdown", "id": "225e9356-246f-4b41-a054-7802f14b9980", "metadata": {}, "source": [ "## Loading the model from kaggle" ] }, { "cell_type": "code", "execution_count": 4, "id": "d553e22c-b794-4ce1-8c66-1179467515b9", "metadata": { "tags": [] }, "outputs": [], "source": [ "#For safety, add this to kaggle or google secrets\n", "os.environ['KAGGLE_USERNAME'] = 'ayomidezulkazeem' #username\n", "os.environ['KAGGLE_KEY'] = '245a313b018fe8355198d9c48413b991' #kaggle_key" ] }, { "cell_type": "code", "execution_count": 5, "id": "3c9b993f-8fa3-47b2-a100-f3d604a69bf8", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Path to model files: /home/jupyter/.cache/kagglehub/models/ayomidezulkazeem/hydrosense/keras/gemma-hydrosense-instruct-2b/1\n" ] } ], "source": [ "import kagglehub\n", "\n", "# Download latest version\n", "path = kagglehub.model_download(\"ayomidezulkazeem/hydrosense/keras/gemma-hydrosense-instruct-2b\")\n", "\n", "print(\"Path to model files:\", path)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ec3555c5-18f8-4de6-ac9d-bf8525cb1a69", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model path: /home/jupyter/.cache/kagglehub/models/ayomidezulkazeem/hydrosense/keras/gemma-hydrosense-instruct-2b/1\n" ] } ], "source": [ "print(\"Model path:\", path)" ] }, { "cell_type": "code", "execution_count": 7, "id": "624658c2-e268-43c1-8007-a3d73859dc6e", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-10-31 19:30:18.754472: E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:152] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)\n", "2024-10-31 19:30:18.795603: W external/local_xla/xla/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 2097152000 exceeds 10% of free system memory.\n", "2024-10-31 19:30:22.851420: W external/local_xla/xla/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 2097152000 exceeds 10% of free system memory.\n", "2024-10-31 19:30:23.277000: W external/local_xla/xla/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 2097152000 exceeds 10% of free system memory.\n", "normalizer.cc(51) LOG(INFO) precompiled_charsmap is empty. use identity normalization.\n", "2024-10-31 19:30:42.746775: W external/local_xla/xla/tsl/framework/cpu_allocator_impl.cc:83] Allocation of 2097152000 exceeds 10% of free system memory.\n" ] } ], "source": [ "model_path = \"/home/jupyter/.cache/kagglehub/models/ayomidezulkazeem/hydrosense/keras/gemma-hydrosense-instruct-2b/1\"\n", "gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(model_path)" ] }, { "cell_type": "markdown", "id": "8cbbc6ff-00b7-4986-bfda-b6ca83734472", "metadata": {}, "source": [ "## Post-Processing the Output" ] }, { "cell_type": "code", "execution_count": 8, "id": "b684e998-f39b-44cf-ba37-e292730a512e", "metadata": { "tags": [] }, "outputs": [], "source": [ "def post_process_output(prompt, generated_text):\n", " # Remove the prompt if it's repeated at the beginning of the answer\n", " answer = generated_text.strip()\n", " if answer.startswith(prompt):\n", " answer = answer[len(prompt):].strip()\n", "\n", " # Remove any leading colons or whitespace\n", " answer = answer.lstrip(':')\n", "\n", " # Ensure the answer starts with a capital letter\n", " answer = answer.capitalize()\n", "\n", " # Ensure the answer ends with a period if it doesn't already\n", " if not answer.endswith('.'):\n", " answer += '.'\n", "\n", " return f\"{answer}\"" ] }, { "cell_type": "code", "execution_count": 9, "id": "6024cf30-930b-4fbd-ba69-dcb26f8351b8", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "I0000 00:00:1730403102.029145 5684 service.cc:148] XLA service 0x7f34ec079e20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:\n", "I0000 00:00:1730403102.032560 5684 service.cc:156] StreamExecutor device (0): Host, Default Version\n", "I0000 00:00:1730403102.142617 5684 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Based on the last service date of 2023-02-14 and the bi-annual maintenance interval, the next service due for lift station-485083 is 2023-06-12.\n" ] } ], "source": [ "prompt = \"When is the next maintenance due for Lift Station-485083 based on its last service date?\"\n", "generated_text = gemma_lm.generate(prompt, max_length=100)\n", "\n", "# Apply post-processing\n", "formatted_output = post_process_output(prompt, generated_text)\n", "print(formatted_output)" ] }, { "cell_type": "markdown", "id": "10cbb2e1-0028-4837-ad95-73e46a470561", "metadata": {}, "source": [ "## Gradio Interface" ] }, { "cell_type": "code", "execution_count": 10, "id": "f4d8e9e3-bc6f-405a-90e9-f76a5306a0b8", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "* Running on public URL: https://e357bb4e5bce8bd94b.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "