{ "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": null, "id": "c44c5494-950d-4d2f-8d4f-b87b57c5b330", "metadata": {}, "outputs": [], "source": [ "import os\n", "from dotenv import load_dotenv\n", "from openai import OpenAI" ] }, { "cell_type": "code", "execution_count": null, "id": "d1715421-cead-400b-99af-986388a97aff", "metadata": {}, "outputs": [], "source": [ "import gradio as gr # oh yeah!" ] }, { "cell_type": "code", "execution_count": null, "id": "337d5dfc-0181-4e3b-8ab9-e78e0c3f657b", "metadata": {}, "outputs": [], "source": [ "# Load environment variables in a file called .env\n", "# Print the key prefixes to help with any debugging\n", "# You can choose whichever providers you like - or all Ollama\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": null, "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", "anthropic_url = \"https://api.anthropic.com/v1/\"\n", "gemini_url = \"https://generativelanguage.googleapis.com/v1beta/openai/\"\n", "\n", "anthropic = OpenAI(api_key=anthropic_api_key, base_url=anthropic_url)\n", "gemini = OpenAI(api_key=google_api_key, base_url=gemini_url)" ] }, { "cell_type": "code", "execution_count": null, "id": "02ef9b69-ef31-427d-86d0-b8c799e1c1b1", "metadata": {}, "outputs": [], "source": [ "# Let's wrap a call to GPT-4.1-mini in a simple function\n", "\n", "system_message = \"You are a helpful assistant\"\n", "\n", "def message_gpt(prompt):\n", " messages = [{\"role\": \"system\", \"content\": system_message}, {\"role\": \"user\", \"content\": prompt}]\n", " response = openai.chat.completions.create(model=\"gpt-4.1-mini\", messages=messages)\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": null, "id": "aef7d314-2b13-436b-b02d-8de3b72b193f", "metadata": {}, "outputs": [], "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": null, "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": null, "id": "977bb496", "metadata": {}, "outputs": [], "source": [ "shout(\"hello\")" ] }, { "cell_type": "code", "execution_count": null, "id": "083ea451-d3a0-4d13-b599-93ed49b975e4", "metadata": {}, "outputs": [], "source": [ "gr.Interface(fn=shout, inputs=\"textbox\", outputs=\"textbox\", flagging_mode=\"never\").launch()" ] }, { "cell_type": "markdown", "id": "002ab4a6", "metadata": {}, "source": [ "
\n",
" \n",
" | \n",
" \n",
" NOTE: Using Gradio's Share tool\n", " I'm about to show you a really cool way to share your Gradio UI with others. This deploys your gradio app as a demo on gradio's website, and then allows gradio to call the 'shout' function. This uses an advanced technology known as 'HTTP tunneling' (like ngrok for people who know it) which isn't allowed by many Antivirus programs and corporate environments. If you get an error, just skip the next cell.\n", " \n", " | \n",
"
\n",
" \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",
"
\n",
" \n",
" | \n",
" \n",
" Gradio Resources\n", " If you'd like to go deeper on Gradio, check out the amazing documentation - a wonderful rabbit hole.\n", " https://www.gradio.app/guides/quickstart Gradio is primarily designed for Demos, Prototypes and MVPs, but I've also used it frequently to make internal apps for power users.\n", " \n", " | \n",
"