Muhammadidrees commited on
Commit
83bb91a
·
verified ·
1 Parent(s): ab6fe89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -3
app.py CHANGED
@@ -31,6 +31,17 @@ model = LlamaForCausalLM.from_pretrained(
31
  generator = model.generate
32
  print("✅ ChatDoctor model loaded successfully!\n")
33
 
 
 
 
 
 
 
 
 
 
 
 
34
  # =============================
35
  # Stopping Criteria
36
  # =============================
@@ -54,16 +65,41 @@ class StopOnTokens(StoppingCriteria):
54
  # =============================
55
  conversation_history = []
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # =============================
58
  # Get Response Function
59
  # =============================
60
  def get_response(user_input, history_context):
61
  """Generate response from ChatDoctor model"""
 
 
 
 
 
62
  human_invitation = "Patient: "
63
  doctor_invitation = "ChatDoctor: "
64
 
65
  # Build conversation from history
66
- history_text = []
67
  for human, assistant in history_context:
68
  if human:
69
  history_text.append(human_invitation + human)
@@ -202,14 +238,14 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
202
  # Chatbot Interface
203
  chatbot = gr.Chatbot(
204
  height=500,
205
- placeholder="<div style='text-align: center; padding: 40px;'><h3>👋 Welcome to ChatDoctor!</h3><p>I'm here to discuss your health concerns. How can I assist you today?</p></div>",
206
  show_label=False,
207
  avatar_images=(None, "🤖"),
208
  )
209
 
210
  with gr.Row():
211
  msg = gr.Textbox(
212
- placeholder="Type your message here... (e.g., 'I have a headache')",
213
  show_label=False,
214
  scale=9,
215
  container=False
 
31
  generator = model.generate
32
  print("✅ ChatDoctor model loaded successfully!\n")
33
 
34
+ # =============================
35
+ # System Prompt for Medical Focus
36
+ # =============================
37
+ SYSTEM_PROMPT = """You are ChatDoctor, a professional medical AI assistant. Your role is to:
38
+ - Provide helpful medical information and guidance
39
+ - Ask relevant follow-up questions about symptoms
40
+ - Offer general health advice
41
+ - Remind patients to consult healthcare professionals for serious concerns
42
+
43
+ You should ONLY engage in health and medical-related conversations. If someone greets you casually or asks non-medical questions, politely redirect them to ask about their health concerns."""
44
+
45
  # =============================
46
  # Stopping Criteria
47
  # =============================
 
65
  # =============================
66
  conversation_history = []
67
 
68
+ # =============================
69
+ # Check if message is medical-related
70
+ # =============================
71
+ def is_greeting_or_casual(message):
72
+ """Check if message is just a casual greeting"""
73
+ casual_patterns = [
74
+ "hey", "hi", "hello", "sup", "what's up", "whats up",
75
+ "how are you", "good morning", "good evening", "good afternoon"
76
+ ]
77
+
78
+ message_lower = message.lower().strip()
79
+
80
+ # Check if it's ONLY a greeting (not combined with medical query)
81
+ if len(message_lower.split()) <= 3:
82
+ for pattern in casual_patterns:
83
+ if message_lower == pattern or message_lower.startswith(pattern):
84
+ return True
85
+
86
+ return False
87
+
88
  # =============================
89
  # Get Response Function
90
  # =============================
91
  def get_response(user_input, history_context):
92
  """Generate response from ChatDoctor model"""
93
+
94
+ # Handle casual greetings by redirecting to medical topics
95
+ if is_greeting_or_casual(user_input):
96
+ return "Hello! I'm ChatDoctor, your AI medical assistant. I'm here to help with your health concerns and medical questions. What health-related issue can I assist you with today?"
97
+
98
  human_invitation = "Patient: "
99
  doctor_invitation = "ChatDoctor: "
100
 
101
  # Build conversation from history
102
+ history_text = [SYSTEM_PROMPT]
103
  for human, assistant in history_context:
104
  if human:
105
  history_text.append(human_invitation + human)
 
238
  # Chatbot Interface
239
  chatbot = gr.Chatbot(
240
  height=500,
241
+ placeholder="<div style='text-align: center; padding: 40px;'><h3>👋 Welcome to ChatDoctor!</h3><p>I'm here to discuss your health concerns. Please describe your symptoms or health question.</p></div>",
242
  show_label=False,
243
  avatar_images=(None, "🤖"),
244
  )
245
 
246
  with gr.Row():
247
  msg = gr.Textbox(
248
+ placeholder="Describe your symptoms or health concern here...",
249
  show_label=False,
250
  scale=9,
251
  container=False