Playing with Google Gemini Pro

Abhishek Selokar
4 min readFeb 15, 2024

--

Imagine a tool that can craft captivating stories, translate languages, summarize images, write poems for you, and answer your questions with clarity, and if you are a developer, it becomes your one-stop solution for code generation and code completion. Are you ready to embark on this exciting journey to play with such a tool? Let’s get our hands dirty by playing on the vast beach of those awesome models and making a castle out of our imagination!!!

Source

What is Gemini? Well let’s hear it out from Demis Hassabis, CEO and Co-Founder, Google DeepMind

……the most capable and general model we’ve ever built. Gemini is the result of large-scale collaborative efforts by teams across Google, including our colleagues at Google Research. It was built from the ground up to be multimodal, which means it can generalize and seamlessly understand, operate across and combine different types of information including text, code, audio, image and video. Source

Currently, it provides two models:

  1. Gemini-pro : Generating text from text inputs
  2. Gemini-pro-vision: Generating text from image inputs

Without wasting time on unnecessary talks, let's look step-by-step at how to use both of these models on our favourite platform,Google Colab.

Step 1: First, obtain your Gemini API key over here. Copy it and save it for future use.

Step 2: Open Google Colab and add the key to the secrets manager under the “🔑” in the left panel, and give it the name GOOGLE_API_KEYand put the saved API key in the Value.

Step 3: Install the required library using the following command:

pip install -q -U google-generativeai

Import the necessary packages, such as

import google.generativeai as genai
from google.colab import userdata

Set up an API key

GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)

Generating text from text inputs: Gemini-pro

def get_response(prompt):
model=genai.GenerativeModel('gemini-1.5-pro')
response=model.generate_content(prompt)
return response.text
# Example 1
Input :
print(get_response("What are major plant diseases?"))

Output:
**Fungal Diseases**

* Anthracnose
* Botrytis blight
* Downy mildew
* Fusarium wilt
* Powdery mildew
* Rust
* Scab

**Bacterial Diseases**

* Bacterial blight
* Canker
* Fire blight
* Leaf spot
* Soft rot
* Wilt

**Viral Diseases**

* Cucumber mosaic virus
* Tobacco mosaic virus
* Tomato spotted wilt virus

**Phytoplasma Diseases**

* Aster yellows
* Pierce's disease
* Stolbur

**Nematode Diseases**

* Root-knot nematodes
* Cyst nematodes
* Lesion nematodes

**Abiotic Diseases**

* Drought stress
* Nutrient deficiencies
* Heat stress
* Cold injury
* Pesticide damage
# Example 2:

Input :
print(get_response("Write a python code to reverse a string?"))

Output:
```python
def reverse_string(string):
"""Reverses the order of characters in a string.

Args:
string: The string to reverse.

Returns:
The reversed string.
"""

# Check if the string is empty or None.
if not string:
return string

# Create a variable to store the reversed string.
reversed_string = ""

# Iterate over the string in reverse order.
for character in reversed(string):
# Append the character to the reversed string.
reversed_string += character

# Return the reversed string.
return reversed_string
```
# Example 3
Input :
print(get_response("Translate the text to Hindi: \
India is great land with diverse culture"))

Output:
भारत विविध संस्कृतियों वाला एक महान देश है

Generating text from image inputs: Gemini-pro-vision

def get_img_response(prompt,image):
model=genai.GenerativeModel('gemini-1.5-pro')
response=model.generate_content([prompt,image])
return response.text
import PIL.Image

img = PIL.Image.open('/content/Screenshot.png')
img
Image by author
Input:
print(get_img_response("what are the ingredients present in the given image",img))

Output:
The ingredients present in the given image are:
1. Orange juice
2. Vodka
3. Lemonade
4. Whiskey

Example 2:

# Example 2
img2 = PIL.Image.open('/content/plants-09-01319-g003.jpg')
img2
Source
Input:
print(get_img_response("How many plant disease are shown in the given\
image and name each of them",img2))

Output:
There are 12 plant diseases shown in the given image. They are:

1. Apple scab
2. Cherry powdery mildew
3. Corn northern leaf blight
4. Grape black rot
5. Grape leaf blight
6. Orange Haunglongbing (Citrus greening)
7. Peach bacterial spot
8. Potato early blight
9. Squash powdery mildew
10. Strawberry leaf scorch
11. Tomato early blight
12. Tomato late blight

Example 3:


img3 = PIL.Image.open('/content/Screenshot.png')
img3
Source
Input:
print(get_img_response("Describe the image",img3))

Output:
This is a photograph of multiple hot air balloons in the sky.\
The sky is clear and blue, with no clouds in sight. \
The balloons are all different colors, including red, yellow, blue, green,\
and purple. Some of the balloons are also striped or have patterns on them. \
The balloons are all flying at different heights, with some of them\
being higher than others.

Thanks for your patience. Let’s meet at another interesting blog and get to know more about LLMs and their power.

Reference:

Google Quickstart

--

--

Abhishek Selokar

Masters Student @ Indian Institute Of Technology, Kharagpur || Thirsty to learn more about AI