Skip to content

Commit be7918e

Browse files
Se creó con Colab
1 parent dfe1195 commit be7918e

1 file changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "Tce3stUlHN0L"
7+
},
8+
"source": [
9+
"##### Copyright 2023 Google LLC"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 4,
15+
"metadata": {
16+
"id": "tuOe1ymfHZPu"
17+
},
18+
"outputs": [],
19+
"source": [
20+
"# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
21+
"# you may not use this file except in compliance with the License.\n",
22+
"# You may obtain a copy of the License at\n",
23+
"#\n",
24+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
25+
"#\n",
26+
"# Unless required by applicable law or agreed to in writing, software\n",
27+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
28+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
29+
"# See the License for the specific language governing permissions and\n",
30+
"# limitations under the License."
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {
36+
"id": "FKwyTRdwB8aW"
37+
},
38+
"source": [
39+
"## Setup"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 5,
45+
"metadata": {
46+
"id": "RXInneX6xx7c"
47+
},
48+
"outputs": [],
49+
"source": [
50+
"!pip install -U -q \"google-generativeai>=0.8.2\""
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"source": [],
56+
"metadata": {
57+
"id": "Yw7aMYZ2t0N-"
58+
},
59+
"execution_count": 5,
60+
"outputs": []
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 7,
65+
"metadata": {
66+
"id": "kWIuwKG2_oWE",
67+
"outputId": "31dc66f5-5c9f-4102-e2e6-781b3106d0c9",
68+
"colab": {
69+
"base_uri": "https://localhost:8080/",
70+
"height": 564
71+
}
72+
},
73+
"outputs": [
74+
{
75+
"output_type": "error",
76+
"ename": "MessageError",
77+
"evalue": "Error: credential propagation was unsuccessful",
78+
"traceback": [
79+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
80+
"\u001b[0;31mMessageError\u001b[0m Traceback (most recent call last)",
81+
"\u001b[0;32m<ipython-input-7-cf3dd51f0a0c>\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdrive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mdrive\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"/gdrive\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
82+
"\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/drive.py\u001b[0m in \u001b[0;36mmount\u001b[0;34m(mountpoint, force_remount, timeout_ms, readonly)\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmountpoint\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mforce_remount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout_ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m120000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreadonly\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 99\u001b[0m \u001b[0;34m\"\"\"Mount your Google Drive at the specified mountpoint path.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 100\u001b[0;31m return _mount(\n\u001b[0m\u001b[1;32m 101\u001b[0m \u001b[0mmountpoint\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[0mforce_remount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mforce_remount\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
83+
"\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/drive.py\u001b[0m in \u001b[0;36m_mount\u001b[0;34m(mountpoint, force_remount, timeout_ms, ephemeral, readonly)\u001b[0m\n\u001b[1;32m 135\u001b[0m )\n\u001b[1;32m 136\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mephemeral\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 137\u001b[0;31m _message.blocking_request(\n\u001b[0m\u001b[1;32m 138\u001b[0m \u001b[0;34m'request_auth'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 139\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'authType'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'dfs_ephemeral'\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
84+
"\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/_message.py\u001b[0m in \u001b[0;36mblocking_request\u001b[0;34m(request_type, request, timeout_sec, parent)\u001b[0m\n\u001b[1;32m 174\u001b[0m \u001b[0mrequest_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparent\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpect_reply\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 175\u001b[0m )\n\u001b[0;32m--> 176\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mread_reply_from_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout_sec\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
85+
"\u001b[0;32m/usr/local/lib/python3.11/dist-packages/google/colab/_message.py\u001b[0m in \u001b[0;36mread_reply_from_input\u001b[0;34m(message_id, timeout_sec)\u001b[0m\n\u001b[1;32m 101\u001b[0m ):\n\u001b[1;32m 102\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m'error'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mreply\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 103\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mMessageError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreply\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'error'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 104\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mreply\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'data'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
86+
"\u001b[0;31mMessageError\u001b[0m: Error: credential propagation was unsuccessful"
87+
]
88+
}
89+
],
90+
"source": [
91+
"# import necessary modules.\n",
92+
"\n",
93+
"import google.generativeai as genai\n",
94+
"\n",
95+
"import base64\n",
96+
"import json\n",
97+
"\n",
98+
"try:\n",
99+
" # Mount google drive\n",
100+
" from google.colab import drive\n",
101+
"\n",
102+
" drive.mount(\"/gdrive\")\n",
103+
"\n",
104+
" # The SDK will automatically read it from the GOOGLE_API_KEY environment variable.\n",
105+
" # In Colab get the key from Colab-secrets (\"🔑\" in the left panel).\n",
106+
" import os\n",
107+
" from google.colab import userdata\n",
108+
"\n",
109+
" os.environ[\"GOOGLE_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n",
110+
"except ImportError:\n",
111+
" pass\n",
112+
"\n",
113+
"# Parse the arguments\n",
114+
"\n",
115+
"model = \"gemini-1.5-flash\" # @param {isTemplate: true}\n",
116+
"contents_b64 = b'W3sicGFydHMiOiBbeyJ0ZXh0IjogIkhlbGxvIn1dfV0='\n",
117+
"generation_config_b64 = \"e30=\" # @param {isTemplate: true}\n",
118+
"safety_settings_b64 = \"e30=\" # @param {isTemplate: true}\n",
119+
"\n",
120+
"contents = json.loads(base64.b64decode(contents_b64))\n",
121+
"\n",
122+
"generation_config = json.loads(base64.b64decode(generation_config_b64))\n",
123+
"safety_settings = json.loads(base64.b64decode(safety_settings_b64))\n",
124+
"\n",
125+
"stream = False\n",
126+
"\n",
127+
"print(json.dumps(contents, indent=4))"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"source": [
133+
"# @title Example form fields\n",
134+
"# @markdown Forms support many types of fields.\n",
135+
"\n",
136+
"no_type_checking = '' # @param\n",
137+
"string_type = 'example' # @param {type: \"string\"}\n",
138+
"slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n",
139+
"number = 102 # @param {type: \"number\"}\n",
140+
"date = '2010-11-05' # @param {type: \"date\"}\n",
141+
"pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n",
142+
"select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n",
143+
"# @markdown ---"
144+
],
145+
"metadata": {
146+
"id": "g1gW9mM_ubOf"
147+
},
148+
"execution_count": null,
149+
"outputs": []
150+
},
151+
{
152+
"cell_type": "code",
153+
"source": [
154+
"# @title Example form fields\n",
155+
"# @markdown Forms support many types of fields.\n",
156+
"\n",
157+
"no_type_checking = '' # @param\n",
158+
"string_type = 'example' # @param {type: \"string\"}\n",
159+
"slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n",
160+
"number = 102 # @param {type: \"number\"}\n",
161+
"date = '2010-11-05' # @param {type: \"date\"}\n",
162+
"pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n",
163+
"select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n",
164+
"# @markdown ---"
165+
],
166+
"metadata": {
167+
"id": "h-iZ7VmkueMp"
168+
},
169+
"execution_count": null,
170+
"outputs": []
171+
},
172+
{
173+
"cell_type": "code",
174+
"source": [
175+
"# @title Example form fields\n",
176+
"# @markdown Forms support many types of fields.\n",
177+
"\n",
178+
"no_type_checking = '' # @param\n",
179+
"string_type = 'example' # @param {type: \"string\"}\n",
180+
"slider_value = 142 # @param {type: \"slider\", min: 100, max: 200}\n",
181+
"number = 102 # @param {type: \"number\"}\n",
182+
"date = '2010-11-05' # @param {type: \"date\"}\n",
183+
"pick_me = \"monday\" # @param ['monday', 'tuesday', 'wednesday', 'thursday']\n",
184+
"select_or_input = \"apples\" # @param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n",
185+
"# @markdown ---"
186+
],
187+
"metadata": {
188+
"id": "hhcqKxL3ufQN"
189+
},
190+
"execution_count": null,
191+
"outputs": []
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"metadata": {
196+
"id": "E7zAD69vE92b"
197+
},
198+
"source": [
199+
"## Call `generate_content`"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {
206+
"id": "LB2LxPmAB95V"
207+
},
208+
"outputs": [],
209+
"source": [
210+
"from IPython.display import display\n",
211+
"from IPython.display import Markdown\n",
212+
"\n",
213+
"# Call the model and print the response.\n",
214+
"gemini = genai.GenerativeModel(model_name=model)\n",
215+
"\n",
216+
"response = gemini.generate_content(\n",
217+
" contents,\n",
218+
" generation_config=generation_config,\n",
219+
" safety_settings=safety_settings,\n",
220+
" stream=stream,\n",
221+
")\n",
222+
"\n",
223+
"display(Markdown(response.text))"
224+
]
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"metadata": {
229+
"id": "9c9d345e9868"
230+
},
231+
"source": [
232+
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
233+
" <td>\n",
234+
" <a target=\"_blank\" href=\"https://ai.google.dev/gemini-api/docs\"><img src=\"https://ai.google.dev/static/site-assets/images/docs/notebook-site-button.png\" height=\"32\" width=\"32\" />Docs on ai.google.dev</a>\n",
235+
" </td>\n",
236+
" <td>\n",
237+
" <a target=\"_blank\" href=\"https://github.com/google-gemini/cookbook/blob/main/quickstarts\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />More notebooks in the Cookbook</a>\n",
238+
" </td>\n",
239+
"</table>"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"source": [],
245+
"metadata": {
246+
"id": "NrPCiw9Lt_LM"
247+
},
248+
"execution_count": null,
249+
"outputs": []
250+
},
251+
{
252+
"cell_type": "code",
253+
"source": [
254+
"from google.colab import drive\n",
255+
"drive.mount('/content/drive')"
256+
],
257+
"metadata": {
258+
"id": "98RE5lG5uE57"
259+
},
260+
"execution_count": null,
261+
"outputs": []
262+
}
263+
],
264+
"metadata": {
265+
"colab": {
266+
"collapsed_sections": [
267+
"Tce3stUlHN0L"
268+
],
269+
"name": "aistudio_gemini_prompt_freeform_nofiles.ipynb",
270+
"provenance": []
271+
},
272+
"kernelspec": {
273+
"display_name": "Python 3",
274+
"name": "python3"
275+
}
276+
},
277+
"nbformat": 4,
278+
"nbformat_minor": 0
279+
}

0 commit comments

Comments
 (0)