Update app.py
Browse files123456789101112131415161718197 unmodified lines2728293031323334353637383940414243441 unmodified line464748495051521 unmodified line545556575859606162636465666768697071727374757677787980818283import 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"""client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]7 unmodified linesresponse = ""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# CSS for styling the interfacecss = """1 unmodified linebackground-color: #06688E; /* Dark background */color: white; /* Text color for better visibility */}.gr-button {background-color: #42B3CE !important; /* White button color */color: black !important; /* Black text for contrast */1 unmodified linepadding: 8px 16px !important;border-radius: 5px !important;}.gr-button:hover {background-color: #e0e0e0 !important; /* Slightly lighter button on hover */}.gr-slider-container {color: white !important; /* Slider labels in white */}""""""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a virtual health assistant designed to provide accurate and reliable information related to health, wellness, and medical topics. Your primary goal is to assist users with their health-related queries, offer general guidance, and suggest when to consult a licensed medical professional.If a user asks a question that is unrelated to health, wellness, or medical topics, respond politely but firmly with:'I'm sorry, I can't help with that because I am a virtual health assistant designed to assist with health-related needs. Please let me know if you have any health-related questions.'Never provide advice or information outside the health domain. Remain professional, empathetic, and clear in all responses. Always prioritize user safety and encourage professional medical consultation for critical or complex health concerns..", label="System message", visible=False),gr.Button("Chatgpt"),gr.Button("Llama"),gr.Button("Claude"),],css=css, # Pass the custom CSS here)if __name__ == "__main__":demo.launch(share=True)123456789101112137 unmodified lines2122232425262728293031323334353637383940414243441 unmodified line4647484950511 unmodified line5354555657585960616263646566676869707172737475767778798081828384import gradio as grfrom huggingface_hub import InferenceClientclient = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")def respond(model_name,message,history: list[tuple[str, str]],system_message,):messages = [{"role": "system", "content": system_message}]7 unmodified linesresponse = ""if model_name == "Llama":for message in client.chat_completion(messages,max_tokens=512,stream=True,temperature=0.7,top_p=0.95,):token = message.choices[0].delta.contentresponse += tokenelif model_name == "Chatgpt":response = "ChatGPT functionality is not yet implemented."elif model_name == "Claude":response = "Claude functionality is not yet implemented."else:response = "Model not recognized."return response# CSS for styling the interfacecss = """1 unmodified linebackground-color: #06688E; /* Dark background */color: white; /* Text color for better visibility */}.gr-button {background-color: #42B3CE !important; /* White button color */color: black !important; /* Black text for contrast */1 unmodified linepadding: 8px 16px !important;border-radius: 5px !important;}.gr-button:hover {background-color: #e0e0e0 !important; /* Slightly lighter button on hover */}"""# Define the button click handler to hide the dropdowndef on_button_click(model_name, message, history, system_message, dropdown_state):return respond(model_name, message, history, system_message), gr.update(visible=False) # Hide dropdown after click# Define the Gradio interface with buttons and model selectiondemo = gr.Interface(fn=on_button_click,inputs=[gr.Textbox(value="Hello!", label="User Message"),gr.Textbox(value="You are a virtual health assistant. Your primary goal is to assist with health-related queries.", label="System Message", visible=False),gr.Button("Chatgpt"),gr.Button("Llama"),gr.Button("Claude"),gr.State() # To track the state of dropdown visibility],outputs=["text", # For the model's responsegr.Textbox(visible=False) # Dropdown (this textbox will be hidden after the button is clicked)],css=css, # Pass the custom CSS here)if __name__ == "__main__":demo.launch(share=True)
app.py+34-33