NotASI/Gemini-Pro-Playground
14
1import os2import time3import uuid4from typing import List, Tuple, Optional, Dict, Union5 6import google.generativeai as genai7import gradio as gr8from PIL import Image9 10print("google-generativeai:", genai.__version__)11 12GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")13 14TITLE = """<h1 align="center">🎮Chat with Gemini 1.5 Pro🔥 (Deprecated)</h1>"""15SUBTITLE = """16<h2 align="center">New version here: https://huggingface.co/spaces/NotAiLOL/Gemini-Playground-Beta-Preview</h2>17<h2 align="center">Try <b>Gemini 1.5 Pro Experimental 0801</b> 🐦🔥 -- Beat GPT-4o in Lmsys Leaderboard (2024/8/4)</h2>18"""19NOTICES = """20Notices:21- UPDATES (2024-8-12): END OF SUPPORT, new version: https://huggingface.co/spaces/NotAiLOL/Gemini-Playground-Beta-Preview22- This version will be removed on the 1st Sep 2024.23"""24DUPLICATE = """25<div style="text-align: center; display: flex; justify-content: center; align-items: center;">26 <a href="https://huggingface.co/spaces/NotAiLOL/Gemini-Pro-Playground?duplicate=true">27 <img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">28 </a>29 <span>Duplicate the Space and run securely with your 30 <a href="https://makersuite.google.com/app/apikey">GOOGLE API KEY</a>.31 </span>32</div>33"""34 35AVATAR_IMAGES = (36 None,37 "https://media.roboflow.com/spaces/gemini-icon.png"38)39 40IMAGE_CACHE_DIRECTORY = "/tmp"41IMAGE_WIDTH = 51242CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]43 44 45def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:46 if not stop_sequences:47 return None48 return [sequence.strip() for sequence in stop_sequences.split(",")]49 50 51def preprocess_image(image: Image.Image) -> Optional[Image.Image]:52 image_height = int(image.height * IMAGE_WIDTH / image.width)53 return image.resize((IMAGE_WIDTH, image_height))54 55 56def cache_pil_image(image: Image.Image) -> str:57 image_filename = f"{uuid.uuid4()}.jpeg"58 os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)59 image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)60 image.save(image_path, "JPEG")61 return image_path62 63 64def preprocess_chat_history(65 history: CHAT_HISTORY66) -> List[Dict[str, Union[str, List[str]]]]:67 messages = []68 for user_message, model_message in history:69 if isinstance(user_message, tuple):70 pass71 elif user_message is not None:72 messages.append({'role': 'user', 'parts': [user_message]})73 if model_message is not None:74 messages.append({'role': 'model', 'parts': [model_message]})75 return messages76 77 78def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:79 for file in files:80 image = Image.open(file).convert('RGB')81 image = preprocess_image(image)82 image_path = cache_pil_image(image)83 chatbot.append(((image_path,), None))84 return chatbot85 86 87def user(text_prompt: str, chatbot: CHAT_HISTORY):88 if text_prompt:89 chatbot.append((text_prompt, None))90 return "", chatbot91 92 93# def bot(94# google_key: str,95# model_name: str,96# files: Optional[List[str]],97# temperature: float,98# max_output_tokens: int,99# stop_sequences: str,100# top_k: int,101# top_p: float,102# chatbot: CHAT_HISTORY103# ):104# if len(chatbot) == 0:105# return chatbot106 107# google_key = google_key if google_key else GOOGLE_API_KEY108# if not google_key:109# raise ValueError(110# "GOOGLE_API_KEY is not set. "111# "Please follow the instructions in the README to set it up.")112 113# genai.configure(api_key=google_key)114# generation_config = genai.types.GenerationConfig(115# temperature=temperature,116# max_output_tokens=max_output_tokens,117# stop_sequences=preprocess_stop_sequences(stop_sequences=stop_sequences),118# top_k=top_k,119# top_p=top_p)120 121# if files:122# text_prompt = [chatbot[-1][0]] \123# if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \124# else []125# image_prompt = [Image.open(file).convert('RGB') for file in files]126# model = genai.GenerativeModel(model_name)127# response = model.generate_content(128# text_prompt + image_prompt,129# stream=True,130# generation_config=generation_config)131# else:132# messages = preprocess_chat_history(chatbot)133# model = genai.GenerativeModel(model_name)134# response = model.generate_content(135# messages,136# stream=True,137# generation_config=generation_config)138 139# # streaming effect140# chatbot[-1][1] = ""141# for chunk in response:142# for i in range(0, len(chunk.text), 10):143# section = chunk.text[i:i + 10]144# chatbot[-1][1] += section145# time.sleep(0.01)146# yield chatbot147 148# -------------------------------------------------------------------149 150def bot(151 google_key: str,152 model_name: str,153 files: Optional[List[str]],154 temperature: float,155 max_output_tokens: int,156 stop_sequences: str,157 top_k: int,158 top_p: float,159 chatbot: CHAT_HISTORY160):161 if len(chatbot) == 0:162 return chatbot163 164 google_key = google_key if google_key else GOOGLE_API_KEY165 if not google_key:166 raise ValueError(167 "GOOGLE_API_KEY is not set. "168 "Please follow the instructions in the README to set it up.")169 170 genai.configure(api_key=google_key)171 generation_config = genai.types.GenerationConfig(172 temperature=temperature,173 max_output_tokens=max_output_tokens,174 stop_sequences=preprocess_stop_sequences(stop_sequences=stop_sequences),175 top_k=top_k,176 top_p=top_p)177 178 if files:179 text_prompt = [chatbot[-1][0]] \180 if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \181 else []182 image_prompt = [Image.open(file).convert('RGB') for file in files]183 model = genai.GenerativeModel(model_name)184 response = model.generate_content(185 text_prompt + image_prompt,186 stream=True,187 generation_config=generation_config)188 else:189 messages = preprocess_chat_history(chatbot)190 model = genai.GenerativeModel(model_name)191 response = model.generate_content(192 messages,193 stream=True,194 generation_config=generation_config195 )196 197 # streaming effect198 chatbot[-1][1] = ""199 for chunk in response:200 if not chunk.text:201 print("chunk.text is empty")202 continue203 204 print(f"chunk.text: {chunk.text}")205 206 try:207 for i in range(0, len(chunk.text)):208 section = chunk.text[i:i + 1]209 chatbot[-1][1] += section210 time.sleep(0.01)211 yield chatbot212 except IndexError as e:213 print(f"IndexError: {e}")214 # Handle the error appropriately215 216# -------------------------------------------------------------------217 218model_selection = gr.Dropdown(219 ["gemini-1.5-flash",220 "gemini-1.5-pro",221 "gemini-1.5-pro-exp-0801"222 ],223 label="Select Gemini Model",224 value="gemini-1.5-pro"225)226 227google_key_component = gr.Textbox(228 label="GOOGLE API KEY",229 value="",230 type="password",231 placeholder="...",232 info="You have to provide your own GOOGLE_API_KEY for this app to function properly",233 visible=GOOGLE_API_KEY is None234)235chatbot_component = gr.Chatbot(236 label='Gemini',237 bubble_full_width=False,238 avatar_images=AVATAR_IMAGES,239 scale=2,240 height=400241)242text_prompt_component = gr.Textbox(243 placeholder="Hi there! [press Enter]", show_label=False, autofocus=True, scale=8244)245upload_button_component = gr.UploadButton(246 label="Upload Images", file_count="multiple", file_types=["image"], scale=1247)248run_button_component = gr.Button(value="Run", variant="primary", scale=1)249temperature_component = gr.Slider(250 minimum=0,251 maximum=1.0,252 value=0.4,253 step=0.05,254 label="Temperature",255 info=(256 "Temperature controls the degree of randomness in token selection. Lower "257 "temperatures are good for prompts that expect a true or correct response, "258 "while higher temperatures can lead to more diverse or unexpected results. "259 ))260max_output_tokens_component = gr.Slider(261 minimum=1,262 maximum=8192,263 value=4096,264 step=1,265 label="Token limit",266 info=(267 "Token limit determines the maximum amount of text output from one prompt. A "268 "token is approximately four characters. The default value is 4096."269 ))270stop_sequences_component = gr.Textbox(271 label="Add stop sequence",272 value="",273 type="text",274 placeholder="STOP, END",275 info=(276 "A stop sequence is a series of characters (including spaces) that stops "277 "response generation if the model encounters it. The sequence is not included "278 "as part of the response. You can add up to five stop sequences."279 ))280top_k_component = gr.Slider(281 minimum=1,282 maximum=40,283 value=32,284 step=1,285 label="Top-K",286 info=(287 "Top-k changes how the model selects tokens for output. A top-k of 1 means the "288 "selected token is the most probable among all tokens in the model’s "289 "vocabulary (also called greedy decoding), while a top-k of 3 means that the "290 "next token is selected from among the 3 most probable tokens (using "291 "temperature)."292 ))293top_p_component = gr.Slider(294 minimum=0,295 maximum=1,296 value=1,297 step=0.01,298 label="Top-P",299 info=(300 "Top-p changes how the model selects tokens for output. Tokens are selected "301 "from most probable to least until the sum of their probabilities equals the "302 "top-p value. For example, if tokens A, B, and C have a probability of .3, .2, "303 "and .1 and the top-p value is .5, then the model will select either A or B as "304 "the next token (using temperature). "305 ))306 307user_inputs = [308 text_prompt_component,309 chatbot_component310]311 312bot_inputs = [313 google_key_component,314 model_selection,315 upload_button_component,316 temperature_component,317 max_output_tokens_component,318 stop_sequences_component,319 top_k_component,320 top_p_component,321 chatbot_component322]323 324with gr.Blocks() as demo:325 gr.HTML(TITLE)326 gr.HTML(SUBTITLE)327 gr.Markdown(NOTICES)328 gr.HTML(DUPLICATE)329 with gr.Column():330 google_key_component.render()331 chatbot_component.render()332 text_prompt_component.render()333 with gr.Row():334 model_selection.render()335 upload_button_component.render()336 run_button_component.render()337 with gr.Accordion("Parameters", open=False):338 temperature_component.render()339 max_output_tokens_component.render()340 stop_sequences_component.render()341 with gr.Accordion("Advanced", open=False):342 top_k_component.render()343 top_p_component.render()344 345 run_button_component.click(346 fn=user,347 inputs=user_inputs,348 outputs=[text_prompt_component, chatbot_component],349 queue=False350 ).then(351 fn=bot, inputs=bot_inputs, outputs=[chatbot_component],352 )353 354 text_prompt_component.submit(355 fn=user,356 inputs=user_inputs,357 outputs=[text_prompt_component, chatbot_component],358 queue=False359 ).then(360 fn=bot, inputs=bot_inputs, outputs=[chatbot_component],361 )362 363 upload_button_component.upload(364 fn=upload,365 inputs=[upload_button_component, chatbot_component],366 outputs=[chatbot_component],367 queue=False368 )369 370demo.queue(max_size=99).launch(debug=False, show_error=True)371 