Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
agents.py
CHANGED
|
@@ -65,7 +65,7 @@ def extract_json_array(text: str):
|
|
| 65 |
match = re.search(r"(\[\s*{.*?}\s*\])", text, re.DOTALL)
|
| 66 |
if not match:
|
| 67 |
# Try to find any JSON array in the text
|
| 68 |
-
match = re.search(r"\[.*?\]", text, re.DOTALL)
|
| 69 |
if not match:
|
| 70 |
return []
|
| 71 |
|
|
@@ -89,6 +89,11 @@ def extract_json_array(text: str):
|
|
| 89 |
json_str = re.sub(r',\s*]', ']', json_str)
|
| 90 |
# Fix unquoted keys
|
| 91 |
json_str = re.sub(r'(\w+):', r'"\1":', json_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return json.loads(json_str)
|
| 93 |
except Exception as e3:
|
| 94 |
print("[extract_json_array] JSON fixing failed:", e3)
|
|
@@ -128,6 +133,11 @@ def safe_json_parse(content: str, fallback_value=None):
|
|
| 128 |
fixed_content = re.sub(r'(\w+):', r'"\1":', fixed_content)
|
| 129 |
# Fix single quotes to double quotes
|
| 130 |
fixed_content = fixed_content.replace("'", '"')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
return json.loads(fixed_content)
|
| 132 |
except Exception as e3:
|
| 133 |
print(f"[safe_json_parse] JSON fixing failed: {e3}")
|
|
@@ -203,6 +213,21 @@ async def extract_books_node(state):
|
|
| 203 |
if manual_books:
|
| 204 |
books = manual_books
|
| 205 |
print("[extract_books_node] Manual extraction successful:", books)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
print("[extract_books_node] Parsed books:", books)
|
| 208 |
|
|
|
|
| 65 |
match = re.search(r"(\[\s*{.*?}\s*\])", text, re.DOTALL)
|
| 66 |
if not match:
|
| 67 |
# Try to find any JSON array in the text
|
| 68 |
+
match = re.search(r"(\[.*?\])", text, re.DOTALL)
|
| 69 |
if not match:
|
| 70 |
return []
|
| 71 |
|
|
|
|
| 89 |
json_str = re.sub(r',\s*]', ']', json_str)
|
| 90 |
# Fix unquoted keys
|
| 91 |
json_str = re.sub(r'(\w+):', r'"\1":', json_str)
|
| 92 |
+
# Fix extra quotes around objects
|
| 93 |
+
json_str = re.sub(r'"\s*{\s*"', '{"', json_str)
|
| 94 |
+
json_str = re.sub(r'"\s*}\s*"', '"}', json_str)
|
| 95 |
+
# Fix missing commas between objects
|
| 96 |
+
json_str = re.sub(r'"\s*}\s*{', '"},{', json_str)
|
| 97 |
return json.loads(json_str)
|
| 98 |
except Exception as e3:
|
| 99 |
print("[extract_json_array] JSON fixing failed:", e3)
|
|
|
|
| 133 |
fixed_content = re.sub(r'(\w+):', r'"\1":', fixed_content)
|
| 134 |
# Fix single quotes to double quotes
|
| 135 |
fixed_content = fixed_content.replace("'", '"')
|
| 136 |
+
# Fix extra quotes around objects
|
| 137 |
+
fixed_content = re.sub(r'"\s*{\s*"', '{"', fixed_content)
|
| 138 |
+
fixed_content = re.sub(r'"\s*}\s*"', '"}', fixed_content)
|
| 139 |
+
# Fix missing commas between objects
|
| 140 |
+
fixed_content = re.sub(r'"\s*}\s*{', '"},{', fixed_content)
|
| 141 |
return json.loads(fixed_content)
|
| 142 |
except Exception as e3:
|
| 143 |
print(f"[safe_json_parse] JSON fixing failed: {e3}")
|
|
|
|
| 213 |
if manual_books:
|
| 214 |
books = manual_books
|
| 215 |
print("[extract_books_node] Manual extraction successful:", books)
|
| 216 |
+
else:
|
| 217 |
+
# Last resort: try to extract from the specific malformed pattern we saw
|
| 218 |
+
print("[extract_books_node] Attempting pattern-based extraction")
|
| 219 |
+
# Look for patterns like "title": "Book Name"
|
| 220 |
+
title_matches = re.findall(r'"title":\s*"([^"]+)"', content)
|
| 221 |
+
author_matches = re.findall(r'"author":\s*"([^"]+)"', content)
|
| 222 |
+
|
| 223 |
+
if title_matches:
|
| 224 |
+
for i, title in enumerate(title_matches):
|
| 225 |
+
author = author_matches[i] if i < len(author_matches) else "Unknown"
|
| 226 |
+
manual_books.append({"title": title, "author": author})
|
| 227 |
+
|
| 228 |
+
if manual_books:
|
| 229 |
+
books = manual_books
|
| 230 |
+
print("[extract_books_node] Pattern-based extraction successful:", books)
|
| 231 |
|
| 232 |
print("[extract_books_node] Parsed books:", books)
|
| 233 |
|