Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import transformers
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import logging
|
| 6 |
from typing import List, Dict
|
| 7 |
import gc
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# Setup logging
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.INFO,
|
| 13 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 14 |
)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
transformers.logging.set_verbosity_info()
|
| 20 |
|
| 21 |
class HealthAssistant:
|
| 22 |
def __init__(self, use_smaller_model=True):
|
| 23 |
-
# Use a smaller model for testing/CPU
|
| 24 |
if use_smaller_model:
|
| 25 |
-
self.model_name = "facebook/opt-125m"
|
| 26 |
else:
|
| 27 |
self.model_name = "Qwen/Qwen2-VL-7B-Instruct"
|
| 28 |
|
|
@@ -36,53 +33,40 @@ class HealthAssistant:
|
|
| 36 |
try:
|
| 37 |
logger.info(f"Starting model initialization: {self.model_name}")
|
| 38 |
|
| 39 |
-
# First try loading tokenizer
|
| 40 |
-
logger.info("Loading tokenizer...")
|
| 41 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 42 |
self.model_name,
|
| 43 |
trust_remote_code=True
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
raise ValueError("Failed to load tokenizer")
|
| 47 |
-
logger.info("Tokenizer loaded successfully")
|
| 48 |
|
| 49 |
-
# Then load the model
|
| 50 |
-
logger.info("Loading model...")
|
| 51 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
self.model_name,
|
| 53 |
-
torch_dtype=torch.float32,
|
| 54 |
low_cpu_mem_usage=True,
|
| 55 |
trust_remote_code=True
|
| 56 |
)
|
| 57 |
-
if self.model is None:
|
| 58 |
-
raise ValueError("Failed to load model")
|
| 59 |
|
| 60 |
-
# Move model to CPU explicitly
|
| 61 |
self.model = self.model.to("cpu")
|
| 62 |
-
logger.info("Model loaded successfully and moved to CPU")
|
| 63 |
|
| 64 |
-
# Set padding token if needed
|
| 65 |
if self.tokenizer.pad_token is None:
|
| 66 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 67 |
-
logger.info("Set padding token")
|
| 68 |
|
|
|
|
| 69 |
return True
|
| 70 |
-
|
| 71 |
except Exception as e:
|
| 72 |
logger.error(f"Error in model initialization: {str(e)}")
|
| 73 |
-
raise
|
| 74 |
|
| 75 |
def is_initialized(self):
|
| 76 |
-
"""Check if model is properly initialized"""
|
| 77 |
return (self.model is not None and
|
| 78 |
self.tokenizer is not None and
|
| 79 |
-
hasattr(self.model, 'generate')
|
| 80 |
-
hasattr(self.tokenizer, 'encode'))
|
| 81 |
|
| 82 |
def generate_response(self, message: str, history: List = None) -> str:
|
| 83 |
try:
|
| 84 |
if not self.is_initialized():
|
| 85 |
-
|
| 86 |
|
| 87 |
# Prepare prompt
|
| 88 |
prompt = self._prepare_prompt(message, history)
|
|
@@ -94,7 +78,7 @@ class HealthAssistant:
|
|
| 94 |
padding=True,
|
| 95 |
truncation=True,
|
| 96 |
max_length=512
|
| 97 |
-
)
|
| 98 |
|
| 99 |
# Generate
|
| 100 |
with torch.no_grad():
|
|
@@ -127,16 +111,16 @@ class HealthAssistant:
|
|
| 127 |
|
| 128 |
def _prepare_prompt(self, message: str, history: List = None) -> str:
|
| 129 |
parts = [
|
| 130 |
-
"You are a helpful healthcare assistant
|
| 131 |
self._get_health_context() or "No health data available yet."
|
| 132 |
]
|
| 133 |
|
| 134 |
if history:
|
| 135 |
parts.append("Previous conversation:")
|
| 136 |
-
for
|
| 137 |
parts.extend([
|
| 138 |
-
f"User: {
|
| 139 |
-
f"Assistant: {
|
| 140 |
])
|
| 141 |
|
| 142 |
parts.extend([
|
|
@@ -197,7 +181,7 @@ class GradioInterface:
|
|
| 197 |
def __init__(self):
|
| 198 |
try:
|
| 199 |
logger.info("Initializing Health Assistant...")
|
| 200 |
-
self.assistant = HealthAssistant(use_smaller_model=True)
|
| 201 |
if not self.assistant.is_initialized():
|
| 202 |
raise RuntimeError("Health Assistant failed to initialize properly")
|
| 203 |
logger.info("Health Assistant initialized successfully")
|
|
@@ -230,14 +214,15 @@ class GradioInterface:
|
|
| 230 |
return "❌ Error adding medication."
|
| 231 |
|
| 232 |
def create_interface(self):
|
| 233 |
-
with gr.Blocks(title="Health Assistant"
|
| 234 |
gr.Markdown("# 🏥 AI Health Assistant")
|
| 235 |
|
| 236 |
with gr.Tabs():
|
|
|
|
| 237 |
with gr.Tab("💬 Health Chat"):
|
| 238 |
chatbot = gr.Chatbot(
|
| 239 |
-
|
| 240 |
-
|
| 241 |
)
|
| 242 |
with gr.Row():
|
| 243 |
msg = gr.Textbox(
|
|
@@ -249,6 +234,7 @@ class GradioInterface:
|
|
| 249 |
send_btn = gr.Button("Send", scale=1)
|
| 250 |
clear_btn = gr.Button("Clear Chat")
|
| 251 |
|
|
|
|
| 252 |
with gr.Tab("📊 Health Metrics"):
|
| 253 |
with gr.Row():
|
| 254 |
weight_input = gr.Number(label="Weight (kg)")
|
|
@@ -257,6 +243,7 @@ class GradioInterface:
|
|
| 257 |
metrics_btn = gr.Button("Save Metrics")
|
| 258 |
metrics_status = gr.Markdown()
|
| 259 |
|
|
|
|
| 260 |
with gr.Tab("💊 Medication Manager"):
|
| 261 |
with gr.Row():
|
| 262 |
med_name = gr.Textbox(label="Medication Name")
|
|
@@ -266,6 +253,7 @@ class GradioInterface:
|
|
| 266 |
med_btn = gr.Button("Add Medication")
|
| 267 |
med_status = gr.Markdown()
|
| 268 |
|
|
|
|
| 269 |
msg.submit(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 270 |
send_btn.click(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 271 |
clear_btn.click(lambda: [], None, chatbot)
|
|
@@ -282,6 +270,8 @@ class GradioInterface:
|
|
| 282 |
outputs=[med_status]
|
| 283 |
)
|
| 284 |
|
|
|
|
|
|
|
| 285 |
return demo
|
| 286 |
|
| 287 |
def main():
|
|
@@ -291,10 +281,9 @@ def main():
|
|
| 291 |
demo = interface.create_interface()
|
| 292 |
logger.info("Launching Gradio interface...")
|
| 293 |
demo.launch(
|
| 294 |
-
share=False,
|
| 295 |
server_name="0.0.0.0",
|
| 296 |
server_port=7860,
|
| 297 |
-
|
| 298 |
)
|
| 299 |
except Exception as e:
|
| 300 |
logger.error(f"Error starting application: {e}")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import logging
|
| 5 |
from typing import List, Dict
|
| 6 |
import gc
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Setup logging
|
| 10 |
logging.basicConfig(
|
| 11 |
level=logging.INFO,
|
| 12 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 13 |
)
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Force CPU usage and set memory optimizations
|
| 17 |
+
torch.set_num_threads(4)
|
|
|
|
| 18 |
|
| 19 |
class HealthAssistant:
|
| 20 |
def __init__(self, use_smaller_model=True):
|
|
|
|
| 21 |
if use_smaller_model:
|
| 22 |
+
self.model_name = "facebook/opt-125m"
|
| 23 |
else:
|
| 24 |
self.model_name = "Qwen/Qwen2-VL-7B-Instruct"
|
| 25 |
|
|
|
|
| 33 |
try:
|
| 34 |
logger.info(f"Starting model initialization: {self.model_name}")
|
| 35 |
|
|
|
|
|
|
|
| 36 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 37 |
self.model_name,
|
| 38 |
trust_remote_code=True
|
| 39 |
)
|
| 40 |
+
logger.info("Tokenizer loaded")
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
self.model_name,
|
| 44 |
+
torch_dtype=torch.float32,
|
| 45 |
low_cpu_mem_usage=True,
|
| 46 |
trust_remote_code=True
|
| 47 |
)
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 49 |
self.model = self.model.to("cpu")
|
|
|
|
| 50 |
|
|
|
|
| 51 |
if self.tokenizer.pad_token is None:
|
| 52 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
|
|
|
| 53 |
|
| 54 |
+
logger.info("Model loaded successfully")
|
| 55 |
return True
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
logger.error(f"Error in model initialization: {str(e)}")
|
| 59 |
+
raise
|
| 60 |
|
| 61 |
def is_initialized(self):
|
|
|
|
| 62 |
return (self.model is not None and
|
| 63 |
self.tokenizer is not None and
|
| 64 |
+
hasattr(self.model, 'generate'))
|
|
|
|
| 65 |
|
| 66 |
def generate_response(self, message: str, history: List = None) -> str:
|
| 67 |
try:
|
| 68 |
if not self.is_initialized():
|
| 69 |
+
return "System is still initializing. Please try again in a moment."
|
| 70 |
|
| 71 |
# Prepare prompt
|
| 72 |
prompt = self._prepare_prompt(message, history)
|
|
|
|
| 78 |
padding=True,
|
| 79 |
truncation=True,
|
| 80 |
max_length=512
|
| 81 |
+
)
|
| 82 |
|
| 83 |
# Generate
|
| 84 |
with torch.no_grad():
|
|
|
|
| 111 |
|
| 112 |
def _prepare_prompt(self, message: str, history: List = None) -> str:
|
| 113 |
parts = [
|
| 114 |
+
"You are a helpful healthcare assistant providing accurate and helpful medical information.",
|
| 115 |
self._get_health_context() or "No health data available yet."
|
| 116 |
]
|
| 117 |
|
| 118 |
if history:
|
| 119 |
parts.append("Previous conversation:")
|
| 120 |
+
for h in history[-3:]:
|
| 121 |
parts.extend([
|
| 122 |
+
f"User: {h[0]}",
|
| 123 |
+
f"Assistant: {h[1]}"
|
| 124 |
])
|
| 125 |
|
| 126 |
parts.extend([
|
|
|
|
| 181 |
def __init__(self):
|
| 182 |
try:
|
| 183 |
logger.info("Initializing Health Assistant...")
|
| 184 |
+
self.assistant = HealthAssistant(use_smaller_model=True)
|
| 185 |
if not self.assistant.is_initialized():
|
| 186 |
raise RuntimeError("Health Assistant failed to initialize properly")
|
| 187 |
logger.info("Health Assistant initialized successfully")
|
|
|
|
| 214 |
return "❌ Error adding medication."
|
| 215 |
|
| 216 |
def create_interface(self):
|
| 217 |
+
with gr.Blocks(title="Health Assistant") as demo:
|
| 218 |
gr.Markdown("# 🏥 AI Health Assistant")
|
| 219 |
|
| 220 |
with gr.Tabs():
|
| 221 |
+
# Chat Interface
|
| 222 |
with gr.Tab("💬 Health Chat"):
|
| 223 |
chatbot = gr.Chatbot(
|
| 224 |
+
value=[],
|
| 225 |
+
height=450
|
| 226 |
)
|
| 227 |
with gr.Row():
|
| 228 |
msg = gr.Textbox(
|
|
|
|
| 234 |
send_btn = gr.Button("Send", scale=1)
|
| 235 |
clear_btn = gr.Button("Clear Chat")
|
| 236 |
|
| 237 |
+
# Health Metrics
|
| 238 |
with gr.Tab("📊 Health Metrics"):
|
| 239 |
with gr.Row():
|
| 240 |
weight_input = gr.Number(label="Weight (kg)")
|
|
|
|
| 243 |
metrics_btn = gr.Button("Save Metrics")
|
| 244 |
metrics_status = gr.Markdown()
|
| 245 |
|
| 246 |
+
# Medication Manager
|
| 247 |
with gr.Tab("💊 Medication Manager"):
|
| 248 |
with gr.Row():
|
| 249 |
med_name = gr.Textbox(label="Medication Name")
|
|
|
|
| 253 |
med_btn = gr.Button("Add Medication")
|
| 254 |
med_status = gr.Markdown()
|
| 255 |
|
| 256 |
+
# Event handlers
|
| 257 |
msg.submit(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 258 |
send_btn.click(self.chat_response, [msg, chatbot], [msg, chatbot])
|
| 259 |
clear_btn.click(lambda: [], None, chatbot)
|
|
|
|
| 270 |
outputs=[med_status]
|
| 271 |
)
|
| 272 |
|
| 273 |
+
demo.queue()
|
| 274 |
+
|
| 275 |
return demo
|
| 276 |
|
| 277 |
def main():
|
|
|
|
| 281 |
demo = interface.create_interface()
|
| 282 |
logger.info("Launching Gradio interface...")
|
| 283 |
demo.launch(
|
|
|
|
| 284 |
server_name="0.0.0.0",
|
| 285 |
server_port=7860,
|
| 286 |
+
share=False
|
| 287 |
)
|
| 288 |
except Exception as e:
|
| 289 |
logger.error(f"Error starting application: {e}")
|