Spaces:
Runtime error
Runtime error
Commit ·
fb41f3c
1
Parent(s): 9512f60
Fixed json pasring
Browse files- business_continuity.py +1 -35
business_continuity.py
CHANGED
|
@@ -119,39 +119,6 @@ class RecoveryStrategiesResponse(BaseModel):
|
|
| 119 |
message: str
|
| 120 |
request_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
| 121 |
|
| 122 |
-
def clean_json_response(response_text: str) -> str:
|
| 123 |
-
"""Clean and extract JSON from API response"""
|
| 124 |
-
logger.info("Cleaning JSON response")
|
| 125 |
-
if not response_text:
|
| 126 |
-
logger.error("Empty response text provided")
|
| 127 |
-
raise ValueError("Empty response text")
|
| 128 |
-
cleaned = response_text.strip()
|
| 129 |
-
patterns = [
|
| 130 |
-
r'```json\s*',
|
| 131 |
-
r'```',
|
| 132 |
-
]
|
| 133 |
-
for pattern in patterns:
|
| 134 |
-
cleaned = re.sub(pattern, '', cleaned, flags=re.MULTILINE | re.DOTALL)
|
| 135 |
-
cleaned = cleaned.strip()
|
| 136 |
-
# Use regex to extract the first valid JSON object
|
| 137 |
-
match = re.search(r'\{[\s\S]*\}', cleaned)
|
| 138 |
-
if not match:
|
| 139 |
-
logger.error(f"No JSON object found. Response preview: {cleaned[:300]}...")
|
| 140 |
-
raise ValueError("No JSON object found in response")
|
| 141 |
-
json_str = match.group(0)
|
| 142 |
-
logger.info(f"Extracted JSON length: {len(json_str)}")
|
| 143 |
-
logger.info(f"JSON preview: {json_str[:150]}...")
|
| 144 |
-
logger.info(f"JSON ending: ...{json_str[-50:]}")
|
| 145 |
-
try:
|
| 146 |
-
json.loads(json_str)
|
| 147 |
-
logger.info("JSON syntax validation passed")
|
| 148 |
-
except json.JSONDecodeError as e:
|
| 149 |
-
logger.error(f"JSON syntax validation failed: {str(e)}")
|
| 150 |
-
logger.error(f"Problematic JSON: {json_str}")
|
| 151 |
-
raise ValueError(f"Invalid JSON syntax: {str(e)}")
|
| 152 |
-
return json_str
|
| 153 |
-
|
| 154 |
-
|
| 155 |
def validate_strategies_data(data: dict) -> dict:
|
| 156 |
"""Validate and clean strategies data"""
|
| 157 |
logger.info("Validating strategies data")
|
|
@@ -289,8 +256,7 @@ Focus on actionable, specific strategies tailored to this exact process and depa
|
|
| 289 |
logger.info(f"Request {request_id}: Raw response first 200 chars: {api_response[:200]}")
|
| 290 |
logger.info(f"Request {request_id}: Raw response last 200 chars: {api_response[-200:]}")
|
| 291 |
|
| 292 |
-
|
| 293 |
-
strategies_data = json.loads(json_str)
|
| 294 |
logger.info(f"Request {request_id}: JSON parsing successful")
|
| 295 |
|
| 296 |
except (json.JSONDecodeError, ValueError) as e:
|
|
|
|
| 119 |
message: str
|
| 120 |
request_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def validate_strategies_data(data: dict) -> dict:
|
| 123 |
"""Validate and clean strategies data"""
|
| 124 |
logger.info("Validating strategies data")
|
|
|
|
| 256 |
logger.info(f"Request {request_id}: Raw response first 200 chars: {api_response[:200]}")
|
| 257 |
logger.info(f"Request {request_id}: Raw response last 200 chars: {api_response[-200:]}")
|
| 258 |
|
| 259 |
+
strategies_data = json.loads(api_response)
|
|
|
|
| 260 |
logger.info(f"Request {request_id}: JSON parsing successful")
|
| 261 |
|
| 262 |
except (json.JSONDecodeError, ValueError) as e:
|