Update app.py
Browse files
app.py
CHANGED
|
@@ -227,33 +227,48 @@ def analyze_symptoms(text):
|
|
| 227 |
prediction = "No health condition detected"
|
| 228 |
score = 0.0
|
| 229 |
|
|
|
|
| 230 |
if result is None:
|
| 231 |
logger.warning("Model output is None")
|
| 232 |
elif isinstance(result, (str, int, float, bool)):
|
| 233 |
logger.warning(f"Invalid model output type: {type(result)}, value: {result}")
|
| 234 |
-
elif isinstance(result, tuple):
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
elif isinstance(result, dict):
|
| 238 |
logger.debug("Model returned single dictionary; wrapping in list")
|
| 239 |
result = [result]
|
| 240 |
-
|
| 241 |
-
if isinstance(result, list):
|
| 242 |
-
if len(result) == 0:
|
| 243 |
-
logger.warning("Model output is empty list")
|
| 244 |
-
elif not all(isinstance(item, dict) for item in result):
|
| 245 |
-
logger.warning(f"Non-dictionary items in result: {result}")
|
| 246 |
-
elif not all("label" in item and "score" in item for item in result):
|
| 247 |
-
logger.warning(f"Missing label or score in result: {result}")
|
| 248 |
-
else:
|
| 249 |
prediction = result[0]["label"]
|
| 250 |
score = result[0]["score"]
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
if is_fallback_model:
|
| 259 |
logger.warning("Using fallback DistilBERT model")
|
|
|
|
| 227 |
prediction = "No health condition detected"
|
| 228 |
score = 0.0
|
| 229 |
|
| 230 |
+
# Handle all possible output types
|
| 231 |
if result is None:
|
| 232 |
logger.warning("Model output is None")
|
| 233 |
elif isinstance(result, (str, int, float, bool)):
|
| 234 |
logger.warning(f"Invalid model output type: {type(result)}, value: {result}")
|
| 235 |
+
elif isinstance(result, (tuple, list)):
|
| 236 |
+
# Flatten nested tuples/lists
|
| 237 |
+
flattened = []
|
| 238 |
+
def flatten(item):
|
| 239 |
+
if isinstance(item, (tuple, list)):
|
| 240 |
+
for subitem in item:
|
| 241 |
+
flatten(subitem)
|
| 242 |
+
else:
|
| 243 |
+
flattened.append(item)
|
| 244 |
+
flatten(result)
|
| 245 |
+
result = flattened
|
| 246 |
+
if not result:
|
| 247 |
+
logger.warning("Flattened model output is empty")
|
| 248 |
+
elif isinstance(result, list):
|
| 249 |
+
if not all(isinstance(item, dict) for item in result):
|
| 250 |
+
logger.warning(f"Non-dictionary items in result: {result}")
|
| 251 |
+
elif not all("label" in item and "score" in item for item in result):
|
| 252 |
+
logger.warning(f"Missing label or score in result: {result}")
|
| 253 |
+
else:
|
| 254 |
+
prediction = result[0]["label"]
|
| 255 |
+
score = result[0]["score"]
|
| 256 |
elif isinstance(result, dict):
|
| 257 |
logger.debug("Model returned single dictionary; wrapping in list")
|
| 258 |
result = [result]
|
| 259 |
+
if "label" in result[0] and "score" in result[0]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
prediction = result[0]["label"]
|
| 261 |
score = result[0]["score"]
|
| 262 |
+
else:
|
| 263 |
+
logger.warning(f"Missing label or score in dictionary: {result}")
|
| 264 |
+
|
| 265 |
+
# Validate prediction and score
|
| 266 |
+
if not isinstance(prediction, str):
|
| 267 |
+
logger.warning(f"Invalid label type: {type(prediction)}, value: {prediction}")
|
| 268 |
+
prediction = "No health condition detected"
|
| 269 |
+
if not isinstance(score, (int, float)) or score < 0 or score > 1:
|
| 270 |
+
logger.warning(f"Invalid score: {score}")
|
| 271 |
+
score = 0.0
|
| 272 |
|
| 273 |
if is_fallback_model:
|
| 274 |
logger.warning("Using fallback DistilBERT model")
|