Spaces:
Paused
Paused
Commit
Β·
16f55db
1
Parent(s):
c6f267d
optimization
Browse files
ai_med_extract/agents/__pycache__/patient_summary_agent.cpython-311.pyc
CHANGED
|
Binary files a/ai_med_extract/agents/__pycache__/patient_summary_agent.cpython-311.pyc and b/ai_med_extract/agents/__pycache__/patient_summary_agent.cpython-311.pyc differ
|
|
|
ai_med_extract/utils/__pycache__/model_loader_gguf.cpython-311.pyc
CHANGED
|
Binary files a/ai_med_extract/utils/__pycache__/model_loader_gguf.cpython-311.pyc and b/ai_med_extract/utils/__pycache__/model_loader_gguf.cpython-311.pyc differ
|
|
|
ai_med_extract/utils/__pycache__/model_manager.cpython-311.pyc
ADDED
|
Binary file (22 kB). View file
|
|
|
test_gguf_spaces.py
CHANGED
|
@@ -1,148 +1,249 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Test script for GGUF
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
-
import time
|
| 10 |
import logging
|
|
|
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
|
| 28 |
-
# Test the
|
| 29 |
-
|
| 30 |
filename = "Phi-3-mini-4k-instruct-q4.gguf"
|
| 31 |
|
| 32 |
-
logger.info(f"
|
| 33 |
-
logger.info(f"Filename: {filename}")
|
| 34 |
-
logger.info("Environment: Simulating Hugging Face Space")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
from ai_med_extract.utils.model_loader_gguf import GGUFModelPipeline
|
| 39 |
-
logger.info("β GGUFModelPipeline import successful")
|
| 40 |
-
except ImportError as e:
|
| 41 |
-
logger.error(f"β Failed to import GGUFModelPipeline: {e}")
|
| 42 |
-
return False
|
| 43 |
|
| 44 |
-
# Test
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
pipeline = GGUFModelPipeline(model_name, filename, timeout=300)
|
| 48 |
-
load_time = time.time() - start_time
|
| 49 |
-
logger.info(f"β Model loaded successfully in {load_time:.2f}s")
|
| 50 |
-
|
| 51 |
-
# Check if Spaces optimization was applied
|
| 52 |
-
if hasattr(pipeline, 'model'):
|
| 53 |
-
model = pipeline.model
|
| 54 |
-
logger.info(f"β Context window: {getattr(model, 'n_ctx', 'N/A')}")
|
| 55 |
-
logger.info(f"β Threads: {getattr(model, 'n_threads', 'N/A')}")
|
| 56 |
-
logger.info(f"β Batch size: {getattr(model, 'n_batch', 'N/A')}")
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
load_time = time.time() - start_time
|
| 60 |
-
logger.error(f"β Model loading failed after {load_time:.2f}s: {e}")
|
| 61 |
-
return False
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
logger.info(f"
|
| 73 |
-
logger.info(f"
|
| 74 |
-
logger.info(f"Sample output: {result[:100]}...")
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# Test memory usage
|
| 81 |
-
try:
|
| 82 |
-
import psutil
|
| 83 |
-
process = psutil.Process()
|
| 84 |
-
memory_info = process.memory_info()
|
| 85 |
-
memory_mb = memory_info.rss / 1024 / 1024
|
| 86 |
-
logger.info(f"β Memory usage: {memory_mb:.1f} MB")
|
| 87 |
|
| 88 |
-
if memory_mb > 8000: # 8GB warning
|
| 89 |
-
logger.warning(f"β High memory usage: {memory_mb:.1f} MB")
|
| 90 |
-
else:
|
| 91 |
-
logger.info("β Memory usage within acceptable limits")
|
| 92 |
-
|
| 93 |
-
except ImportError:
|
| 94 |
-
logger.info("β psutil not available - cannot check memory usage")
|
| 95 |
-
|
| 96 |
-
logger.info("π All tests passed! GGUF model is optimized for Spaces.")
|
| 97 |
-
return True
|
| 98 |
-
|
| 99 |
except Exception as e:
|
| 100 |
-
logger.error(f"
|
| 101 |
-
return False
|
| 102 |
|
| 103 |
-
def
|
| 104 |
-
"""Test
|
|
|
|
|
|
|
| 105 |
try:
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
-
logger.info(f"
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
-
logger.error(f"
|
| 118 |
-
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def main():
|
| 121 |
"""Main test function"""
|
| 122 |
-
logger.info("Starting GGUF Spaces
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
gguf_success = test_gguf_spaces_optimization()
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# Summary
|
| 131 |
-
logger.info("\n" + "="*
|
| 132 |
-
logger.info("
|
| 133 |
-
logger.info("="*
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
logger.info("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
else:
|
| 142 |
-
logger.
|
| 143 |
-
logger.
|
| 144 |
|
| 145 |
-
return
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
success = main()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Test script for GGUF models on Hugging Face Spaces
|
| 4 |
+
Specifically tests the patient summary generation with GGUF models
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
| 8 |
import sys
|
|
|
|
| 9 |
import logging
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
# Set environment variables for Hugging Face Spaces
|
| 17 |
+
os.environ['HF_HOME'] = '/tmp/huggingface'
|
| 18 |
+
os.environ['GGUF_N_THREADS'] = '1' # Single thread for Spaces
|
| 19 |
+
os.environ['GGUF_N_BATCH'] = '16' # Small batch size for Spaces
|
| 20 |
+
|
| 21 |
+
def test_gguf_model_direct():
|
| 22 |
+
"""Test GGUF model loading directly"""
|
| 23 |
+
logger.info("π§ͺ Testing GGUF Model Loading Directly...")
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
from ai_med_extract.utils.model_loader_gguf import GGUFModelPipeline
|
| 27 |
|
| 28 |
+
# Test with the specific model and filename
|
| 29 |
+
model_repo = "microsoft/Phi-3-mini-4k-instruct-gguf"
|
| 30 |
filename = "Phi-3-mini-4k-instruct-q4.gguf"
|
| 31 |
|
| 32 |
+
logger.info(f"Loading GGUF model: {model_repo}/{filename}")
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Create pipeline directly
|
| 35 |
+
pipeline = GGUFModelPipeline(model_repo, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Test generation
|
| 38 |
+
prompt = "Generate a brief medical summary: Patient has fever and cough."
|
| 39 |
+
result = pipeline.generate(prompt, max_tokens=100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
logger.info(f"β
Direct GGUF test successful: {len(result)} characters")
|
| 42 |
+
logger.info(f"Sample output: {result[:200]}...")
|
| 43 |
+
|
| 44 |
+
return True, result
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.error(f"β Direct GGUF test failed: {e}")
|
| 48 |
+
return False, str(e)
|
| 49 |
+
|
| 50 |
+
def test_gguf_via_model_manager():
|
| 51 |
+
"""Test GGUF model via the unified model manager"""
|
| 52 |
+
logger.info("π§ͺ Testing GGUF Model via Model Manager...")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
sys.path.append('.')
|
| 56 |
+
from ai_med_extract.utils.model_manager import model_manager
|
| 57 |
+
|
| 58 |
+
# Get GGUF model loader
|
| 59 |
+
loader = model_manager.get_model_loader(
|
| 60 |
+
"microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 61 |
+
"gguf",
|
| 62 |
+
"Phi-3-mini-4k-instruct-q4.gguf"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Check if it's actually using GGUF or fallback
|
| 66 |
+
model_info = loader.get_model_info()
|
| 67 |
+
logger.info(f"Model info: {model_info}")
|
| 68 |
+
|
| 69 |
+
if model_info['type'] == 'gguf':
|
| 70 |
+
logger.info("β
GGUF model loaded successfully via manager")
|
| 71 |
|
| 72 |
+
# Test generation
|
| 73 |
+
prompt = "Generate a brief medical summary: Patient has chest pain."
|
| 74 |
+
result = loader.generate(prompt, max_tokens=100)
|
| 75 |
|
| 76 |
+
logger.info(f"β
GGUF generation via manager: {len(result)} characters")
|
| 77 |
+
logger.info(f"Sample output: {result[:200]}...")
|
|
|
|
| 78 |
|
| 79 |
+
return True, result
|
| 80 |
+
else:
|
| 81 |
+
logger.warning("β οΈ Model manager returned fallback instead of GGUF")
|
| 82 |
+
return False, "Fallback model used"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
+
logger.error(f"β GGUF via model manager test failed: {e}")
|
| 86 |
+
return False, str(e)
|
| 87 |
|
| 88 |
+
def test_patient_summarizer_gguf():
|
| 89 |
+
"""Test patient summarizer with GGUF model"""
|
| 90 |
+
logger.info("π§ͺ Testing Patient Summarizer with GGUF Model...")
|
| 91 |
+
|
| 92 |
try:
|
| 93 |
+
sys.path.append('.')
|
| 94 |
+
from ai_med_extract.agents.patient_summary_agent import PatientSummarizerAgent
|
| 95 |
+
|
| 96 |
+
# Create agent with GGUF model
|
| 97 |
+
agent = PatientSummarizerAgent(
|
| 98 |
+
"microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 99 |
+
"gguf",
|
| 100 |
+
"Phi-3-mini-4k-instruct-q4.gguf"
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
# Sample patient data
|
| 104 |
+
sample_data = {
|
| 105 |
+
"result": {
|
| 106 |
+
"patientname": "John Doe",
|
| 107 |
+
"patientnumber": "12345",
|
| 108 |
+
"agey": "45",
|
| 109 |
+
"gender": "Male",
|
| 110 |
+
"allergies": ["Penicillin"],
|
| 111 |
+
"social_history": "Non-smoker, occasional alcohol",
|
| 112 |
+
"past_medical_history": ["Hypertension", "Diabetes"],
|
| 113 |
+
"encounters": [
|
| 114 |
+
{
|
| 115 |
+
"visit_date": "2024-01-15",
|
| 116 |
+
"chief_complaint": "Chest pain",
|
| 117 |
+
"symptoms": "Sharp chest pain, shortness of breath",
|
| 118 |
+
"diagnosis": ["Angina", "Hypertension"],
|
| 119 |
+
"dr_notes": "Patient reports chest pain for 2 days",
|
| 120 |
+
"vitals": {"BP": "140/90", "HR": "85", "SpO2": "98%"},
|
| 121 |
+
"medications": ["Aspirin", "Metoprolol"],
|
| 122 |
+
"treatment": "Prescribed medications, follow-up in 1 week"
|
| 123 |
+
}
|
| 124 |
+
]
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
|
| 128 |
+
# Generate clinical summary
|
| 129 |
+
logger.info("Generating clinical summary...")
|
| 130 |
+
summary = agent.generate_clinical_summary(sample_data)
|
| 131 |
|
| 132 |
+
logger.info(f"β
Patient summary generated: {len(summary)} characters")
|
| 133 |
+
logger.info(f"Summary preview: {summary[:300]}...")
|
| 134 |
+
|
| 135 |
+
return True, summary
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
+
logger.error(f"β Patient summarizer GGUF test failed: {e}")
|
| 139 |
+
return False, str(e)
|
| 140 |
+
|
| 141 |
+
def test_huggingface_spaces_optimization():
|
| 142 |
+
"""Test Hugging Face Spaces optimization features"""
|
| 143 |
+
logger.info("π§ͺ Testing Hugging Face Spaces Optimization...")
|
| 144 |
+
|
| 145 |
+
try:
|
| 146 |
+
# Check if we're in a Hugging Face Space
|
| 147 |
+
is_hf_space = os.environ.get('SPACE_ID') is not None
|
| 148 |
+
|
| 149 |
+
if is_hf_space:
|
| 150 |
+
logger.info("π Detected Hugging Face Space - testing optimization...")
|
| 151 |
+
|
| 152 |
+
# Test with ultra-conservative settings
|
| 153 |
+
os.environ['GGUF_N_THREADS'] = '1'
|
| 154 |
+
os.environ['GGUF_N_BATCH'] = '16'
|
| 155 |
+
|
| 156 |
+
# Test model loading with optimized settings
|
| 157 |
+
from ai_med_extract.utils.model_loader_gguf import GGUFModelPipeline
|
| 158 |
+
|
| 159 |
+
pipeline = GGUFModelPipeline(
|
| 160 |
+
"microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 161 |
+
"Phi-3-mini-4k-instruct-q4.gguf"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Quick test
|
| 165 |
+
result = pipeline.generate("Test prompt", max_tokens=50)
|
| 166 |
+
|
| 167 |
+
logger.info(f"β
Spaces optimization test passed: {len(result)} characters")
|
| 168 |
+
return True, "Spaces optimization working"
|
| 169 |
+
else:
|
| 170 |
+
logger.info("π Local environment detected - spaces optimization not applicable")
|
| 171 |
+
return True, "Local environment"
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
logger.error(f"β Spaces optimization test failed: {e}")
|
| 175 |
+
return False, str(e)
|
| 176 |
|
| 177 |
def main():
|
| 178 |
"""Main test function"""
|
| 179 |
+
logger.info("π Starting GGUF Hugging Face Spaces Tests...")
|
| 180 |
+
logger.info("=" * 70)
|
| 181 |
|
| 182 |
+
test_results = []
|
|
|
|
| 183 |
|
| 184 |
+
# Run all tests
|
| 185 |
+
tests = [
|
| 186 |
+
("Direct GGUF Loading", test_gguf_model_direct),
|
| 187 |
+
("GGUF via Model Manager", test_gguf_via_model_manager),
|
| 188 |
+
("Patient Summarizer GGUF", test_patient_summarizer_gguf),
|
| 189 |
+
("Spaces Optimization", test_huggingface_spaces_optimization)
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
+
for test_name, test_func in tests:
|
| 193 |
+
logger.info(f"\nπ§ͺ Running {test_name} Test...")
|
| 194 |
+
try:
|
| 195 |
+
start_time = time.time()
|
| 196 |
+
result, output = test_func()
|
| 197 |
+
end_time = time.time()
|
| 198 |
+
|
| 199 |
+
test_results.append((test_name, result, end_time - start_time))
|
| 200 |
+
|
| 201 |
+
if result:
|
| 202 |
+
logger.info(f"β
{test_name} PASSED in {end_time - start_time:.2f}s")
|
| 203 |
+
else:
|
| 204 |
+
logger.warning(f"β οΈ {test_name} FAILED in {end_time - start_time:.2f}s")
|
| 205 |
+
logger.warning(f"Output: {output}")
|
| 206 |
+
|
| 207 |
+
except Exception as e:
|
| 208 |
+
logger.error(f"β {test_name} test crashed: {e}")
|
| 209 |
+
test_results.append((test_name, False, 0))
|
| 210 |
|
| 211 |
# Summary
|
| 212 |
+
logger.info("\n" + "=" * 70)
|
| 213 |
+
logger.info("π TEST SUMMARY")
|
| 214 |
+
logger.info("=" * 70)
|
| 215 |
+
|
| 216 |
+
passed = 0
|
| 217 |
+
total = len(test_results)
|
| 218 |
+
|
| 219 |
+
for test_name, result, duration in test_results:
|
| 220 |
+
status = "β
PASS" if result else "β FAIL"
|
| 221 |
+
logger.info(f"{test_name}: {status} ({duration:.2f}s)")
|
| 222 |
+
if result:
|
| 223 |
+
passed += 1
|
| 224 |
+
|
| 225 |
+
logger.info(f"\nOverall: {passed}/{total} tests passed")
|
| 226 |
+
|
| 227 |
+
if passed == total:
|
| 228 |
+
logger.info("π All tests passed! GGUF models are working perfectly on Hugging Face Spaces!")
|
| 229 |
+
logger.info("β¨ You can now use GGUF models for patient summaries!")
|
| 230 |
+
else:
|
| 231 |
+
logger.warning(f"β οΈ {total - passed} tests failed. Check the logs above for details.")
|
| 232 |
|
| 233 |
+
# Recommendations for Spaces
|
| 234 |
+
logger.info("\nπ‘ RECOMMENDATIONS FOR HUGGING FACE SPACES:")
|
| 235 |
+
if passed >= total * 0.8:
|
| 236 |
+
logger.info("β
System is ready for production use on Spaces")
|
| 237 |
+
logger.info("β
GGUF models are optimized for memory constraints")
|
| 238 |
+
logger.info("β
Patient summaries will work with real GGUF models")
|
| 239 |
+
elif passed >= total * 0.6:
|
| 240 |
+
logger.info("β οΈ System is mostly working but has some issues")
|
| 241 |
+
logger.info("β οΈ GGUF models may need configuration adjustments")
|
| 242 |
else:
|
| 243 |
+
logger.error("β System has significant issues with GGUF models")
|
| 244 |
+
logger.error("β Review and fix failed tests before deployment")
|
| 245 |
|
| 246 |
+
return passed == total
|
| 247 |
|
| 248 |
if __name__ == "__main__":
|
| 249 |
success = main()
|