Spaces:
Running
Running
Update src/ai_processor.py
Browse files- src/ai_processor.py +16 -6
src/ai_processor.py
CHANGED
|
@@ -45,9 +45,9 @@ class AIProcessor:
|
|
| 45 |
HfFolder.save_token(self.config.HF_TOKEN)
|
| 46 |
logging.info("HuggingFace token set successfully")
|
| 47 |
|
| 48 |
-
# YOLO detection on CPU
|
| 49 |
try:
|
| 50 |
-
self.models_cache['det'] = YOLO(self.config.YOLO_MODEL_PATH)
|
| 51 |
logging.info("✅ YOLO detection model loaded on CPU")
|
| 52 |
except Exception as e:
|
| 53 |
logging.warning(f"YOLO model not available: {e}")
|
|
@@ -171,7 +171,10 @@ class AIProcessor:
|
|
| 171 |
if not vs:
|
| 172 |
return "Clinical guidelines unavailable"
|
| 173 |
docs = vs.as_retriever(search_kwargs={'k':10}).invoke(query)
|
| 174 |
-
return '\n\n'.join(
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
@spaces.GPU(enable_queue=True, duration=120)
|
| 177 |
def generate_final_report(self, patient_info, visual_results, guideline_context, image_pil, max_new_tokens=None):
|
|
@@ -214,13 +217,21 @@ class AIProcessor:
|
|
| 214 |
do_sample=False
|
| 215 |
)
|
| 216 |
report = out[0]['generated_text'][-1].get('content','')
|
| 217 |
-
return report or self._generate_fallback_report(
|
|
|
|
|
|
|
| 218 |
|
| 219 |
def _generate_fallback_report(self, patient_info, visual_results, guideline_context):
|
| 220 |
"""Produce text-only fallback."""
|
| 221 |
dp = visual_results.get('detection_image_path','N/A')
|
| 222 |
sp = visual_results.get('segmentation_image_path','N/A')
|
| 223 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
def save_and_commit_image(self, image_pil):
|
| 226 |
"""Save locally and optionally to HuggingFace."""
|
|
@@ -260,7 +271,6 @@ class AIProcessor:
|
|
| 260 |
image = Image.open(image)
|
| 261 |
return self.full_analysis_pipeline(image, questionnaire_data)
|
| 262 |
|
| 263 |
-
|
| 264 |
def _assess_risk_legacy(self, questionnaire_data):
|
| 265 |
"""Legacy risk assessment for backward compatibility"""
|
| 266 |
risk_factors = []
|
|
|
|
| 45 |
HfFolder.save_token(self.config.HF_TOKEN)
|
| 46 |
logging.info("HuggingFace token set successfully")
|
| 47 |
|
| 48 |
+
# YOLO detection on CPU (force CPU to avoid CUDA init)
|
| 49 |
try:
|
| 50 |
+
self.models_cache['det'] = YOLO(self.config.YOLO_MODEL_PATH, device='cpu')
|
| 51 |
logging.info("✅ YOLO detection model loaded on CPU")
|
| 52 |
except Exception as e:
|
| 53 |
logging.warning(f"YOLO model not available: {e}")
|
|
|
|
| 171 |
if not vs:
|
| 172 |
return "Clinical guidelines unavailable"
|
| 173 |
docs = vs.as_retriever(search_kwargs={'k':10}).invoke(query)
|
| 174 |
+
return '\n\n'.join(
|
| 175 |
+
f"Source: {d.metadata.get('source','?')}, Page: {d.metadata.get('page','?')}\n{d.page_content}"
|
| 176 |
+
for d in docs
|
| 177 |
+
)
|
| 178 |
|
| 179 |
@spaces.GPU(enable_queue=True, duration=120)
|
| 180 |
def generate_final_report(self, patient_info, visual_results, guideline_context, image_pil, max_new_tokens=None):
|
|
|
|
| 217 |
do_sample=False
|
| 218 |
)
|
| 219 |
report = out[0]['generated_text'][-1].get('content','')
|
| 220 |
+
return report or self._generate_fallback_report(
|
| 221 |
+
patient_info, visual_results, guideline_context
|
| 222 |
+
)
|
| 223 |
|
| 224 |
def _generate_fallback_report(self, patient_info, visual_results, guideline_context):
|
| 225 |
"""Produce text-only fallback."""
|
| 226 |
dp = visual_results.get('detection_image_path','N/A')
|
| 227 |
sp = visual_results.get('segmentation_image_path','N/A')
|
| 228 |
+
return (
|
| 229 |
+
f"# Report\n{patient_info}\n"
|
| 230 |
+
f"Type: {visual_results['wound_type']}\n"
|
| 231 |
+
f"Detection Image: {dp}\n"
|
| 232 |
+
f"Segmentation Image: {sp}\n"
|
| 233 |
+
f"Guidelines: {guideline_context[:200]}..."
|
| 234 |
+
)
|
| 235 |
|
| 236 |
def save_and_commit_image(self, image_pil):
|
| 237 |
"""Save locally and optionally to HuggingFace."""
|
|
|
|
| 271 |
image = Image.open(image)
|
| 272 |
return self.full_analysis_pipeline(image, questionnaire_data)
|
| 273 |
|
|
|
|
| 274 |
def _assess_risk_legacy(self, questionnaire_data):
|
| 275 |
"""Legacy risk assessment for backward compatibility"""
|
| 276 |
risk_factors = []
|