Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
gary-boon
Claude Opus 4.5
commited on
Commit
·
99f6209
1
Parent(s):
474927d
fix: Sanitize JSON response for NaN/Inf float values
Browse filesAdd sanitize_for_json() helper that recursively traverses response
and replaces NaN/Infinity values with null. Fixes JSON serialization
errors when attention/entropy calculations produce invalid floats.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <[email protected]>
- backend/model_service.py +29 -1
backend/model_service.py
CHANGED
|
@@ -27,6 +27,33 @@ from .architectural_analysis import extract_architectural_data
|
|
| 27 |
logging.basicConfig(level=logging.INFO)
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
app = FastAPI(title="Visualisable.ai Model Service", version="0.1.0")
|
| 31 |
|
| 32 |
# CORS configuration for local development and production
|
|
@@ -1779,7 +1806,8 @@ async def analyze_research_attention(request: Dict[str, Any], authenticated: boo
|
|
| 1779 |
|
| 1780 |
logger.info(f"✅ Research attention analysis complete: {len(generated_tokens)} tokens, {generation_time:.2f}s")
|
| 1781 |
|
| 1782 |
-
|
|
|
|
| 1783 |
|
| 1784 |
except Exception as e:
|
| 1785 |
logger.error(f"Research attention analysis error: {e}")
|
|
|
|
| 27 |
logging.basicConfig(level=logging.INFO)
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
+
import math
|
| 31 |
+
|
| 32 |
+
def sanitize_for_json(obj):
|
| 33 |
+
"""
|
| 34 |
+
Recursively sanitize a nested data structure for JSON serialization.
|
| 35 |
+
Replaces NaN and Infinity float values with None (JSON null).
|
| 36 |
+
"""
|
| 37 |
+
if isinstance(obj, dict):
|
| 38 |
+
return {k: sanitize_for_json(v) for k, v in obj.items()}
|
| 39 |
+
elif isinstance(obj, list):
|
| 40 |
+
return [sanitize_for_json(item) for item in obj]
|
| 41 |
+
elif isinstance(obj, float):
|
| 42 |
+
if math.isnan(obj) or math.isinf(obj):
|
| 43 |
+
return None # JSON null
|
| 44 |
+
return obj
|
| 45 |
+
elif isinstance(obj, (np.floating, np.float32, np.float64)):
|
| 46 |
+
val = float(obj)
|
| 47 |
+
if math.isnan(val) or math.isinf(val):
|
| 48 |
+
return None
|
| 49 |
+
return val
|
| 50 |
+
elif isinstance(obj, (np.integer, np.int32, np.int64)):
|
| 51 |
+
return int(obj)
|
| 52 |
+
elif isinstance(obj, np.ndarray):
|
| 53 |
+
return sanitize_for_json(obj.tolist())
|
| 54 |
+
else:
|
| 55 |
+
return obj
|
| 56 |
+
|
| 57 |
app = FastAPI(title="Visualisable.ai Model Service", version="0.1.0")
|
| 58 |
|
| 59 |
# CORS configuration for local development and production
|
|
|
|
| 1806 |
|
| 1807 |
logger.info(f"✅ Research attention analysis complete: {len(generated_tokens)} tokens, {generation_time:.2f}s")
|
| 1808 |
|
| 1809 |
+
# Sanitize response to handle NaN/Inf values that break JSON serialization
|
| 1810 |
+
return sanitize_for_json(response)
|
| 1811 |
|
| 1812 |
except Exception as e:
|
| 1813 |
logger.error(f"Research attention analysis error: {e}")
|