Spaces:
Sleeping
Sleeping
init space
Browse files- app.py +58 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
|
6 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
7 |
+
|
8 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
9 |
+
model = genai.GenerativeModel('gemini-pro')
|
10 |
+
|
11 |
+
header = r"""
|
12 |
+
<center>
|
13 |
+
<h1>Gemini Chat</h1>
|
14 |
+
<h2>
|
15 |
+
This demo is for code reference, instead of use it, clone it and set your own Api key
|
16 |
+
</h2>
|
17 |
+
Get an Api Key: <a href='https://aistudio.google.com/app/apikey'>Google Ai Studio</a>
|
18 |
+
</center>
|
19 |
+
"""
|
20 |
+
|
21 |
+
footer = r"""
|
22 |
+
<center>
|
23 |
+
<b>
|
24 |
+
Demo for <a href='https://gemini.google.com/app'>Google Gemini</a>
|
25 |
+
</b>
|
26 |
+
</center>
|
27 |
+
"""
|
28 |
+
|
29 |
+
with gr.Blocks(title="Gemini Chat") as app:
|
30 |
+
|
31 |
+
gr.HTML(header)
|
32 |
+
|
33 |
+
with gr.Row(equal_height=True):
|
34 |
+
with gr.Column():
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
msg = gr.Textbox()
|
37 |
+
clear = gr.Button("Clear")
|
38 |
+
|
39 |
+
def user(user_message, history):
|
40 |
+
return "", history + [[user_message, None]]
|
41 |
+
|
42 |
+
def bot(history):
|
43 |
+
response = model.generate_content(history[-1][0], stream=True)
|
44 |
+
history[-1][1] = ""
|
45 |
+
for chunk in response:
|
46 |
+
history[-1][1] += chunk.text
|
47 |
+
yield history
|
48 |
+
|
49 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
50 |
+
bot, chatbot, chatbot
|
51 |
+
)
|
52 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
gr.HTML(footer)
|
56 |
+
|
57 |
+
app.queue()
|
58 |
+
app.launch(share=True, debug=True, show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
google-generativeai
|
2 |
+
|