Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions Access_GitHub_Link.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"language_info": {
"name": "python"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/aka76bm/0xDeCA10B/blob/main/Access_GitHub_Link.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SKG_3zy_AhjA"
},
"source": [
"# Setup\n",
"\n",
"Please ensure you have imported a Gemini API key from AI Studio.\n",
"You can do this directly in the Secrets tab on the left.\n",
"\n",
"After doing so, please run the setup cell below."
]
},
{
"cell_type": "code",
"metadata": {
"id": "swM2wHGtAhjI"
},
"source": [
"!pip install -U -q \"google\"\n",
"!pip install -U -q \"google.genai\"\n",
"\n",
"import os\n",
"from google.colab import userdata\n",
"from google.colab import drive\n",
"os.environ[\"GEMINI_API_KEY\"] = userdata.get(\"GOOGLE_API_KEY\")\n",
"\n",
"drive.mount(\"/content/drive\")\n",
"# Please ensure that uploaded files are available in the AI Studio folder or change the working folder.\n",
"os.chdir(\"/content/drive/MyDrive/Google AI Studio\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "DWbbJywhAhjL"
},
"source": [
"# Generated Code"
]
},
{
"cell_type": "code",
"metadata": {
"id": "VywpA5h0AhjM"
},
"source": [
"# To run this code you need to install the following dependencies:\n",
"# pip install google-genai\n",
"\n",
"import base64\n",
"import mimetypes\n",
"import os\n",
"from google import genai\n",
"from google.genai import types\n",
"\n",
"\n",
"def save_binary_file(file_name, data):\n",
" f = open(file_name, \"wb\")\n",
" f.write(data)\n",
" f.close()\n",
" print(f\"File saved to to: {file_name}\")\n",
"\n",
"\n",
"def generate():\n",
" client = genai.Client(\n",
" api_key=os.environ.get(\"GEMINI_API_KEY\"),\n",
" )\n",
"\n",
" model = \"gemini-2.5-flash-image\"\n",
" contents = [\n",
" types.Content(\n",
" role=\"user\",\n",
" parts=[\n",
" types.Part.from_text(text=\"\"\"INSERT_INPUT_HERE\"\"\"),\n",
" ],\n",
" ),\n",
" ]\n",
" generate_content_config = types.GenerateContentConfig(\n",
" response_modalities=[\n",
" \"IMAGE\",\n",
" \"TEXT\",\n",
" ],\n",
" )\n",
"\n",
" file_index = 0\n",
" for chunk in client.models.generate_content_stream(\n",
" model=model,\n",
" contents=contents,\n",
" config=generate_content_config,\n",
" ):\n",
" if (\n",
" chunk.candidates is None\n",
" or chunk.candidates[0].content is None\n",
" or chunk.candidates[0].content.parts is None\n",
" ):\n",
" continue\n",
" if chunk.candidates[0].content.parts[0].inline_data and chunk.candidates[0].content.parts[0].inline_data.data:\n",
" file_name = f\"ENTER_FILE_NAME_{file_index}\"\n",
" file_index += 1\n",
" inline_data = chunk.candidates[0].content.parts[0].inline_data\n",
" data_buffer = inline_data.data\n",
" file_extension = mimetypes.guess_extension(inline_data.mime_type)\n",
" save_binary_file(f\"{file_name}{file_extension}\", data_buffer)\n",
" else:\n",
" print(chunk.text)\n",
"\n",
"if __name__ == \"__main__\":\n",
" generate()\n"
],
"execution_count": null,
"outputs": []
}
]
}
Loading