{ "cells": [ { "cell_type": "markdown", "id": "8b0e11f2-9ea4-48c2-b8d2-d0a4ba967827", "metadata": {}, "source": [ "# Gradio Day!\n", "\n", "Today we will build User Interfaces using the outrageously simple Gradio framework.\n", "\n", "Prepare for joy!\n", "\n", "Please note: your Gradio screens may appear in 'dark mode' or 'light mode' depending on your computer settings." ] }, { "cell_type": "code", "execution_count": 2, "id": "c0242da2-967b-487e-aec3-bba9831c7300", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: google-generativeai in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (0.8.4)\n", "Requirement already satisfied: google-ai-generativelanguage==0.6.15 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (0.6.15)\n", "Requirement already satisfied: google-api-core in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (2.24.2)\n", "Requirement already satisfied: google-api-python-client in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (2.165.0)\n", "Requirement already satisfied: google-auth>=2.15.0 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (2.38.0)\n", "Requirement already satisfied: protobuf in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (5.29.4)\n", "Requirement already satisfied: pydantic in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (2.9.2)\n", "Requirement already satisfied: tqdm in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-generativeai) (4.67.1)\n", "Requirement already satisfied: typing-extensions in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from google-generativeai) (4.12.2)\n", "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-ai-generativelanguage==0.6.15->google-generativeai) (1.26.1)\n", "Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-auth>=2.15.0->google-generativeai) (5.5.2)\n", "Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-auth>=2.15.0->google-generativeai) (0.4.1)\n", "Requirement already satisfied: rsa<5,>=3.1.4 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-auth>=2.15.0->google-generativeai) (4.9)\n", "Requirement already satisfied: googleapis-common-protos<2.0.0,>=1.56.2 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-core->google-generativeai) (1.69.2)\n", "Requirement already satisfied: requests<3.0.0,>=2.18.0 in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from google-api-core->google-generativeai) (2.32.3)\n", "Requirement already satisfied: httplib2<1.0.0,>=0.19.0 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-python-client->google-generativeai) (0.22.0)\n", "Requirement already satisfied: google-auth-httplib2<1.0.0,>=0.2.0 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-python-client->google-generativeai) (0.2.0)\n", "Requirement already satisfied: uritemplate<5,>=3.0.1 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-python-client->google-generativeai) (4.1.1)\n", "Requirement already satisfied: annotated-types>=0.6.0 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from pydantic->google-generativeai) (0.7.0)\n", "Requirement already satisfied: pydantic-core==2.23.4 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from pydantic->google-generativeai) (2.23.4)\n", "Requirement already satisfied: colorama in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from tqdm->google-generativeai) (0.4.6)\n", "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-core->google-generativeai) (1.71.0)\n", "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from google-api-core->google-generativeai) (1.71.0)\n", "Requirement already satisfied: pyparsing!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,<4,>=2.4.2 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from httplib2<1.0.0,>=0.19.0->google-api-python-client->google-generativeai) (3.2.1)\n", "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in c:\\adm017\\llm_engineering\\.venv\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth>=2.15.0->google-generativeai) (0.6.1)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from requests<3.0.0,>=2.18.0->google-api-core->google-generativeai) (3.4.1)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from requests<3.0.0,>=2.18.0->google-api-core->google-generativeai) (3.10)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from requests<3.0.0,>=2.18.0->google-api-core->google-generativeai) (2.3.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\kzk1kh\\appdata\\roaming\\python\\python312\\site-packages (from requests<3.0.0,>=2.18.0->google-api-core->google-generativeai) (2025.1.31)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "[notice] A new release of pip is available: 23.2.1 -> 25.0.1\n", "[notice] To update, run: python.exe -m pip install --upgrade pip\n" ] } ], "source": [ "!pip install google-generativeai" ] }, { "cell_type": "code", "execution_count": 1, "id": "3e973b42-9f29-4e4a-87ac-d5da55809e19", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\adm017\\llm_engineering\\.venv\\Scripts\\python.exe\n" ] } ], "source": [ "import sys\n", "print(sys.executable)" ] }, { "cell_type": "code", "execution_count": 2, "id": "c44c5494-950d-4d2f-8d4f-b87b57c5b330", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "import requests\n", "from bs4 import BeautifulSoup\n", "from typing import List\n", "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import google.generativeai\n", "import anthropic" ] }, { "cell_type": "code", "execution_count": 4, "id": "d1715421-cead-400b-99af-986388a97aff", "metadata": {}, "outputs": [], "source": [ "import gradio as gr # oh yeah!" ] }, { "cell_type": "code", "execution_count": 5, "id": "337d5dfc-0181-4e3b-8ab9-e78e0c3f657b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OpenAI API Key exists and begins sk-proj-\n", "Anthropic API Key exists and begins sk-ant-\n", "Google API Key exists and begins AIzaSyBX\n" ] } ], "source": [ "# Load environment variables in a file called .env\n", "# Print the key prefixes to help with any debugging\n", "\n", "load_dotenv(override=True)\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n", "google_api_key = os.getenv('GOOGLE_API_KEY')\n", "\n", "if openai_api_key:\n", " print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n", "else:\n", " print(\"OpenAI API Key not set\")\n", " \n", "if anthropic_api_key:\n", " print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n", "else:\n", " print(\"Anthropic API Key not set\")\n", "\n", "if google_api_key:\n", " print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n", "else:\n", " print(\"Google API Key not set\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "22586021-1795-4929-8079-63f5bb4edd4c", "metadata": {}, "outputs": [], "source": [ "# Connect to OpenAI, Anthropic and Google; comment out the Claude or Google lines if you're not using them\n", "\n", "openai = OpenAI()\n", "\n", "claude = anthropic.Anthropic()\n", "\n", "google.generativeai.configure()" ] }, { "cell_type": "code", "execution_count": 7, "id": "b16e6021-6dc4-4397-985a-6679d6c8ffd5", "metadata": {}, "outputs": [], "source": [ "# A generic system message - no more snarky adversarial AIs!\n", "\n", "system_message = \"You are a helpful assistant\"" ] }, { "cell_type": "code", "execution_count": 8, "id": "02ef9b69-ef31-427d-86d0-b8c799e1c1b1", "metadata": {}, "outputs": [], "source": [ "# Let's wrap a call to GPT-4o-mini in a simple function\n", "\n", "def message_gpt(prompt):\n", " messages = [\n", " {\"role\": \"system\", \"content\": system_message},\n", " {\"role\": \"user\", \"content\": prompt}\n", " ]\n", " completion = openai.chat.completions.create(\n", " model='gpt-4o-mini',\n", " messages=messages,\n", " )\n", " return completion.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 9, "id": "aef7d314-2b13-436b-b02d-8de3b72b193f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Today's date is October 3, 2023.\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This can reveal the \"training cut off\", or the most recent date in the training data\n", "\n", "message_gpt(\"What is today's date?\")" ] }, { "cell_type": "markdown", "id": "f94013d1-4f27-4329-97e8-8c58db93636a", "metadata": {}, "source": [ "## User Interface time!" ] }, { "cell_type": "code", "execution_count": 10, "id": "bc664b7a-c01d-4fea-a1de-ae22cdd5141a", "metadata": {}, "outputs": [], "source": [ "# here's a simple function\n", "\n", "def shout(text):\n", " print(f\"Shout has been called with input {text}\")\n", " return text.upper()" ] }, { "cell_type": "code", "execution_count": 11, "id": "083ea451-d3a0-4d13-b599-93ed49b975e4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shout has been called with input hello\n" ] }, { "data": { "text/plain": [ "'HELLO'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shout(\"hello\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "08f1f15a-122e-4502-b112-6ee2817dda32", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "\n",
" ![]() | \n",
" \n",
" Before you read the next few cells\n", " \n", " Try to do this yourself - go back to the company brochure in week1, day5 and add a Gradio UI to the end. Then come and look at the solution.\n", " \n", " | \n",
"