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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 18 |
+
|
| 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 |
+
- Features:
|
| 31 |
+
- Langchain integration
|
| 32 |
+
- Google search
|
| 33 |
+
"""
|
| 34 |
+
ERRORS = """
|
| 35 |
+
Known errors ⚠️:
|
| 36 |
+
"""
|
| 37 |
+
FUTURE_IMPLEMENTATIONS = """
|
| 38 |
+
To be implemented 🚀:
|
| 39 |
+
- Select other Gemini / Gemma models
|
| 40 |
+
- Upload files
|
| 41 |
+
- More tools other than web search
|
| 42 |
+
"""
|
| 43 |
+
IMAGE_WIDTH = 512
|
| 44 |
+
|
| 45 |
+
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
| 46 |
+
return [seq.strip() for seq in stop_sequences.split(",")] if stop_sequences else None
|
| 47 |
+
|
| 48 |
+
def preprocess_image(image: Image.Image) -> Image.Image:
|
| 49 |
+
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 50 |
+
return image.resize((IMAGE_WIDTH, image_height))
|
| 51 |
+
|
| 52 |
+
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
| 53 |
+
return "", chatbot + [[text_prompt, None]]
|
| 54 |
+
|
| 55 |
+
def bot(
|
| 56 |
+
google_key: str,
|
| 57 |
+
image_prompt: Optional[Image.Image],
|
| 58 |
+
temperature: float,
|
| 59 |
+
max_output_tokens: int,
|
| 60 |
+
stop_sequences: str,
|
| 61 |
+
top_k: int,
|
| 62 |
+
top_p: float,
|
| 63 |
+
chatbot: List[Tuple[str, str]]
|
| 64 |
+
):
|
| 65 |
+
google_key = google_key or GEMINI_API_KEY
|
| 66 |
+
if not google_key:
|
| 67 |
+
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 68 |
+
|
| 69 |
+
text_prompt = chatbot[-1][0]
|
| 70 |
+
genai.configure(api_key=google_key)
|
| 71 |
+
generation_config = genai.types.GenerationConfig(
|
| 72 |
+
temperature=temperature,
|
| 73 |
+
max_output_tokens=max_output_tokens,
|
| 74 |
+
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 75 |
+
top_k=top_k,
|
| 76 |
+
top_p=top_p,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
model_name = "gemini-1.5-flash" # if image_prompt is None else "gemini-pro-vision"
|
| 80 |
+
model = genai.GenerativeModel(model_name)
|
| 81 |
+
inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
| 82 |
+
|
| 83 |
+
response = model.generate_content(inputs, stream=True, generation_config=generation_config)
|
| 84 |
+
response.resolve()
|
| 85 |
+
|
| 86 |
+
chatbot[-1][1] = ""
|
| 87 |
+
for chunk in response:
|
| 88 |
+
for i in range(0, len(chunk.text), 10):
|
| 89 |
+
chatbot[-1][1] += chunk.text[i:i + 10]
|
| 90 |
+
time.sleep(0.01)
|
| 91 |
+
yield chatbot
|
| 92 |
+
|
| 93 |
+
google_key_component = gr.Textbox(
|
| 94 |
+
label = "GOOGLE API KEY",
|
| 95 |
+
type = "password",
|
| 96 |
+
placeholder = "...",
|
| 97 |
+
visible = GEMINI_API_KEY is None
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
image_prompt_component = gr.Image(
|
| 101 |
+
type = "pil",
|
| 102 |
+
label = "Image"
|
| 103 |
+
)
|
| 104 |
+
chatbot_component = gr.Chatbot(
|
| 105 |
+
# label = 'Gemini',
|
| 106 |
+
bubble_full_width = False
|
| 107 |
+
)
|
| 108 |
+
text_prompt_component = gr.Textbox(
|
| 109 |
+
placeholder = "Chat with Gemini",
|
| 110 |
+
label = "Ask me anything and press Enter"
|
| 111 |
+
)
|
| 112 |
+
run_button_component = gr.Button(
|
| 113 |
+
"Run"
|
| 114 |
+
)
|
| 115 |
+
temperature_component = gr.Slider(
|
| 116 |
+
minimum = 0,
|
| 117 |
+
maximum = 1.0,
|
| 118 |
+
value = 0.5,
|
| 119 |
+
step = 0.05,
|
| 120 |
+
label = "Temperature"
|
| 121 |
+
)
|
| 122 |
+
max_output_tokens_component = gr.Slider(
|
| 123 |
+
minimum = 1,
|
| 124 |
+
maximum = 8192,
|
| 125 |
+
value = 4096,
|
| 126 |
+
step = 1,
|
| 127 |
+
label = "Max Output Tokens"
|
| 128 |
+
)
|
| 129 |
+
stop_sequences_component = gr.Textbox(
|
| 130 |
+
label = "Add stop sequence",
|
| 131 |
+
placeholder = "STOP, END"
|
| 132 |
+
)
|
| 133 |
+
top_k_component = gr.Slider(
|
| 134 |
+
minimum = 1,
|
| 135 |
+
maximum = 40,
|
| 136 |
+
value = 32,
|
| 137 |
+
step = 1,
|
| 138 |
+
label = "Top-K"
|
| 139 |
+
)
|
| 140 |
+
top_p_component = gr.Slider(
|
| 141 |
+
minimum = 0,
|
| 142 |
+
maximum = 1,
|
| 143 |
+
value = 1,
|
| 144 |
+
step = 0.01,
|
| 145 |
+
label = "Top-P"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
user_inputs = [
|
| 149 |
+
text_prompt_component,
|
| 150 |
+
chatbot_component
|
| 151 |
+
]
|
| 152 |
+
bot_inputs = [
|
| 153 |
+
google_key_component,
|
| 154 |
+
image_prompt_component,
|
| 155 |
+
temperature_component,
|
| 156 |
+
max_output_tokens_component,
|
| 157 |
+
stop_sequences_component,
|
| 158 |
+
top_k_component,
|
| 159 |
+
top_p_component,
|
| 160 |
+
chatbot_component
|
| 161 |
+
]
|
| 162 |
+
|
| 163 |
+
with gr.Blocks() as demo:
|
| 164 |
+
gr.HTML(TITLE)
|
| 165 |
+
with gr.Row():
|
| 166 |
+
gr.Markdown(NOTICE)
|
| 167 |
+
gr.Markdown(ABOUT)
|
| 168 |
+
gr.Markdown(ERRORS)
|
| 169 |
+
gr.Markdown(FUTURE_IMPLEMENTATIONS)
|
| 170 |
+
with gr.Column():
|
| 171 |
+
google_key_component.render()
|
| 172 |
+
with gr.Row():
|
| 173 |
+
image_prompt_component.render()
|
| 174 |
+
chatbot_component.render()
|
| 175 |
+
text_prompt_component.render()
|
| 176 |
+
run_button_component.render()
|
| 177 |
+
with gr.Accordion("Parameters", open=False):
|
| 178 |
+
temperature_component.render()
|
| 179 |
+
max_output_tokens_component.render()
|
| 180 |
+
stop_sequences_component.render()
|
| 181 |
+
with gr.Accordion("Advanced", open=False):
|
| 182 |
+
top_k_component.render()
|
| 183 |
+
top_p_component.render()
|
| 184 |
+
|
| 185 |
+
run_button_component.click(
|
| 186 |
+
fn = user,
|
| 187 |
+
inputs = user_inputs,
|
| 188 |
+
outputs = [
|
| 189 |
+
text_prompt_component,
|
| 190 |
+
chatbot_component
|
| 191 |
+
],
|
| 192 |
+
queue = False
|
| 193 |
+
).then(
|
| 194 |
+
fn = bot,
|
| 195 |
+
inputs = bot_inputs,
|
| 196 |
+
outputs = [
|
| 197 |
+
chatbot_component
|
| 198 |
+
]
|
| 199 |
+
)
|
| 200 |
+
text_prompt_component.submit(
|
| 201 |
+
fn = user,
|
| 202 |
+
inputs = user_inputs,
|
| 203 |
+
outputs = [
|
| 204 |
+
text_prompt_component,
|
| 205 |
+
chatbot_component
|
| 206 |
+
],
|
| 207 |
+
queue = False
|
| 208 |
+
).then(
|
| 209 |
+
fn = bot,
|
| 210 |
+
inputs = bot_inputs,
|
| 211 |
+
outputs = [
|
| 212 |
+
chatbot_component
|
| 213 |
+
]
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
google-generativeai
|