Spaces:
Sleeping
Sleeping
added prompt for model behaviour
Browse files- app.py +14 -6
- app_optimized.py +196 -0
- config.py +29 -11
app.py
CHANGED
|
@@ -112,15 +112,22 @@ def generate_response(prompt, max_tokens=None, temperature=None, top_p=None):
|
|
| 112 |
print(f"π Generation params: max_tokens={max_tokens}, temp={temperature}, top_p={top_p}")
|
| 113 |
|
| 114 |
try:
|
| 115 |
-
#
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Tokenize input with proper truncation
|
| 119 |
inputs = tokenizer(
|
| 120 |
full_prompt,
|
| 121 |
return_tensors="pt",
|
| 122 |
truncation=True,
|
| 123 |
-
max_length=
|
| 124 |
padding=True
|
| 125 |
)
|
| 126 |
|
|
@@ -162,11 +169,12 @@ def generate_response(prompt, max_tokens=None, temperature=None, top_p=None):
|
|
| 162 |
# Fallback extraction
|
| 163 |
response = full_response[len(full_prompt):].strip()
|
| 164 |
|
| 165 |
-
# Clean up response
|
| 166 |
-
if not response or len(response.strip()) <
|
| 167 |
-
response = "
|
| 168 |
|
| 169 |
print(f"β
Generated response length: {len(response)} characters")
|
|
|
|
| 170 |
|
| 171 |
# Clean up memory
|
| 172 |
del inputs, outputs
|
|
|
|
| 112 |
print(f"π Generation params: max_tokens={max_tokens}, temp={temperature}, top_p={top_p}")
|
| 113 |
|
| 114 |
try:
|
| 115 |
+
# Format prompt for DialoGPT - simpler approach
|
| 116 |
+
if "dialogpt" in current_model_name.lower():
|
| 117 |
+
# DialoGPT works better with direct conversation format
|
| 118 |
+
full_prompt = f"Medical Question: {prompt}\nMedical Assistant:"
|
| 119 |
+
else:
|
| 120 |
+
# Use full system prompt for other models
|
| 121 |
+
full_prompt = f"{MEDICAL_SYSTEM_PROMPT}\n\nPatient/User: {prompt}\nMedical Assistant:"
|
| 122 |
+
|
| 123 |
+
print(f"π Using prompt format: {full_prompt[:100]}{'...' if len(full_prompt) > 100 else ''}")
|
| 124 |
|
| 125 |
# Tokenize input with proper truncation
|
| 126 |
inputs = tokenizer(
|
| 127 |
full_prompt,
|
| 128 |
return_tensors="pt",
|
| 129 |
truncation=True,
|
| 130 |
+
max_length=512, # Reduced for DialoGPT
|
| 131 |
padding=True
|
| 132 |
)
|
| 133 |
|
|
|
|
| 169 |
# Fallback extraction
|
| 170 |
response = full_response[len(full_prompt):].strip()
|
| 171 |
|
| 172 |
+
# Clean up response - keep it natural as per CareConnect guidelines
|
| 173 |
+
if not response or len(response.strip()) < 3:
|
| 174 |
+
response = "Could you please provide more details about your question so I can help you better? π"
|
| 175 |
|
| 176 |
print(f"β
Generated response length: {len(response)} characters")
|
| 177 |
+
print(f"π Response preview: {response[:150]}{'...' if len(response) > 150 else ''}")
|
| 178 |
|
| 179 |
# Clean up memory
|
| 180 |
del inputs, outputs
|
app_optimized.py
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import logging
|
| 5 |
+
import gc
|
| 6 |
+
import warnings
|
| 7 |
+
import os
|
| 8 |
+
from huggingface_hub import login
|
| 9 |
+
|
| 10 |
+
# Login with the secret token
|
| 11 |
+
login(token=os.getenv("HF_TOKEN"))
|
| 12 |
+
|
| 13 |
+
# Suppress warnings
|
| 14 |
+
warnings.filterwarnings("ignore")
|
| 15 |
+
logging.getLogger("transformers").setLevel(logging.ERROR)
|
| 16 |
+
|
| 17 |
+
# Configuration for optimized performance
|
| 18 |
+
MODEL_NAME = "microsoft/DialoGPT-medium"
|
| 19 |
+
MAX_NEW_TOKENS = 150
|
| 20 |
+
TEMPERATURE = 0.8
|
| 21 |
+
TOP_P = 0.9
|
| 22 |
+
|
| 23 |
+
# Medical enhancement prompt - detailed CareConnect specifications
|
| 24 |
+
MEDICAL_CONTEXT = """You are a friendly and smart medical assistant. Your job is to give short, clear, and helpful health information.
|
| 25 |
+
|
| 26 |
+
Your answers should:
|
| 27 |
+
- Stay focused. No long essays or extra fluff.
|
| 28 |
+
- Give basic helpful steps for common symptoms like fever, cough, or headache (e.g., rest, drink fluids, take paracetamol if needed).
|
| 29 |
+
- For any serious or unclear issues, remind the user to see a doctor β but do it briefly and naturally.
|
| 30 |
+
- Keep responses concise and under 4 sentences when possible.
|
| 31 |
+
|
| 32 |
+
Tone:
|
| 33 |
+
- Friendly, supportive, and calm.
|
| 34 |
+
- No robotic warnings unless needed. Keep it real and human.
|
| 35 |
+
- Use emojis like π or π occasionally to appear friendly.
|
| 36 |
+
|
| 37 |
+
Important rules:
|
| 38 |
+
- NEVER include text in parentheses in your responses.
|
| 39 |
+
- NEVER include any meta-instructions in your responses.
|
| 40 |
+
- NEVER include reminders about what you should do in future responses.
|
| 41 |
+
- DO NOT include phrases like "We're here to help" or "I'm just an AI".
|
| 42 |
+
- DO NOT include any text that instructs you what to do or how to behave.
|
| 43 |
+
- DO NOT include any sentences that start with "If the user asks..." or "Remember..."
|
| 44 |
+
- DO NOT include "(smile)" - instead, use actual emojis like π or π when appropriate.
|
| 45 |
+
- DO NOT include numbered references like [1], [2], etc. in your responses.
|
| 46 |
+
- DO NOT include any text that explains what your response is doing.
|
| 47 |
+
- DO NOT include "user:" or "assistant:" prefixes in your responses.
|
| 48 |
+
- DO NOT include hypothetical user questions in your responses.
|
| 49 |
+
- DO NOT refuse to answer harmless non-medical questions like jokes or general knowledge.
|
| 50 |
+
- Don't give exact dosages or diagnoses.
|
| 51 |
+
- Be consistent in your responses regardless of the user's role."""
|
| 52 |
+
|
| 53 |
+
# Global variables
|
| 54 |
+
model = None
|
| 55 |
+
tokenizer = None
|
| 56 |
+
|
| 57 |
+
def load_model():
|
| 58 |
+
"""Load DialoGPT model optimized for CPU"""
|
| 59 |
+
global model, tokenizer
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
print(f"π₯ Loading medical chatbot model: {MODEL_NAME}")
|
| 63 |
+
|
| 64 |
+
# Load tokenizer
|
| 65 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, padding_side="left")
|
| 66 |
+
if tokenizer.pad_token is None:
|
| 67 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 68 |
+
|
| 69 |
+
# Load model with CPU optimization
|
| 70 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 71 |
+
MODEL_NAME,
|
| 72 |
+
torch_dtype=torch.float32, # Use float32 for CPU
|
| 73 |
+
low_cpu_mem_usage=True,
|
| 74 |
+
trust_remote_code=True
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
print(f"β
Model loaded successfully!")
|
| 78 |
+
return True
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"β Failed to load model: {str(e)}")
|
| 82 |
+
return False
|
| 83 |
+
|
| 84 |
+
def generate_medical_response(prompt):
|
| 85 |
+
"""Generate medical response with DialoGPT"""
|
| 86 |
+
global model, tokenizer
|
| 87 |
+
|
| 88 |
+
if model is None or tokenizer is None:
|
| 89 |
+
return "β Model not loaded. Please wait for initialization."
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# Enhanced prompt for medical context
|
| 93 |
+
medical_prompt = f"{MEDICAL_CONTEXT}\n\nUser: {prompt}\nAssistant:"
|
| 94 |
+
|
| 95 |
+
print(f"π Processing: {prompt[:50]}{'...' if len(prompt) > 50 else ''}")
|
| 96 |
+
|
| 97 |
+
# Tokenize
|
| 98 |
+
inputs = tokenizer.encode(medical_prompt, return_tensors="pt", max_length=400, truncation=True)
|
| 99 |
+
|
| 100 |
+
# Generate with optimized parameters
|
| 101 |
+
with torch.no_grad():
|
| 102 |
+
outputs = model.generate(
|
| 103 |
+
inputs,
|
| 104 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 105 |
+
temperature=TEMPERATURE,
|
| 106 |
+
top_p=TOP_P,
|
| 107 |
+
do_sample=True,
|
| 108 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 109 |
+
repetition_penalty=1.1,
|
| 110 |
+
early_stopping=True,
|
| 111 |
+
num_return_sequences=1
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Decode response
|
| 115 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 116 |
+
|
| 117 |
+
# Extract only the assistant's response
|
| 118 |
+
if "Assistant:" in full_response:
|
| 119 |
+
response = full_response.split("Assistant:")[-1].strip()
|
| 120 |
+
else:
|
| 121 |
+
response = full_response[len(medical_prompt):].strip()
|
| 122 |
+
|
| 123 |
+
# Clean up response - keep it natural as per prompt guidelines
|
| 124 |
+
if not response or len(response) < 10:
|
| 125 |
+
response = "I'd be happy to help with your medical question. Could you please provide more specific details? π"
|
| 126 |
+
|
| 127 |
+
print(f"β
Response generated: {len(response)} characters")
|
| 128 |
+
|
| 129 |
+
# Memory cleanup
|
| 130 |
+
del inputs, outputs
|
| 131 |
+
gc.collect()
|
| 132 |
+
|
| 133 |
+
return response
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
print(f"β Generation error: {str(e)}")
|
| 137 |
+
return f"I encountered a technical issue. Please try rephrasing your question. For immediate medical concerns, please consult a healthcare professional."
|
| 138 |
+
|
| 139 |
+
def chat_interface(message, history):
|
| 140 |
+
"""Main chat interface function"""
|
| 141 |
+
if not message or not message.strip():
|
| 142 |
+
return "Please enter a medical question."
|
| 143 |
+
|
| 144 |
+
# Generate response
|
| 145 |
+
response = generate_medical_response(message.strip())
|
| 146 |
+
|
| 147 |
+
return response
|
| 148 |
+
|
| 149 |
+
# Load model on startup
|
| 150 |
+
print("π₯ Initializing Medical Chatbot...")
|
| 151 |
+
model_loaded = load_model()
|
| 152 |
+
|
| 153 |
+
if not model_loaded:
|
| 154 |
+
print("β οΈ WARNING: Model failed to load. Responses may be limited.")
|
| 155 |
+
|
| 156 |
+
# Create Gradio interface
|
| 157 |
+
demo = gr.ChatInterface(
|
| 158 |
+
chat_interface,
|
| 159 |
+
type="messages",
|
| 160 |
+
title="π₯ Medical Information Assistant",
|
| 161 |
+
description="""
|
| 162 |
+
A medical information chatbot powered by AI. This assistant provides educational health information.
|
| 163 |
+
|
| 164 |
+
β οΈ **Important Disclaimer**: This chatbot provides general health information for educational purposes only.
|
| 165 |
+
It should not replace professional medical advice, diagnosis, or treatment. Always consult qualified
|
| 166 |
+
healthcare professionals for medical concerns.
|
| 167 |
+
""",
|
| 168 |
+
examples=[
|
| 169 |
+
"What are the symptoms of diabetes?",
|
| 170 |
+
"How can I maintain a healthy heart?",
|
| 171 |
+
"What should I know about high blood pressure?",
|
| 172 |
+
"Tell me about the importance of regular exercise",
|
| 173 |
+
"What are common causes of headaches?",
|
| 174 |
+
"How can I improve my sleep quality?"
|
| 175 |
+
],
|
| 176 |
+
cache_examples=False,
|
| 177 |
+
theme=gr.themes.Soft(),
|
| 178 |
+
css="""
|
| 179 |
+
.gradio-container {
|
| 180 |
+
max-width: 800px !important;
|
| 181 |
+
margin: auto !important;
|
| 182 |
+
}
|
| 183 |
+
.message {
|
| 184 |
+
border-radius: 10px !important;
|
| 185 |
+
}
|
| 186 |
+
"""
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
demo.launch(
|
| 191 |
+
server_name="0.0.0.0",
|
| 192 |
+
server_port=7860,
|
| 193 |
+
share=True,
|
| 194 |
+
show_error=True,
|
| 195 |
+
debug=True
|
| 196 |
+
)
|
config.py
CHANGED
|
@@ -54,17 +54,35 @@ GENERATION_DEFAULTS = {
|
|
| 54 |
"no_repeat_ngram_size": 2 # Reduced for better performance
|
| 55 |
}
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
MEDICAL_SYSTEM_PROMPT = """You are a
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# UI settings
|
| 70 |
UI_CONFIG = {
|
|
|
|
| 54 |
"no_repeat_ngram_size": 2 # Reduced for better performance
|
| 55 |
}
|
| 56 |
|
| 57 |
+
# Medical system prompt from CareConnect - detailed specifications
|
| 58 |
+
MEDICAL_SYSTEM_PROMPT = """You are a friendly and smart medical assistant. Your job is to give short, clear, and helpful health information.
|
| 59 |
+
|
| 60 |
+
Your answers should:
|
| 61 |
+
- Stay focused. No long essays or extra fluff.
|
| 62 |
+
- Give basic helpful steps for common symptoms like fever, cough, or headache (e.g., rest, drink fluids, take paracetamol if needed).
|
| 63 |
+
- For any serious or unclear issues, remind the user to see a doctor β but do it briefly and naturally.
|
| 64 |
+
- Keep responses concise and under 4 sentences when possible.
|
| 65 |
+
|
| 66 |
+
Tone:
|
| 67 |
+
- Friendly, supportive, and calm.
|
| 68 |
+
- No robotic warnings unless needed. Keep it real and human.
|
| 69 |
+
- Use emojis like π or π occasionally to appear friendly.
|
| 70 |
+
|
| 71 |
+
Important rules:
|
| 72 |
+
- NEVER include text in parentheses in your responses.
|
| 73 |
+
- NEVER include any meta-instructions in your responses.
|
| 74 |
+
- NEVER include reminders about what you should do in future responses.
|
| 75 |
+
- DO NOT include phrases like "We're here to help" or "I'm just an AI".
|
| 76 |
+
- DO NOT include any text that instructs you what to do or how to behave.
|
| 77 |
+
- DO NOT include any sentences that start with "If the user asks..." or "Remember..."
|
| 78 |
+
- DO NOT include "(smile)" - instead, use actual emojis like π or π when appropriate.
|
| 79 |
+
- DO NOT include numbered references like [1], [2], etc. in your responses.
|
| 80 |
+
- DO NOT include any text that explains what your response is doing.
|
| 81 |
+
- DO NOT include "user:" or "assistant:" prefixes in your responses.
|
| 82 |
+
- DO NOT include hypothetical user questions in your responses.
|
| 83 |
+
- DO NOT refuse to answer harmless non-medical questions like jokes or general knowledge.
|
| 84 |
+
- Don't give exact dosages or diagnoses.
|
| 85 |
+
- Be consistent in your responses regardless of the user's role."""
|
| 86 |
|
| 87 |
# UI settings
|
| 88 |
UI_CONFIG = {
|