Spaces:
Paused
Paused
Updated app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
References:
|
| 3 |
- https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
|
|
@@ -5,6 +279,7 @@ References:
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import time
|
|
|
|
| 8 |
from typing import List, Tuple, Optional
|
| 9 |
import google.generativeai as genai
|
| 10 |
import gradio as gr
|
|
@@ -39,16 +314,27 @@ To be implemented 🚀:
|
|
| 39 |
"""
|
| 40 |
IMAGE_WIDTH = 512
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 47 |
return image.resize((IMAGE_WIDTH, image_height))
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return "", chatbot + [[text_prompt, None]]
|
| 51 |
|
|
|
|
| 52 |
def bot(
|
| 53 |
google_key: str,
|
| 54 |
image_prompt: Optional[Image.Image],
|
|
@@ -73,11 +359,17 @@ def bot(
|
|
| 73 |
top_p=top_p,
|
| 74 |
)
|
| 75 |
|
| 76 |
-
model_name = "gemini-1.5-flash"
|
| 77 |
model = genai.GenerativeModel(model_name)
|
| 78 |
-
inputs = [text_prompt] if image_prompt is None else [
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
response.resolve()
|
| 82 |
|
| 83 |
chatbot[-1][1] = ""
|
|
@@ -87,74 +379,59 @@ def bot(
|
|
| 87 |
time.sleep(0.01)
|
| 88 |
yield chatbot
|
| 89 |
|
|
|
|
| 90 |
google_key_component = gr.Textbox(
|
| 91 |
-
label
|
| 92 |
-
type
|
| 93 |
-
placeholder
|
| 94 |
-
visible
|
| 95 |
)
|
| 96 |
|
| 97 |
-
image_prompt_component = gr.Image(
|
| 98 |
-
|
| 99 |
-
label = "Image"
|
| 100 |
-
)
|
| 101 |
-
chatbot_component = gr.Chatbot(
|
| 102 |
-
# label = 'Gemini',
|
| 103 |
-
bubble_full_width = False
|
| 104 |
-
)
|
| 105 |
text_prompt_component = gr.Textbox(
|
| 106 |
-
placeholder
|
| 107 |
-
label
|
| 108 |
-
)
|
| 109 |
-
run_button_component = gr.Button(
|
| 110 |
-
"Run"
|
| 111 |
)
|
|
|
|
| 112 |
temperature_component = gr.Slider(
|
| 113 |
-
minimum
|
| 114 |
-
maximum
|
| 115 |
-
value
|
| 116 |
-
step
|
| 117 |
-
label
|
| 118 |
)
|
| 119 |
max_output_tokens_component = gr.Slider(
|
| 120 |
-
minimum
|
| 121 |
-
maximum
|
| 122 |
-
value
|
| 123 |
-
step
|
| 124 |
-
label
|
| 125 |
)
|
| 126 |
stop_sequences_component = gr.Textbox(
|
| 127 |
-
label
|
| 128 |
-
placeholder
|
| 129 |
)
|
| 130 |
top_k_component = gr.Slider(
|
| 131 |
-
minimum
|
| 132 |
-
maximum
|
| 133 |
-
value
|
| 134 |
-
step
|
| 135 |
-
label
|
| 136 |
)
|
| 137 |
top_p_component = gr.Slider(
|
| 138 |
-
minimum
|
| 139 |
-
maximum
|
| 140 |
-
value
|
| 141 |
-
step
|
| 142 |
-
label
|
| 143 |
)
|
| 144 |
|
| 145 |
-
user_inputs = [
|
| 146 |
-
text_prompt_component,
|
| 147 |
-
chatbot_component
|
| 148 |
-
]
|
| 149 |
bot_inputs = [
|
| 150 |
-
google_key_component,
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
max_output_tokens_component,
|
| 154 |
-
stop_sequences_component,
|
| 155 |
-
top_k_component,
|
| 156 |
-
top_p_component,
|
| 157 |
-
chatbot_component
|
| 158 |
]
|
| 159 |
# ============================== Stable - END ==============================
|
| 160 |
|
|
@@ -166,31 +443,74 @@ References:
|
|
| 166 |
GEMINI_API_KEY_NIGHTLY = os.getenv("GEMINI_API_KEY_NIGHTLY")
|
| 167 |
model_nightly_name = "gemini-1.5-flash"
|
| 168 |
|
|
|
|
| 169 |
def transform_history(history):
|
| 170 |
new_history = []
|
| 171 |
-
for
|
| 172 |
-
new_history.append({"
|
| 173 |
-
new_history.append({"
|
| 174 |
return new_history
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
def response(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
genai.configure(api_key=GEMINI_API_KEY_NIGHTLY)
|
| 178 |
-
model_nightly = genai.GenerativeModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
global chat
|
| 181 |
chat = model_nightly.start_chat(history=[])
|
| 182 |
chat.history = transform_history(history)
|
| 183 |
-
response = chat.send_message(
|
| 184 |
response.resolve()
|
| 185 |
|
| 186 |
for i in range(len(response.text)):
|
| 187 |
time.sleep(0.05)
|
| 188 |
-
yield response.text[:i+1]
|
|
|
|
| 189 |
|
| 190 |
# ============================== Nightly - END ==============================
|
| 191 |
|
| 192 |
-
with gr.Blocks(theme
|
| 193 |
-
# ============================== Stable - START ==============================
|
| 194 |
with gr.Tab("Chat with Gemini 1.5 Flash"):
|
| 195 |
gr.HTML(TITLE)
|
| 196 |
with gr.Row():
|
|
@@ -214,60 +534,43 @@ with gr.Blocks(theme = gr.themes.Soft()) as demo:
|
|
| 214 |
top_p_component.render()
|
| 215 |
|
| 216 |
run_button_component.click(
|
| 217 |
-
fn
|
| 218 |
-
inputs
|
| 219 |
-
outputs
|
| 220 |
-
|
| 221 |
-
chatbot_component
|
| 222 |
-
],
|
| 223 |
-
queue = False
|
| 224 |
).then(
|
| 225 |
-
fn
|
| 226 |
-
inputs
|
| 227 |
-
outputs
|
| 228 |
-
chatbot_component
|
| 229 |
-
]
|
| 230 |
)
|
| 231 |
text_prompt_component.submit(
|
| 232 |
-
fn
|
| 233 |
-
inputs
|
| 234 |
-
outputs
|
| 235 |
-
|
| 236 |
-
chatbot_component
|
| 237 |
-
],
|
| 238 |
-
queue = False
|
| 239 |
).then(
|
| 240 |
-
fn
|
| 241 |
-
inputs
|
| 242 |
-
outputs
|
| 243 |
-
chatbot_component
|
| 244 |
-
]
|
| 245 |
)
|
| 246 |
-
# ============================== Stable - END ==============================
|
| 247 |
-
|
| 248 |
with gr.Tab("Chat with Gemma 2"):
|
| 249 |
-
gr.HTML(
|
| 250 |
-
"""
|
| 251 |
-
<h1 align="center">Still in development</h1>
|
| 252 |
-
"""
|
| 253 |
-
)
|
| 254 |
|
| 255 |
-
# ============================== Nightly - START ==============================
|
| 256 |
with gr.Tab("Nightly -- Chat with Gemini 1.5"):
|
| 257 |
-
gr.HTML(
|
| 258 |
-
"""
|
| 259 |
-
<h1 align="center">This section will test out the next version of the stable version.</h1>
|
| 260 |
-
"""
|
| 261 |
-
)
|
| 262 |
gr.ChatInterface(
|
| 263 |
response,
|
| 264 |
-
chatbot
|
| 265 |
-
title
|
| 266 |
-
retry_btn
|
| 267 |
-
undo_btn
|
| 268 |
-
clear_btn
|
| 269 |
-
fill_height
|
|
|
|
| 270 |
)
|
| 271 |
# ============================== Nightly - END ==============================
|
| 272 |
|
| 273 |
-
demo.queue().launch(debug
|
|
|
|
| 1 |
+
# """
|
| 2 |
+
# References:
|
| 3 |
+
# - https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
|
| 4 |
+
# """
|
| 5 |
+
|
| 6 |
+
# import os
|
| 7 |
+
# import time
|
| 8 |
+
# from typing import List, Tuple, Optional
|
| 9 |
+
# import google.generativeai as genai
|
| 10 |
+
# import gradio as gr
|
| 11 |
+
# from PIL import Image
|
| 12 |
+
# from dotenv import load_dotenv
|
| 13 |
+
|
| 14 |
+
# load_dotenv()
|
| 15 |
+
|
| 16 |
+
# GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 17 |
+
|
| 18 |
+
# # ============================== Stable - START ==============================
|
| 19 |
+
# TITLE = """<h1 align="center">🎮Chat with Gemini 1.5🔥 -- Beta Preview</h1>"""
|
| 20 |
+
# NOTICE = """
|
| 21 |
+
# Notices 📜:
|
| 22 |
+
# - This app is still in development
|
| 23 |
+
# - Some features may not work as expected
|
| 24 |
+
# """
|
| 25 |
+
# ABOUT = """
|
| 26 |
+
# Updates (2024-8-12): Created the App
|
| 27 |
+
|
| 28 |
+
# Info:
|
| 29 |
+
# - Model: Gemini 1.5 Flash
|
| 30 |
+
# """
|
| 31 |
+
# ERRORS = """
|
| 32 |
+
# Known errors ⚠️:
|
| 33 |
+
# """
|
| 34 |
+
# FUTURE_IMPLEMENTATIONS = """
|
| 35 |
+
# To be implemented 🚀:
|
| 36 |
+
# - Select other Gemini / Gemma models
|
| 37 |
+
# - Upload files
|
| 38 |
+
# - More tools other than web search
|
| 39 |
+
# """
|
| 40 |
+
# IMAGE_WIDTH = 512
|
| 41 |
+
|
| 42 |
+
# def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
| 43 |
+
# return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 44 |
+
|
| 45 |
+
# def preprocess_image(image: Image.Image) -> Image.Image:
|
| 46 |
+
# image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 47 |
+
# return image.resize((IMAGE_WIDTH, image_height))
|
| 48 |
+
|
| 49 |
+
# def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
| 50 |
+
# return "", chatbot + [[text_prompt, None]]
|
| 51 |
+
|
| 52 |
+
# def bot(
|
| 53 |
+
# google_key: str,
|
| 54 |
+
# image_prompt: Optional[Image.Image],
|
| 55 |
+
# temperature: float,
|
| 56 |
+
# max_output_tokens: int,
|
| 57 |
+
# stop_sequences: str,
|
| 58 |
+
# top_k: int,
|
| 59 |
+
# top_p: float,
|
| 60 |
+
# chatbot: List[Tuple[str, str]]
|
| 61 |
+
# ):
|
| 62 |
+
# google_key = google_key or GEMINI_API_KEY
|
| 63 |
+
# if not google_key:
|
| 64 |
+
# raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 65 |
+
|
| 66 |
+
# text_prompt = chatbot[-1][0]
|
| 67 |
+
# genai.configure(api_key=google_key)
|
| 68 |
+
# generation_config = genai.types.GenerationConfig(
|
| 69 |
+
# temperature=temperature,
|
| 70 |
+
# max_output_tokens=max_output_tokens,
|
| 71 |
+
# stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 72 |
+
# top_k=top_k,
|
| 73 |
+
# top_p=top_p,
|
| 74 |
+
# )
|
| 75 |
+
|
| 76 |
+
# model_name = "gemini-1.5-flash" # if image_prompt is None else "gemini-pro-vision"
|
| 77 |
+
# model = genai.GenerativeModel(model_name)
|
| 78 |
+
# inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
| 79 |
+
|
| 80 |
+
# response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 81 |
+
# response.resolve()
|
| 82 |
+
|
| 83 |
+
# chatbot[-1][1] = ""
|
| 84 |
+
# for chunk in response:
|
| 85 |
+
# for i in range(0, len(chunk.text), 10):
|
| 86 |
+
# chatbot[-1][1] += chunk.text[i:i + 10]
|
| 87 |
+
# time.sleep(0.01)
|
| 88 |
+
# yield chatbot
|
| 89 |
+
|
| 90 |
+
# google_key_component = gr.Textbox(
|
| 91 |
+
# label = "GOOGLE API KEY",
|
| 92 |
+
# type = "password",
|
| 93 |
+
# placeholder = "...",
|
| 94 |
+
# visible = GEMINI_API_KEY is None
|
| 95 |
+
# )
|
| 96 |
+
|
| 97 |
+
# image_prompt_component = gr.Image(
|
| 98 |
+
# type = "pil",
|
| 99 |
+
# label = "Image"
|
| 100 |
+
# )
|
| 101 |
+
# chatbot_component = gr.Chatbot(
|
| 102 |
+
# # label = 'Gemini',
|
| 103 |
+
# bubble_full_width = False
|
| 104 |
+
# )
|
| 105 |
+
# text_prompt_component = gr.Textbox(
|
| 106 |
+
# placeholder = "Chat with Gemini",
|
| 107 |
+
# label = "Ask me anything and press Enter"
|
| 108 |
+
# )
|
| 109 |
+
# run_button_component = gr.Button(
|
| 110 |
+
# "Run"
|
| 111 |
+
# )
|
| 112 |
+
# temperature_component = gr.Slider(
|
| 113 |
+
# minimum = 0,
|
| 114 |
+
# maximum = 1.0,
|
| 115 |
+
# value = 0.5,
|
| 116 |
+
# step = 0.05,
|
| 117 |
+
# label = "Temperature"
|
| 118 |
+
# )
|
| 119 |
+
# max_output_tokens_component = gr.Slider(
|
| 120 |
+
# minimum = 1,
|
| 121 |
+
# maximum = 8192,
|
| 122 |
+
# value = 4096,
|
| 123 |
+
# step = 1,
|
| 124 |
+
# label = "Max Output Tokens"
|
| 125 |
+
# )
|
| 126 |
+
# stop_sequences_component = gr.Textbox(
|
| 127 |
+
# label = "Add stop sequence",
|
| 128 |
+
# placeholder = "STOP, END"
|
| 129 |
+
# )
|
| 130 |
+
# top_k_component = gr.Slider(
|
| 131 |
+
# minimum = 1,
|
| 132 |
+
# maximum = 40,
|
| 133 |
+
# value = 32,
|
| 134 |
+
# step = 1,
|
| 135 |
+
# label = "Top-K"
|
| 136 |
+
# )
|
| 137 |
+
# top_p_component = gr.Slider(
|
| 138 |
+
# minimum = 0,
|
| 139 |
+
# maximum = 1,
|
| 140 |
+
# value = 1,
|
| 141 |
+
# step = 0.01,
|
| 142 |
+
# label = "Top-P"
|
| 143 |
+
# )
|
| 144 |
+
|
| 145 |
+
# user_inputs = [
|
| 146 |
+
# text_prompt_component,
|
| 147 |
+
# chatbot_component
|
| 148 |
+
# ]
|
| 149 |
+
# bot_inputs = [
|
| 150 |
+
# google_key_component,
|
| 151 |
+
# image_prompt_component,
|
| 152 |
+
# temperature_component,
|
| 153 |
+
# max_output_tokens_component,
|
| 154 |
+
# stop_sequences_component,
|
| 155 |
+
# top_k_component,
|
| 156 |
+
# top_p_component,
|
| 157 |
+
# chatbot_component
|
| 158 |
+
# ]
|
| 159 |
+
# # ============================== Stable - END ==============================
|
| 160 |
+
|
| 161 |
+
# # ============================== Nightly - START ==============================
|
| 162 |
+
# """
|
| 163 |
+
# References:
|
| 164 |
+
# - https://medium.com/latinxinai/simple-chatbot-gradio-google-gemini-api-4ce02fbaf09f
|
| 165 |
+
# """
|
| 166 |
+
# GEMINI_API_KEY_NIGHTLY = os.getenv("GEMINI_API_KEY_NIGHTLY")
|
| 167 |
+
# model_nightly_name = "gemini-1.5-flash"
|
| 168 |
+
|
| 169 |
+
# def transform_history(history):
|
| 170 |
+
# new_history = []
|
| 171 |
+
# for chat in history:
|
| 172 |
+
# new_history.append({"parts": [{"text": chat[0]}], "role": "user"})
|
| 173 |
+
# new_history.append({"parts": [{"text": chat[1]}], "role": "model"})
|
| 174 |
+
# return new_history
|
| 175 |
+
|
| 176 |
+
# def response(message, history):
|
| 177 |
+
# genai.configure(api_key=GEMINI_API_KEY_NIGHTLY)
|
| 178 |
+
# model_nightly = genai.GenerativeModel(model_nightly_name)
|
| 179 |
+
|
| 180 |
+
# global chat
|
| 181 |
+
# chat = model_nightly.start_chat(history=[])
|
| 182 |
+
# chat.history = transform_history(history)
|
| 183 |
+
# response = chat.send_message(message)
|
| 184 |
+
# response.resolve()
|
| 185 |
+
|
| 186 |
+
# for i in range(len(response.text)):
|
| 187 |
+
# time.sleep(0.05)
|
| 188 |
+
# yield response.text[:i+1]
|
| 189 |
+
|
| 190 |
+
# # ============================== Nightly - END ==============================
|
| 191 |
+
|
| 192 |
+
# with gr.Blocks(theme = gr.themes.Soft()) as demo:
|
| 193 |
+
# # ============================== Stable - START ==============================
|
| 194 |
+
# with gr.Tab("Chat with Gemini 1.5 Flash"):
|
| 195 |
+
# gr.HTML(TITLE)
|
| 196 |
+
# with gr.Row():
|
| 197 |
+
# gr.Markdown(NOTICE)
|
| 198 |
+
# gr.Markdown(ABOUT)
|
| 199 |
+
# gr.Markdown(ERRORS)
|
| 200 |
+
# gr.Markdown(FUTURE_IMPLEMENTATIONS)
|
| 201 |
+
# with gr.Column():
|
| 202 |
+
# google_key_component.render()
|
| 203 |
+
# with gr.Row():
|
| 204 |
+
# image_prompt_component.render()
|
| 205 |
+
# chatbot_component.render()
|
| 206 |
+
# text_prompt_component.render()
|
| 207 |
+
# run_button_component.render()
|
| 208 |
+
# with gr.Accordion("Parameters", open=False):
|
| 209 |
+
# temperature_component.render()
|
| 210 |
+
# max_output_tokens_component.render()
|
| 211 |
+
# stop_sequences_component.render()
|
| 212 |
+
# with gr.Accordion("Advanced", open=False):
|
| 213 |
+
# top_k_component.render()
|
| 214 |
+
# top_p_component.render()
|
| 215 |
+
|
| 216 |
+
# run_button_component.click(
|
| 217 |
+
# fn = user,
|
| 218 |
+
# inputs = user_inputs,
|
| 219 |
+
# outputs = [
|
| 220 |
+
# text_prompt_component,
|
| 221 |
+
# chatbot_component
|
| 222 |
+
# ],
|
| 223 |
+
# queue = False
|
| 224 |
+
# ).then(
|
| 225 |
+
# fn = bot,
|
| 226 |
+
# inputs = bot_inputs,
|
| 227 |
+
# outputs = [
|
| 228 |
+
# chatbot_component
|
| 229 |
+
# ]
|
| 230 |
+
# )
|
| 231 |
+
# text_prompt_component.submit(
|
| 232 |
+
# fn = user,
|
| 233 |
+
# inputs = user_inputs,
|
| 234 |
+
# outputs = [
|
| 235 |
+
# text_prompt_component,
|
| 236 |
+
# chatbot_component
|
| 237 |
+
# ],
|
| 238 |
+
# queue = False
|
| 239 |
+
# ).then(
|
| 240 |
+
# fn = bot,
|
| 241 |
+
# inputs = bot_inputs,
|
| 242 |
+
# outputs = [
|
| 243 |
+
# chatbot_component
|
| 244 |
+
# ]
|
| 245 |
+
# )
|
| 246 |
+
# # ============================== Stable - END ==============================
|
| 247 |
+
|
| 248 |
+
# with gr.Tab("Chat with Gemma 2"):
|
| 249 |
+
# gr.HTML(
|
| 250 |
+
# """
|
| 251 |
+
# <h1 align="center">Still in development</h1>
|
| 252 |
+
# """
|
| 253 |
+
# )
|
| 254 |
+
|
| 255 |
+
# # ============================== Nightly - START ==============================
|
| 256 |
+
# with gr.Tab("Nightly -- Chat with Gemini 1.5"):
|
| 257 |
+
# gr.HTML(
|
| 258 |
+
# """
|
| 259 |
+
# <h1 align="center">This section will test out the next version of the stable version.</h1>
|
| 260 |
+
# """
|
| 261 |
+
# )
|
| 262 |
+
# gr.ChatInterface(
|
| 263 |
+
# response,
|
| 264 |
+
# chatbot = gr.Chatbot(height=600),
|
| 265 |
+
# title = 'Chat with Gemini 1.5',
|
| 266 |
+
# retry_btn = "Retry",
|
| 267 |
+
# undo_btn = "Undo",
|
| 268 |
+
# clear_btn = "Clear",
|
| 269 |
+
# fill_height = True
|
| 270 |
+
# )
|
| 271 |
+
# # ============================== Nightly - END ==============================
|
| 272 |
+
|
| 273 |
+
# demo.queue().launch(debug = True, show_error = True)
|
| 274 |
+
|
| 275 |
"""
|
| 276 |
References:
|
| 277 |
- https://medium.com/@turna.fardousi/building-a-multimodal-chatbot-with-gemini-api-8015bfbee538
|
|
|
|
| 279 |
|
| 280 |
import os
|
| 281 |
import time
|
| 282 |
+
import PIL.Image
|
| 283 |
from typing import List, Tuple, Optional
|
| 284 |
import google.generativeai as genai
|
| 285 |
import gradio as gr
|
|
|
|
| 314 |
"""
|
| 315 |
IMAGE_WIDTH = 512
|
| 316 |
|
| 317 |
+
|
| 318 |
+
def preprocess_stop_sequences(
|
| 319 |
+
stop_sequences: str
|
| 320 |
+
) -> Optional[List[str]]:
|
| 321 |
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 322 |
|
| 323 |
+
|
| 324 |
+
def preprocess_image(
|
| 325 |
+
image: Image.Image
|
| 326 |
+
) -> Image.Image:
|
| 327 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 328 |
return image.resize((IMAGE_WIDTH, image_height))
|
| 329 |
|
| 330 |
+
|
| 331 |
+
def user(
|
| 332 |
+
text_prompt: str,
|
| 333 |
+
chatbot: List[Tuple[str, str]]
|
| 334 |
+
):
|
| 335 |
return "", chatbot + [[text_prompt, None]]
|
| 336 |
|
| 337 |
+
|
| 338 |
def bot(
|
| 339 |
google_key: str,
|
| 340 |
image_prompt: Optional[Image.Image],
|
|
|
|
| 359 |
top_p=top_p,
|
| 360 |
)
|
| 361 |
|
| 362 |
+
model_name = "gemini-1.5-flash"
|
| 363 |
model = genai.GenerativeModel(model_name)
|
| 364 |
+
inputs = [text_prompt] if image_prompt is None else [
|
| 365 |
+
text_prompt, preprocess_image(image_prompt)
|
| 366 |
+
]
|
| 367 |
+
|
| 368 |
+
response = model.generate_content(
|
| 369 |
+
inputs,
|
| 370 |
+
stream=True,
|
| 371 |
+
generation_config=generation_config
|
| 372 |
+
)
|
| 373 |
response.resolve()
|
| 374 |
|
| 375 |
chatbot[-1][1] = ""
|
|
|
|
| 379 |
time.sleep(0.01)
|
| 380 |
yield chatbot
|
| 381 |
|
| 382 |
+
|
| 383 |
google_key_component = gr.Textbox(
|
| 384 |
+
label="GOOGLE API KEY",
|
| 385 |
+
type="password",
|
| 386 |
+
placeholder="...",
|
| 387 |
+
visible=GEMINI_API_KEY is None
|
| 388 |
)
|
| 389 |
|
| 390 |
+
image_prompt_component = gr.Image(type="pil", label="Image")
|
| 391 |
+
chatbot_component = gr.Chatbot(bubble_full_width=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
text_prompt_component = gr.Textbox(
|
| 393 |
+
placeholder="Chat with Gemini",
|
| 394 |
+
label="Ask me anything and press Enter"
|
|
|
|
|
|
|
|
|
|
| 395 |
)
|
| 396 |
+
run_button_component = gr.Button("Run")
|
| 397 |
temperature_component = gr.Slider(
|
| 398 |
+
minimum=0,
|
| 399 |
+
maximum=1.0,
|
| 400 |
+
value=0.5,
|
| 401 |
+
step=0.05,
|
| 402 |
+
label="Temperature"
|
| 403 |
)
|
| 404 |
max_output_tokens_component = gr.Slider(
|
| 405 |
+
minimum=1,
|
| 406 |
+
maximum=8192,
|
| 407 |
+
value=4096,
|
| 408 |
+
step=1,
|
| 409 |
+
label="Max Output Tokens"
|
| 410 |
)
|
| 411 |
stop_sequences_component = gr.Textbox(
|
| 412 |
+
label="Add stop sequence",
|
| 413 |
+
placeholder="STOP, END"
|
| 414 |
)
|
| 415 |
top_k_component = gr.Slider(
|
| 416 |
+
minimum=1,
|
| 417 |
+
maximum=40,
|
| 418 |
+
value=32,
|
| 419 |
+
step=1,
|
| 420 |
+
label="Top-K"
|
| 421 |
)
|
| 422 |
top_p_component = gr.Slider(
|
| 423 |
+
minimum=0,
|
| 424 |
+
maximum=1,
|
| 425 |
+
value=1,
|
| 426 |
+
step=0.01,
|
| 427 |
+
label="Top-P"
|
| 428 |
)
|
| 429 |
|
| 430 |
+
user_inputs = [text_prompt_component, chatbot_component]
|
|
|
|
|
|
|
|
|
|
| 431 |
bot_inputs = [
|
| 432 |
+
google_key_component, image_prompt_component, temperature_component,
|
| 433 |
+
max_output_tokens_component, stop_sequences_component, top_k_component,
|
| 434 |
+
top_p_component, chatbot_component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
]
|
| 436 |
# ============================== Stable - END ==============================
|
| 437 |
|
|
|
|
| 443 |
GEMINI_API_KEY_NIGHTLY = os.getenv("GEMINI_API_KEY_NIGHTLY")
|
| 444 |
model_nightly_name = "gemini-1.5-flash"
|
| 445 |
|
| 446 |
+
|
| 447 |
def transform_history(history):
|
| 448 |
new_history = []
|
| 449 |
+
for user_msg, model_msg in history:
|
| 450 |
+
new_history.append({"role": "user", "parts": [{"text": user_msg}]})
|
| 451 |
+
new_history.append({"role": "model", "parts": [{"text": model_msg}]})
|
| 452 |
return new_history
|
| 453 |
|
| 454 |
+
|
| 455 |
+
def upload_to_gemini(path, mime_type=None):
|
| 456 |
+
file = genai.upload_file(path, mime_type=mime_type)
|
| 457 |
+
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
| 458 |
+
return file.uri
|
| 459 |
+
|
| 460 |
+
|
| 461 |
def response(message, history):
|
| 462 |
+
message_text = message["text"]
|
| 463 |
+
message_files = message["file"]
|
| 464 |
+
if message_files and isinstance(message_files, list):
|
| 465 |
+
image_uris = [upload_to_gemini(file_path) for file_path in message_files]
|
| 466 |
+
message_content = [{"text": message_text}] + image_uris
|
| 467 |
+
else:
|
| 468 |
+
message_content = {"text": message_text}
|
| 469 |
genai.configure(api_key=GEMINI_API_KEY_NIGHTLY)
|
| 470 |
+
model_nightly = genai.GenerativeModel(
|
| 471 |
+
model_nightly_name,
|
| 472 |
+
safety_settings=[
|
| 473 |
+
{
|
| 474 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 475 |
+
"threshold": "BLOCK_NONE"
|
| 476 |
+
},
|
| 477 |
+
{
|
| 478 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 479 |
+
"threshold": "BLOCK_NONE"
|
| 480 |
+
},
|
| 481 |
+
{
|
| 482 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 483 |
+
"threshold": "BLOCK_NONE"
|
| 484 |
+
},
|
| 485 |
+
{
|
| 486 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 487 |
+
"threshold": "BLOCK_NONE"
|
| 488 |
+
},
|
| 489 |
+
],
|
| 490 |
+
generation_config={
|
| 491 |
+
"temperature": 1,
|
| 492 |
+
"top_p": 0.95,
|
| 493 |
+
"top_k": 64,
|
| 494 |
+
"max_output_tokens": 8192,
|
| 495 |
+
"response_mime_type": "text/plain",
|
| 496 |
+
}
|
| 497 |
+
)
|
| 498 |
|
| 499 |
global chat
|
| 500 |
chat = model_nightly.start_chat(history=[])
|
| 501 |
chat.history = transform_history(history)
|
| 502 |
+
response = chat.send_message(message_content)
|
| 503 |
response.resolve()
|
| 504 |
|
| 505 |
for i in range(len(response.text)):
|
| 506 |
time.sleep(0.05)
|
| 507 |
+
yield response.text[:i + 1]
|
| 508 |
+
|
| 509 |
|
| 510 |
# ============================== Nightly - END ==============================
|
| 511 |
|
| 512 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 513 |
+
# ============================== Stable - START ==============================
|
| 514 |
with gr.Tab("Chat with Gemini 1.5 Flash"):
|
| 515 |
gr.HTML(TITLE)
|
| 516 |
with gr.Row():
|
|
|
|
| 534 |
top_p_component.render()
|
| 535 |
|
| 536 |
run_button_component.click(
|
| 537 |
+
fn=user,
|
| 538 |
+
inputs=user_inputs,
|
| 539 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 540 |
+
queue=False
|
|
|
|
|
|
|
|
|
|
| 541 |
).then(
|
| 542 |
+
fn=bot,
|
| 543 |
+
inputs=bot_inputs,
|
| 544 |
+
outputs=[chatbot_component]
|
|
|
|
|
|
|
| 545 |
)
|
| 546 |
text_prompt_component.submit(
|
| 547 |
+
fn=user,
|
| 548 |
+
inputs=user_inputs,
|
| 549 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 550 |
+
queue=False
|
|
|
|
|
|
|
|
|
|
| 551 |
).then(
|
| 552 |
+
fn=bot,
|
| 553 |
+
inputs=bot_inputs,
|
| 554 |
+
outputs=[chatbot_component]
|
|
|
|
|
|
|
| 555 |
)
|
| 556 |
+
# ============================== Stable - END ==============================
|
| 557 |
+
|
| 558 |
with gr.Tab("Chat with Gemma 2"):
|
| 559 |
+
gr.HTML("""<h1 align="center">Still in development</h1>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
+
# ============================== Nightly - START ==============================
|
| 562 |
with gr.Tab("Nightly -- Chat with Gemini 1.5"):
|
| 563 |
+
gr.HTML("""<h1 align="center">This section will test out the next version of the stable version.</h1>""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
gr.ChatInterface(
|
| 565 |
response,
|
| 566 |
+
chatbot=gr.Chatbot(height=600),
|
| 567 |
+
title='Chat with Gemini 1.5',
|
| 568 |
+
retry_btn="Retry",
|
| 569 |
+
undo_btn="Undo",
|
| 570 |
+
clear_btn="Clear",
|
| 571 |
+
fill_height=True,
|
| 572 |
+
multimodal=True
|
| 573 |
)
|
| 574 |
# ============================== Nightly - END ==============================
|
| 575 |
|
| 576 |
+
demo.queue().launch(debug=True, show_error=True, share=True)
|