diff --git a/notebooks/2025-03-20-google-gemini-2.0-flash-api.ipynb b/notebooks/2025-03-20-google-gemini-2.0-flash-api.ipynb new file mode 100644 index 0000000..b5e4c91 --- /dev/null +++ b/notebooks/2025-03-20-google-gemini-2.0-flash-api.ipynb @@ -0,0 +1,718 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Gemini 2.0 Flash Exploration\n", + "\n", + "- **Objective:** Explore gemini 2.0 flash for image comprehrension.\n", + "- This should be run in Google Colab, which will authenticate the user to their existing Google Cloud Project. It will have to be modified set up authentication in an app." + ], + "metadata": { + "id": "yZws-hg25uyl" + } + }, + { + "cell_type": "code", + "source": [ + "!pip install filetype" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "M0HxilHxAlWp", + "outputId": "1b7294aa-05ce-4153-ab29-f5baf85dcf00" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Collecting filetype\n", + " Downloading filetype-1.2.0-py2.py3-none-any.whl.metadata (6.5 kB)\n", + "Downloading filetype-1.2.0-py2.py3-none-any.whl (19 kB)\n", + "Installing collected packages: filetype\n", + "Successfully installed filetype-1.2.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "u-fOuz-4AYMA" + }, + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "import vertexai\n", + "from vertexai.preview.generative_models import GenerativeModel, Part\n", + "from google.colab import files, userdata\n", + "import io\n", + "import filetype\n", + "import base64\n", + "import time\n", + "import pprint" + ] + }, + { + "cell_type": "code", + "source": [ + "from google.colab import auth\n", + "auth.authenticate_user() # authentication to your google projects account on Colab only\n", + "\n", + "PROJECT_ID = userdata.get('PROJECT_ID') # Replace with your project ID\n", + "LOCATION = \"us-east1\" # Replace with your location (e.g., us-central1)\n", + "\n", + "vertexai.init(project=PROJECT_ID, location=LOCATION)\n", + "\n", + "# Initialize the Vision model\n", + "model = GenerativeModel(\"gemini-2.0-flash\")" + ], + "metadata": { + "id": "huBMlFGXAtPH" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "uploaded = files.upload()\n", + "\n", + "for file_name, file_content in uploaded.items():\n", + " with open(file_name, 'wb') as new_file:\n", + " new_file.write(file_content)\n", + " print(f'User uploaded file \"{file_name}\"')\n", + "\n", + "def encode_image(image_path):\n", + " with open(image_path, \"rb\") as image_file:\n", + " return base64.b64encode(image_file.read()).decode('utf-8')\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + }, + "id": "H1WprwU7AyYh", + "outputId": "968bcd66-01c9-446f-c24d-771e1e944667" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + " \n", + " \n", + " Upload widget is only available when the cell has been executed in the\n", + " current browser session. Please rerun this cell to enable.\n", + " \n", + " " + ] + }, + "metadata": {} + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Saving fake_signed_petitions_1-10.pdf to fake_signed_petitions_1-10.pdf\n", + "User uploaded file \"fake_signed_petitions_1-10.pdf\"\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "def extract_signature_info(base64_image):\n", + "\n", + " prompt=\"Using the written text in the image create a list of dictionaries where each dictionary consists of keys 'Name', 'Address', 'Date', and 'Ward'. Fill in the values of each dictionary with the correct entries for each key. Write all the values of the dictionary in full. Only output the list of dictionaries. No other intro text is necessary. Avoid all caps. The output should be in JSON format, and look like \\\n", + " {'data': [{\\\"Name\\\": \\\"John Doe\\\", \\\n", + " \\\"Address\\\": \\\"123 Picket Lane\\\", \\\n", + " \\\"Date\\\": \\\"11/23/2024\\\", \\\n", + " \\\"Ward\\\": \\\"2\\\"}, \\\n", + " {\\\"Name\\\": \\\"Jane Plane\\\", \\\n", + " \\\"Address\\\": \\\"456 Fence Field\\\", \\\n", + " \\\"Date\\\": \\\"11/23/2024\\\", \\\n", + " \\\"Ward\\\": \\\"3\\\"}, \\\n", + " ]}.\"\n", + "\n", + " image_data = base64.b64decode(base64_image)\n", + "\n", + " # Detect the image type using filetype library\n", + " kind = filetype.guess(image_data)\n", + "\n", + " # Set mime_type based on detected image type\n", + " if kind is not None:\n", + " mime_type = kind.mime\n", + " else:\n", + " mime_type = \"image/jpeg\" # Fallback to jpeg if type detection fails\n", + " print(\"Warning: Could not determine image type, assuming JPEG.\")\n", + "\n", + " image_part = Part.from_data(data=image_data, mime_type=mime_type)\n", + " signature_list = model.generate_content([prompt, image_part])\n", + " return signature_list.text\n", + "\n", + "image_path = file_name\n", + "base64_image = encode_image(image_path)\n", + "\n", + "# Print the length of the base64 string for debugging\n", + "print(f\"Length of base64_image: {len(base64_image)}\")\n", + "\n", + "# Check if the base64 string is valid by decoding a small portion\n", + "try:\n", + " base64.b64decode(base64_image[:24]) # Decode a small portion for validation\n", + " print(\"Base64 string appears valid.\")\n", + "except Exception as e:\n", + " print(f\"Error validating base64 string: {e}\")\n", + " print(\"Please check the encoding process and the image file.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "hTgTRUZcA7Xs", + "outputId": "09825cba-ecc3-4584-ad55-af0a9b1a7e90" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Length of base64_image: 6954688\n", + "Base64 string appears valid.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "signator_list = extract_signature_info(base64_image)\n", + "print(signator_list)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "z2rNfBO0BLqG", + "outputId": "34e0efb1-cadd-4baa-ed84-43e48fca2ad6" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "```json\n", + "{\n", + "\"data\": [\n", + " {\n", + " \"Name\": \"Alexis Walter\",\n", + " \"Address\": \"23407 Hawkins Lock\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Jenny Jones\",\n", + " \"Address\": \"82424 Rachel Views\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Jack Stewart\",\n", + " \"Address\": \"756 Ashley Court Suite 408\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Brady Herrera\",\n", + " \"Address\": \"88242 Gray Well\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Robert Ayala\",\n", + " \"Address\": \"6563 Charles Greens Apt. 636\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Erik Acosta\",\n", + " \"Address\": \"734 William Oval\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"William Hopkins\",\n", + " \"Address\": \"1869 Cathy Hews Apt. 061\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Debbie Sosa\",\n", + " \"Address\": \"995 Johnson Lane Apt. 926\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Kenneth Ho\",\n", + " \"Address\": \"777 Rivera Mews Suite 338\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Christina Goodman\",\n", + " \"Address\": \"543 Melissa Ferry\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Cindy Garcia\",\n", + " \"Address\": \"152 Sandra Landing Apt. 815\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Patricia Holden\",\n", + " \"Address\": \"54895 Miller Mountains Suite 374\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\"\n", + " },\n", + " {\n", + " \"Name\": \"Maria Burgess\",\n", + " \"Address\": \"7016 Logan Square Apt. 184\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Melinda Pierce\",\n", + " \"Address\": \"2000 Cooper Mews\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"3\"\n", + " },\n", + " {\n", + " \"Name\": \"Steven Spencer\",\n", + " \"Address\": \"2453 Hoffman Island Apt. 578\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"3\"\n", + " },\n", + " {\n", + " \"Name\": \"Jonathon Williams\",\n", + " \"Address\": \"128 Patterson Junction Apt. 650\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Jeremy Figueroa\",\n", + " \"Address\": \"3890 Edward Parkways\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Shelby Duncan\",\n", + " \"Address\": \"3047 Stephen Brook Suite 970\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Tina Briggs\",\n", + " \"Address\": \"632 Jones Knolls Suite 974\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Phyllis Johnson\",\n", + " \"Address\": \"9762 Wright Plaza\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Elizabeth Rios\",\n", + " \"Address\": \"7982 Teresa Garden Suite 334\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Erin Steuart\",\n", + " \"Address\": \"84 Taylor Brooks Suite 469\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\"\n", + " },\n", + " {\n", + " \"Name\": \"Elaine Weaver\",\n", + " \"Address\": \"8199 Castro Way\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\"\n", + " },\n", + " {\n", + " \"Name\": \"Lance Mullen\",\n", + " \"Address\": \"360 Snow Trafficway\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Blake Davis\",\n", + " \"Address\": \"400 Linda Orchard Apt. 510\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Ralph Evans\",\n", + " \"Address\": \"94925 Simpson Roads\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\" \n", + " },\n", + " {\n", + " \"Name\": \"Peter Harvey\",\n", + " \"Address\": \"12239 Victoria Mountains Suite 294\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Raymond Green\",\n", + " \"Address\": \"624 Williams Ville\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Wesley Harrington\",\n", + " \"Address\": \"5293 Maynard Gardens Apt. 407\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Michelle Day\",\n", + " \"Address\": \"351 Sanchez Courts Suite 250\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Samantha Herrera\",\n", + " \"Address\": \"4453 John Causeway Apt. 743\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Wanda Edwards\",\n", + " \"Address\": \"100 Anderson Plains Apt. 353\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Sergio Sawyer\",\n", + " \"Address\": \"55643 Johnson Passage\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Jorge Dougherty\",\n", + " \"Address\": \"38503 Sanchez Vista\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"Jordan McDonald\",\n", + " \"Address\": \"165 Madison Ramp\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Alexis Simmons\",\n", + " \"Address\": \"66198 Becker Underpass\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\"\n", + " },\n", + " {\n", + " \"Name\": \"Kaitlyn Owen\",\n", + " \"Address\": \"472 Kelly Corner Apt. 122\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"2\"\n", + " },\n", + " {\n", + " \"Name\": \"Brett Bennett\",\n", + " \"Address\": \"6194 Fletcher Pine\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Ronald Cole Jr.\",\n", + " \"Address\": \"184 Davis Views\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Megan Nielsen\",\n", + " \"Address\": \"439 Herrera Pass Apt. 790\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Laura Nguyen\",\n", + " \"Address\": \"92602 Robles Fall Suite 060\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Ryan Murphy\",\n", + " \"Address\": \"270 Schwartz Island Apt. 558\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Gerald Butler\",\n", + " \"Address\": \"6850 Jackson Walk Suite 914\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"Samantha Burns MD\",\n", + " \"Address\": \"514 Carlson Crescent\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"3\"\n", + " },\n", + " {\n", + " \"Name\": \"Tim Parker\",\n", + " \"Address\": \"31448 Vazquez Path Apt. 999\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"6\"\n", + " },\n", + " {\n", + " \"Name\": \"Lindsay Kennedy\",\n", + " \"Address\": \"28661 Cross Prairie Apt. 048\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"7\"\n", + " },\n", + " {\n", + " \"Name\": \"Adam Welch\",\n", + " \"Address\": \"5211 Sloan Wall\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " },\n", + " {\n", + " \"Name\": \"John Sheppard\",\n", + " \"Address\": \"8704 Stacy Parks\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"1\"\n", + " },\n", + " {\n", + " \"Name\": \"William Morse Jr.\",\n", + " \"Address\": \"594 Harold fork\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"4\"\n", + " },\n", + " {\n", + " \"Name\": \"Tina Carroll\",\n", + " \"Address\": \"733 Cantu Villages\",\n", + " \"Date\": \"1/15\",\n", + " \"Ward\": \"5\"\n", + " }\n", + "]\n", + "}\n", + "```\n" + ] + } + ] + } + ] +} \ No newline at end of file