Spaces:
Running
Running
sunheycho
commited on
Commit
·
4b79c41
1
Parent(s):
be7a2aa
Fix JSON parsing issues in product comparison agents
Browse files- Clean up malformed JSON keys and values in fallback parsing
- Remove extra quotes, commas, and null values from parsed data
- Ensure consistent JSON structure for frontend rendering
- Fix product info, specifications, comparison, and recommendation parsing
- product_comparison.py +21 -48
product_comparison.py
CHANGED
@@ -340,9 +340,9 @@ class ImageProcessingAgent(BaseAgent):
|
|
340 |
for line in lines:
|
341 |
if ':' in line:
|
342 |
key, value = line.split(':', 1)
|
343 |
-
key = key.strip().lower().replace(' ', '_')
|
344 |
-
value = value.strip().strip('"').strip("'")
|
345 |
-
if key and value:
|
346 |
extracted[key] = value
|
347 |
return extracted
|
348 |
except Exception as e:
|
@@ -484,9 +484,9 @@ class FeatureExtractionAgent(BaseAgent):
|
|
484 |
for line in lines:
|
485 |
if ':' in line:
|
486 |
key, value = line.split(':', 1)
|
487 |
-
key = key.strip().lower().replace(' ', '_').strip('
|
488 |
-
value = value.strip().strip('"').strip("'")
|
489 |
-
if key and value:
|
490 |
specs[key] = value
|
491 |
return specs
|
492 |
except Exception as e:
|
@@ -624,35 +624,19 @@ class ComparisonAgent(BaseAgent):
|
|
624 |
return comparison
|
625 |
except json.JSONDecodeError:
|
626 |
# If LLM output is not valid JSON, extract key sections using simple parsing
|
627 |
-
sections = {}
|
628 |
-
current_section = None
|
629 |
-
section_content = []
|
630 |
-
|
631 |
lines = response_text.split('\n')
|
|
|
|
|
632 |
for line in lines:
|
633 |
line = line.strip()
|
634 |
-
if not line:
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
sections[current_section] = section_content
|
641 |
-
section_content = []
|
642 |
-
|
643 |
-
current_section = line.strip(":").lower().replace(" ", "_")
|
644 |
-
elif line.startswith("-") and current_section:
|
645 |
-
# This is a list item
|
646 |
-
section_content.append(line.strip("- "))
|
647 |
-
elif current_section:
|
648 |
-
# This is content for the current section
|
649 |
-
section_content.append(line)
|
650 |
|
651 |
-
|
652 |
-
if current_section and section_content:
|
653 |
-
sections[current_section] = section_content
|
654 |
-
|
655 |
-
return sections
|
656 |
except Exception as e:
|
657 |
print(f"Error in LLM comparison: {e}")
|
658 |
# Fall back to simple comparison
|
@@ -835,24 +819,13 @@ class RecommendationAgent(BaseAgent):
|
|
835 |
lines = response_text.split('\n')
|
836 |
recommendation = {}
|
837 |
|
838 |
-
# Look for recommendation indicator
|
839 |
for line in lines:
|
840 |
-
if
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
elif "confidence" in line.lower() and ":" in line:
|
847 |
-
try:
|
848 |
-
conf_str = line.split(":", 1)[1].strip()
|
849 |
-
conf = float(conf_str)
|
850 |
-
recommendation["confidence"] = conf
|
851 |
-
except ValueError:
|
852 |
-
recommendation["confidence"] = 0.5
|
853 |
-
elif "use cases" in line.lower() and ":" in line:
|
854 |
-
use_cases = line.split(":", 1)[1].strip().strip('"')
|
855 |
-
recommendation["use_cases"] = [uc.strip() for uc in use_cases.split(",")]
|
856 |
|
857 |
return recommendation
|
858 |
except Exception as e:
|
|
|
340 |
for line in lines:
|
341 |
if ':' in line:
|
342 |
key, value = line.split(':', 1)
|
343 |
+
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
344 |
+
value = value.strip().strip('"').strip("'").rstrip(',')
|
345 |
+
if key and value and value != 'null':
|
346 |
extracted[key] = value
|
347 |
return extracted
|
348 |
except Exception as e:
|
|
|
484 |
for line in lines:
|
485 |
if ':' in line:
|
486 |
key, value = line.split(':', 1)
|
487 |
+
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
488 |
+
value = value.strip().strip('"').strip("'").rstrip(',')
|
489 |
+
if key and value and value != 'null':
|
490 |
specs[key] = value
|
491 |
return specs
|
492 |
except Exception as e:
|
|
|
624 |
return comparison
|
625 |
except json.JSONDecodeError:
|
626 |
# If LLM output is not valid JSON, extract key sections using simple parsing
|
|
|
|
|
|
|
|
|
627 |
lines = response_text.split('\n')
|
628 |
+
comparison = {}
|
629 |
+
|
630 |
for line in lines:
|
631 |
line = line.strip()
|
632 |
+
if ':' in line and not line.startswith(' '):
|
633 |
+
key, value = line.split(':', 1)
|
634 |
+
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
635 |
+
value = value.strip().strip('"').strip("'").rstrip(',')
|
636 |
+
if key and value and value != 'null':
|
637 |
+
comparison[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
638 |
|
639 |
+
return comparison
|
|
|
|
|
|
|
|
|
640 |
except Exception as e:
|
641 |
print(f"Error in LLM comparison: {e}")
|
642 |
# Fall back to simple comparison
|
|
|
819 |
lines = response_text.split('\n')
|
820 |
recommendation = {}
|
821 |
|
|
|
822 |
for line in lines:
|
823 |
+
if ':' in line:
|
824 |
+
key, value = line.split(':', 1)
|
825 |
+
key = key.strip().lower().replace(' ', '_').strip('"').strip("'")
|
826 |
+
value = value.strip().strip('"').strip("'").rstrip(',')
|
827 |
+
if key and value and value != 'null':
|
828 |
+
recommendation[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
829 |
|
830 |
return recommendation
|
831 |
except Exception as e:
|