Spaces:
Paused
Paused
Updated preview verion
Browse files- .gitignore +2 -1
- Tabs/Gemini_Chatbot_Preview.py +111 -0
- app.py +10 -2
.gitignore
CHANGED
@@ -1 +1,2 @@
|
|
1 |
-
.env
|
|
|
|
1 |
+
.env
|
2 |
+
env/
|
Tabs/Gemini_Chatbot_Preview.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
import gradio as gr
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
GEMINI_API_KEY_PREVIEW = os.getenv("GEMINI_API_KEY_PREVIEW")
|
9 |
+
model_name = "gemini-1.5-flash-exp-0827"
|
10 |
+
|
11 |
+
genai.configure(api_key=GEMINI_API_KEY_PREVIEW)
|
12 |
+
|
13 |
+
def clear_chat_history_preview():
|
14 |
+
chat.history = []
|
15 |
+
|
16 |
+
def undo_chat_preview():
|
17 |
+
last_send, last_received = chat.rewind()
|
18 |
+
|
19 |
+
def transform_history(history):
|
20 |
+
new_history = []
|
21 |
+
for user_msg, model_msg in history:
|
22 |
+
new_history.append({"role": "user", "parts": [user_msg]})
|
23 |
+
new_history.append({"role": "model", "parts": [model_msg]})
|
24 |
+
return new_history
|
25 |
+
|
26 |
+
def chatbot_preview(message, history, model_id, system_message, max_tokens, temperature, top_p,):
|
27 |
+
global model, chat
|
28 |
+
model = genai.GenerativeModel(
|
29 |
+
model_name=model_id,
|
30 |
+
system_instruction=system_message,
|
31 |
+
safety_settings=[
|
32 |
+
{
|
33 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
34 |
+
"threshold": "BLOCK_NONE"
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
38 |
+
"threshold": "BLOCK_NONE"
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
42 |
+
"threshold": "BLOCK_NONE"
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
46 |
+
"threshold": "BLOCK_NONE"
|
47 |
+
}
|
48 |
+
],
|
49 |
+
generation_config={
|
50 |
+
"temperature": temperature,
|
51 |
+
"top_p": top_p,
|
52 |
+
"top_k": 64,
|
53 |
+
"max_output_tokens": max_tokens,
|
54 |
+
"response_mime_type": "text/plain",
|
55 |
+
}
|
56 |
+
)
|
57 |
+
chat = model.start_chat(history=[])
|
58 |
+
message_text = message["text"]
|
59 |
+
message_files = message["files"]
|
60 |
+
if message_files:
|
61 |
+
image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]
|
62 |
+
message_content = [message_text] + image_uris
|
63 |
+
else:
|
64 |
+
message_content = [message_text]
|
65 |
+
|
66 |
+
response = chat.send_message(message_content, stream=True)
|
67 |
+
response.resolve()
|
68 |
+
|
69 |
+
return response.text
|
70 |
+
|
71 |
+
gemini_chatbot_interface_preview = gr.Chatbot(
|
72 |
+
height=500,
|
73 |
+
likeable=True,
|
74 |
+
avatar_images=(
|
75 |
+
None,
|
76 |
+
"https://media.roboflow.com/spaces/gemini-icon.png"
|
77 |
+
),
|
78 |
+
show_copy_button=True,
|
79 |
+
show_share_button=True,
|
80 |
+
render_markdown=True
|
81 |
+
)
|
82 |
+
|
83 |
+
clear_chat_button_preview = gr.ClearButton(
|
84 |
+
components=[gemini_chatbot_interface_preview],
|
85 |
+
value="🗑️ Clear"
|
86 |
+
)
|
87 |
+
|
88 |
+
undo_chat_button_preview = gr.Button(
|
89 |
+
value="↩️ Undo"
|
90 |
+
)
|
91 |
+
|
92 |
+
gemini_chatbot_preview = gr.ChatInterface(
|
93 |
+
fn=chatbot_preview,
|
94 |
+
chatbot=gemini_chatbot_interface_preview,
|
95 |
+
multimodal=True,
|
96 |
+
clear_btn=clear_chat_button_preview,
|
97 |
+
undo_btn=undo_chat_button_preview,
|
98 |
+
additional_inputs=[
|
99 |
+
gr.Dropdown(choices=["emini-1.5-pro", "gemini-1.5-pro-exp-0801", "gemini-1.5-pro-exp-0827", "gemini-1.5-flash", "gemini-1.5-flash-exp-0827", "gemini-1.5-flash-8b-exp-0827"], label="Models"),
|
100 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
101 |
+
gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),
|
102 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=1, step=0.1, label="Temperature"),
|
103 |
+
gr.Slider(
|
104 |
+
minimum=0.1,
|
105 |
+
maximum=1.0,
|
106 |
+
value=0.95,
|
107 |
+
step=0.05,
|
108 |
+
label="Top-p (nucleus sampling)",
|
109 |
+
),
|
110 |
+
],
|
111 |
+
)
|
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from Tabs.Gemini_Chabot_Stable import gemini_chatbot, clear_chat_button, undo_chat_button, clear_chat_history, undo_chat, TITLE, NOTICE, ERRORS, FUTURE_IMPLEMENTATIONS, ABOUT
|
|
|
3 |
|
4 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
5 |
# ============================== Stable - START ==============================
|
6 |
with gr.Tab("Chat with Gemini 1.5 Flash"):
|
7 |
gr.HTML(TITLE)
|
@@ -23,8 +25,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
23 |
gr.HTML("""<h1 align="center">Still in development</h1>""")
|
24 |
|
25 |
# ============================== Nightly - START ==============================
|
26 |
-
with gr.Tab("
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
# ============================== Nightly - END ==============================
|
29 |
|
30 |
demo.queue().launch(debug=True, show_error=True)
|
|
|
1 |
import gradio as gr
|
2 |
from Tabs.Gemini_Chabot_Stable import gemini_chatbot, clear_chat_button, undo_chat_button, clear_chat_history, undo_chat, TITLE, NOTICE, ERRORS, FUTURE_IMPLEMENTATIONS, ABOUT
|
3 |
+
from Tabs.Gemini_Chatbot_Preview import gemini_chatbot_preview, clear_chat_button_preview, undo_chat_button_preview, clear_chat_history_preview, undo_chat_preview
|
4 |
|
5 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
6 |
+
gr.HTML("""<h3 align="center">I strongly recommond duplicating this space for intensive uses!!!</h3>""")
|
7 |
# ============================== Stable - START ==============================
|
8 |
with gr.Tab("Chat with Gemini 1.5 Flash"):
|
9 |
gr.HTML(TITLE)
|
|
|
25 |
gr.HTML("""<h1 align="center">Still in development</h1>""")
|
26 |
|
27 |
# ============================== Nightly - START ==============================
|
28 |
+
with gr.Tab("Chat with Gemini 1.5 - Preview"):
|
29 |
+
gemini_chatbot_preview.render()
|
30 |
+
clear_chat_button_preview.click(
|
31 |
+
fn=clear_chat_history_preview
|
32 |
+
)
|
33 |
+
undo_chat_button_preview.click(
|
34 |
+
fn=undo_chat_preview
|
35 |
+
)
|
36 |
# ============================== Nightly - END ==============================
|
37 |
|
38 |
demo.queue().launch(debug=True, show_error=True)
|