Spaces:
Running
Running
Update sozo_gen.py
Browse files- sozo_gen.py +42 -56
sozo_gen.py
CHANGED
|
@@ -258,31 +258,17 @@ def concat_media(file_paths: List[str], output_path: Path):
|
|
| 258 |
finally:
|
| 259 |
list_file.unlink(missing_ok=True)
|
| 260 |
|
| 261 |
-
#
|
| 262 |
import json as _original_json
|
| 263 |
-
import builtins
|
| 264 |
-
|
| 265 |
-
# Store original functions
|
| 266 |
-
_original_str = builtins.str
|
| 267 |
-
_original_dumps = _original_json.dumps
|
| 268 |
-
|
| 269 |
-
def safe_str(obj):
|
| 270 |
-
"""Safe str() that handles Firebase compatibility."""
|
| 271 |
-
try:
|
| 272 |
-
result = _original_str(obj)
|
| 273 |
-
# Clean for Firebase - remove problematic characters
|
| 274 |
-
clean_result = ''.join(char for char in result if ord(char) >= 32 or char in '\n\r\t')
|
| 275 |
-
# Limit length
|
| 276 |
-
if len(clean_result) > 1000:
|
| 277 |
-
clean_result = clean_result[:1000] + "... (truncated)"
|
| 278 |
-
return clean_result
|
| 279 |
-
except Exception:
|
| 280 |
-
return "Error occurred during processing"
|
| 281 |
|
| 282 |
def safe_json_dumps(obj, **kwargs):
|
| 283 |
"""Safe JSON dumps that handles non-serializable types."""
|
| 284 |
def make_serializable(item):
|
| 285 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
return item
|
| 287 |
elif isinstance(item, (list, tuple)):
|
| 288 |
return [make_serializable(x) for x in item]
|
|
@@ -292,42 +278,42 @@ def safe_json_dumps(obj, **kwargs):
|
|
| 292 |
return str(item)
|
| 293 |
|
| 294 |
try:
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
return
|
| 299 |
-
|
| 300 |
-
#
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
if
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
|
| 332 |
# --- Main Business Logic Functions for Flask ---
|
| 333 |
|
|
|
|
| 258 |
finally:
|
| 259 |
list_file.unlink(missing_ok=True)
|
| 260 |
|
| 261 |
+
# DROP-IN FIX - requires zero code changes
|
| 262 |
import json as _original_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
def safe_json_dumps(obj, **kwargs):
|
| 265 |
"""Safe JSON dumps that handles non-serializable types."""
|
| 266 |
def make_serializable(item):
|
| 267 |
+
if item is None:
|
| 268 |
+
return None
|
| 269 |
+
elif isinstance(item, bool): # Check bool before int since bool is subclass of int
|
| 270 |
+
return item
|
| 271 |
+
elif isinstance(item, (int, float, str)):
|
| 272 |
return item
|
| 273 |
elif isinstance(item, (list, tuple)):
|
| 274 |
return [make_serializable(x) for x in item]
|
|
|
|
| 278 |
return str(item)
|
| 279 |
|
| 280 |
try:
|
| 281 |
+
serializable_obj = make_serializable(obj)
|
| 282 |
+
return _original_json.dumps(serializable_obj, **kwargs)
|
| 283 |
+
except Exception:
|
| 284 |
+
return f'"{str(obj)}"'
|
| 285 |
+
|
| 286 |
+
# Patch json.dumps
|
| 287 |
+
_original_json.dumps = safe_json_dumps
|
| 288 |
+
|
| 289 |
+
# Patch Firebase Reference.update to handle error strings safely
|
| 290 |
+
try:
|
| 291 |
+
from firebase_admin import db
|
| 292 |
+
_original_update = db.Reference.update
|
| 293 |
+
|
| 294 |
+
def safe_update(self, value):
|
| 295 |
+
"""Safe wrapper for Firebase update that cleans error strings."""
|
| 296 |
+
if isinstance(value, dict) and 'error' in value:
|
| 297 |
+
try:
|
| 298 |
+
# Clean the error string for Firebase
|
| 299 |
+
error_str = str(value['error'])
|
| 300 |
+
clean_error = ''.join(char for char in error_str if 32 <= ord(char) <= 126 or char in '\n\r\t ')
|
| 301 |
+
if len(clean_error) > 1000:
|
| 302 |
+
clean_error = clean_error[:1000] + "... (truncated)"
|
| 303 |
+
value = dict(value) # Create a copy
|
| 304 |
+
value['error'] = clean_error
|
| 305 |
+
except Exception:
|
| 306 |
+
# If cleaning fails, use a generic error message
|
| 307 |
+
value = dict(value) # Create a copy
|
| 308 |
+
value['error'] = 'An error occurred during processing'
|
| 309 |
+
|
| 310 |
+
return _original_update(self, value)
|
| 311 |
+
|
| 312 |
+
db.Reference.update = safe_update
|
| 313 |
+
|
| 314 |
+
except ImportError:
|
| 315 |
+
# firebase_admin not available, skip patching
|
| 316 |
+
pass
|
| 317 |
|
| 318 |
# --- Main Business Logic Functions for Flask ---
|
| 319 |
|