Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ๐น Load HF token from Space Secrets
|
| 6 |
HF_TOKEN = os.environ.get('telemedpro')
|
| 7 |
|
| 8 |
-
# ๐น
|
| 9 |
SYSTEM_MESSAGE = (
|
| 10 |
"You are Dr. Alex, a highly knowledgeable yet empathetic doctor. "
|
| 11 |
"You always provide clear, safe, and well-structured medical advice in simple language. "
|
|
@@ -13,22 +13,18 @@ SYSTEM_MESSAGE = (
|
|
| 13 |
"You behave politely, patiently, and with care, like a trusted family doctor."
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# ๐น Initialize
|
| 17 |
client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B")
|
| 18 |
|
| 19 |
-
# ๐น Respond function
|
| 20 |
def respond(message, history, system_message=SYSTEM_MESSAGE, max_tokens=512, temperature=0.7, top_p=0.95):
|
| 21 |
try:
|
| 22 |
# Start with system message
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
|
| 25 |
-
# Append previous conversation
|
| 26 |
if history:
|
| 27 |
-
|
| 28 |
-
user_msg = h[0] if h[0] else ""
|
| 29 |
-
ai_msg = h[1] if h[1] else ""
|
| 30 |
-
messages.append({"role": "user", "content": user_msg})
|
| 31 |
-
messages.append({"role": "assistant", "content": ai_msg})
|
| 32 |
|
| 33 |
# Append current user message
|
| 34 |
messages.append({"role": "user", "content": message})
|
|
@@ -67,6 +63,6 @@ with gr.Blocks() as demo:
|
|
| 67 |
gr.Markdown("## ๐ฉบ AI Health Mentor โ Dr. Alex")
|
| 68 |
chatbot.render()
|
| 69 |
|
| 70 |
-
# ๐น Launch
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch(show_error=True)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# ๐น Load HF token from Space Secrets
|
| 6 |
HF_TOKEN = os.environ.get('telemedpro')
|
| 7 |
|
| 8 |
+
# ๐น Fixed persona system message
|
| 9 |
SYSTEM_MESSAGE = (
|
| 10 |
"You are Dr. Alex, a highly knowledgeable yet empathetic doctor. "
|
| 11 |
"You always provide clear, safe, and well-structured medical advice in simple language. "
|
|
|
|
| 13 |
"You behave politely, patiently, and with care, like a trusted family doctor."
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# ๐น Initialize InferenceClient once
|
| 17 |
client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B")
|
| 18 |
|
| 19 |
+
# ๐น Respond function
|
| 20 |
def respond(message, history, system_message=SYSTEM_MESSAGE, max_tokens=512, temperature=0.7, top_p=0.95):
|
| 21 |
try:
|
| 22 |
# Start with system message
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
|
| 25 |
+
# Append previous conversation safely
|
| 26 |
if history:
|
| 27 |
+
messages.extend(history) # โ
safe, don't manipulate
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Append current user message
|
| 30 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 63 |
gr.Markdown("## ๐ฉบ AI Health Mentor โ Dr. Alex")
|
| 64 |
chatbot.render()
|
| 65 |
|
| 66 |
+
# ๐น Launch
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch(show_error=True)
|