AK391 / gemini-gradio

8 stars 1 forks source link

Add the grounding tools (google_search_retrieval) for realtime search #1

Open SOSONAGI opened 3 days ago

SOSONAGI commented 3 days ago

Gemini Pro Chat Interface with Gradio

Simple implementation of Gemini Pro with Gradio chat interface, including real-time search capability.

Code

import gradio as gr
import google.generativeai as genai

GEMINI_API_KEY = "YOUR_API_KEY"  
genai.configure(api_key=GEMINI_API_KEY)

model = genai.GenerativeModel('models/gemini-1.5-pro-002')

def process_query(message, history):
    try:
        response = model.generate_content(
            contents=message,
            tools='google_search_retrieval'
        )
        return response.text
    except Exception as e:
        return f"Error occurred: {str(e)}"

interface = gr.ChatInterface(
    fn=process_query,
    title='Gemini-Gradio Integration with Search',
    description="Chat with Gemini Pro model (includes real-time search capability).",
    examples=[
        "Explain quantum gravity to a 5-year old.",
        "How many R are there in the word Strawberry?",
        "Who won Wimbledon this year?"
    ],
    theme=gr.themes.Soft()
)

if __name__ == "__main__":
    interface.launch(share=True)  

Setup

  1. Install required packages:

    pip install gradio google-generativeai
  2. Replace YOUR_API_KEY with your actual Gemini API key

  3. Run the script:

    python your_script.py

Hope this will help for others :)

AK391 commented 3 days ago

thanks for sharing also adding support for search with #2