Getting started using Gemini AI model by Google

 In this article I am going to demonstrate you how can we use Gemini AI model by Google step by step. I will demonstrate the example with text prompt first and then i will show you how you can use image in prompt and get the answers from the Gemini AI model.

 


So lets get started.

Prerequisites:

 Open visual studio code and open the folder where you want to create the python application. Now go to view menu of vs code and click on Terminal menu, it will open new Power-Shell terminal. Now we will install some python packages for that use below commands.

 pip install google-generativeai  

 

 pip install ipython  

Gemini Text Prompt Example: 

Now create file named "gemini_text.py" and add below code snippet to it.

 

 import os  
 import google.generativeai as genai  
 from IPython.display import Markdown  
   
 os.environ['GOOGLE_API_KEY'] = "your google api key obtained from aistudio.google.com"  
 genai.configure(api_key = os.environ['GOOGLE_API_KEY'])  
   
 model = genai.GenerativeModel('gemini-pro')  
 response = model.generate_content("List 5 planets each with an interesting fact")  
   
 print(Markdown(response.text).data)  

 Now run the following command to check the output.

 python gemini_text.py  

 you can view the output as per below image.

gemini example

Output is:

1. **Venus:**
   - Known as Earth's sister planet due to its similar size and mass.
   - Covered in a thick, carbon dioxide-rich atmosphere that traps heat, resulting in a scorching surface temperature of around 460°C (860°F).
   - Has a slow rotation, taking 243 Earth days to complete one rotation, and no natural satellite.

2. **Mars:**
   - Referred to as the Red Planet due to its reddish appearance caused by iron oxide on its surface.
   - Has a thin atmosphere consisting mostly of carbon dioxide and a surface marked by craters, canyons, and volcanoes.
   - Known for its two moons, Phobos and Deimos, and has polar ice caps composed of water ice and carbon dioxide ice.

3. **Jupiter:**
   - The largest planet in the solar system, known as a gas giant primarily composed of hydrogen and helium.
   - Has a distinctive banded appearance due to its rapid rotation, which creates swirling cloud patterns.
   - Possesses a strong magnetic field and is surrounded by a faint ring system and numerous moons, including the four Galilean moons: Io, Europa, Ganymede, and Callisto.


 

in above output you can see that Gemini has given the correct output for prompt entered.

  

Gemini Text and Image Prompt Example: 

 Now create the new file named "gemini_text_image.py" and add below code snippet to test image prompt 

 import PIL.Image  
 import os  
 import google.generativeai as genai  
 from IPython.display import Markdown  
   
 os.environ['GOOGLE_API_KEY'] = "your api key from https://makersuite.google.com"  
 genai.configure(api_key = os.environ['GOOGLE_API_KEY'])  
   
 image = PIL.Image.open('random_image.jpg')  
 vision_model = genai.GenerativeModel('gemini-pro-vision')  
 response = vision_model.generate_content(["describe the story from the Picture",image])  
   
   
 print(Markdown(response.text).data)  

I have used below image for testing the image prompt from Gemini AI Model.

image prompt sample

 I have kept above image file in same root directory of application and provide its path in application.

Now run the below command to test the output.

 python gemini_text_image.py  

 You will get below output on running the application.

image prompt output

Output is:

The picture shows a plate of chicken tikka masala. The chicken is cooked in a tandoor oven and is served with a mint sauce. The dish is garnished with cilantro and red onions. The story behind chicken tikka masala is that it was created in the 1950s by a Pakistani chef named Ali Ahmed Aslam. Aslam was working at a restaurant in Glasgow, Scotland, when he was asked to create a dish that would appeal to British tastes. He came up with a recipe that combined chicken tikka, a popular Indian dish, with a creamy tomato sauce. The dish was a hit with customers and soon became a popular dish in Britain.

 

In above figure you can see that the Gemini model given the correct answer to the image prompt.

This way you can use Gemini AI model in your application with few code blocks, hope you will get some heads-up about Gemini AI Model implementation.
 

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Devexpress Datebox date formatting in angular 6 with example

Restore the lost focus of Auto post back controls in asp.net update Panel control