SmartHeal commited on
Commit
cfd566e
·
verified ·
1 Parent(s): 83e490e

Update src/ai_processor.py

Browse files
Files changed (1) hide show
  1. src/ai_processor.py +16 -1
src/ai_processor.py CHANGED
@@ -159,11 +159,24 @@ def _medgemma_generate_gpu_with_pipeline(
159
  except Exception:
160
  BitsAndBytesConfig = None
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  hf_token = token or os.getenv("HF_TOKEN")
163
  mid = model_id or "unsloth/medgemma-4b-it-bnb-4bit"
164
 
165
  # device / dtype
166
- use_cuda = torch.cuda.is_available()
167
  device = 0 if use_cuda else -1
168
  dtype = torch.bfloat16 if use_cuda else torch.float32
169
 
@@ -224,6 +237,8 @@ def _medgemma_generate_gpu_with_pipeline(
224
  return (text or "").strip() or "⚠️ Empty response"
225
 
226
 
 
 
227
  def generate_medgemma_report(
228
  patient_info: str,
229
  visual_results: Dict,
 
159
  except Exception:
160
  BitsAndBytesConfig = None
161
 
162
+ # <<< START OF FIX >>>
163
+ # Force CUDA initialization to prevent IndexError in bitsandbytes/triton check.
164
+ # This ensures the CUDA context is ready before transformers and bnb probe the device.
165
+ use_cuda = torch.cuda.is_available()
166
+ if use_cuda:
167
+ try:
168
+ torch.tensor([1.0]).cuda()
169
+ except Exception as e:
170
+ # If even this fails, CUDA is truly not working.
171
+ print(f"WARNING: CUDA pre-initialization failed: {e}")
172
+ use_cuda = False
173
+ # <<< END OF FIX >>>
174
+
175
  hf_token = token or os.getenv("HF_TOKEN")
176
  mid = model_id or "unsloth/medgemma-4b-it-bnb-4bit"
177
 
178
  # device / dtype
179
+ # use_cuda is already defined above
180
  device = 0 if use_cuda else -1
181
  dtype = torch.bfloat16 if use_cuda else torch.float32
182
 
 
237
  return (text or "").strip() or "⚠️ Empty response"
238
 
239
 
240
+
241
+
242
  def generate_medgemma_report(
243
  patient_info: str,
244
  visual_results: Dict,