File size: 1,283 Bytes
ca03a71
 
 
 
b4741a8
544fb6a
 
b4741a8
28b9d60
 
 
b4741a8
2f32d8e
b4741a8
ca03a71
 
 
 
 
28b9d60
ca03a71
 
 
 
 
 
 
 
 
 
 
 
 
 
b4741a8
 
ca03a71
 
 
 
b4741a8
ca03a71
544fb6a
 
ca03a71
b4741a8
 
 
ca03a71
 
9ae3367
ca03a71
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import os
from google import genai
from google.genai import types
import gradio as gr
from IPython.display import display
from IPython.display import Markdown

client = genai.Client(
    api_key=os.environ.get("GEMINI_API_KEY"),
)

def generate(prompt, *args):

    model = "gemini-2.0-flash"
    contents = [
        types.Content(
            role="user",
            parts=[
                types.Part.from_text(text=f"{prompt}"),
            ],
        ),
    ]
    tools = [
        types.Tool(google_search=types.GoogleSearch())
    ]
    generate_content_config = types.GenerateContentConfig(
        temperature=0.35,
        top_p=0.95,
        top_k=40,
        max_output_tokens=8192,
        tools=tools,
        response_mime_type="text/plain",
    )

    response = ""
    for chunk in client.models.generate_content_stream(
        model=model,
        contents=contents,
        config=generate_content_config,
    ):
        response += chunk.text
        
    display(Markdown(response))
    return response


if __name__ == "__main__":
    iface = gr.ChatInterface(
        fn=generate,
        title=gr.Markdown("# Chatbot with Google Search"),
        description="A chatbot powered by Gemini 2.0 Flash and Google Search.",
    )
    iface.launch()