Raemi commited on
Commit
01c7111
·
verified ·
1 Parent(s): 436ee5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -39
app.py CHANGED
@@ -1,9 +1,5 @@
1
  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') # Add your token in Space Settings → Secrets
7
 
8
  # 🔹 Fixed persona system message
9
  PERSONA_MESSAGE = (
@@ -13,54 +9,66 @@ PERSONA_MESSAGE = (
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 (non-streaming for stability)
20
- def respond(message, history, max_tokens=512, temperature=0.7, top_p=0.95):
21
- try:
22
- messages = [{"role": "system", "content": PERSONA_MESSAGE}]
 
 
 
 
 
23
 
24
- # Append previous conversation safely
25
- if history:
26
- for h in history:
27
- user_msg = h[0] if h[0] else ""
28
- ai_msg = h[1] if h[1] else ""
29
- messages.append({"role": "user", "content": user_msg})
30
- messages.append({"role": "assistant", "content": ai_msg})
31
 
32
- # Append current user message
33
- messages.append({"role": "user", "content": message})
 
 
 
 
 
34
 
35
- # 🔹 Non-streaming call for stability
36
- result = client.chat_completion(
37
- messages,
38
- max_tokens=max_tokens,
39
- temperature=temperature,
40
- top_p=top_p
41
- )
42
 
43
- return result.choices[0].message.content
44
 
45
- except Exception as e:
46
- return f"⚠️ Space error: {e}"
 
 
 
 
 
 
 
 
 
 
47
 
48
- # 🔹 Gradio Chat Interface
49
  chatbot = gr.ChatInterface(
50
  respond,
51
  type="messages",
52
  additional_inputs=[
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
- # 🔹 Layout
60
  with gr.Blocks() as demo:
61
- gr.Markdown("## 🩺 AI Health Mentor — Dr. Alex")
 
 
62
  chatbot.render()
63
 
64
- # 🔹 Launch Space
65
  if __name__ == "__main__":
66
- demo.launch(show_error=True)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
3
 
4
  # 🔹 Fixed persona system message
5
  PERSONA_MESSAGE = (
 
9
  "You behave politely, patiently, and with care, like a trusted family doctor."
10
  )
11
 
12
+ def respond(
13
+ message,
14
+ history: list[dict[str, str]],
15
+ max_tokens,
16
+ temperature,
17
+ top_p,
18
+ hf_token: gr.OAuthToken,
19
+ ):
20
+ """
21
+ Respond function for Gradio ChatInterface. Uses Hugging Face InferenceClient with fixed persona.
22
+ """
23
+ client = InferenceClient(token=hf_token.token, model="m42-health/Llama3-Med42-70B")
24
 
25
+ # Always include persona as system role
26
+ messages = [{"role": "system", "content": PERSONA_MESSAGE}]
 
 
 
 
 
27
 
28
+ # Add conversation history
29
+ if history:
30
+ for h in history:
31
+ # Ensure history is in proper format
32
+ messages.append({"role": "user", "content": h["user"]})
33
+ if "assistant" in h:
34
+ messages.append({"role": "assistant", "content": h["assistant"]})
35
 
36
+ # Append current user message
37
+ messages.append({"role": "user", "content": message})
 
 
 
 
 
38
 
39
+ response = ""
40
 
41
+ # Stream model output token by token
42
+ for msg in client.chat_completion(
43
+ messages,
44
+ max_tokens=max_tokens,
45
+ stream=True,
46
+ temperature=temperature,
47
+ top_p=top_p,
48
+ ):
49
+ if len(msg.choices) > 0 and hasattr(msg.choices[0].delta, "content"):
50
+ token = msg.choices[0].delta.content
51
+ response += token
52
+ yield response
53
 
54
+ # 🔹 Chatbot Interface
55
  chatbot = gr.ChatInterface(
56
  respond,
57
  type="messages",
58
  additional_inputs=[
59
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
62
+ ],
63
  )
64
 
65
+ # 🔹 App Layout
66
  with gr.Blocks() as demo:
67
+ with gr.Sidebar():
68
+ gr.LoginButton() # Hugging Face login
69
+ gr.Markdown("### 🩺 Persona: Dr. Alex\nThis chatbot behaves as a helpful doctor.")
70
  chatbot.render()
71
 
72
+ # 🔹 Run app
73
  if __name__ == "__main__":
74
+ demo.launch()