Spaces:
Paused
Paused
Update stable branch
Browse filesFiles changed (5)
- .gitignore+3-1
- Tabs/Gemini_Chabot_Stable.py+143-36
- Tabs/Gemini_Chatbot_Preview.py+1-1
- Tabs/Gemma_Chatbot.pyDeleted-82
- app.py+8-11
12234.envenv/env/.python-versionGoogle-AI-Playground/5 unmodified lines678991011122 unmodified lines1516171818192021212223241 unmodified line26272829293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012134353637383940414243444546474849505152535455565758596061621221231248 unmodified lines133134135771361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691708 unmodified lines1791801819218218318418518 unmodified lines2042052061172072082092102112122132142152162172182192202212222232242255 unmodified linesload_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")model_name = "gemini-1.5-flash-exp-0827"# model_name = "gemini-1.5-flash-exp-0827"
TITLE = """<h1 align="center">🎮Chat with Gemini 1.5🔥</h1>"""NOTICE = """2 unmodified lines- Some features may not work as expected"""ABOUT = """**Updates (2024-8-28)** 📈: Upgrade model to SOTA Gemini 1.5 Flash Experimental 0827**Updates (2024-9-25)** 📈: Upgrade model to SOTA Gemini 1.5 Flash Experimental 0924
**Info** 📄:- Model: Gemini 1.5 Flash Experimental 0827- Model: Gemini 1.5 Flash Experimental 0924 and other Gemini models- Chat with Gemini 1.5 Flash model with images and documents"""ERRORS = """1 unmodified line"""FUTURE_IMPLEMENTATIONS = """Future features 🚀:- Select other Gemini / Gemma models- More tools such as web search"""
# genai.configure(api_key=GEMINI_API_KEY)# model = genai.GenerativeModel(# model_name,# safety_settings=[# {# "category": "HARM_CATEGORY_HARASSMENT",# "threshold": "BLOCK_NONE"# },# {# "category": "HARM_CATEGORY_HATE_SPEECH",# "threshold": "BLOCK_NONE"# },# {# "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",# "threshold": "BLOCK_NONE"# },# {# "category": "HARM_CATEGORY_DANGEROUS_CONTENT",# "threshold": "BLOCK_NONE"# }# ],# generation_config={# "temperature": 1,# "top_p": 0.95,# "top_k": 64,# "max_output_tokens": 8192,# "response_mime_type": "text/plain",# }# )# chat = model.start_chat(history=[])
# def clear_chat_history():# chat.history = [] # def undo_chat():# last_send, last_received = chat.rewind()
# def transform_history(history):# new_history = []# for user_msg, model_msg in history:# new_history.append({"role": "user", "parts": [user_msg]})# new_history.append({"role": "model", "parts": [model_msg]})# return new_history
# def chatbot_stable(message, history):# message_text = message["text"]# message_files = message["files"]# if message_files:# image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]# message_content = [message_text] + image_uris# else:# message_content = [message_text]
# response = chat.send_message(message_content, stream=True)# response.resolve()
# return response.text
# gemini_chatbot_interface = gr.Chatbot(# height=400,# likeable=True,# avatar_images=(# None,# "https://media.roboflow.com/spaces/gemini-icon.png"# ),# show_copy_button=True,# show_share_button=True,# render_markdown=True# )
# clear_chat_button = gr.ClearButton(# components=[gemini_chatbot_interface],# value="🗑️ Clear"# )
# undo_chat_button = gr.Button(# value="↩️ Undo"# )
# gemini_chatbot = gr.ChatInterface(# fn=chatbot_stable,# chatbot=gemini_chatbot_interface,# multimodal=True,# clear_btn=clear_chat_button,# undo_btn=undo_chat_button# )
model_list = ["gemini-1.5-pro", "gemini-1.5-pro-002", "gemini-1.5-pro-exp-0827", "gemini-1.5-flash", "gemini-1.5-flash-002", "gemini-1.5-flash-8b-exp-0924"]
genai.configure(api_key=GEMINI_API_KEY)model = genai.GenerativeModel( model_name, safety_settings=[ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE" } ], generation_config={ "temperature": 1, "top_p": 0.95, "top_k": 64, "max_output_tokens": 8192, "response_mime_type": "text/plain", })chat = model.start_chat(history=[])
def clear_chat_history(): chat.history = []8 unmodified lines new_history.append({"role": "model", "parts": [model_msg]}) return new_history
def chatbot_stable(message, history):def chatbot_stable(message, history, model_id, system_message, max_tokens, temperature, top_p,): global model, chat model = genai.GenerativeModel( model_name=model_id, system_instruction=system_message, safety_settings=[ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE" }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE" } ], generation_config={ "temperature": temperature, "top_p": top_p, "top_k": 64, "max_output_tokens": max_tokens, "response_mime_type": "text/plain", } ) chat = model.start_chat(history=[]) message_text = message["text"] message_files = message["files"] if message_files:8 unmodified lines return response.text
gemini_chatbot_interface = gr.Chatbot( height=400, height=500, likeable=True, avatar_images=( None,18 unmodified lines chatbot=gemini_chatbot_interface, multimodal=True, clear_btn=clear_chat_button, undo_btn=undo_chat_button undo_btn=undo_chat_button, additional_inputs=[ gr.Dropdown( choices=model_list, value="gemini-1.5-flash-002", label="Models" ), gr.Textbox(value="You are a friendly Chatbot.", label="System message"), gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=1, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ],)5 unmodified lines678991011125 unmodified linesload_dotenv()
GEMINI_API_KEY_PREVIEW = os.getenv("GEMINI_API_KEY_PREVIEW")model_list = ["gemini-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"]model_list = ["gemini-1.5-pro", "gemini-1.5-pro-002", "gemini-1.5-pro-exp-0827", "gemini-1.5-flash", "gemini-1.5-flash-002", "gemini-1.5-flash-8b-exp-0924"]
genai.configure(api_key=GEMINI_API_KEY_PREVIEW)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import gradio as grfrom huggingface_hub import InferenceClientimport osfrom dotenv import load_dotenv
load_dotenv()HF_TOKEN = os.getenv("HF_TOKEN")
model_list = ["google/gemma-2-2b-it", "google/gemma-2-9b-it", "google/gemma-2-27b-it"]
def respond( message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p,): client = InferenceClient( model_id, token=HF_TOKEN, ) messages = [{"role": "system", "content": system_message}]
for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content
response += token yield response
gemma_chatbot = gr.ChatInterface( respond, additional_inputs=[ gr.Dropdown( choices=model_list, label="Model", value="google/gemma-2-27b-it", ), gr.Textbox( value="You are a friendly Chatbot.", label="System message" ), gr.Slider( minimum=1, maximum=4096, value=512, step=1, label="Max new tokens" ), gr.Slider( minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature" ), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ],)123445615 unmodified lines222324262728293031322526272829303132333435363435import gradio as grfrom Tabs.Gemini_Chabot_Stable import gemini_chatbot, clear_chat_button, undo_chat_button, clear_chat_history, undo_chat, TITLE, NOTICE, ERRORS, FUTURE_IMPLEMENTATIONS, ABOUTfrom Tabs.Gemini_Chatbot_Preview import gemini_chatbot_preview, clear_chat_button_preview, undo_chat_button_preview, clear_chat_history_preview, undo_chat_previewfrom Tabs.Gemma_Chatbot import gemma_chatbot
with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.HTML("""<h3 align="center">I strongly recommond duplicate this space for intensive uses!!!</h3>""")15 unmodified lines # ============================== Stable - END ============================== # ============================== Nightly - START ============================== with gr.Tab("Chat with Gemini 1.5 - Preview"): gemini_chatbot_preview.render() clear_chat_button_preview.click( fn=clear_chat_history_preview ) undo_chat_button_preview.click( fn=undo_chat_preview ) # gemini_chatbot_preview.render() # clear_chat_button_preview.click( # fn=clear_chat_history_preview # ) # undo_chat_button_preview.click( # fn=undo_chat_preview # ) gr.HTML("<h1 align='center'>🚧 Work in progress 🚧</h1>") # ============================== Nightly - END ============================== with gr.Tab("Chat with Gemma 2"): # gemma_chatbot.render() gr.HTML("""<h3 align="center">This feature is under development. Please check back later.</h3>""")
demo.queue().launch(debug=True, show_error=True).gitignore+3-1
Tabs/Gemini_Chabot_Stable.py+143-36
Tabs/Gemini_Chatbot_Preview.py+1-1
Tabs/Gemma_Chatbot.pyDeleted-82
app.py+8-11