Spaces:
Runtime error
Runtime error
clean json
Browse files- utils/optimizer.py +22 -5
utils/optimizer.py
CHANGED
|
@@ -87,7 +87,7 @@ class ContentOptimizer:
|
|
| 87 |
"""
|
| 88 |
]
|
| 89 |
|
| 90 |
-
|
| 91 |
"""Initialize optimization prompts with RAG integration"""
|
| 92 |
self.rag_enhancement_prompt = """
|
| 93 |
You are a Generative Engine Optimization (GEO) specialist with access to best practices knowledge.
|
|
@@ -186,7 +186,6 @@ class ContentOptimizer:
|
|
| 186 |
```
|
| 187 |
""".strip()
|
| 188 |
|
| 189 |
-
|
| 190 |
|
| 191 |
def optimize_content_with_rag(self, content: str, optimization_type: str = "geo_standard", analyze_only: bool = False) -> Dict[str, Any]:
|
| 192 |
try:
|
|
@@ -313,20 +312,38 @@ class ContentOptimizer:
|
|
| 313 |
r.append("Transform statements into Q&A pairs.")
|
| 314 |
return r
|
| 315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
def _parse_optimization_result(self, response_text: str) -> Dict[str, Any]:
|
| 317 |
try:
|
| 318 |
start = response_text.find('{')
|
| 319 |
end = response_text.rfind('}') + 1
|
| 320 |
if start != -1 and end != -1:
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
return {
|
| 323 |
'raw_response': response_text,
|
| 324 |
-
'parsing_error': '
|
|
|
|
|
|
|
| 325 |
}
|
| 326 |
except Exception as e:
|
| 327 |
return {
|
| 328 |
'raw_response': response_text,
|
| 329 |
-
'parsing_error': f'
|
|
|
|
|
|
|
| 330 |
}
|
| 331 |
|
| 332 |
# Legacy support methods
|
|
|
|
| 87 |
"""
|
| 88 |
]
|
| 89 |
|
| 90 |
+
def setup_prompts(self):
|
| 91 |
"""Initialize optimization prompts with RAG integration"""
|
| 92 |
self.rag_enhancement_prompt = """
|
| 93 |
You are a Generative Engine Optimization (GEO) specialist with access to best practices knowledge.
|
|
|
|
| 186 |
```
|
| 187 |
""".strip()
|
| 188 |
|
|
|
|
| 189 |
|
| 190 |
def optimize_content_with_rag(self, content: str, optimization_type: str = "geo_standard", analyze_only: bool = False) -> Dict[str, Any]:
|
| 191 |
try:
|
|
|
|
| 312 |
r.append("Transform statements into Q&A pairs.")
|
| 313 |
return r
|
| 314 |
|
| 315 |
+
def _clean_json_string(self, json_str: str) -> str:
|
| 316 |
+
json_str = json_str.replace("...", "")
|
| 317 |
+
json_str = re.sub(r",\s*([}\]])", r"\\1", json_str)
|
| 318 |
+
json_str = json_str.strip('`')
|
| 319 |
+
return json_str
|
| 320 |
+
|
| 321 |
def _parse_optimization_result(self, response_text: str) -> Dict[str, Any]:
|
| 322 |
try:
|
| 323 |
start = response_text.find('{')
|
| 324 |
end = response_text.rfind('}') + 1
|
| 325 |
if start != -1 and end != -1:
|
| 326 |
+
json_str = self._clean_json_string(response_text[start:end])
|
| 327 |
+
return json.loads(json_str)
|
| 328 |
+
return {
|
| 329 |
+
'raw_response': response_text,
|
| 330 |
+
'parsing_error': 'No JSON structure found in response',
|
| 331 |
+
'geo_analysis': {},
|
| 332 |
+
'recommendations': []
|
| 333 |
+
}
|
| 334 |
+
except json.JSONDecodeError as e:
|
| 335 |
return {
|
| 336 |
'raw_response': response_text,
|
| 337 |
+
'parsing_error': f'JSON decode error: {str(e)}',
|
| 338 |
+
'geo_analysis': {},
|
| 339 |
+
'recommendations': []
|
| 340 |
}
|
| 341 |
except Exception as e:
|
| 342 |
return {
|
| 343 |
'raw_response': response_text,
|
| 344 |
+
'parsing_error': f'Unexpected error: {str(e)}',
|
| 345 |
+
'geo_analysis': {},
|
| 346 |
+
'recommendations': []
|
| 347 |
}
|
| 348 |
|
| 349 |
# Legacy support methods
|