Update app.py
Browse files7 unmodified lines8910111213141516171819202122232425262728293031323318 unmodified lines525354555657585960616263646566676869707172737475767778798081828384858687887 unmodified linesdef switch_client(model_name: str):return InferenceClient(model_name)def respond(message,history: list[dict],system_message,max_tokens,temperature,top_p,model_name):# Switch client based on model selectionglobal clientclient = switch_client(model_name)messages = [{"role": "system", "content": system_message}]for val in history:messages.append({"role": val['role'], "content": val['content']})messages.append({"role": "user", "content": message})# Get the response from the modelresponse = client.chat_completion(messages,18 unmodified lines# Function to handle model selection and pseudonymsdef respond_with_pseudonym(message,history: list[dict],system_message,max_tokens,temperature,top_p,selected_pseudonym):# Find the actual model name from the pseudonymmodel_name = next(model[0] for model in model_choices if model[1] == selected_pseudonym)# Call the existing respond functionresponse = respond(message, history, system_message, max_tokens, temperature, top_p, model_name)# No longer adding the pseudonym at the end of the responsereturn response# Gradio Chat Interfacedemo = gr.ChatInterface(respond_with_pseudonym,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, 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)",),gr.Dropdown(pseudonyms, label="Select Model", value=pseudonyms[0]) # Pseudonym selection dropdown],)7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546474849505118 unmodified lines7071727374757677787980818283848586878889909192937 unmodified linesdef switch_client(model_name: str):return InferenceClient(model_name)# Define presets for each modelpresets = {"mistralai/Mistral-7B-Instruct-v0.3": {"Fast": {"max_tokens": 256, "temperature": 1.0, "top_p": 0.9},"Normal": {"max_tokens": 512, "temperature": 0.7, "top_p": 0.95},"Quality": {"max_tokens": 1024, "temperature": 0.5, "top_p": 0.90},"Unreal Performance": {"max_tokens": 2048, "temperature": 0.6, "top_p": 0.75},}}# Fixed system messageSYSTEM_MESSAGE = "Lake 1 Base"def respond(message,history: list,model_name,preset_name):# Switch client based on model selectionglobal clientclient = switch_client(model_name)messages = [{"role": "system", "content": SYSTEM_MESSAGE}]# Ensure history is a list of dictionariesfor val in history:if isinstance(val, dict) and 'role' in val and 'content' in val:messages.append({"role": val['role'], "content": val['content']})messages.append({"role": "user", "content": message})# Get the preset settingspreset = presets[model_name][preset_name]max_tokens = preset["max_tokens"]temperature = preset["temperature"]top_p = preset["top_p"]# Get the response from the modelresponse = client.chat_completion(messages,18 unmodified lines# Function to handle model selection and pseudonymsdef respond_with_pseudonym(message,history: list,selected_pseudonym,selected_preset):# Find the actual model name from the pseudonymmodel_name = next(model[0] for model in model_choices if model[1] == selected_pseudonym)# Call the existing respond functionresponse = respond(message, history, model_name, selected_preset)return response# Gradio Chat Interfacedemo = gr.ChatInterface(fn=respond_with_pseudonym,additional_inputs=[gr.Dropdown(choices=list(presets["mistralai/Mistral-7B-Instruct-v0.3"].keys()), label="Select Preset", value="Fast"), # Preset selection dropdowngr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]) # Pseudonym selection dropdown],)
app.py+33-28