@@ -10,7 +10,9 @@ def __init__(self, model_name="gpt-4o-mini"):
1010
1111 def explain (self , patient_data , prob , feature_explanation = None , similar_cases = None ):
1212 if self .client is None :
13- return self ._fallback_explanation (patient_data , prob , feature_explanation , similar_cases )
13+ return self ._fallback_explanation (
14+ patient_data , prob , feature_explanation , similar_cases
15+ )
1416
1517 prompt = f"""
1618 Patient data: { patient_data }
@@ -23,14 +25,15 @@ def explain(self, patient_data, prob, feature_explanation=None, similar_cases=No
2325
2426 try :
2527 response = self .client .chat .completions .create (
26- model = self .model_name ,
27- messages = [{"role" : "user" , "content" : prompt }]
28+ model = self .model_name , messages = [{"role" : "user" , "content" : prompt }]
2829 )
2930 return response .choices [0 ].message .content
3031 except Exception as exc :
3132 return f"{ self ._fallback_explanation (patient_data , prob , feature_explanation , similar_cases )} LLM explanation unavailable: { exc } "
3233
33- def _fallback_explanation (self , patient_data , prob , feature_explanation = None , similar_cases = None ):
34+ def _fallback_explanation (
35+ self , patient_data , prob , feature_explanation = None , similar_cases = None
36+ ):
3437 age = patient_data ["age" ]
3538 bmi = patient_data ["bmi" ]
3639 bp = patient_data ["bp" ]
@@ -55,7 +58,9 @@ def _fallback_explanation(self, patient_data, prob, feature_explanation=None, si
5558 top_impacts = self ._top_feature_text (feature_explanation )
5659 memory_text = ""
5760 if similar_cases :
58- memory_text = f" I found { len (similar_cases )} similar prior case(s) in memory for comparison."
61+ memory_text = (
62+ f" I found { len (similar_cases )} similar prior case(s) in memory for comparison."
63+ )
5964
6065 return (
6166 f"The model estimated a { prob :.1%} risk using a calculated BMI of { bmi :.1f} . "
@@ -67,9 +72,6 @@ def _top_feature_text(self, feature_explanation):
6772 return ""
6873
6974 top_features = feature_explanation ["features" ][:3 ]
70- parts = [
71- f"{ item ['feature' ]} { item ['direction' ]} "
72- for item in top_features
73- ]
75+ parts = [f"{ item ['feature' ]} { item ['direction' ]} " for item in top_features ]
7476 method = feature_explanation .get ("method" , "model" )
7577 return f"The { method .upper ()} explanation highlights: { ', ' .join (parts )} . "
0 commit comments