Update app.py
Browse files
app.py
CHANGED
|
@@ -26,8 +26,9 @@ print("Model loaded successfully!")
|
|
| 26 |
print(f"Device map: {model.hf_device_map}")
|
| 27 |
print(f"Model device: {next(model.parameters()).device}")
|
| 28 |
|
|
|
|
| 29 |
# =======================================================
|
| 30 |
-
# Generate Doctor Response (Stateless)
|
| 31 |
# =======================================================
|
| 32 |
def generate_doctor_response(history):
|
| 33 |
user_message = history[-1]["content"]
|
|
@@ -37,25 +38,23 @@ def generate_doctor_response(history):
|
|
| 37 |
yield history
|
| 38 |
return
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
prompt = """
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
- **Doctor:** Diagnose symptoms, ask relevant follow-up questions, and provide medical advice.
|
| 45 |
-
- **Nutritionist:** Give diet and lifestyle recommendations.
|
| 46 |
-
- **Medical Teacher:** Explain complex medical terms and conditions in simple, understandable language.
|
| 47 |
|
| 48 |
-
|
| 49 |
-
1.
|
| 50 |
-
2.
|
| 51 |
-
3.
|
| 52 |
-
4.
|
| 53 |
-
⚕️ *This is AI-generated information and not a substitute for professional medical advice
|
| 54 |
-
5. Keep answers concise, clear, and actionable.
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Tokenize input
|
| 61 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
@@ -80,8 +79,8 @@ Patient: """ + user_message + "\nDoctor:"
|
|
| 80 |
|
| 81 |
# Keep concise output
|
| 82 |
response = ". ".join(response.split(". ")[:3]).strip()
|
| 83 |
-
if response.lower().startswith("
|
| 84 |
-
response = response[
|
| 85 |
if len(response) < 10:
|
| 86 |
response = "I understand your concern. Could you please provide more details about your symptoms?"
|
| 87 |
|
|
|
|
| 26 |
print(f"Device map: {model.hf_device_map}")
|
| 27 |
print(f"Model device: {next(model.parameters()).device}")
|
| 28 |
|
| 29 |
+
|
| 30 |
# =======================================================
|
| 31 |
+
# Generate Doctor Response (Stateless + Clean Replies)
|
| 32 |
# =======================================================
|
| 33 |
def generate_doctor_response(history):
|
| 34 |
user_message = history[-1]["content"]
|
|
|
|
| 38 |
yield history
|
| 39 |
return
|
| 40 |
|
| 41 |
+
# 🩺 New Prompt (no 'Patient:' or 'Doctor:' lines)
|
| 42 |
+
prompt = f"""
|
| 43 |
+
You are a compassionate and professional medical expert.
|
| 44 |
+
Your role is to help users by providing clear, empathetic, and accurate medical information.
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
Guidelines:
|
| 47 |
+
1. Do NOT include words like 'Doctor:' or 'Patient:' in your replies.
|
| 48 |
+
2. Respond naturally and directly to the user's concern.
|
| 49 |
+
3. Keep answers short, clear, and medically sound.
|
| 50 |
+
4. Add a disclaimer when appropriate:
|
| 51 |
+
⚕️ *This is AI-generated information and not a substitute for professional medical advice.*
|
|
|
|
| 52 |
|
| 53 |
+
Now, please respond to the user's message below:
|
| 54 |
|
| 55 |
+
User: {user_message}
|
| 56 |
+
Assistant:
|
| 57 |
+
"""
|
| 58 |
|
| 59 |
# Tokenize input
|
| 60 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
| 79 |
|
| 80 |
# Keep concise output
|
| 81 |
response = ". ".join(response.split(". ")[:3]).strip()
|
| 82 |
+
if response.lower().startswith("assistant:"):
|
| 83 |
+
response = response[10:].strip()
|
| 84 |
if len(response) < 10:
|
| 85 |
response = "I understand your concern. Could you please provide more details about your symptoms?"
|
| 86 |
|