Poonawala commited on
Commit
8790e06
·
verified ·
1 Parent(s): 60031f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -40
app.py CHANGED
@@ -1,11 +1,17 @@
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,11 +21,14 @@ def respond(
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,56 +43,132 @@ def respond(
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
- background-color: #06688E; /* Dark background */
47
- color: white; /* Text color for better visibility */
48
  }
49
 
50
- .gr-button {
51
- background-color: #42B3CE !important; /* White button color */
52
- color: black !important; /* Black text for contrast */
53
- border: none !important;
54
- padding: 8px 16px !important;
55
- border-radius: 5px !important;
56
  }
57
 
58
- .gr-button:hover {
59
- background-color: #e0e0e0 !important; /* Slightly lighter button on hover */
 
 
 
60
  }
61
 
62
- .gr-slider-container {
63
- color: white !important; /* Slider labels in white */
 
64
  }
65
- """
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  """
68
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
69
- """
70
- demo = gr.ChatInterface(
71
- respond,
72
- additional_inputs=[
73
- 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),
74
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens", visible=False),
75
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False),
76
- gr.Slider(
77
- minimum=0.1,
78
- maximum=1.0,
79
- value=0.95,
80
- step=0.05,
81
- label="Top-p (nucleus sampling)",visible=False
82
- ),
83
- ],
84
- css=css, # Pass the custom CSS here
85
- )
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if __name__ == "__main__":
89
- demo.launch(share=True)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Initialize the InferenceClient
 
 
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
  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
  temperature=temperature,
44
  top_p=top_p,
45
  ):
46
+ token = message.choices.delta.content
47
 
48
  response += token
49
  yield response
50
 
51
+ # Custom CSS to make it look like ChatGPT
 
52
  css = """
53
  body {
54
+ font-family: 'Inter', sans-serif;
55
+ background-color: #f0f0f0;
56
  }
57
 
58
+ .gradio-container {
59
+ max-width: 700px;
60
+ margin: auto;
 
 
 
61
  }
62
 
63
+ .gr-chat-container {
64
+ border: 1px solid #ddd;
65
+ border-radius: 5px;
66
+ padding: 10px;
67
+ background-color: #fff;
68
  }
69
 
70
+ .gr-message {
71
+ padding: 10px;
72
+ border-bottom: 1px solid #ddd;
73
  }
 
74
 
75
+ .gr-message:last-child {
76
+ border-bottom: none;
77
+ }
78
+
79
+ .gr-user-message {
80
+ color: #333;
81
+ background-color: #f7f7f7;
82
+ border-radius: 5px;
83
+ padding: 5px;
84
+ }
85
+
86
+ .gr-assistant-message {
87
+ color: #333;
88
+ background-color: #fff;
89
+ border-radius: 5px;
90
+ padding: 5px;
91
+ }
92
+
93
+ .gr-input {
94
+ padding: 10px;
95
+ border: 1px solid #ccc;
96
+ border-radius: 5px;
97
+ width: 100%;
98
+ }
99
+
100
+ .gr-input:focus {
101
+ border-color: #aaa;
102
+ }
103
+
104
+ .gr-button {
105
+ background-color: #4CAF50;
106
+ color: #fff;
107
+ padding: 10px 20px;
108
+ border: none;
109
+ border-radius: 5px;
110
+ cursor: pointer;
111
+ }
112
+
113
+ .gr-button:hover {
114
+ background-color: #3e8e41;
115
+ }
116
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ # Create a custom chat interface using gr.Blocks
119
+ with gr.Blocks(css=css) as demo:
120
+ gr.Markdown("# Health Assistant Chatbot")
121
+ gr.Markdown("### Ask me any health-related questions.")
122
+ chatbot = gr.Chatbot(
123
+ value=[],
124
+ show_user_avatar=False,
125
+ show_bot_avatar=False,
126
+ width="100%",
127
+ )
128
+ input_box = gr.Textbox(
129
+ label="Type your message here",
130
+ placeholder="Type your message here",
131
+ show_label=False,
132
+ )
133
+ system_message = gr.Textbox(
134
+ 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.",
135
+ label="System message",
136
+ visible=False,
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)