Google Gemini API : Guide to Generating Text, Audio, and Image Responses by Python

Google Gemini API : Guide to Generating Text, Audio, and Image Responses by Python
2024-10-22•Basic•5 min

If you’re like most developers working in AI, you know how much hassle it can be to juggle multiple tools for generating text, processing audio, or analyzing images. Google’s Gemini API changes all of that, putting everything you need into one powerful interface. Whether you’re building a chatbot, generating content from images, or transcribing audio, the Gemini API has you covered.

In this blog, I’ll walk you through the process of connecting to the Gemini API using Python. Don’t worry—it’s way easier than you might think. By the end, you’ll know how to generate text, analyze audio, interpret images, and even build an interactive chatbot. Let's get started!

Setting Up Your Workspace

Before we dive into code, there are a few quick things you need to set up:

  • Install the necessary Python libraries.
  • pip install google-generativeai pillow
  • Get Your Google Gemini API Key.
  • Head over to Google AI Studio to grab your API key. This key allows your Python code to talk to the Google Gemini API.

    Google AI Studio

    Once you’ve got your API key, create a file named api_key.py to store it:

    # api_key.py
    key = "Your_API_Key_Here"
    

    This file keeps your API key safe and easy to reference in your projects

    Generating Text with the Gemini API

    Let’s start simple: generating text. Whether you need answers to specific questions or want to create written content on the fly, Gemini’s text generation feature is your go-to.

    Here’s a quick script to generate text using a prompt:

    # prompt_to_text.py
    import google.generativeai as genai
    from api_key import key
    
    # Set the Google API key
    genai.configure(api_key=key)
    
    # Initialize the Gemini model
    model = genai.GenerativeModel("gemini-1.5-flash")
    
    # Ask a question
    response = model.generate_content("Who were the first two people to walk on the moon?")
    print(response.text)

    What’s happening here?

    We’re simply asking the Gemini API a question, and it responds with a text-based answer. In this case, we ask about the first two astronauts to step on the moon. It’s that easy!

    Turning Audio into Text

    What if you have an audio file and want to know what’s being said or even who’s speaking? The Gemini API has your back here, too.

    Check out this example where we use an audio file and a text prompt to generate a response:

    # audio_to_text.py
    import google.generativeai as genai
    from api_key import key
    
    # Set the Google API key
    genai.configure(api_key=key)
    
    # Initialize the Gemini model
    model = genai.GenerativeModel("gemini-1.5-flash")
    
    # Load an audio file (Replace 'sample.mp3' with your own file)
    with open('sample.mp3', 'rb') as audio_file:
        audio_data = audio_file.read()
    
    # Pass audio and a prompt to the Gemini API
    response = model.generate_content([
        "Who is speaking in this audio clip?",
        {
            "mime_type": "audio/mp3",
            "data": audio_data
        }
    ])
    
    # Print the API’s response
    print(response.text)

    Why is this cool?

    With just a couple of lines of code, you can take any audio file, send it to the Gemini API, and get back a full textual interpretation. It’s incredibly useful for podcasts, interviews, or even real-time voice assistants.

    Understanding Images with Gemini

    Gemini isn’t just about words—it’s also great with images. If you’ve got a picture and want to generate text about it, the API can interpret and describe the image for you.

    Here’s how you can do it:

    # image_to_prompt.py
    import PIL.Image
    import google.generativeai as genai
    from api_key import key
    
    # Set the Google API key
    genai.configure(api_key=key)
    
    # Load your image
    image = PIL.Image.open("images/heart.jpg")
    
    # Pass both text and image to the model
    response = model.generate_content(["Tell me about this image", image])
    
    # Get the response
    print(response.text)

    Why use this?

    This feature is perfect if you’re working on something like an image recognition app or need to generate captions for images dynamically.

    Streaming Text Responses

    Sometimes you need more than just a single block of text. The Gemini API can also stream large text responses in chunks, which is perfect for handling bigger requests.

    Here’s how you can stream content:

    # text_stream.py
    import google.generativeai as genai
    from api_key import key
    
    # Set the Google API key
    genai.configure(api_key=key)
    
    # Stream the response in chunks
    response = model.generate_content("Tell me about space.", stream=True)
    
    # Print each chunk of the response as it streams in
    for chunk in response:
        print(chunk.text)

    Why streaming matters:

    If you’re dealing with large amounts of text, streaming allows you to process and display the content incrementally rather than waiting for the entire response.

    Creating an Interactive Chatbot

    Want to take things up a notch? Let’s build a chatbot using the Gemini API. The following code sets up a chatbot that can engage in a conversation, responding to the user’s input in real time.

    # gemini_chatting.py
    import google.generativeai as genai
    from api_key import key
    
    # Set the Google API key
    genai.configure(api_key=key)
    
    # Start a chat session with some history
    chat = model.start_chat(
        history=[
            {"role": "user", "parts": "Hi!"},
            {"role": "model", "parts": "Hello! How can I assist you today?"}
        ]
    )
    
    # Chat loop
    print("\nWelcome to the Gemini Chat! Type 'exit' to end the chat.\n")
    
    while True:
        user_input = input("You: ")
    
        if user_input.lower() == 'exit':
            print("Goodbye!")
            break
    
        # Stream the chat response
        response_stream = chat.send_message(user_input, stream=True)
    
        print("Gemini:", end=" ")
    
        # Print the streamed response
        for chunk in response_stream:
            print(chunk.text, end="", flush=True)
    
        print("\n" + "_" * 80)

    What’s happening here?

    We’re setting up a chat where the user can input text and receive responses in real time. It’s a great way to create an interactive, AI-powered chatbot.

    With Google Gemini’s versatile API, you now have the power to generate responses from text, audio, and images. You’ve seen how easy it is to ask questions, transcribe audio, analyze images, stream text, and even create a chatbot. The best part? All these features work seamlessly through a single API.

    So go ahead, integrate Google Gemini into your projects, and watch your applications come to life with AI-powered intelligence.

    Happy coding!