Skip to content

Commit

Permalink
Add Upload files example notebook, link from quickstarts (google-gemi…
Browse files Browse the repository at this point in the history
…ni#41)

* Add Upload files example notebook, link from quickstarts

* review comments

* fix links
  • Loading branch information
williamito authored Apr 9, 2024
1 parent b1a095b commit ea9eda8
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 27 deletions.
218 changes: 218 additions & 0 deletions examples/Upload_files.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Upload files to Google Colab\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/Upload_files.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
"</table>"
],
"metadata": {
"id": "PzjeBM__IE1k"
}
},
{
"cell_type": "markdown",
"metadata": {
"id": "1vDOxWIpHPyD"
},
"source": [
"You can upload files to Google Colab to quickly experiment with your own data. For example, you can upload video or image files to use with the Files API, or a upload a text file to read in with a long context model like Gemini 1.5 Pro. This example shows you how to upload files to the Colab runtime and use them in your code.\n",
"\n",
"First, download the following file to your local machine:\n",
"\n",
"* [a11.txt](https://storage.googleapis.com/generativeai-downloads/data/a11.txt)\n",
"\n",
"It contains a transcript of transmissions from the Apollo 11 mission, originally from https://www.nasa.gov/history/alsj/a11/a11trans.html.\n",
"\n",
"Next, upload the file to Google Colab. To do so, first click **Files** on the left sidebar, then click the **Upload** button:\n",
"\n",
"<img width=400 src=\"https://ai.google.dev/tutorials/images/colab_upload.png\">\n",
"\n",
"You're now able to use the file in Colab!"
]
},
{
"cell_type": "code",
"source": [
"with open('a11.txt') as file:\n",
" text_data = file.read()\n",
"\n",
"# Print first 10 lines\n",
"for line in text_data.splitlines()[:10]:\n",
" print(line)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "a90kMgiDvNlk",
"outputId": "670a4434-6afc-43e9-e4b0-60ef86a02548"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"INTRODUCTION\n",
"\n",
"This is the transcription of the Technical Air-to-Ground Voice Transmission (GOSS NET 1) from the Apollo 11 mission.\n",
"\n",
"Communicators in the text may be identified according to the following list.\n",
"\n",
"Spacecraft:\n",
"CDR\tCommander\tNeil A. Armstrong\n",
"CMP\tCommand module pilot \tMichael Collins\n",
"LMP\tLunar module pilot\tEdwin E. ALdrin, Jr.\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"This makes it simple to use the file with the Gemini API."
],
"metadata": {
"id": "HxFNPBonwZc3"
}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "_d_yY8XWGQ12",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "96454844-3863-4274-ae4e-3c392875650c"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m142.1/142.1 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m663.6/663.6 kB\u001b[0m \u001b[31m10.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "TeVyF3GtGQ13"
},
"outputs": [],
"source": [
"import google.generativeai as genai"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l8g4hTRotheH"
},
"source": [
"To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/gemini-api-cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "iWd---jVKV5M"
},
"outputs": [],
"source": [
"from google.colab import userdata\n",
"\n",
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
"genai.configure(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "code",
"source": [
"model = genai.GenerativeModel('gemini-1.5-pro-latest')\n",
"\n",
"response = model.generate_content(['What is this transcript?', text_data])\n",
"print(response.text)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 610
},
"id": "9b-uSKn8wofx",
"outputId": "b52781c2-fcf8-451a-d407-489cfdfadbbf"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"## Analysis of Apollo 11 Air-to-Ground Voice Transcription\n",
"\n",
"The transcript you provided is a valuable historical document, capturing the real-time communication between the Apollo 11 crew and Mission Control during their groundbreaking journey to the Moon and back. It provides fascinating insights into the challenges, technical details, and even the lighter moments of this momentous mission.\n",
"\n",
"Here are some key aspects of the transcript:\n",
"\n",
"**Technical details and problem-solving:** \n",
"\n",
"* The transcript reveals the intricate details of the mission, including specific procedures, system checks, and real-time data exchange. We can observe the crew and ground control working together to resolve issues like:\n",
" * RCS temperature problems after separation from the S-IVB.\n",
" * Gimbal motor malfunctions.\n",
" * Oxygen flow transducer issues.\n",
" * Pressure discrepancies.\n",
" * Communication challenges.\n",
"\n",
"**Human element and teamwork:** \n",
"\n",
"* Beyond the technical aspects, the transcript offers a glimpse into the human side of the mission. We witness the camaraderie and teamwork between the crew and ground control, evident in their lighthearted exchanges, jokes, and expressions of mutual support and congratulations. \n",
"* We also see moments of tension and concern, particularly during critical phases like TLI and powered descent, highlighting the immense pressure and responsibility that both the crew and ground control faced.\n",
"\n",
"**Historical context:**\n",
"\n",
"* The transcript places the mission within the context of the Cold War space race, with mentions of the Soviet Luna 15 mission and political statements about the future of space exploration.\n",
"* It also captures the excitement and global impact of Apollo 11, referencing news reports, public reactions, and the involvement of various countries in supporting the mission. \n",
"\n",
"**Scientific observations:**\n",
"\n",
"* The transcript records the astronauts' observations of the Earth, Moon, and weather patterns from their unique vantage point in space. Their detailed descriptions provide valuable scientific data and offer a sense of wonder and awe at the beauty and vastness of the cosmos.\n",
"\n",
"**Overall significance:**\n",
"\n",
"This transcript serves as a testament to human ingenuity, courage, and collaboration in achieving the seemingly impossible. It is a reminder of the historical significance of Apollo 11 and its enduring legacy as a pivotal moment in human history. \n",
"\n"
]
}
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
20 changes: 11 additions & 9 deletions quickstarts/File_API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"id": "_d_yY8XWGQ12",
"outputId": "1b65678f-7dae-43bc-b051-f3cde9e700fa",
Expand All @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"id": "TeVyF3GtGQ13"
},
Expand Down Expand Up @@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"id": "iWd---jVKV5M"
},
Expand Down Expand Up @@ -134,12 +134,14 @@
"id": "2wsJ0vHNNtdJ"
},
"source": [
"First, we will prepare a sample image to upload to the API."
"First, we will prepare a sample image to upload to the API.\n",
"\n",
"Note: You can also [upload your own files](https://github.com/google-gemini/cookbook/tree/main/examples/Upload_files.ipynb) to use."
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"id": "EfuQVRXIGqvt",
"outputId": "063fed0e-36ae-47d3-d3d4-3d950f6bb465",
Expand Down Expand Up @@ -186,7 +188,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"id": "N9NxXGZKKusG",
"outputId": "63d30212-03bb-4876-8614-819a97819a59",
Expand Down Expand Up @@ -236,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"id": "SHMVCWHkFhJW",
"outputId": "554da5f2-58f8-4b30-823a-4cc4233e66bc",
Expand Down Expand Up @@ -272,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {
"id": "ZYVFqmLkl5nE",
"outputId": "2a200671-ba6f-4270-93ad-d21cfc13c944",
Expand Down Expand Up @@ -311,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"id": "d4eO8ZXoKdZf",
"outputId": "b828802e-194d-441d-fd75-abe26d357845",
Expand Down
21 changes: 3 additions & 18 deletions quickstarts/Video.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@
"\n",
"We will extract 1 frame a second from a the short film \"Big Buck Bunny\" file using [OpenCV](https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html).\n",
"\n",
"> \"Big Buck Bunny\" is (c) copyright 2008, Blender Foundation / www.bigbuckbunny.org and [licensed](https://peach.blender.org/about/) under the [Creative Commons Attribution 3.0](http://creativecommons.org/licenses/by/3.0/) License."
"> \"Big Buck Bunny\" is (c) copyright 2008, Blender Foundation / www.bigbuckbunny.org and [licensed](https://peach.blender.org/about/) under the [Creative Commons Attribution 3.0](http://creativecommons.org/licenses/by/3.0/) License.\n",
"\n",
"Note: You can also [upload your own files](https://github.com/google-gemini/cookbook/tree/main/examples/Upload_files.ipynb) to use."
]
},
{
Expand Down Expand Up @@ -462,23 +464,6 @@
"print(f\"Completed deleting files!\\n\\nDeleted: {len(uploaded_files)} files\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OexqR86FIiCu"
},
"source": [
"## Use your own files by uploading to colab\n",
"\n",
"This notebook useds the Files API with files that were downloaded from the internet. If you're running this in Colab and want to use your own files, you first need to upload them to the colab instance.\n",
"\n",
"Click **Files** on the left sidebar, then click the **Upload** button:\n",
"\n",
"<img width=400 src=\"https://ai.google.dev/tutorials/images/colab_upload.png\">\n",
"\n",
"Then use the code above with the path to your file.\n"
]
},
{
"cell_type": "markdown",
"source": [
Expand Down

0 comments on commit ea9eda8

Please sign in to comment.