{ "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!" ] }, { "cell_type": "code", "execution_count": 1, "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": 2, "id": "d1715421-cead-400b-99af-986388a97aff", "metadata": {}, "outputs": [], "source": [ "import gradio as gr # oh yeah!" ] }, { "cell_type": "code", "execution_count": 3, "id": "337d5dfc-0181-4e3b-8ab9-e78e0c3f657b", "metadata": {}, "outputs": [], "source": [ "# Load environment variables in a file called .env\n", "\n", "load_dotenv()\n", "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n", "os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n", "os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')" ] }, { "cell_type": "code", "execution_count": 4, "id": "22586021-1795-4929-8079-63f5bb4edd4c", "metadata": {}, "outputs": [], "source": [ "# Connect to OpenAI, Anthropic and Google\n", "\n", "openai = OpenAI()\n", "\n", "claude = anthropic.Anthropic()\n", "\n", "google.generativeai.configure()" ] }, { "cell_type": "code", "execution_count": 5, "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": 6, "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": 7, "id": "aef7d314-2b13-436b-b02d-8de3b72b193f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Today's date is April 27, 2024.\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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": 8, "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": 9, "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": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shout(\"hello\")" ] }, { "cell_type": "code", "execution_count": 10, "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": [ "