Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -429,14 +429,33 @@ def parse_thinking_response(text: str) -> tuple:
|
|
| 429 |
def parse_tool_use(text: str) -> Optional[Dict[str, Any]]:
|
| 430 |
"""Parse tool use from model response"""
|
| 431 |
try:
|
| 432 |
-
#
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
if
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
except:
|
| 441 |
pass
|
| 442 |
return None
|
|
|
|
| 429 |
def parse_tool_use(text: str) -> Optional[Dict[str, Any]]:
|
| 430 |
"""Parse tool use from model response"""
|
| 431 |
try:
|
| 432 |
+
# Try to find JSON with "tool" key - handle nested braces
|
| 433 |
+
# First try: the entire text might be JSON
|
| 434 |
+
text_stripped = text.strip()
|
| 435 |
+
if text_stripped.startswith("{") and text_stripped.endswith("}"):
|
| 436 |
+
parsed = json.loads(text_stripped)
|
| 437 |
+
if "tool" in parsed:
|
| 438 |
+
return parsed
|
| 439 |
+
|
| 440 |
+
# Second try: find JSON object containing "tool"
|
| 441 |
+
brace_count = 0
|
| 442 |
+
start_idx = None
|
| 443 |
+
for i, char in enumerate(text):
|
| 444 |
+
if char == '{':
|
| 445 |
+
if brace_count == 0:
|
| 446 |
+
start_idx = i
|
| 447 |
+
brace_count += 1
|
| 448 |
+
elif char == '}':
|
| 449 |
+
brace_count -= 1
|
| 450 |
+
if brace_count == 0 and start_idx is not None:
|
| 451 |
+
json_str = text[start_idx:i+1]
|
| 452 |
+
try:
|
| 453 |
+
parsed = json.loads(json_str)
|
| 454 |
+
if "tool" in parsed:
|
| 455 |
+
return parsed
|
| 456 |
+
except:
|
| 457 |
+
pass
|
| 458 |
+
start_idx = None
|
| 459 |
except:
|
| 460 |
pass
|
| 461 |
return None
|