Raemi commited on
Commit
0eeeabc
·
verified ·
1 Parent(s): 39e5fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -49
app.py CHANGED
@@ -1,70 +1,72 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="m42-health/Llama3-Med42-70B")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
41
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  chatbot = gr.ChatInterface(
47
- respond,
48
  type="messages",
49
  additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
  ],
61
  )
62
 
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
  chatbot.render()
67
 
68
-
69
  if __name__ == "__main__":
70
- demo.launch()
 
1
  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. "
12
+ "You avoid making unsafe claims and encourage users to seek professional help when needed. "
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})
 
 
 
 
 
 
 
 
 
35
 
36
+ # Stream model output
37
+ response = ""
38
+ for msg in client.chat_completion(
39
+ messages,
40
+ max_tokens=max_tokens,
41
+ stream=True,
42
+ temperature=temperature,
43
+ top_p=top_p,
44
+ ):
45
+ if msg.choices and hasattr(msg.choices[0].delta, "content") and msg.choices[0].delta.content:
46
+ token = msg.choices[0].delta.content
47
+ response += token
48
+ yield response
49
 
50
+ except Exception as e:
51
+ yield f"⚠️ Space error: {e}"
52
 
53
+ # 🔹 Gradio Chat Interface
 
 
54
  chatbot = gr.ChatInterface(
55
+ fn=respond,
56
  type="messages",
57
  additional_inputs=[
58
+ gr.Textbox(value=SYSTEM_MESSAGE, label="System message"),
59
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
60
+ gr.Slider(minimum=0.1, maximum=2.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"),
 
 
 
 
 
 
62
  ],
63
  )
64
 
65
+ # 🔹 Layout
66
  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)