File size: 1,518 Bytes
0577e62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
import os
import gradio as gr
import google.generativeai as genai


GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-pro')

header = r"""
<center>
    <h1>Gemini Chat</h1>
    <h2>
        This demo is for code reference, instead of use it, clone it and set your own Api key
    </h2>
    Get an Api Key: <a href='https://aistudio.google.com/app/apikey'>Google Ai Studio</a>
</center>
"""

footer = r"""
<center>
    <b>
        Demo for <a href='https://gemini.google.com/app'>Google Gemini</a>
    </b>
</center>
"""

with gr.Blocks(title="Gemini Chat") as app:

    gr.HTML(header)

    with gr.Row(equal_height=True):
        with gr.Column():
            chatbot = gr.Chatbot()
            msg = gr.Textbox()
            clear = gr.Button("Clear")

            def user(user_message, history):
                return "", history + [[user_message, None]]

            def bot(history):
                response = model.generate_content(history[-1][0], stream=True)
                history[-1][1] = ""
                for chunk in response:
                    history[-1][1] += chunk.text
                    yield history

            msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
                bot, chatbot, chatbot
            )
            clear.click(lambda: None, None, chatbot, queue=False)
    
    with gr.Row():
        gr.HTML(footer)
    
app.queue()
app.launch(share=True, debug=True, show_error=True)