Muhammadidrees commited on
Commit
cb05c44
·
verified ·
1 Parent(s): ec1a12b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -37
app.py CHANGED
@@ -38,39 +38,37 @@ def generate_doctor_response(history):
38
  yield history
39
  return
40
 
41
- # Enhanced Medical Prompt - Comprehensive Doctor Approach
42
- prompt = f"""You are an experienced and compassionate medical doctor providing a comprehensive consultation.
43
 
44
- Based on the patient's concern, provide a detailed response that includes:
 
 
 
 
 
45
 
46
- 1. **Assessment**: Acknowledge their symptoms and provide initial medical assessment
47
- 2. **Possible Causes**: Explain potential causes or conditions
48
- 3. **Medications**: Recommend appropriate over-the-counter or prescription medications (with dosages when relevant)
49
- 4. **Nutrition & Diet**: Suggest specific foods, nutrients, or dietary changes that can help
50
- 5. **Lifestyle Modifications**: Recommend lifestyle changes, exercises, rest, or habits to adopt
51
- 6. **Follow-up**: Advise when to see a doctor or what warning signs to watch for
 
52
 
53
- Guidelines:
54
- - Do NOT use labels like "Doctor:" or "Patient:" in your response
55
- - Be professional, empathetic, and thorough like a real doctor
56
- - Provide specific, actionable recommendations
57
- - Use medical terminology but explain it simply
58
- - Structure your response clearly with the categories above
59
- - End with: "⚕️ *Please consult a healthcare provider for proper diagnosis and personalized treatment plan.*"
60
 
61
- Patient's concern: {user_message}
62
-
63
- Comprehensive Medical Response:"""
64
 
65
  # Tokenize input
66
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
67
 
