Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| # ๐น Load HF token from Space Secrets | |
| HF_TOKEN = os.environ.get('telemedpro') | |
| # ๐น Fixed persona system message | |
| SYSTEM_MESSAGE = ( | |
| "You are Dr. Alex, a highly knowledgeable yet empathetic doctor. " | |
| "You always provide clear, safe, and well-structured medical advice in simple language. " | |
| "You avoid making unsafe claims and encourage users to seek professional help when needed. " | |
| "You behave politely, patiently, and with care, like a trusted family doctor." | |
| ) | |
| # ๐น Initialize InferenceClient once | |
| client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B") | |
| # ๐น Respond function | |
| def respond(message, history, system_message=SYSTEM_MESSAGE, max_tokens=512, temperature=0.7, top_p=0.95): | |
| try: | |
| # Start with system message | |
| messages = [{"role": "system", "content": system_message}] | |
| # Append previous conversation safely | |
| if history: | |
| messages.extend(history) # โ safe, don't manipulate | |
| # Append current user message | |
| messages.append({"role": "user", "content": message}) | |
| # Stream model output | |
| response = "" | |
| for msg in client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| stream=True, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ): | |
| if msg.choices and hasattr(msg.choices[0].delta, "content") and msg.choices[0].delta.content: | |
| token = msg.choices[0].delta.content | |
| response += token | |
| yield response | |
| except Exception as e: | |
| yield f"โ ๏ธ Space error: {e}" | |
| # ๐น Gradio Chat Interface | |
| chatbot = gr.ChatInterface( | |
| fn=respond, | |
| type="messages", | |
| additional_inputs=[ | |
| gr.Textbox(value=SYSTEM_MESSAGE, label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"), | |
| gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), | |
| ], | |
| ) | |
| # ๐น Layout | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ๐ฉบ AI Health Mentor โ Dr. Alex") | |
| chatbot.render() | |
| # ๐น Launch | |
| if __name__ == "__main__": | |
| demo.launch(show_error=True) | |