|
|
import gradio as gr |
|
|
from huggingface_hub import InferenceClient |
|
|
import os |
|
|
|
|
|
|
|
|
HF_TOKEN = os.environ.get('telemedpro') |
|
|
|
|
|
|
|
|
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." |
|
|
) |
|
|
|
|
|
|
|
|
client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B") |
|
|
|
|
|
|
|
|
def respond(message, history, system_message=SYSTEM_MESSAGE, max_tokens=512, temperature=0.7, top_p=0.95): |
|
|
try: |
|
|
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
|
|
|
|
|
if history: |
|
|
messages.extend(history) |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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"), |
|
|
], |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## ๐ฉบ AI Health Mentor โ Dr. Alex") |
|
|
chatbot.render() |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(show_error=True) |
|
|
|