Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
|
|
|
|
5 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
6 |
|
7 |
-
def is_health_related(message):
|
8 |
-
# Simple heuristic to check if the message is health-related
|
9 |
-
health_keywords = ["health", "medical", "disease", "symptom", "treatment", "doctor", "patient", "medicine"]
|
10 |
-
message = message.lower()
|
11 |
-
for keyword in health_keywords:
|
12 |
-
if keyword in message:
|
13 |
-
return True
|
14 |
-
return False
|
15 |
|
16 |
def respond(
|
17 |
message,
|
@@ -21,14 +15,11 @@ def respond(
|
|
21 |
temperature,
|
22 |
top_p,
|
23 |
):
|
24 |
-
if not is_health_related(message):
|
25 |
-
return "Sorry, I can't help you with that because I am just a bot who can help with health-related queries."
|
26 |
-
|
27 |
messages = [{"role": "system", "content": system_message}]
|
28 |
|
29 |
for val in history:
|
30 |
-
if val:
|
31 |
-
messages.append({"role": "user", "content": val})
|
32 |
if val[1]:
|
33 |
messages.append({"role": "assistant", "content": val[1]})
|
34 |
|
@@ -43,12 +34,13 @@ def respond(
|
|
43 |
temperature=temperature,
|
44 |
top_p=top_p,
|
45 |
):
|
46 |
-
token = message.choices.delta.content
|
47 |
|
48 |
response += token
|
49 |
yield response
|
50 |
|
51 |
-
|
|
|
52 |
css = """
|
53 |
body {
|
54 |
font-family: 'Inter', sans-serif;
|
@@ -115,60 +107,26 @@ body {
|
|
115 |
}
|
116 |
"""
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
max_tokens = gr.Slider(
|
139 |
-
minimum=1,
|
140 |
-
maximum=2048,
|
141 |
-
value=512,
|
142 |
-
step=1,
|
143 |
-
label="Max new tokens",
|
144 |
-
visible=False,
|
145 |
-
)
|
146 |
-
temperature = gr.Slider(
|
147 |
-
minimum=0.1,
|
148 |
-
maximum=4.0,
|
149 |
-
value=0.7,
|
150 |
-
step=0.1,
|
151 |
-
label="Temperature",
|
152 |
-
visible=False,
|
153 |
-
)
|
154 |
-
top_p = gr.Slider(
|
155 |
-
minimum=0.1,
|
156 |
-
maximum=1.0,
|
157 |
-
value=0.95,
|
158 |
-
step=0.05,
|
159 |
-
label="Top-p (nucleus sampling)",
|
160 |
-
visible=False,
|
161 |
-
)
|
162 |
-
|
163 |
-
def update_chat(message, history, system_message, max_tokens, temperature, top_p):
|
164 |
-
response = respond(message, history, system_message, max_tokens, temperature, top_p)
|
165 |
-
return chatbot.update(value=history + [(message, response)])
|
166 |
-
|
167 |
-
input_box.submit(
|
168 |
-
update_chat,
|
169 |
-
inputs=[input_box, chatbot, system_message, max_tokens, temperature, top_p],
|
170 |
-
outputs=chatbot,
|
171 |
-
)
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
-
demo.launch(share=True)
|
|
|
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("meta-llama/Llama-3.2-3B-Instruct")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
for val in history:
|
21 |
+
if val[0]:
|
22 |
+
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
|
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
+
token = message.choices[0].delta.content
|
38 |
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
+
|
43 |
+
# CSS for styling the interface
|
44 |
css = """
|
45 |
body {
|
46 |
font-family: 'Inter', sans-serif;
|
|
|
107 |
}
|
108 |
"""
|
109 |
|
110 |
+
"""
|
111 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
112 |
+
"""
|
113 |
+
demo = gr.ChatInterface(
|
114 |
+
respond,
|
115 |
+
additional_inputs=[
|
116 |
+
gr.Textbox(value="You are a virtual Doctor Assistant. Your role is to assist healthcare professionals by providing accurate, evidence-based medical information, offering treatment options, and supporting patient care. Always prioritize patient safety, provide concise answers, and clearly state that your advice does not replace a doctor's judgment. Do not diagnose or prescribe treatments without human oversight.", label="System message", visible=False),
|
117 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),
|
118 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
|
119 |
+
gr.Slider(
|
120 |
+
minimum=0.1,
|
121 |
+
maximum=1.0,
|
122 |
+
value=0.95,
|
123 |
+
step=0.05,
|
124 |
+
label="Top-p (nucleus sampling)",visible=False
|
125 |
+
),
|
126 |
+
],
|
127 |
+
css=css, # Pass the custom CSS here
|
128 |
+
)
|
129 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
+
demo.launch(share=True)
|