MaxChat commited on
Commit
db0df04
·
verified ·
1 Parent(s): cbb2423

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -46
app.py CHANGED
@@ -1,12 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- 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
6
- """
7
- #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
- client = InferenceClient("stanford-crfm/meditron-7b")
9
-
10
 
11
  def respond(
12
  message,
@@ -19,67 +14,48 @@ def respond(
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
  for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
 
26
 
27
  messages.append({"role": "user", "content": message})
28
 
29
  response = ""
30
 
31
- for message in client.chat_completion(
32
  messages,
33
  max_tokens=max_tokens,
34
  stream=True,
35
  temperature=temperature,
36
  top_p=top_p,
37
  ):
38
- token = message.choices[0].delta.content
39
-
 
 
40
  response += token
41
  yield response
42
 
 
 
 
 
 
 
 
 
43
 
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
- gr.Textbox(
51
- value=(
52
- "You are a knowledgeable and professional medical assistant specializing in medications, including their indications, dosages, "
53
- "side effects, interactions, and safety precautions.\n\n"
54
- "Your role is to provide clear, medically accurate, and responsible information to users asking about medications.\n\n"
55
- "Important guidelines:\n"
56
- "1. Always consider the age of the user before discussing dosage or suitability. If age is not provided, politely ask for it before offering detailed advice.\n"
57
- "2. Clearly distinguish between adult, pediatric, and geriatric recommendations.\n"
58
- "3. Use easy-to-understand language while remaining professional and neutral.\n"
59
- "4. Do not make personalized recommendations, diagnose, or prescribe.\n"
60
- "5. For serious concerns or symptoms, encourage the user to consult a healthcare provider.\n"
61
- "6. Include common side effects, precautions, and interactions if relevant to the question.\n"
62
- "7. Politely decline to answer if the query lacks enough information to ensure safety.\n"
63
- "8. Use only trusted medical sources in the style of your response (e.g., WHO, FDA, Mayo Clinic, MedlinePlus).\n\n"
64
- "Always include this disclaimer at the end of your response:\n"
65
- "\"This information is for educational purposes only and does not substitute professional medical advice. "
66
- "Please consult a licensed healthcare provider before taking any medication, especially in children, older adults, or people with medical conditions.\""
67
- ),
68
- label="System message",
69
- lines=12,
70
- ),
71
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
72
- gr.Slider(minimum=0.1, maximum=4.0, value=0.2, step=0.1, label="Temperature"),
73
- gr.Slider(
74
- minimum=0.1,
75
- maximum=1.0,
76
- value=1.0,
77
- step=0.05,
78
- label="Top-p (nucleus sampling)",
79
- ),
80
  ],
81
  )
82
 
83
-
84
  if __name__ == "__main__":
85
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
5
 
6
  def respond(
7
  message,
 
14
  messages = [{"role": "system", "content": system_message}]
15
 
16
  for val in history:
17
+ if val and len(val) == 2:
18
+ if val[0]:
19
+ messages.append({"role": "user", "content": val[0]})
20
+ if val[1]:
21
+ messages.append({"role": "assistant", "content": val[1]})
22
 
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
 
27
+ for message_chunk in client.chat_completion(
28
  messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
+ delta = message_chunk.choices[0].delta
35
+ if delta is None or delta.content is None:
36
+ continue
37
+ token = delta.content
38
  response += token
39
  yield response
40
 
41
+ # ✅ 5-line concise system prompt
42
+ default_system_prompt = (
43
+ "You are a professional medical assistant specializing in medications. "
44
+ "Always check the user's age before giving any dosage or advice. "
45
+ "Provide clear, factual, and safe responses based on trusted sources like WHO or FDA. "
46
+ "Do not diagnose, prescribe, or make personal medical recommendations. "
47
+ "Always include a medical disclaimer at the end."
48
+ )
49
 
 
 
 
50
  demo = gr.ChatInterface(
51
  respond,
52
  additional_inputs=[
53
+ gr.Textbox(value=default_system_prompt, label="System message", lines=5),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
57
  ],
58
  )
59
 
 
60
  if __name__ == "__main__":
61
  demo.launch()