68
  gen_config = GenerationConfig(
69
- temperature=0.7,
70
- top_p=0.92,
71
- top_k=50,
72
  do_sample=True,
73
- max_new_tokens=600,
74
  pad_token_id=tokenizer.pad_token_id,
75
  eos_token_id=tokenizer.eos_token_id,
76
  repetition_penalty=1.18,
@@ -101,34 +99,61 @@ Comprehensive Medical Response:"""
101
 
102
 
103
  def clean_medical_response(response: str) -> str:
104
- """Clean and format the medical response."""
105
  # Remove common prefixes
106
- prefixes = ["assistant:", "doctor:", "response:", "comprehensive medical response:", "medical response:"]
 
107
  response_lower = response.lower()
108
  for prefix in prefixes:
109
  if response_lower.startswith(prefix):
110
  response = response[len(prefix):].strip()
 
111
  break
112
 
113
- # Remove any remaining role labels
114
  lines = response.split('\n')
115
  cleaned_lines = []
 
 
 
116
  for line in lines:
117
- if not line.lower().strip().startswith(('doctor:', 'assistant:', 'patient:')):
 
 
 
 
 
 
118
  cleaned_lines.append(line)
 
119
  response = '\n'.join(cleaned_lines)
120
 
121
- # Ensure proper ending
122
- if response and response[-1] not in '.!?':
123
- response += '.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- # Add disclaimer if not present
126
- if '⚕️' not in response and 'consult' not in response.lower():
127
- response += '\n\n⚕️ *Please consult a healthcare provider for proper diagnosis and personalized treatment plan.*'
 
128
 
129
  # Fallback for very short responses
130
- if len(response.strip()) < 30:
131
- response = "I understand your concern. To provide you with comprehensive medical guidance including medications, diet, and lifestyle recommendations, could you please describe your symptoms in more detail? For example, when did they start, how severe are they, and have you noticed any triggers?"
132
 
133
  return response.strip()
134
 
@@ -149,8 +174,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
149
 
150
  gr.HTML("""
151
  <div class="medical-header">
152
- <h1>🏥 AI Medical Doctor Consultation</h1>
153
- <p>Comprehensive Medical GuidanceMedications Nutrition Lifestyle</p>
154
  </div>
155
  """)
156
 
 
38
  yield history
39
  return
40
 
41
+ # Enhanced Medical Prompt - Conversational Doctor Style
42
+ prompt = f"""You are a friendly, experienced doctor having a natural conversation with a patient.
43
 
44
+ Respond in a warm, conversational way covering:
45
+ - What might be causing their symptoms (keep it simple)
46
+ - Specific medications they can take (with dosages)
47
+ - Foods and drinks that will help
48
+ - Lifestyle tips and rest recommendations
49
+ - When they should see a doctor in person
50
 
51
+ Important rules:
52
+ - Write like you're talking to a friend, not writing a formal report
53
+ - NO labels like "Doctor:", "Patient:", "Assessment:", "Medications:" etc.
54
+ - Keep it natural and flowing, not structured lists
55
+ - Be warm and reassuring
56
+ - Keep response under 200 words
57
+ - End with a simple disclaimer
58
 
59
+ Patient says: {user_message}
 
 
 
 
 
 
60
 
61
+ Your response:"""
 
 
62
 
63
  # Tokenize input
64
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
65
 
66
  gen_config = GenerationConfig(
67
+ temperature=0.8,
68
+ top_p=0.95,
69
+ top_k=40,
70
  do_sample=True,
71
+ max_new_tokens=300,
72
  pad_token_id=tokenizer.pad_token_id,
73
  eos_token_id=tokenizer.eos_token_id,
74
  repetition_penalty=1.18,
 
99
 
100
 
101
  def clean_medical_response(response: str) -> str:
102
+ """Clean and format the medical response for natural conversation."""
103
  # Remove common prefixes
104
+ prefixes = ["assistant:", "doctor:", "response:", "comprehensive medical response:",
105
+ "medical response:", "your response:", "dear patient"]
106
  response_lower = response.lower()
107
  for prefix in prefixes:
108
  if response_lower.startswith(prefix):
109
  response = response[len(prefix):].strip()
110
+ response = response.lstrip(',').strip()
111
  break
112
 
113
+ # Remove section headers/labels
114
  lines = response.split('\n')
115
  cleaned_lines = []
116
+ skip_patterns = ['doctor:', 'assistant:', 'patient:', 'assessment:', 'medications:',
117
+ 'nutrition:', 'lifestyle:', 'follow-up:', '1.', '2.', '3.', '4.', '5.']
118
+
119
  for line in lines:
120
+ line_lower = line.lower().strip()
121
+ should_skip = False
122
+ for pattern in skip_patterns:
123
+ if line_lower.startswith(pattern):
124
+ should_skip = True
125
+ break
126
+ if not should_skip and line.strip():
127
  cleaned_lines.append(line)
128
+
129
  response = '\n'.join(cleaned_lines)
130
 
131
+ # Stop at repetitive text or gibberish
132
+ if 'accordingly' in response.lower() or 'respectively' in response.lower():
133
+ sentences = response.split('.')
134
+ good_sentences = []
135
+ for sent in sentences:
136
+ if 'accordingly' not in sent.lower() and 'respectively' not in sent.lower():
137
+ good_sentences.append(sent)
138
+ else:
139
+ break
140
+ response = '. '.join(good_sentences)
141
+
142
+ # Limit to reasonable length
143
+ sentences = [s.strip() for s in response.split('.') if s.strip()]
144
+ if len(sentences) > 8:
145
+ response = '. '.join(sentences[:8]) + '.'
146
+ else:
147
+ response = '. '.join(sentences) + '.'
148
 
149
+ # Add disclaimer if not present and response is substantial
150
+ if len(response) > 50:
151
+ if '⚕️' not in response and 'consult' not in response.lower():
152
+ response += '\n\n⚕️ *If symptoms worsen or persist beyond 3-4 days, please see a doctor.*'
153
 
154
  # Fallback for very short responses
155
+ if len(response.strip()) < 40:
156
+ response = "I understand you're not feeling well. Could you tell me a bit more about your symptoms? How long have you had them, and have you taken anything for them yet?"
157
 
158
  return response.strip()
159
 
 
174
 
175
  gr.HTML("""
176
  <div class="medical-header">
177
+ <h1>🏥 AI Doctor Chat</h1>
178
+ <p>Friendly Medical AdviceLike Talking to Your Doctor</p>
179
  </div>
180
  """)
181