Update app.py
Browse files
app.py
CHANGED
@@ -47,19 +47,14 @@ demo = gr.ChatInterface(
|
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
gr.Textbox(value='''
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
-
|
57 |
-
|
58 |
-
- Treatment approaches and procedures
|
59 |
-
- Preventive care and wellness
|
60 |
-
- Emergency guidance
|
61 |
-
- Mental health support
|
62 |
-
- Nutritional advice
|
63 |
|
64 |
QUERY TYPES AND RESPONSE FORMATS:
|
65 |
|
@@ -196,7 +191,66 @@ IMPORTANT NOTES:
|
|
196 |
<start_of_turn>user
|
197 |
{query}<end_of_turn>
|
198 |
<start_of_turn>model
|
199 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
201 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
202 |
gr.Slider(
|
|
|
47 |
respond,
|
48 |
additional_inputs=[
|
49 |
gr.Textbox(value='''
|
50 |
+
# Update MEDICAL_PROMPT to be more restrictive
|
51 |
+
MEDICAL_PROMPT = PromptTemplate(
|
52 |
+
input_variables=["query"],
|
53 |
+
template="""<bos><start_of_turn>system
|
54 |
+
You are Gemma, a medical AI assistant. You MUST ONLY answer health and medical-related questions.
|
55 |
+
Your responses should be professional, accurate, and focused on medical topics only.
|
56 |
+
For any non-medical questions, respond with a redirection to medical topics.
|
57 |
+
For medication queries, provide general information and recommend consulting a healthcare professional.
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
QUERY TYPES AND RESPONSE FORMATS:
|
60 |
|
|
|
191 |
<start_of_turn>user
|
192 |
{query}<end_of_turn>
|
193 |
<start_of_turn>model
|
194 |
+
"""
|
195 |
+
)
|
196 |
+
|
197 |
+
# Update is_medical_query to be more comprehensive
|
198 |
+
def is_medical_query(query):
|
199 |
+
medical_keywords_and_greetings = [
|
200 |
+
"health", "disease", "symptom", "doctor", "medicine", "medical", "treatment",
|
201 |
+
"hospital", "clinic", "diagnosis", "patient", "drug", "prescription", "therapy",
|
202 |
+
"cancer", "diabetes", "heart", "blood", "pain", "surgery", "vaccine", "infection",
|
203 |
+
"allergy", "diet", "nutrition", "vitamin", "exercise", "mental health", "depression",
|
204 |
+
"anxiety", "disorder", "syndrome", "chronic", "acute", "emergency", "pharmacy",
|
205 |
+
"dosage", "side effect", "contraindication", "body", "organ", "immune", "virus",
|
206 |
+
"bacterial", "fungal", "parasite", "genetic", "hereditary", "congenital", "prenatal",
|
207 |
+
"headaches", "ache", "stomach ache", "skin", "head", "arm", "leg", "chest", "back", "throat", "eye", "ear", "nose", "mouth"
|
208 |
+
]
|
209 |
+
|
210 |
+
# Remove greetings from the keyword list
|
211 |
+
medical_keywords = [word for word in medical_keywords_and_greetings if word not in ["hello", "hi", "greetings", "good morning", "good afternoon", "good evening", "hey"]]
|
212 |
+
|
213 |
+
query_lower = query.lower()
|
214 |
+
return any(keyword in query_lower for keyword in medical_keywords)
|
215 |
+
|
216 |
+
# Update chat_with_model to enforce medical-only responses
|
217 |
+
def chat_with_model(message, history):
|
218 |
+
try:
|
219 |
+
context = "\n".join([f"User: {msg}\nAssistant: {res}" for msg, res in history])
|
220 |
+
full_query = f"{context}\nUser: {message}"
|
221 |
+
|
222 |
+
if not is_medical_query(full_query):
|
223 |
+
return "I'm specialized in medical topics only. I cannot answer this question. How can I assist with a health-related concern instead?"
|
224 |
+
|
225 |
+
response = medical_chain.run(query=full_query)
|
226 |
+
clean_response = response.split("<end_of_turn>")[0].strip()
|
227 |
+
|
228 |
+
# Check if the response is medical-related
|
229 |
+
if not is_medical_query(clean_response):
|
230 |
+
return "I can only provide information on medical topics. Please ask a medical question."
|
231 |
+
|
232 |
+
return clean_response
|
233 |
+
|
234 |
+
except Exception as e:
|
235 |
+
return f"I apologize, but I encountered an error: {str(e)}. Please try again."
|
236 |
+
|
237 |
+
# Update Gradio examples to be medical-specific
|
238 |
+
iface = gr.ChatInterface(
|
239 |
+
fn=chat_with_model,
|
240 |
+
title="MedexDroid - Medical Assistant",
|
241 |
+
examples=[
|
242 |
+
"What are the symptoms of diabetes?",
|
243 |
+
"How can I improve my diet for heart health?",
|
244 |
+
"What is the treatment for a migraine?",
|
245 |
+
"What are the side effects of aspirin?",
|
246 |
+
"What are the causes of high blood pressure?"
|
247 |
+
],
|
248 |
+
description="An AI Medical Assistant. Please ask health-related questions only.",
|
249 |
+
theme=gr.themes.Soft(),
|
250 |
+
css=".gradio-container {background-color: #f0f4f8}"
|
251 |
+
)
|
252 |
+
|
253 |
+
''', label="System message"),
|
254 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
255 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
256 |
gr.Slider(
|