Spaces:
Sleeping
Sleeping
Commit
·
b757101
1
Parent(s):
766afd5
Fixed prefix fraud_
Browse files
app.py
CHANGED
|
@@ -215,21 +215,28 @@ def extract_json_from_markdown(text: str) -> str:
|
|
| 215 |
"""
|
| 216 |
Extract JSON content from markdown code block.
|
| 217 |
Handles cases where the LLM wraps the output in ```json ... ```
|
|
|
|
| 218 |
"""
|
| 219 |
# Look for ```json ... ```
|
| 220 |
match = re.search(r'```(?:json)?\s*\n?(.*?)\n?```', text, re.DOTALL | re.IGNORECASE)
|
| 221 |
if match:
|
| 222 |
json_str = match.group(1).strip()
|
| 223 |
else:
|
| 224 |
-
# Fallback: strip any leading/trailing whitespace and assume it's raw JSON
|
| 225 |
json_str = text.strip()
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
|
| 229 |
-
json_str =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
return json_str
|
| 232 |
|
|
|
|
| 233 |
# --- FASTAPI ENDPOINTS ---
|
| 234 |
@app.get("/")
|
| 235 |
async def root():
|
|
|
|
| 215 |
"""
|
| 216 |
Extract JSON content from markdown code block.
|
| 217 |
Handles cases where the LLM wraps the output in ```json ... ```
|
| 218 |
+
Cleans up problematic escape characters for json.loads.
|
| 219 |
"""
|
| 220 |
# Look for ```json ... ```
|
| 221 |
match = re.search(r'```(?:json)?\s*\n?(.*?)\n?```', text, re.DOTALL | re.IGNORECASE)
|
| 222 |
if match:
|
| 223 |
json_str = match.group(1).strip()
|
| 224 |
else:
|
|
|
|
| 225 |
json_str = text.strip()
|
| 226 |
|
| 227 |
+
# Remove LLM-inserted escape sequences like \$ or \"
|
| 228 |
+
# First, convert escaped newlines and tabs to spaces
|
| 229 |
+
json_str = json_str.replace('\\n', ' ').replace('\\t', ' ')
|
| 230 |
+
|
| 231 |
+
# Then remove unneeded backslashes before non-JSON characters
|
| 232 |
+
json_str = re.sub(r'\\(?=[^"\\/bfnrtu])', '', json_str)
|
| 233 |
+
|
| 234 |
+
# Collapse multiple spaces
|
| 235 |
+
json_str = re.sub(r'\s+', ' ', json_str).strip()
|
| 236 |
|
| 237 |
return json_str
|
| 238 |
|
| 239 |
+
|
| 240 |
# --- FASTAPI ENDPOINTS ---
|
| 241 |
@app.get("/")
|
| 242 |
async def root():
|