sachinchandrankallar commited on
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 model in Hugging Face Spaces with optimized settings
4
- This tests the ultra-conservative memory settings for Spaces
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
- def test_gguf_spaces_optimization():
17
- """Test GGUF model with Spaces-optimized settings"""
18
-
19
- # Set environment variables for Hugging Face Spaces
20
- os.environ['HF_HOME'] = '/tmp/huggingface'
21
- os.environ['SPACE_ID'] = 'test_space' # Simulate being in a Space
22
- os.environ['GGUF_N_THREADS'] = '1'
23
- os.environ['GGUF_N_BATCH'] = '16'
24
 
25
  try:
26
- logger.info("Testing GGUF model with Spaces optimization...")
27
 
28
- # Test the exact model name from your API call
29
- model_name = "microsoft/Phi-3-mini-4k-instruct-gguf"
30
  filename = "Phi-3-mini-4k-instruct-q4.gguf"
31
 
32
- logger.info(f"Model: {model_name}")
33
- logger.info(f"Filename: {filename}")
34
- logger.info("Environment: Simulating Hugging Face Space")
35
 
36
- # Test import
37
- try:
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 model loading with timeout
45
- start_time = time.time()
46
- try:
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
- # Test basic generation with reduced tokens
64
- try:
65
- test_prompt = "Generate a brief medical summary: Patient has fever and cough."
66
- logger.info("Testing basic generation with reduced tokens...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- start_gen = time.time()
69
- result = pipeline.generate(test_prompt, max_tokens=50) # Reduced from 100
70
- gen_time = time.time() - start_gen
71
 
72
- logger.info(f"βœ“ Generation successful in {gen_time:.2f}s")
73
- logger.info(f"Generated text length: {len(result)} characters")
74
- logger.info(f"Sample output: {result[:100]}...")
75
 
76
- except Exception as e:
77
- logger.error(f"βœ— Generation failed: {e}")
78
- return False
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"βœ— Test failed with unexpected error: {e}")
101
- return False
102
 
103
- def test_fallback_pipeline():
104
- """Test the fallback pipeline when GGUF fails"""
 
 
105
  try:
106
- logger.info("Testing fallback pipeline...")
 
 
 
 
 
 
 
 
107
 
108
- from ai_med_extract.utils.model_loader_gguf import create_fallback_pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
- fallback = create_fallback_pipeline()
111
- result = fallback.generate("Test prompt")
 
112
 
113
- logger.info(f"βœ“ Fallback pipeline working: {len(result)} characters generated")
114
- return True
 
 
115
 
116
  except Exception as e:
117
- logger.error(f"βœ— Fallback pipeline failed: {e}")
118
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  def main():
121
  """Main test function"""
122
- logger.info("Starting GGUF Spaces optimization tests...")
 
123
 
124
- # Test 1: GGUF model with Spaces optimization
125
- gguf_success = test_gguf_spaces_optimization()
126
 
127
- # Test 2: Fallback pipeline
128
- fallback_success = test_fallback_pipeline()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  # Summary
131
- logger.info("\n" + "="*60)
132
- logger.info("SPACES OPTIMIZATION TEST SUMMARY")
133
- logger.info("="*60)
134
- logger.info(f"GGUF Spaces Optimization: {'βœ“ PASS' if gguf_success else 'βœ— FAIL'}")
135
- logger.info(f"Fallback Pipeline: {'βœ“ PASS' if fallback_success else 'βœ— PASS'}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
- if gguf_success:
138
- logger.info("πŸŽ‰ GGUF model is optimized for Hugging Face Spaces!")
139
- logger.info("Your API should work without 500 errors.")
140
- logger.info("Memory usage has been optimized for containerized environments.")
 
 
 
 
 
141
  else:
142
- logger.warning("⚠️ GGUF model still has issues. The fallback will be used.")
143
- logger.info("Your API will still work but with reduced functionality.")
144
 
145
- return gguf_success
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()