Spaces:
Running
A newer version of the Gradio SDK is available:
6.1.0
Google Gemini AI Calculator
Created by Vaibhav Manwatkar as a community contribution.
Overview
This simple Python application demonstrates how to integrate with Google's Gemini AI model using the google-generativeai library. The application asks Gemini to solve a basic mathematical problem (2+2) and displays the AI's response, showcasing the fundamental interaction with Google's Generative AI API.
Features
- Google Gemini Integration: Uses Google's latest Gemini 2.0 Flash Experimental model
- Environment Variable Management: Secure API key handling using
python-dotenv - Simple Mathematical Query: Demonstrates AI's ability to perform basic calculations
- Clean Output: Displays the AI's response in a readable format
Prerequisites
- Python 3.7 or higher
- Google API key with access to Gemini API
- Required Python packages (see Installation section)
Installation
Clone or download this file to your local machine
Install required dependencies:
pip install google-generativeai python-dotenvOr if using
uv:uv add google-generativeai python-dotenvSet up your Google API key:
- Get your API key from Google AI Studio
- Create a
.envfile in the same directory as the script - Add your API key to the
.envfile:GOOGLE_API_KEY=your_actual_api_key_here
Usage
Run the application:
python 1_lab1_google.pyExpected output:
4
Code Structure
from dotenv import load_dotenv
load_dotenv(override=True)
import os
import google.generativeai as genai
# Configure the API key
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
# Initialize the model
model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
# Generate content
response = model.generate_content(["What is 2+2?"])
print(response.text)
Key Components
1. Environment Setup
load_dotenv(override=True): Loads environment variables from.envfileos.getenv('GOOGLE_API_KEY'): Retrieves the API key securely
2. Model Configuration
genai.configure(): Sets up the API key for authenticationgenai.GenerativeModel(): Initializes the Gemini model with specified version
3. Content Generation
model.generate_content(): Sends a prompt to the AI modelresponse.text: Extracts the text response from the AI
Model Information
- Model Used:
gemini-2.0-flash-exp(Gemini 2.0 Flash Experimental) - Capabilities: Text generation, reasoning, mathematical calculations
- Input Format: List of strings or single string
- Output Format: Response object with
.textattribute
Error Handling
The application includes a pyright: ignore[reportMissingImports] comment to suppress type checker warnings for the google.generativeai import, which is a common practice when the package might not be installed in all environments.
Troubleshooting
Common Issues
ModuleNotFoundError: Install the required package:
pip install google-generativeaiAPI Key Error: Ensure your
.envfile contains a validGOOGLE_API_KEYAuthentication Error: Verify your API key has access to the Gemini API
Extending the Application
This basic example can be extended to:
- Ask more complex mathematical questions
- Implement conversation loops
- Add error handling for API failures
- Create a user interface for interactive queries
- Process different types of prompts beyond mathematics
Dependencies
google-generativeai: Google's official Python client for Generative AIpython-dotenv: Loads environment variables from.envfiles
License
This project is part of the community contributions for the Agents course and follows the same licensing terms.
Contributing
Feel free to fork this project and submit improvements or additional features as pull requests.
Author
Vaibhav Manwatkar
- GitHub: @learnwithvaibhavm
- This is a community contribution to the Agents course