Raemi commited on
Commit
800ea60
·
verified ·
1 Parent(s): bb6c44d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -2,10 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # 🔹 Hardcoded token for internal use (or use os.environ for safety)
6
- HF_TOKEN = os.getenv('telemedpro')
7
 
8
- # Persona system message
9
  PERSONA_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,36 +13,32 @@ PERSONA_MESSAGE = (
13
  "You behave politely, patiently, and with care, like a trusted family doctor."
14
  )
15
 
16
- # Create HF InferenceClient
17
- client = InferenceClient(
18
- model="m42-health/Llama3-Med42-70B",
19
- token=HF_TOKEN
20
- )
21
 
22
- # Respond function with safe error handling
23
  def respond(message, history, max_tokens, temperature, top_p):
24
  try:
25
- # Initialize messages with persona
26
  messages = [{"role": "system", "content": PERSONA_MESSAGE}]
27
 
28
- # Add conversation history safely
29
- for h in history:
30
- user_msg = h[0] if h[0] is not None else ""
31
- ai_msg = h[1] if h[1] is not None else ""
32
- messages.append({"role": "user", "content": user_msg})
33
- messages.append({"role": "assistant", "content": ai_msg})
 
34
 
35
  # Add current user message
36
  messages.append({"role": "user", "content": message})
37
 
38
- # Stream AI response token by token
39
  response = ""
 
40
  for msg in client.chat_completion(
41
  messages,
42
  max_tokens=max_tokens,
43
  stream=True,
44
  temperature=temperature,
45
- top_p=top_p
46
  ):
47
  if msg.choices and msg.choices[0].delta.content:
48
  token = msg.choices[0].delta.content
@@ -50,7 +46,6 @@ def respond(message, history, max_tokens, temperature, top_p):
50
  yield response
51
 
52
  except Exception as e:
53
- # Return error as a string so Space never crashes
54
  yield f"⚠️ Space error: {e}"
55
 
56
  # 🔹 Gradio Chat Interface
@@ -66,10 +61,9 @@ chatbot = gr.ChatInterface(
66
 
67
  # 🔹 Layout
68
  with gr.Blocks() as demo:
69
- with gr.Row():
70
- gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
71
  chatbot.render()
72
 
73
- # 🔹 Launch the Space safely
74
  if __name__ == "__main__":
75
  demo.launch(show_error=True)
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # 🔹 Load HF token from secret
6
+ HF_TOKEN = os.environ.get('telemedpro') # store your HF token in Space Secrets
7
 
8
+ # 🔹 Fixed persona system message
9
  PERSONA_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
+ # 🔹 Create InferenceClient once
17
+ client = InferenceClient(token=HF_TOKEN, model="m42-health/Llama3-Med42-70B")
 
 
 
18
 
 
19
  def respond(message, history, max_tokens, temperature, top_p):
20
  try:
 
21
  messages = [{"role": "system", "content": PERSONA_MESSAGE}]
22
 
23
+ # Add previous conversation
24
+ if history:
25
+ for h in history:
26
+ user_msg = h[0] if h[0] else ""
27
+ ai_msg = h[1] if h[1] else ""
28
+ messages.append({"role": "user", "content": user_msg})
29
+ messages.append({"role": "assistant", "content": ai_msg})
30
 
31
  # Add current user message
32
  messages.append({"role": "user", "content": message})
33
 
 
34
  response = ""
35
+ # Stream model output token by token
36
  for msg in client.chat_completion(
37
  messages,
38
  max_tokens=max_tokens,
39
  stream=True,
40
  temperature=temperature,
41
+ top_p=top_p,
42
  ):
43
  if msg.choices and msg.choices[0].delta.content:
44
  token = msg.choices[0].delta.content
 
46
  yield response
47
 
48
  except Exception as e:
 
49
  yield f"⚠️ Space error: {e}"
50
 
51
  # 🔹 Gradio Chat Interface
 
61
 
62
  # 🔹 Layout
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
 
65
  chatbot.render()
66
 
67
+ # 🔹 Launch Space
68
  if __name__ == "__main__":
69
  demo.launch(show_error=True)