Update app.py
Browse files12345678910113 unmodified lines15161718192021222324258 unmodified lines3435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import 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,3 unmodified linestemperature,top_p,):messages = [{"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]})8 unmodified linestemperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response# CSS for styling the interfacecss = """body {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 */border: none !important;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 Doctor Assistant. Your role is to assist healthcare professionals by providing accurate, evidence-based medical information, offering treatment options, and supporting patient care. Always prioritize patient safety, provide concise answers, and clearly state that your advice does not replace a doctor's judgment. Do not diagnose or prescribe treatments without human oversight.", label="System message", visible=False),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",visible=False),],css=css, # Pass the custom CSS here)if __name__ == "__main__":demo.launch(share=True)12345678910111213141516173 unmodified lines21222324252627282930313233348 unmodified lines434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174import gradio as grfrom huggingface_hub import InferenceClient# Initialize the InferenceClientclient = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")def is_health_related(message):# Simple heuristic to check if the message is health-relatedhealth_keywords = ["health", "medical", "disease", "symptom", "treatment", "doctor", "patient", "medicine"]message = message.lower()for keyword in health_keywords:if keyword in message:return Truereturn Falsedef respond(message,3 unmodified linestemperature,top_p,):if not is_health_related(message):return "Sorry, I can't help you with that because I am just a bot who can help with health-related queries."messages = [{"role": "system", "content": system_message}]for val in history:if val:messages.append({"role": "user", "content": val})if val[1]:messages.append({"role": "assistant", "content": val[1]})8 unmodified linestemperature=temperature,top_p=top_p,):token = message.choices.delta.contentresponse += tokenyield response# Custom CSS to make it look like ChatGPTcss = """body {font-family: 'Inter', sans-serif;background-color: #f0f0f0;}.gradio-container {max-width: 700px;margin: auto;}.gr-chat-container {border: 1px solid #ddd;border-radius: 5px;padding: 10px;background-color: #fff;}.gr-message {padding: 10px;border-bottom: 1px solid #ddd;}.gr-message:last-child {border-bottom: none;}.gr-user-message {color: #333;background-color: #f7f7f7;border-radius: 5px;padding: 5px;}.gr-assistant-message {color: #333;background-color: #fff;border-radius: 5px;padding: 5px;}.gr-input {padding: 10px;border: 1px solid #ccc;border-radius: 5px;width: 100%;}.gr-input:focus {border-color: #aaa;}.gr-button {background-color: #4CAF50;color: #fff;padding: 10px 20px;border: none;border-radius: 5px;cursor: pointer;}.gr-button:hover {background-color: #3e8e41;}"""# Create a custom chat interface using gr.Blockswith gr.Blocks(css=css) as demo:gr.Markdown("# Health Assistant Chatbot")gr.Markdown("### Ask me any health-related questions.")chatbot = gr.Chatbot(value=[],show_user_avatar=False,show_bot_avatar=False,width="100%",)input_box = gr.Textbox(label="Type your message here",placeholder="Type your message here",show_label=False,)system_message = gr.Textbox(value="You are a virtual Doctor Assistant. Your role is to assist healthcare professionals by providing accurate, evidence-based medical information, offering treatment options, and supporting patient care. Always prioritize patient safety, provide concise answers, and clearly state that your advice does not replace a doctor's judgment. Do not diagnose or prescribe treatments without human oversight.",label="System message",visible=False,)max_tokens = gr.Slider(minimum=1,maximum=2048,value=512,step=1,label="Max new tokens",visible=False,)temperature = gr.Slider(minimum=0.1,maximum=4.0,value=0.7,step=0.1,label="Temperature",visible=False,)top_p = gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",visible=False,)def update_chat(message, history, system_message, max_tokens, temperature, top_p):response = respond(message, history, system_message, max_tokens, temperature, top_p)return chatbot.update(value=history + [(message, response)])input_box.submit(update_chat,inputs=[input_box, chatbot, system_message, max_tokens, temperature, top_p],outputs=chatbot,)if __name__ == "__main__":demo.launch(share=True)
app.py+125-40