pauladeeptech commited on
Commit
30b553d
·
verified ·
1 Parent(s): 6322133

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -8
app.py CHANGED
@@ -60,17 +60,38 @@ def _maybe_fix_mojibake(text: str) -> str:
60
 
61
 
62
  def _parse_json(raw: str):
 
 
 
 
 
63
  raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL | re.IGNORECASE)
 
 
64
  raw = raw.replace("```json", "").replace("```", "").strip()
 
 
65
  raw = _maybe_fix_mojibake(raw)
66
- match = re.search(r"\{.*\}", raw, re.DOTALL)
67
- if match:
68
- try:
69
- return json.loads(match.group())
70
- except Exception:
71
- pass
72
- return None
73
-
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def render_html(data: dict) -> str:
76
  emoji = data.get("emoji", "📚")
 
60
 
61
 
62
  def _parse_json(raw: str):
63
+ """
64
+ Extract and parse JSON from LLM response
65
+ Handles emoji prefixes and various formats
66
+ """
67
+ # Remove thinking tags
68
  raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL | re.IGNORECASE)
69
+
70
+ # Remove markdown
71
  raw = raw.replace("```json", "").replace("```", "").strip()
72
+
73
+ # Fix encoding
74
  raw = _maybe_fix_mojibake(raw)
75
+
76
+ # ✅ CRITICAL FIX: Extract only the JSON part
77
+ # Sometimes the model outputs: "📚 {json here}"
78
+ # We need to find and extract just the JSON
79
+
80
+ json_content = raw
81
+
82
+ # If there's a { in the response, extract from first { to last }
83
+ if '{' in raw and '}' in raw:
84
+ start = raw.index('{')
85
+ end = raw.rindex('}') + 1
86
+ json_content = raw[start:end]
87
+
88
+ # Try to parse
89
+ try:
90
+ return json.loads(json_content)
91
+ except Exception as e:
92
+ print(f"⚠️ JSON parse failed: {e}")
93
+ print(f"Raw content: {raw[:200]}...")
94
+ return None
95
 
96
  def render_html(data: dict) -> str:
97
  emoji = data.get("emoji", "📚")