import gradio as gr from huggingface_hub import InferenceClient # Define available models (update with your actual model IDs) model_list = { "Safe LM": "HuggingFaceH4/zephyr-7b-beta", # Replace with your Safe LM model ID "Zephyr Beta": "HuggingFaceH4/zephyr-7b-beta", "Another Model": "HuggingFaceH4/zephyr-7b-beta" } def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model): # Ensure history is a list history = history or [] # Build conversation messages for the client messages = [{"role": "system", "content": system_message}] for user_msg, assistant_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) # Append new user message to history with an empty assistant response history = history + [(message, "")] response = "" # Create an InferenceClient for the selected model client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta")) # Stream the response from the client for token_message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = token_message.choices[0].delta.content response += token # Update the assistant's message in the history history[-1] = (message, response) # Yield two outputs: clear the input and update the chat history yield "", history # Custom CSS for pastel colors, gentle backgrounds, and rounded corners css = """ body { background-color: #FAF3E0; } .gradio-container { background-color: #FFFFFF; border-radius: 16px; padding: 20px; } button, input, .gradio-dropdown, .gradio-slider, textarea { border-radius: 16px; } .gradio-chat { border-radius: 16px; } """ with gr.Blocks(css=css) as demo: with gr.Row(): # Left sidebar: Model selector with gr.Column(scale=1): gr.Markdown("## Models") model_dropdown = gr.Dropdown( choices=list(model_list.keys()), label="Select Model", value="Safe LM" ) # Main area: Chat interface and settings with gr.Column(scale=3): gr.Markdown("## Chat Interface") chatbot = gr.Chatbot(label="Chat with your Model", type="messages") user_input = gr.Textbox(placeholder="Enter your message...", label="Your Message") with gr.Row(): send_button = gr.Button("Send") clear_button = gr.Button("Clear Chat") gr.Markdown("### Chat Settings") system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System Message") max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens") temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature") top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)") # Wire the Send button to call the respond() function and update outputs send_button.click( fn=respond, inputs=[user_input, chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown], outputs=[user_input, chatbot], ) # Clear the chat history when Clear Chat is clicked clear_button.click(lambda: None, None, chatbot, queue=False) if __name__ == "__main__": demo.launch()