Update app.py
Browse files1234564596789101415161112137 unmodified lines2122233031323334353637383940412425262728293031323334353637383940414243441 unmodified line464748494950511 unmodified line535455575657586162636459606768697071727361626364656667686970717273747576777879808182818384import 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( model_name, message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p,): messages = [{"role": "system", "content": system_message}]
7 unmodified lines
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.content
response += token yield response
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.content response += token elif 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 line background-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 line padding: 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),# 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 response gr.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