Update app.py
Browse files
app.py
CHANGED
|
@@ -38,39 +38,37 @@ def generate_doctor_response(history):
|
|
| 38 |
yield history
|
| 39 |
return
|
| 40 |
|
| 41 |
-
# Enhanced Medical Prompt -
|
| 42 |
-
prompt = f"""You are
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 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 |
-
|
| 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.
|
| 70 |
-
top_p=0.
|
| 71 |
-
top_k=
|
| 72 |
do_sample=True,
|
| 73 |
-
max_new_tokens=
|
| 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:",
|
|
|
|
| 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
|
| 114 |
lines = response.split('\n')
|
| 115 |
cleaned_lines = []
|
|
|
|
|
|
|
|
|
|
| 116 |
for line in lines:
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
cleaned_lines.append(line)
|
|
|
|
| 119 |
response = '\n'.join(cleaned_lines)
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
if
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
# Add disclaimer if not present
|
| 126 |
-
if
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
# Fallback for very short responses
|
| 130 |
-
if len(response.strip()) <
|
| 131 |
-
response = "I understand
|
| 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
|
| 153 |
-
<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 Advice • Like Talking to Your Doctor</p>
|
| 179 |
</div>
|
| 180 |
""")
|
| 181 |
|