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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -2,10 +2,10 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # Load token from secrets (never hardcode!)
6
- HF_TOKEN = os.environ.get('telemedpro')
7
 
8
- # Persona system prompt
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,39 +13,47 @@ PERSONA_MESSAGE = (
13
  "You behave politely, patiently, and with care, like a trusted family doctor."
14
  )
15
 
16
- # Create HF InferenceClient (safe)
17
  client = InferenceClient(
18
  model="m42-health/Llama3-Med42-70B",
19
  token=HF_TOKEN
20
  )
21
 
 
22
  def respond(message, history, max_tokens, temperature, top_p):
23
- # Always include persona system prompt
24
- messages = [{"role": "system", "content": PERSONA_MESSAGE}]
 
25
 
26
- # Add conversation history
27
- for h in history:
28
- messages.append({"role": "user", "content": h[0]})
29
- messages.append({"role": "assistant", "content": h[1]})
 
 
30
 
31
- # Add user message
32
- messages.append({"role": "user", "content": message})
33
 
34
- # Collect streamed response
35
- response = ""
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
45
- response += token
46
- yield response
47
 
48
- # 🔹 Chat Interface
 
 
 
 
49
  chatbot = gr.ChatInterface(
50
  respond,
51
  type="messages",
@@ -53,13 +61,15 @@ chatbot = gr.ChatInterface(
53
  gr.Slider(1, 2048, value=512, step=1, label="Max tokens"),
54
  gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
55
  gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
56
- ],
57
  )
58
 
 
59
  with gr.Blocks() as demo:
60
  with gr.Row():
61
  gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
62
  chatbot.render()
63
 
 
64
  if __name__ == "__main__":
65
  demo.launch(show_error=True)
 
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
  "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
49
+ response += token
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
57
  chatbot = gr.ChatInterface(
58
  respond,
59
  type="messages",
 
61
  gr.Slider(1, 2048, value=512, step=1, label="Max tokens"),
62
  gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
63
  gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
64
+ ]
65
  )
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)