CoolFace
Apppublic

NotASI/Google-AI-Playground

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
5likes
Gemini_Chabot_Stable.py221 linesDownload Raw Back to Tabs
1import os2import google.generativeai as genai3import gradio as gr4from dotenv import load_dotenv5 6load_dotenv()7 8GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")9# model_name = "gemini-1.5-flash-exp-0827"10 11TITLE = """<h1 align="center">๐ŸŽฎChat with Gemini 1.5๐Ÿ”ฅ</h1>"""12NOTICE = """13**Notices** ๐Ÿ“œ:14- This app is still in development15- Some features may not work as expected16"""17ABOUT = """18**Updates (2024-9-25)** ๐Ÿ“ˆ: Upgrade model to SOTA Gemini 1.5 Flash Experimental 092419"""20ERRORS = """21Known errors โš ๏ธ:22"""23FUTURE_IMPLEMENTATIONS = """24Future features ๐Ÿš€:25- More tools such as web search26"""27 28# genai.configure(api_key=GEMINI_API_KEY)29# model = genai.GenerativeModel(30#     model_name,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": 1,51#         "top_p": 0.95,52#         "top_k": 64,53#         "max_output_tokens": 8192,54#         "response_mime_type": "text/plain",55#     }56# )57# chat = model.start_chat(history=[])58 59# def clear_chat_history():60#     chat.history = []61    62# def undo_chat():63#     last_send, last_received = chat.rewind()64 65# def transform_history(history):66#     new_history = []67#     for user_msg, model_msg in history:68#         new_history.append({"role": "user", "parts": [user_msg]})69#         new_history.append({"role": "model", "parts": [model_msg]})70#     return new_history71 72# def chatbot_stable(message, history):73#     message_text = message["text"]74#     message_files = message["files"]75#     if message_files:76#         image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]77#         message_content = [message_text] + image_uris78#     else:79#         message_content = [message_text]80 81#     response = chat.send_message(message_content, stream=True)82#     response.resolve()83 84#     return response.text85 86# gemini_chatbot_interface = gr.Chatbot(87#     height=400,88#     likeable=True,89#     avatar_images=(90#         None,91#         "https://media.roboflow.com/spaces/gemini-icon.png"92#     ),93#     show_copy_button=True,94#     show_share_button=True,95#     render_markdown=True96# )97 98# clear_chat_button = gr.ClearButton(99#     components=[gemini_chatbot_interface],100#     value="๐Ÿ—‘๏ธ Clear"101# )102 103# undo_chat_button = gr.Button(104#     value="โ†ฉ๏ธ Undo"105# )106 107# gemini_chatbot = gr.ChatInterface(108#     fn=chatbot_stable,109#     chatbot=gemini_chatbot_interface,110#     multimodal=True,111#     clear_btn=clear_chat_button,112#     undo_btn=undo_chat_button113# )114 115model_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"]116 117genai.configure(api_key=GEMINI_API_KEY)118 119def clear_chat_history():120    chat.history = []121    122def undo_chat():123    last_send, last_received = chat.rewind()124 125def transform_history(history):126    new_history = []127    for user_msg, model_msg in history:128        new_history.append({"role": "user", "parts": [user_msg]})129        new_history.append({"role": "model", "parts": [model_msg]})130    return new_history131 132def chatbot_stable(message, history, model_id, system_message, max_tokens, temperature, top_p,):133    global model, chat134    model = genai.GenerativeModel(135        model_name=model_id,136        system_instruction=system_message,137        safety_settings=[138            {139                "category": "HARM_CATEGORY_HARASSMENT",140                "threshold": "BLOCK_NONE"141            },142            {143                "category": "HARM_CATEGORY_HATE_SPEECH",144                "threshold": "BLOCK_NONE"145            },146            {147                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",148                "threshold": "BLOCK_NONE"149            },150            {151                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",152                "threshold": "BLOCK_NONE"153            }154        ],155        generation_config={156            "temperature": temperature,157            "top_p": top_p,158            "top_k": 40,159            "max_output_tokens": max_tokens,160            "response_mime_type": "text/plain",161        }162    )163    chat = model.start_chat(history=[])164    message_text = message["text"]165    message_files = message["files"]166    if message_files:167        image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]168        message_content = [message_text] + image_uris169    else:170        message_content = [message_text]171 172    response = chat.send_message(message_content, stream=True)173    response.resolve()174 175    return response.text176 177gemini_chatbot_interface = gr.Chatbot(178    height=500,179    likeable=True,180    avatar_images=(181        None,182        "https://media.roboflow.com/spaces/gemini-icon.png"183    ),184    show_copy_button=True,185    show_share_button=True,186    render_markdown=True187)188 189clear_chat_button = gr.ClearButton(190    components=[gemini_chatbot_interface],191    value="๐Ÿ—‘๏ธ Clear"192)193 194undo_chat_button = gr.Button(195    value="โ†ฉ๏ธ Undo"196)197 198gemini_chatbot = gr.ChatInterface(199    fn=chatbot_stable,200    chatbot=gemini_chatbot_interface,201    multimodal=True,202    clear_btn=clear_chat_button,203    undo_btn=undo_chat_button,204    additional_inputs=[205        gr.Dropdown(206            choices=model_list,207            value="gemini-1.5-flash-002",208            label="Models"209        ),210        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),211        gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),212        gr.Slider(minimum=0.1, maximum=1.0, value=1, step=0.1, label="Temperature"),213        gr.Slider(214            minimum=0.1,215            maximum=1.0,216            value=0.95,217            step=0.05,218            label="Top-p (nucleus sampling)",219        ),220    ],221)