Raemi commited on
Commit
4e1334f
ยท
verified ยท
1 Parent(s): 0eeeabc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -11
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 (set in Space settings โ†’ Secrets)
6
  HF_TOKEN = os.environ.get('telemedpro')
7
 
8
- # ๐Ÿ”น Default system persona 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,22 +13,18 @@ SYSTEM_MESSAGE = (
13
  "You behave politely, patiently, and with care, like a trusted family doctor."
14
  )
15
 
16
- # ๐Ÿ”น Initialize client once
17
  client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B")
18
 
19
- # ๐Ÿ”น Respond function (non-OAuthToken, stable streaming)
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 (Gradio handles history as list of [user, assistant])
26
  if history:
27
- for h in history:
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 Space
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)