github-actions[bot]
commited on
Commit
·
b168249
1
Parent(s):
a54dc28
Sync from GitHub: 93ddaf17652e6085351336df41fe84c7fe1a4144
Browse files- inference.py +35 -9
inference.py
CHANGED
|
@@ -302,22 +302,48 @@ class InferenceProcessor:
|
|
| 302 |
reasoning_text = ""
|
| 303 |
extraction_json = output_text
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
try:
|
| 306 |
-
# Try to parse
|
| 307 |
-
parsed = json.loads(
|
| 308 |
if "reasoning" in parsed:
|
| 309 |
reasoning_text = parsed["reasoning"]
|
| 310 |
# Remove reasoning from output to get clean extraction JSON
|
| 311 |
extraction_dict = {k: v for k, v in parsed.items() if k != "reasoning"}
|
| 312 |
extraction_json = json.dumps(extraction_dict)
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
| 317 |
if json_match:
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
print(f"🧠 Combined reasoning + extraction completed in {latency:.2f}s")
|
| 323 |
return reasoning_text, extraction_json, latency
|
|
|
|
| 302 |
reasoning_text = ""
|
| 303 |
extraction_json = output_text
|
| 304 |
|
| 305 |
+
# First, remove markdown code fences if present
|
| 306 |
+
cleaned_output = output_text.strip()
|
| 307 |
+
if cleaned_output.startswith('```'):
|
| 308 |
+
# Remove opening ```json or ```
|
| 309 |
+
lines = cleaned_output.split('\n')
|
| 310 |
+
if lines[0].startswith('```'):
|
| 311 |
+
lines = lines[1:]
|
| 312 |
+
# Remove closing ```
|
| 313 |
+
if lines and lines[-1].strip() == '```':
|
| 314 |
+
lines = lines[:-1]
|
| 315 |
+
cleaned_output = '\n'.join(lines).strip()
|
| 316 |
+
|
| 317 |
try:
|
| 318 |
+
# Try to parse the cleaned JSON
|
| 319 |
+
parsed = json.loads(cleaned_output)
|
| 320 |
if "reasoning" in parsed:
|
| 321 |
reasoning_text = parsed["reasoning"]
|
| 322 |
# Remove reasoning from output to get clean extraction JSON
|
| 323 |
extraction_dict = {k: v for k, v in parsed.items() if k != "reasoning"}
|
| 324 |
extraction_json = json.dumps(extraction_dict)
|
| 325 |
+
else:
|
| 326 |
+
# No reasoning field, use entire output as extraction
|
| 327 |
+
extraction_json = cleaned_output
|
| 328 |
+
except json.JSONDecodeError:
|
| 329 |
+
# If parsing fails, try to find JSON pattern in the text
|
| 330 |
+
json_match = re.search(r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}', cleaned_output, re.DOTALL)
|
| 331 |
if json_match:
|
| 332 |
+
json_str = json_match.group(0)
|
| 333 |
+
try:
|
| 334 |
+
parsed = json.loads(json_str)
|
| 335 |
+
if "reasoning" in parsed:
|
| 336 |
+
reasoning_text = parsed["reasoning"]
|
| 337 |
+
extraction_dict = {k: v for k, v in parsed.items() if k != "reasoning"}
|
| 338 |
+
extraction_json = json.dumps(extraction_dict)
|
| 339 |
+
else:
|
| 340 |
+
extraction_json = json_str
|
| 341 |
+
except:
|
| 342 |
+
extraction_json = json_str
|
| 343 |
+
# Everything before JSON is additional reasoning
|
| 344 |
+
prefix = cleaned_output[:json_match.start()].strip()
|
| 345 |
+
if prefix and not reasoning_text:
|
| 346 |
+
reasoning_text = prefix
|
| 347 |
|
| 348 |
print(f"🧠 Combined reasoning + extraction completed in {latency:.2f}s")
|
| 349 |
return reasoning_text, extraction_json, latency
|