Update app.py
Browse files123456789103 unmodified lines1415161718192021222324252 unmodified lines28293031323334353637383940414243444546474849505152536 unmodified lines606162636465664 unmodified lines7172737475767778798081828384858612 unmodified lines99100101102import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""# Default client with the first modelclient = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")3 unmodified linesdef respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,model_name # Add this parameter for model selection):# Switch client based on model selectionglobal client2 unmodified linesmessages = [{"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.contentresponse += tokenyield response# Model names and their pseudonymsmodel_choices = [6 unmodified lines# Function to handle model selection and pseudonymsdef respond_with_pseudonym(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,4 unmodified linesmodel_name = next(model[0] for model in model_choices if model[1] == selected_pseudonym)# Call the existing respond functionresponse = list(respond(message, history, system_message, max_tokens, temperature, top_p, model_name))# Add pseudonym at the end of the responseresponse[-1] += f"\n\n[Response generated by: {selected_pseudonym}]"return response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond_with_pseudonym,additional_inputs=[12 unmodified lines)if __name__ == "__main__":demo.launch()1234563 unmodified lines1011121314151617181920212 unmodified lines242526272829303132333435363738394041424344456 unmodified lines525354555657584 unmodified lines636465666768697071727374757612 unmodified lines89909192import gradio as grfrom huggingface_hub import InferenceClient# Default client with the first modelclient = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")3 unmodified linesdef respond(message,history: list[dict],system_message,max_tokens,temperature,top_p,model_name):# Switch client based on model selectionglobal client2 unmodified linesmessages = [{"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,max_tokens=max_tokens,temperature=temperature,top_p=top_p,)# Extract the content from the responsefinal_response = response.choices[0].message['content']return final_response# Model names and their pseudonymsmodel_choices = [6 unmodified lines# Function to handle model selection and pseudonymsdef respond_with_pseudonym(message,history: list[dict],system_message,max_tokens,temperature,4 unmodified linesmodel_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)# Add pseudonym at the end of the responseresponse += f"\n\n[Response generated by: {selected_pseudonym}]"return response# Gradio Chat Interfacedemo = gr.ChatInterface(respond_with_pseudonym,additional_inputs=[12 unmodified lines)if __name__ == "__main__":demo.launch()
app.py+15-25