Spaces:
Sleeping
Sleeping
YanBoChen
commited on
Commit
·
4c919d2
1
Parent(s):
acc25ea
feat(llm_clients): enhance MeditronClient to support local model loading and improve error handling
Browse files- src/llm_clients.py +117 -39
src/llm_clients.py
CHANGED
|
@@ -10,6 +10,8 @@ Date: 2025-07-29
|
|
| 10 |
import logging
|
| 11 |
import os
|
| 12 |
from typing import Dict, Optional
|
|
|
|
|
|
|
| 13 |
from huggingface_hub import InferenceClient
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
|
@@ -19,34 +21,72 @@ load_dotenv()
|
|
| 19 |
class MeditronClient:
|
| 20 |
def __init__(
|
| 21 |
self,
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
timeout: float = 30.0
|
| 24 |
):
|
| 25 |
"""
|
| 26 |
-
Initialize Meditron
|
| 27 |
|
| 28 |
Args:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
Warning: This model should not be used for professional medical advice.
|
| 33 |
"""
|
| 34 |
-
# Get HF token from environment variable
|
| 35 |
-
hf_token = os.getenv('HF_TOKEN')
|
| 36 |
-
if not hf_token:
|
| 37 |
-
raise ValueError(
|
| 38 |
-
"HF_TOKEN not found in environment variables. "
|
| 39 |
-
"Please set HF_TOKEN in your .env file or environment."
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
self.client = InferenceClient(model=model, token=hf_token)
|
| 43 |
self.logger = logging.getLogger(__name__)
|
| 44 |
self.timeout = timeout
|
| 45 |
-
self.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def analyze_medical_query(
|
| 52 |
self,
|
|
@@ -60,7 +100,7 @@ class MeditronClient:
|
|
| 60 |
Args:
|
| 61 |
query: Medical query text
|
| 62 |
max_tokens: Maximum tokens to generate
|
| 63 |
-
timeout: Specific API call timeout
|
| 64 |
|
| 65 |
Returns:
|
| 66 |
Extracted medical condition information
|
|
@@ -78,34 +118,67 @@ DO NOT provide medical advice.
|
|
| 78 |
<|im_start|>assistant
|
| 79 |
"""
|
| 80 |
|
| 81 |
-
self.logger.info(f"Calling Meditron
|
| 82 |
-
|
| 83 |
-
# Remove timeout parameter as InferenceClient doesn't support it
|
| 84 |
-
response = self.client.text_generation(
|
| 85 |
-
prompt,
|
| 86 |
-
max_new_tokens=max_tokens,
|
| 87 |
-
temperature=0.7,
|
| 88 |
-
top_k=50
|
| 89 |
-
)
|
| 90 |
|
| 91 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# Extract condition from response
|
| 94 |
-
extracted_condition = self._extract_condition(
|
| 95 |
|
| 96 |
return {
|
| 97 |
'extracted_condition': extracted_condition,
|
| 98 |
'confidence': 0.8,
|
| 99 |
-
'raw_response':
|
| 100 |
}
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
-
self.logger.error(f"Meditron
|
| 104 |
self.logger.error(f"Error type: {type(e).__name__}")
|
|
|
|
| 105 |
return {
|
| 106 |
'extracted_condition': '',
|
| 107 |
'confidence': 0,
|
| 108 |
-
'error': str(e)
|
| 109 |
}
|
| 110 |
|
| 111 |
def _extract_condition(self, response: str) -> str:
|
|
@@ -135,7 +208,12 @@ def main():
|
|
| 135 |
Test Meditron client functionality
|
| 136 |
"""
|
| 137 |
try:
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
test_queries = [
|
| 140 |
"patient experiencing chest pain",
|
| 141 |
"sudden weakness on one side",
|
|
@@ -154,12 +232,12 @@ def main():
|
|
| 154 |
except Exception as e:
|
| 155 |
print(f"Client initialization error: {str(e)}")
|
| 156 |
print("This might be due to:")
|
| 157 |
-
print("1.
|
| 158 |
-
print("2.
|
| 159 |
-
print("3.
|
| 160 |
print("\nTo fix:")
|
| 161 |
-
print("1.
|
| 162 |
-
print("2.
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
| 165 |
main()
|
|
|
|
| 10 |
import logging
|
| 11 |
import os
|
| 12 |
from typing import Dict, Optional
|
| 13 |
+
import torch
|
| 14 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 15 |
from huggingface_hub import InferenceClient
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
|
|
|
|
| 21 |
class MeditronClient:
|
| 22 |
def __init__(
|
| 23 |
self,
|
| 24 |
+
model_name: str = "TheBloke/meditron-7B-GPTQ",
|
| 25 |
+
local_model_path: Optional[str] = None,
|
| 26 |
+
use_local: bool = False,
|
| 27 |
timeout: float = 30.0
|
| 28 |
):
|
| 29 |
"""
|
| 30 |
+
Initialize Meditron client for medical query processing.
|
| 31 |
|
| 32 |
Args:
|
| 33 |
+
model_name: Hugging Face model name
|
| 34 |
+
local_model_path: Path to local model files
|
| 35 |
+
use_local: Flag to use local model
|
| 36 |
+
timeout: API call timeout duration
|
| 37 |
|
| 38 |
Warning: This model should not be used for professional medical advice.
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
self.logger = logging.getLogger(__name__)
|
| 41 |
self.timeout = timeout
|
| 42 |
+
self.use_local = use_local
|
| 43 |
+
|
| 44 |
+
if use_local:
|
| 45 |
+
if not local_model_path:
|
| 46 |
+
raise ValueError("local_model_path must be provided when use_local is True")
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
# Load local model using Hugging Face transformers
|
| 50 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
+
model_name,
|
| 52 |
+
local_files_only=True,
|
| 53 |
+
cache_dir=local_model_path
|
| 54 |
+
)
|
| 55 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 56 |
+
model_name,
|
| 57 |
+
local_files_only=True,
|
| 58 |
+
cache_dir=local_model_path,
|
| 59 |
+
device_map="auto",
|
| 60 |
+
torch_dtype=torch.float16
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
self.logger.info(f"Local Meditron model loaded from: {local_model_path}")
|
| 64 |
+
self.logger.warning(
|
| 65 |
+
"Meditron Model: Research tool only. "
|
| 66 |
+
"Not for professional medical diagnosis."
|
| 67 |
+
)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
self.logger.error(f"Failed to load local model: {str(e)}")
|
| 70 |
+
raise ValueError(f"Failed to initialize local Meditron client: {str(e)}")
|
| 71 |
+
else:
|
| 72 |
+
# Existing InferenceClient logic
|
| 73 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 74 |
+
if not hf_token:
|
| 75 |
+
raise ValueError(
|
| 76 |
+
"HF_TOKEN not found in environment variables. "
|
| 77 |
+
"Please set HF_TOKEN in your .env file or environment."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
self.client = InferenceClient(model=model_name, token=hf_token)
|
| 82 |
+
self.logger.info(f"Meditron client initialized with model: {model_name}")
|
| 83 |
+
self.logger.warning(
|
| 84 |
+
"Meditron Model: Research tool only. "
|
| 85 |
+
"Not for professional medical diagnosis."
|
| 86 |
+
)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
self.logger.error(f"Failed to initialize InferenceClient: {str(e)}")
|
| 89 |
+
raise ValueError(f"Failed to initialize Meditron client: {str(e)}")
|
| 90 |
|
| 91 |
def analyze_medical_query(
|
| 92 |
self,
|
|
|
|
| 100 |
Args:
|
| 101 |
query: Medical query text
|
| 102 |
max_tokens: Maximum tokens to generate
|
| 103 |
+
timeout: Specific API call timeout
|
| 104 |
|
| 105 |
Returns:
|
| 106 |
Extracted medical condition information
|
|
|
|
| 118 |
<|im_start|>assistant
|
| 119 |
"""
|
| 120 |
|
| 121 |
+
self.logger.info(f"Calling Meditron with query: {query}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
if self.use_local:
|
| 124 |
+
# Local model inference
|
| 125 |
+
input_ids = self.tokenizer(prompt, return_tensors='pt').input_ids.to(self.model.device)
|
| 126 |
+
|
| 127 |
+
response = self.model.generate(
|
| 128 |
+
input_ids,
|
| 129 |
+
max_new_tokens=max_tokens,
|
| 130 |
+
temperature=0.7,
|
| 131 |
+
do_sample=True,
|
| 132 |
+
top_k=50
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
response_text = self.tokenizer.decode(response[0], skip_special_tokens=True)
|
| 136 |
+
self.logger.info(f"Local model response: {response_text}")
|
| 137 |
+
else:
|
| 138 |
+
# InferenceClient inference
|
| 139 |
+
self.logger.info(f"Using model: {self.client.model}")
|
| 140 |
+
|
| 141 |
+
# Test API connection first
|
| 142 |
+
try:
|
| 143 |
+
test_response = self.client.text_generation(
|
| 144 |
+
"Hello",
|
| 145 |
+
max_new_tokens=5,
|
| 146 |
+
temperature=0.7,
|
| 147 |
+
top_k=50
|
| 148 |
+
)
|
| 149 |
+
self.logger.info("API connection test successful")
|
| 150 |
+
except Exception as test_error:
|
| 151 |
+
self.logger.error(f"API connection test failed: {str(test_error)}")
|
| 152 |
+
return {
|
| 153 |
+
'extracted_condition': '',
|
| 154 |
+
'confidence': 0,
|
| 155 |
+
'error': f"API connection failed: {str(test_error)}"
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
response_text = self.client.text_generation(
|
| 159 |
+
prompt,
|
| 160 |
+
max_new_tokens=max_tokens,
|
| 161 |
+
temperature=0.7,
|
| 162 |
+
top_k=50
|
| 163 |
+
)
|
| 164 |
|
| 165 |
# Extract condition from response
|
| 166 |
+
extracted_condition = self._extract_condition(response_text)
|
| 167 |
|
| 168 |
return {
|
| 169 |
'extracted_condition': extracted_condition,
|
| 170 |
'confidence': 0.8,
|
| 171 |
+
'raw_response': response_text
|
| 172 |
}
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
+
self.logger.error(f"Meditron query error: {str(e)}")
|
| 176 |
self.logger.error(f"Error type: {type(e).__name__}")
|
| 177 |
+
self.logger.error(f"Error details: {repr(e)}")
|
| 178 |
return {
|
| 179 |
'extracted_condition': '',
|
| 180 |
'confidence': 0,
|
| 181 |
+
'error': f"{type(e).__name__}: {str(e)}"
|
| 182 |
}
|
| 183 |
|
| 184 |
def _extract_condition(self, response: str) -> str:
|
|
|
|
| 208 |
Test Meditron client functionality
|
| 209 |
"""
|
| 210 |
try:
|
| 211 |
+
# Test local model loading
|
| 212 |
+
client = MeditronClient(
|
| 213 |
+
local_model_path="/Users/yanbochen/Documents/Life in Canada/CS study related/*Student Course, Guide/CS7180 GenAI/FinalProject_git_copy/models/cache/meditron-7B-GPTQ",
|
| 214 |
+
use_local=True
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
test_queries = [
|
| 218 |
"patient experiencing chest pain",
|
| 219 |
"sudden weakness on one side",
|
|
|
|
| 232 |
except Exception as e:
|
| 233 |
print(f"Client initialization error: {str(e)}")
|
| 234 |
print("This might be due to:")
|
| 235 |
+
print("1. Incorrect local model path")
|
| 236 |
+
print("2. Missing dependencies")
|
| 237 |
+
print("3. Hardware limitations")
|
| 238 |
print("\nTo fix:")
|
| 239 |
+
print("1. Verify local model path")
|
| 240 |
+
print("2. Install required dependencies")
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
main()
|