Update app.py
Browse files
app.py
CHANGED
|
@@ -14,12 +14,12 @@ logging.basicConfig(
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Set environment variables for memory optimization
|
| 17 |
-
os.environ['TRANSFORMERS_CACHE'] = '/home/user/.cache/huggingface/hub'
|
| 18 |
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
|
| 19 |
|
| 20 |
class HealthAssistant:
|
| 21 |
def __init__(self):
|
| 22 |
-
self.model_id = "microsoft/Phi-2" # Using smaller Phi-2 model
|
| 23 |
self.model = None
|
| 24 |
self.tokenizer = None
|
| 25 |
self.pipe = None
|
|
@@ -36,7 +36,6 @@ class HealthAssistant:
|
|
| 36 |
|
| 37 |
logger.info(f"Loading model: {self.model_id}")
|
| 38 |
|
| 39 |
-
# Initialize tokenizer with optimizations
|
| 40 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 41 |
self.model_id,
|
| 42 |
trust_remote_code=True,
|
|
@@ -45,7 +44,6 @@ class HealthAssistant:
|
|
| 45 |
)
|
| 46 |
logger.info("Tokenizer loaded")
|
| 47 |
|
| 48 |
-
# Load model with memory optimizations
|
| 49 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
self.model_id,
|
| 51 |
torch_dtype=torch.float32,
|
|
@@ -56,7 +54,6 @@ class HealthAssistant:
|
|
| 56 |
|
| 57 |
gc.collect()
|
| 58 |
|
| 59 |
-
# Setup pipeline
|
| 60 |
self.pipe = pipeline(
|
| 61 |
"text-generation",
|
| 62 |
model=self.model,
|
|
@@ -74,7 +71,6 @@ class HealthAssistant:
|
|
| 74 |
raise
|
| 75 |
|
| 76 |
def unload_model(self):
|
| 77 |
-
"""Unload model to free up memory"""
|
| 78 |
if hasattr(self, 'model') and self.model is not None:
|
| 79 |
del self.model
|
| 80 |
self.model = None
|
|
@@ -93,7 +89,6 @@ class HealthAssistant:
|
|
| 93 |
if not self.is_model_loaded:
|
| 94 |
self.initialize_model()
|
| 95 |
|
| 96 |
-
# Limit message length
|
| 97 |
message = message[:200] # Truncate long messages
|
| 98 |
|
| 99 |
prompt = self._prepare_prompt(message, history[-self.max_history_length:] if history else None)
|
|
@@ -120,10 +115,6 @@ class HealthAssistant:
|
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Error generating response: {str(e)}")
|
| 122 |
return "I apologize, but I encountered an error. Please try again."
|
| 123 |
-
finally:
|
| 124 |
-
# Attempt to free memory after each generation
|
| 125 |
-
if torch.cuda.is_available():
|
| 126 |
-
torch.cuda.empty_cache()
|
| 127 |
|
| 128 |
def _prepare_prompt(self, message: str, history: List = None) -> str:
|
| 129 |
prompt_parts = [
|
|
@@ -132,11 +123,17 @@ class HealthAssistant:
|
|
| 132 |
]
|
| 133 |
|
| 134 |
if history:
|
| 135 |
-
for
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
prompt_parts.extend([
|
| 142 |
f"Human: {message}",
|
|
@@ -162,7 +159,6 @@ class HealthAssistant:
|
|
| 162 |
|
| 163 |
def add_metrics(self, weight: float, steps: int, sleep: float) -> bool:
|
| 164 |
try:
|
| 165 |
-
# Keep only last 5 metrics
|
| 166 |
if len(self.metrics) >= 5:
|
| 167 |
self.metrics.pop(0)
|
| 168 |
|
|
@@ -178,7 +174,6 @@ class HealthAssistant:
|
|
| 178 |
|
| 179 |
def add_medication(self, name: str, dosage: str, time: str, notes: str = "") -> bool:
|
| 180 |
try:
|
| 181 |
-
# Keep only last 5 medications
|
| 182 |
if len(self.medications) >= 5:
|
| 183 |
self.medications.pop(0)
|
| 184 |
|
|
@@ -209,16 +204,20 @@ class GradioInterface:
|
|
| 209 |
|
| 210 |
try:
|
| 211 |
response = self.assistant.generate_response(message, history)
|
| 212 |
-
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
# Unload model periodically
|
| 215 |
if len(history) % 3 == 0:
|
| 216 |
self.assistant.unload_model()
|
| 217 |
|
| 218 |
return "", history
|
| 219 |
except Exception as e:
|
| 220 |
logger.error(f"Error in chat response: {e}")
|
| 221 |
-
return "", history + [
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
def add_health_metrics(self, weight: float, steps: int, sleep: float) -> str:
|
| 224 |
if not all([weight is not None, steps is not None, sleep is not None]):
|
|
@@ -254,24 +253,23 @@ class GradioInterface:
|
|
| 254 |
""")
|
| 255 |
|
| 256 |
with gr.Tabs():
|
| 257 |
-
# Chat Interface
|
| 258 |
with gr.Tab("💬 Medical Consultation"):
|
| 259 |
chatbot = gr.Chatbot(
|
| 260 |
value=[],
|
| 261 |
height=400,
|
| 262 |
-
|
|
|
|
| 263 |
)
|
| 264 |
with gr.Row():
|
| 265 |
msg = gr.Textbox(
|
| 266 |
placeholder="Ask your health question...",
|
| 267 |
lines=1,
|
| 268 |
-
|
| 269 |
scale=9
|
| 270 |
)
|
| 271 |
send_btn = gr.Button("Send", scale=1)
|
| 272 |
clear_btn = gr.Button("Clear Chat")
|
| 273 |
|
| 274 |
-
# Health Metrics
|
| 275 |
with gr.Tab("📊 Health Metrics"):
|
| 276 |
gr.Markdown("### Track Your Health Metrics")
|
| 277 |
with gr.Row():
|
|
@@ -293,7 +291,6 @@ class GradioInterface:
|
|
| 293 |
metrics_btn = gr.Button("Save Metrics")
|
| 294 |
metrics_status = gr.Markdown()
|
| 295 |
|
| 296 |
-
# Medication Manager
|
| 297 |
with gr.Tab("💊 Medication Manager"):
|
| 298 |
gr.Markdown("### Track Your Medications")
|
| 299 |
med_name = gr.Textbox(
|
|
@@ -316,7 +313,6 @@ class GradioInterface:
|
|
| 316 |
med_btn = gr.Button("Add Medication")
|
| 317 |
med_status = gr.Markdown()
|
| 318 |
|
| 319 |
-
# Event handlers
|
| 320 |
msg.submit(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 321 |
send_btn.click(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 322 |
clear_btn.click(lambda: [], None, chatbot)
|
|
@@ -339,7 +335,7 @@ class GradioInterface:
|
|
| 339 |
Always consult healthcare professionals for medical decisions.
|
| 340 |
""")
|
| 341 |
|
| 342 |
-
demo.queue(
|
| 343 |
|
| 344 |
return demo
|
| 345 |
|
|
@@ -348,6 +344,7 @@ def main():
|
|
| 348 |
interface = GradioInterface()
|
| 349 |
demo = interface.create_interface()
|
| 350 |
demo.launch(
|
|
|
|
| 351 |
show_error=True,
|
| 352 |
share=True
|
| 353 |
)
|
|
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
# Set environment variables for memory optimization
|
| 17 |
+
os.environ['TRANSFORMERS_CACHE'] = '/home/user/.cache/huggingface/hub'
|
| 18 |
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
|
| 19 |
|
| 20 |
class HealthAssistant:
|
| 21 |
def __init__(self):
|
| 22 |
+
self.model_id = "microsoft/Phi-2" # Using smaller Phi-2 model
|
| 23 |
self.model = None
|
| 24 |
self.tokenizer = None
|
| 25 |
self.pipe = None
|
|
|
|
| 36 |
|
| 37 |
logger.info(f"Loading model: {self.model_id}")
|
| 38 |
|
|
|
|
| 39 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 40 |
self.model_id,
|
| 41 |
trust_remote_code=True,
|
|
|
|
| 44 |
)
|
| 45 |
logger.info("Tokenizer loaded")
|
| 46 |
|
|
|
|
| 47 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 48 |
self.model_id,
|
| 49 |
torch_dtype=torch.float32,
|
|
|
|
| 54 |
|
| 55 |
gc.collect()
|
| 56 |
|
|
|
|
| 57 |
self.pipe = pipeline(
|
| 58 |
"text-generation",
|
| 59 |
model=self.model,
|
|
|
|
| 71 |
raise
|
| 72 |
|
| 73 |
def unload_model(self):
|
|
|
|
| 74 |
if hasattr(self, 'model') and self.model is not None:
|
| 75 |
del self.model
|
| 76 |
self.model = None
|
|
|
|
| 89 |
if not self.is_model_loaded:
|
| 90 |
self.initialize_model()
|
| 91 |
|
|
|
|
| 92 |
message = message[:200] # Truncate long messages
|
| 93 |
|
| 94 |
prompt = self._prepare_prompt(message, history[-self.max_history_length:] if history else None)
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
logger.error(f"Error generating response: {str(e)}")
|
| 117 |
return "I apologize, but I encountered an error. Please try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
def _prepare_prompt(self, message: str, history: List = None) -> str:
|
| 120 |
prompt_parts = [
|
|
|
|
| 123 |
]
|
| 124 |
|
| 125 |
if history:
|
| 126 |
+
for h in history:
|
| 127 |
+
if isinstance(h, dict): # New message format
|
| 128 |
+
if h['role'] == 'user':
|
| 129 |
+
prompt_parts.append(f"Human: {h['content'][:100]}")
|
| 130 |
+
else:
|
| 131 |
+
prompt_parts.append(f"Assistant: {h['content'][:100]}")
|
| 132 |
+
else: # Old format (tuple)
|
| 133 |
+
prompt_parts.extend([
|
| 134 |
+
f"Human: {h[0][:100]}",
|
| 135 |
+
f"Assistant: {h[1][:100]}"
|
| 136 |
+
])
|
| 137 |
|
| 138 |
prompt_parts.extend([
|
| 139 |
f"Human: {message}",
|
|
|
|
| 159 |
|
| 160 |
def add_metrics(self, weight: float, steps: int, sleep: float) -> bool:
|
| 161 |
try:
|
|
|
|
| 162 |
if len(self.metrics) >= 5:
|
| 163 |
self.metrics.pop(0)
|
| 164 |
|
|
|
|
| 174 |
|
| 175 |
def add_medication(self, name: str, dosage: str, time: str, notes: str = "") -> bool:
|
| 176 |
try:
|
|
|
|
| 177 |
if len(self.medications) >= 5:
|
| 178 |
self.medications.pop(0)
|
| 179 |
|
|
|
|
| 204 |
|
| 205 |
try:
|
| 206 |
response = self.assistant.generate_response(message, history)
|
| 207 |
+
# Convert to new message format
|
| 208 |
+
history.append({"role": "user", "content": message})
|
| 209 |
+
history.append({"role": "assistant", "content": response})
|
| 210 |
|
|
|
|
| 211 |
if len(history) % 3 == 0:
|
| 212 |
self.assistant.unload_model()
|
| 213 |
|
| 214 |
return "", history
|
| 215 |
except Exception as e:
|
| 216 |
logger.error(f"Error in chat response: {e}")
|
| 217 |
+
return "", history + [
|
| 218 |
+
{"role": "user", "content": message},
|
| 219 |
+
{"role": "assistant", "content": "I apologize, but I encountered an error. Please try again."}
|
| 220 |
+
]
|
| 221 |
|
| 222 |
def add_health_metrics(self, weight: float, steps: int, sleep: float) -> str:
|
| 223 |
if not all([weight is not None, steps is not None, sleep is not None]):
|
|
|
|
| 253 |
""")
|
| 254 |
|
| 255 |
with gr.Tabs():
|
|
|
|
| 256 |
with gr.Tab("💬 Medical Consultation"):
|
| 257 |
chatbot = gr.Chatbot(
|
| 258 |
value=[],
|
| 259 |
height=400,
|
| 260 |
+
label=False,
|
| 261 |
+
type="messages" # Using new message format
|
| 262 |
)
|
| 263 |
with gr.Row():
|
| 264 |
msg = gr.Textbox(
|
| 265 |
placeholder="Ask your health question...",
|
| 266 |
lines=1,
|
| 267 |
+
label=False,
|
| 268 |
scale=9
|
| 269 |
)
|
| 270 |
send_btn = gr.Button("Send", scale=1)
|
| 271 |
clear_btn = gr.Button("Clear Chat")
|
| 272 |
|
|
|
|
| 273 |
with gr.Tab("📊 Health Metrics"):
|
| 274 |
gr.Markdown("### Track Your Health Metrics")
|
| 275 |
with gr.Row():
|
|
|
|
| 291 |
metrics_btn = gr.Button("Save Metrics")
|
| 292 |
metrics_status = gr.Markdown()
|
| 293 |
|
|
|
|
| 294 |
with gr.Tab("💊 Medication Manager"):
|
| 295 |
gr.Markdown("### Track Your Medications")
|
| 296 |
med_name = gr.Textbox(
|
|
|
|
| 313 |
med_btn = gr.Button("Add Medication")
|
| 314 |
med_status = gr.Markdown()
|
| 315 |
|
|
|
|
| 316 |
msg.submit(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 317 |
send_btn.click(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 318 |
clear_btn.click(lambda: [], None, chatbot)
|
|
|
|
| 335 |
Always consult healthcare professionals for medical decisions.
|
| 336 |
""")
|
| 337 |
|
| 338 |
+
demo.queue(max_size=5)
|
| 339 |
|
| 340 |
return demo
|
| 341 |
|
|
|
|
| 344 |
interface = GradioInterface()
|
| 345 |
demo = interface.create_interface()
|
| 346 |
demo.launch(
|
| 347 |
+
server_name="0.0.0.0",
|
| 348 |
show_error=True,
|
| 349 |
share=True
|
| 350 |
)
|