Update app.py
Browse files112 unmodified lines113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194112 unmodified lines],)if __name__ == "__main__":demo.launch()}# System messages for each modelsystem_messages = {"mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base but mine is User","BICORP/Lake-1-Advanced": "Your name is Lake 1 Advanced [Alpha] but mine is User or what I will type as my name"}# Model names and their pseudonymsmodel_choices = [("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base"),("BICORP/Lake-1-Advanced", "Lake 1 Advanced [Alpha]")]# Extract pseudonyms for the dropdownpseudonyms = [model[1] for model in model_choices]def respond(message, history: list, model_name, preset_name):"""Generate a response from the selected model based on the user's message and chat history."""model = models[model_name]tokenizer = tokenizers[model_name]system_message = system_messages[model_name]messages = [{"role": "system", "content": system_message}]for 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})# Prepare the input for the modelinputs = tokenizer([messages], return_tensors="pt", padding=True, truncation=True)# Get the preset settingspreset = presets[model_name][preset_name]max_new_tokens = preset["max_new_tokens"]temperature = preset["temperature"]top_p = preset["top_p"]# Generate the response from the modelresponse = model.generate(input_ids=inputs['input_ids'],attention_mask=inputs['attention_mask'],max_new_tokens=max_new_tokens,temperature=temperature,top_p=top_p,)# Decode the generated responsefinal_response = tokenizer.decode(response[0], skip_special_tokens=True)return final_responsedef respond_with_pseudonym(message, history: list, selected_model, selected_preset):"""Handle the user's message and determine which model to use based on the selected pseudonym."""try:model_name = next(model[0] for model in model_choices if model[1] == selected_model)except StopIteration:return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."return respond(message, history, model_name, selected_preset)# Gradio Chat Interfacedemo = gr.ChatInterface(fn=respond_with_pseudonym,additional_inputs=[gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]),gr.Dropdown(choices=list(presets[model_choices[0][0]].keys()), label="Select Preset", value="Fast")],)if __name__ == "__main__":demo.launch()112 unmodified lines113114115116117112 unmodified lines],)if __name__ == "__main__":demo.launch()
app.py-77