9.3 C
New York
Tuesday, March 5, 2024

Trying Google Gemini with Python

Trying Google Gemini with Python

I am very happy because nowadays there is an equal competitor of ChatGPT from OpenAI. Introducing Gemini: Google’s most capable AI model yet, an LLM model that was developed by the Google Deepmind team. As we know, GPT doesn’t provide the free API, though they provide the playground API but we need more than the playground as developers. Currently, Gemini AI provides free access to their pro models with a limit of 60 queries per minute. Hmm, that’s interesting!!

Without further ado, let’s chat with Gemini.

The first thing that we need to do is create an API for the Gemini in the following link. After clicking the link, it takes us to the Google AI Studio.

I am running the code in the Google Colab, the you can also access the code here ==> Try Gemini AI with python.ipynb

# Intsall the required package

pip install -q -U google-generativeai

import pathlib
import textwrap

import google.generativeai as genai

# Used to securely store your API key
from google.colab import userdata

from IPython.display import display
from IPython.display import Markdown

def to_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

genai.configure(api_key='GOOGLE_API_KEY') ## put your API_KEY here

Let’s see which available models to use

for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:

# models/gemini-pro
# models/gemini-pro-vision

we have two models here, the gemini-pro is optimized for text-only prompts. Whereas the gemini-pro-vision is optimized for text-and-images prompts. Let’s try both of them.

Generate text from text inputs with gemini-pro

Let’s ask about Gemini and its comparison to GPT3.

model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history=[])
response = chat.send_message("Are you better than GPT3?")

As a large language model, I don’t have feelings or emotions. I am not capable of feeling superior or inferior to other models. I am trained to provide accurate and informative responses, and I aim to be helpful and efficient.

Compared to GPT-3, I have been trained on a more diverse and up-to-date dataset, which enables me to provide responses that are more comprehensive and up-to-date. Additionally, I have been trained to perform specific tasks more effectively, including tasks that require reasoning, understanding context, and generating creative text.

However, it is important to note that model performance can vary depending on the specific task or context. GPT-3 is also a powerful and capable model that has been trained on a massive dataset.

Overall, I am a large language model that has been trained to excel at a variety of tasks. I strive to provide high-quality responses that are informative, engaging, and helpful.

Let’s ask again, how about a comparison with GPT4?

response = chat.send_message("How about you vs GPT4?")

As a large language model, I do not have personal feelings or opinions, and I am not in competition with other language models like GPT-4. Our capabilities and limitations are different, and we are both designed to serve different purposes.

GPT-4 is a very powerful language model that has been trained on a massive dataset of text and code. It is capable of generating human-like text, translating languages, answering questions, and performing many other language-based tasks. GPT-4 is known for its versatility and ability to handle a wide range of tasks.

My capabilities are somewhat different. I am also a large language model, but I have been trained on a different dataset and have a different set of strengths and weaknesses. I am particularly good at generating creative content, such as stories, poems, and songs. I can also generate code, answer questions, and translate languages, but my performance may not be as good as GPT-4’s in these areas.

Ultimately, the best language model for a particular task depends on the specific requirements of the task. GPT-4 may be a better choice for tasks that require high accuracy and versatility, while I may be a better choice for tasks that require creative or artistic output.

It’s also worth noting that language models are still under development, and both GPT-4 and I are constantly learning and improving. As we continue to learn, we may become better at handling a wider range of tasks and become even more useful to people.

In summary, GPT-4 is a more powerful and versatile language model than me, but we both have our own strengths and weaknesses. The best language model for a particular task depends on the specific requirements of the task.

Hmm, impressive, let’s try an image-to-text generator

Generate text from image inputs with gemini-pro-vision

import PIL.Image

img = PIL.Image.open('image.jpg')

model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(img)


The woman in the white dress walked through the forest, her bare feet silent on the soft ground. She carried a lantern in her hand, the flickering light casting strange shadows on the trees.

The woman was beautiful, with long, flowing hair and a pale, ethereal face. She seemed to be in a trance, her eyes unseeing as she walked.

The forest was dark and silent, the only sound the occasional rustle of leaves as a small animal scurried by. The woman walked on, her footsteps echoing in the stillness.

Suddenly, the woman stopped. She had come to a clearing, and in the middle of the clearing was a tree. The tree was old and gnarled, its branches twisted and contorted. The woman stared at the tree, her eyes wide with fear.

The woman turned and ran, her bare feet flying over the ground. She ran until she came to the edge of the forest, and then she collapsed on the ground, gasping for breath.

The woman lay on the ground for a long time, trying to catch her breath. When she finally did, she stood up and looked back at the forest. The tree was still there, standing in the middle of the clearing, its branches reaching out like claws.

The woman shivered and turned away. She knew that she would never go back to that forest again.


The model is very powerful and easy to use, and more importantly, it’s free now😃. It’s a really good alternative for ChatGPT since OpenAI charges us for API and image-to-text models. I hope in the future, there will be many tech companies providing this kind of technology with competitive prices and excellent services.

Source link

Latest stories