├── 01_gemini_basico.py ├── 01_gemini_basico_chat.py ├── 01_gemini_basico_chat_voz.py ├── 01_gemini_basico_imagem.py ├── 01_gemini_basico_stream.py ├── Gemini_AI_IMG.ipynb ├── LICENSE ├── README.md └── requirements.txt /01_gemini_basico.py: -------------------------------------------------------------------------------- 1 | import google.generativeai as genai 2 | 3 | genai.configure(api_key="sua-api-key") 4 | 5 | for m in genai.list_models(): 6 | if 'generateContent' in m.supported_generation_methods: 7 | print(m.name) 8 | 9 | model = genai.GenerativeModel('gemini-pro') 10 | 11 | response = model.generate_content("Qual o sentido da vida?") 12 | 13 | print(response.text) 14 | -------------------------------------------------------------------------------- /01_gemini_basico_chat.py: -------------------------------------------------------------------------------- 1 | import google.generativeai as genai 2 | 3 | genai.configure(api_key="sua-api-key") 4 | 5 | for m in genai.list_models(): 6 | if 'generateContent' in m.supported_generation_methods: 7 | print(m.name) 8 | 9 | model = genai.GenerativeModel('gemini-pro') 10 | 11 | chat = model.start_chat(history=[]) 12 | 13 | bem_vindo = "# Bem Vindo ao Assistente Mil Grau com Gemini AI #" 14 | print(len(bem_vindo) * "#") 15 | print(bem_vindo) 16 | print(len(bem_vindo) * "#") 17 | print("### Digite 'sair' para encerrar ###") 18 | print("") 19 | 20 | while True: 21 | texto = input("Escreva sua mensagem: ") 22 | 23 | if texto == "sair": 24 | break 25 | 26 | response = chat.send_message(texto) 27 | print("Gemini:", response.text, "\n") 28 | 29 | print("Encerrando Chat") 30 | -------------------------------------------------------------------------------- /01_gemini_basico_chat_voz.py: -------------------------------------------------------------------------------- 1 | import google.generativeai as genai 2 | 3 | def main(): 4 | assistente_falante = True 5 | ligar_microfone = True 6 | 7 | genai.configure(api_key="sua-api-key") 8 | for m in genai.list_models(): 9 | if 'generateContent' in m.supported_generation_methods: 10 | print(m.name) 11 | 12 | model = genai.GenerativeModel('gemini-pro') 13 | chat = model.start_chat(history=[]) 14 | 15 | ### configura voz 16 | if assistente_falante: 17 | import pyttsx3 18 | engine = pyttsx3.init() 19 | 20 | voices = engine.getProperty('voices') 21 | engine.setProperty('rate', 180) # velocidade 120 = lento 22 | 23 | print("\nLista de Vozes - Verifique o número\n") 24 | for indice, vozes in enumerate(voices): # listar vozes 25 | print(indice, vozes.name) 26 | 27 | voz = 1 28 | engine.setProperty('voice', voices[voz].id) 29 | 30 | if ligar_microfone: 31 | import speech_recognition as sr # pip install SpeechRecognition 32 | r = sr.Recognizer() 33 | mic = sr.Microphone() 34 | 35 | bem_vindo = "# Bem Vindo ao Assistente Mil Grau com Gemini AI #" 36 | print("") 37 | print(len(bem_vindo) * "#") 38 | print(bem_vindo) 39 | print(len(bem_vindo) * "#") 40 | print("### Digite 'desligar' para encerrar ###") 41 | print("") 42 | 43 | while True: 44 | if ligar_microfone: 45 | with mic as fonte: 46 | r.adjust_for_ambient_noise(fonte) 47 | print("Fale alguma coisa (ou diga 'desligar')") 48 | audio = r.listen(fonte) 49 | print("Enviando para reconhecimento") 50 | try: 51 | texto = r.recognize_google(audio, language="pt-BR") 52 | print("Você disse: {}".format(texto)) 53 | except Exception as e: 54 | print("Não entendi o que você disse. Erro", e) 55 | texto = "" 56 | else: 57 | texto = input("Escreva sua mensagem (ou #sair): ") 58 | 59 | if texto.lower() == "desligar": 60 | break 61 | 62 | response = chat.send_message(texto) 63 | print("Gemini:", response.text, "\n") 64 | 65 | if assistente_falante: 66 | engine.say(response.text) 67 | engine.runAndWait() 68 | 69 | print("Encerrando Chat") 70 | 71 | if __name__ == '__main__': 72 | main() -------------------------------------------------------------------------------- /01_gemini_basico_imagem.py: -------------------------------------------------------------------------------- 1 | import google.generativeai as genai 2 | import PIL.Image 3 | 4 | genai.configure(api_key="sua-api-key") 5 | 6 | for m in genai.list_models(): 7 | if 'generateContent' in m.supported_generation_methods: 8 | print(m.name) 9 | 10 | model = genai.GenerativeModel('gemini-pro-vision') 11 | 12 | img = PIL.Image.open('bob_img.png') 13 | 14 | response = model.generate_content(img) 15 | 16 | print("Resposta 1:", response.text) 17 | 18 | response = model.generate_content(["Descreva a imagem e depois diga quantos animais tem nessa imagem?", img]) 19 | response.resolve() 20 | 21 | print("Resposta da pergunta", response.text) 22 | -------------------------------------------------------------------------------- /01_gemini_basico_stream.py: -------------------------------------------------------------------------------- 1 | import google.generativeai as genai 2 | 3 | genai.configure(api_key="sua-api-key") 4 | 5 | for m in genai.list_models(): 6 | if 'generateContent' in m.supported_generation_methods: 7 | print(m.name) 8 | 9 | model = genai.GenerativeModel('gemini-pro') 10 | 11 | response = model.generate_content("Qual o sentido da vida?", stream=True) 12 | 13 | for chunk in response: 14 | print(chunk.text) 15 | print("_"*80) 16 | -------------------------------------------------------------------------------- /Gemini_AI_IMG.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": { 21 | "colab": { 22 | "base_uri": "https://localhost:8080/" 23 | }, 24 | "id": "Ij-g0dQWzJCJ", 25 | "outputId": "00f25887-50e6-4411-8f8c-6775c0e4e493" 26 | }, 27 | "outputs": [ 28 | { 29 | "output_type": "stream", 30 | "name": "stdout", 31 | "text": [ 32 | "Requirement already satisfied: google-generativeai in /usr/local/lib/python3.10/dist-packages (0.3.1)\n", 33 | "Requirement already satisfied: google-ai-generativelanguage==0.4.0 in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (0.4.0)\n", 34 | "Requirement already satisfied: google-auth in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (2.17.3)\n", 35 | "Requirement already satisfied: google-api-core in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (2.11.1)\n", 36 | "Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (3.20.3)\n", 37 | "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from google-generativeai) (4.66.1)\n", 38 | "Requirement already satisfied: proto-plus<2.0.0dev,>=1.22.3 in /usr/local/lib/python3.10/dist-packages (from google-ai-generativelanguage==0.4.0->google-generativeai) (1.22.3)\n", 39 | "Requirement already satisfied: googleapis-common-protos<2.0.dev0,>=1.56.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.61.0)\n", 40 | "Requirement already satisfied: requests<3.0.0.dev0,>=2.18.0 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (2.31.0)\n", 41 | "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (5.3.2)\n", 42 | "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (0.3.0)\n", 43 | "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (1.16.0)\n", 44 | "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth->google-generativeai) (4.9)\n", 45 | "Requirement already satisfied: grpcio<2.0dev,>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.59.3)\n", 46 | "Requirement already satisfied: grpcio-status<2.0.dev0,>=1.33.2 in /usr/local/lib/python3.10/dist-packages (from google-api-core->google-generativeai) (1.48.2)\n", 47 | "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth->google-generativeai) (0.5.1)\n", 48 | "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (3.3.2)\n", 49 | "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (3.6)\n", 50 | "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (2.0.7)\n", 51 | "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3.0.0.dev0,>=2.18.0->google-api-core->google-generativeai) (2023.11.17)\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "!pip install -U google-generativeai\n", 57 | "import google.generativeai as genai\n", 58 | "from google.colab import userdata" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "source": [ 64 | "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", 65 | "genai.configure(api_key=GOOGLE_API_KEY)" 66 | ], 67 | "metadata": { 68 | "id": "jDBV2scCzUbF" 69 | }, 70 | "execution_count": 3, 71 | "outputs": [] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "source": [ 76 | "for m in genai.list_models():\n", 77 | " if 'generateContent' in m.supported_generation_methods:\n", 78 | " print(m.name)" 79 | ], 80 | "metadata": { 81 | "colab": { 82 | "base_uri": "https://localhost:8080/", 83 | "height": 52 84 | }, 85 | "id": "nzMDFBHLz3qs", 86 | "outputId": "9abfdd1f-b16c-4677-b04e-9d8cb5424b9b" 87 | }, 88 | "execution_count": 4, 89 | "outputs": [ 90 | { 91 | "output_type": "stream", 92 | "name": "stdout", 93 | "text": [ 94 | "models/gemini-pro\n", 95 | "models/gemini-pro-vision\n" 96 | ] 97 | } 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "source": [ 103 | "model = genai.GenerativeModel('gemini-pro')" 104 | ], 105 | "metadata": { 106 | "id": "U1Noulgr0CsZ" 107 | }, 108 | "execution_count": 5, 109 | "outputs": [] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "source": [ 114 | "# @title Default title text\n", 115 | "%%time\n", 116 | "response = model.generate_content(\"Oi! Bom dia!\")" 117 | ], 118 | "metadata": { 119 | "colab": { 120 | "base_uri": "https://localhost:8080/", 121 | "height": 52 122 | }, 123 | "id": "utbtmAu201sT", 124 | "outputId": "b99c2644-f080-419c-875e-56383440cb55" 125 | }, 126 | "execution_count": 8, 127 | "outputs": [ 128 | { 129 | "output_type": "stream", 130 | "name": "stdout", 131 | "text": [ 132 | "CPU times: user 57 ms, sys: 9.05 ms, total: 66 ms\n", 133 | "Wall time: 3.19 s\n" 134 | ] 135 | } 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "source": [ 141 | "print(\"Resposta\", response.text)" 142 | ], 143 | "metadata": { 144 | "colab": { 145 | "base_uri": "https://localhost:8080/" 146 | }, 147 | "id": "A4N-Gbkx1MDr", 148 | "outputId": "9e32eaf4-21b8-49be-b65f-e5fb5085eb64" 149 | }, 150 | "execution_count": 9, 151 | "outputs": [ 152 | { 153 | "output_type": "stream", 154 | "name": "stdout", 155 | "text": [ 156 | "Resposta Olá! Bom dia para você também! Que bom começar o dia conversando com você. Espero que seu dia seja cheio de alegria e realizações. Se precisar de alguma coisa, não hesite em me perguntar. Estou aqui para ajudá-lo.\n" 157 | ] 158 | } 159 | ] 160 | } 161 | ] 162 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Inteligência Mil Grau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Gemini API 2 | Exemplos da API da Inteligência Artificial Google Gemini 3 | 4 | ## Instalação 5 | 6 | ### Usando o Google Colab 7 | A versão para Colab é toda on-line via [Google Colab](https://colab.google/) criando um novo notebook! 8 | 9 | ### Usando o Python 10 | 11 | Instale o Python baixando do [site oficial](https://www.python.org/) 12 | Para a instalação dos módulos 13 | ``` 14 | pip install -r requirements.txt 15 | ``` 16 | E para rodar, escolha um dos arquivos ".py" 17 | ``` 18 | python 01_gemini_basico_chat.py 19 | ``` 20 | 21 | ## Vídeo demonstração 22 | 23 | [!["API do Gemini AI da Google Grátis em Python - Assistente Falante"](https://img.youtube.com/vi/bXymjacrklk/0.jpg)](https://www.youtube.com/watch?v=bXymjacrklk) 24 | 25 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | google-generativeai 2 | pyttsx3 3 | SpeechRecognition 4 | --------------------------------------------------------------------------------