ihtesham0345 commited on
Commit
fdbd75a
·
1 Parent(s): f169fdd

Fix JSON Crash: Lower Temp to 0.3 + Enhance Repair Logic + Add Error Fallback

Browse files
Files changed (1) hide show
  1. services/analyzer.py +36 -23
services/analyzer.py CHANGED
@@ -41,19 +41,28 @@ def clean_json_string(text: str) -> str:
41
  def repair_json(json_str: str) -> str:
42
  """Attempt to repair truncated JSON by closing open brackets/braces."""
43
  json_str = json_str.strip()
44
- if not json_str.endswith("}"):
45
- last_brace = json_str.rfind("}")
46
- if last_brace != -1:
47
- json_str = json_str[:last_brace+1]
48
- if json_str.count("{") > json_str.count("}"):
49
- json_str += "}" * (json_str.count("{") - json_str.count("}"))
50
- if json_str.count("[") > json_str.count("]"):
51
- json_str += "]" * (json_str.count("[") - json_str.count("]"))
 
 
 
 
 
 
 
 
 
52
  return json_str
53
 
54
  def analyze_seo_content(content: str) -> dict:
55
  """
56
- Analyzes content using LOCAL Qwen-1.5B.
57
  """
58
  if pipe is None:
59
  return {"error": "Model failed to load on server startup. Check logs."}
@@ -75,22 +84,22 @@ def analyze_seo_content(content: str) -> dict:
75
  "content": f"""Analyze topic: "{content[:2000]}"
76
 
77
  REQUIREMENTS:
78
- - **Core Keywords**: 30 high-value keywords (Fast & Stable).
79
  - **Related Phrases**: 10 variations.
80
  - **Viral Hashtags**: 8 tags.
81
- - **Content Titles**: 3 high-probability titles.
82
- - **Strategy Tips**: 3 actionable tips.
83
- - **Target Audience**: Concise profile.
84
 
85
  JSON OUTPUT STRUCTURE:
86
  {{
87
  "core_keywords": [
88
  {{"keyword": "...", "search_volume": "...", "competition": "...", "relevance": 90}}, ... (Total 30 items)
89
  ],
90
- "related_phrases": ["...", ...],
91
- "viral_hashtags": [{{"tag": "#...", "post_count": "..."}}, ...],
92
- "content_titles": ["...", ...],
93
- "strategy_tips": ["...", ...],
94
  "target_audience": "..."
95
  }}
96
  """
@@ -101,16 +110,13 @@ def analyze_seo_content(content: str) -> dict:
101
  # Qwen/HuggingFace pipeline handles chat templates automatically
102
  outputs = pipe(
103
  messages,
104
- max_new_tokens=1500, # Reduced to 1500 for <30s response time
105
  do_sample=True,
106
- temperature=0.7,
107
  top_p=0.9
108
  )
109
 
110
- # Extract generated text
111
  content_text = outputs[0]["generated_text"][-1]["content"]
112
-
113
- # Clean and Parse
114
  cleaned_json = clean_json_string(content_text)
115
 
116
  try:
@@ -118,7 +124,14 @@ def analyze_seo_content(content: str) -> dict:
118
  except json.JSONDecodeError:
119
  print("⚠️ JSON Parse Error. Attempting repair...")
120
  cleaned_json = repair_json(cleaned_json)
121
- data = json.loads(cleaned_json)
 
 
 
 
 
 
 
122
 
123
  return data
124
 
 
41
  def repair_json(json_str: str) -> str:
42
  """Attempt to repair truncated JSON by closing open brackets/braces."""
43
  json_str = json_str.strip()
44
+
45
+ # Remove potentially invalid trailing content that isn't a closer
46
+ # (e.g. if it ends with "keyword": "val", -> we need to remove comma)
47
+ json_str = json_str.rstrip(", ")
48
+
49
+ # Balance brackets
50
+ open_braces = json_str.count("{")
51
+ close_braces = json_str.count("}")
52
+ open_brackets = json_str.count("[")
53
+ close_brackets = json_str.count("]")
54
+
55
+ if open_braces > close_braces:
56
+ json_str += "}" * (open_braces - close_braces)
57
+
58
+ if open_brackets > close_brackets:
59
+ json_str += "]" * (open_brackets - close_brackets)
60
+
61
  return json_str
62
 
63
  def analyze_seo_content(content: str) -> dict:
64
  """
65
+ Analyzes content using LOCAL Qwen-0.5B.
66
  """
67
  if pipe is None:
68
  return {"error": "Model failed to load on server startup. Check logs."}
 
84
  "content": f"""Analyze topic: "{content[:2000]}"
85
 
86
  REQUIREMENTS:
87
+ - **Core Keywords**: 30 high-value keywords.
88
  - **Related Phrases**: 10 variations.
89
  - **Viral Hashtags**: 8 tags.
90
+ - **Content Titles**: 3 titles.
91
+ - **Strategy Tips**: 3 tips.
92
+ - **Target Audience**: Profile.
93
 
94
  JSON OUTPUT STRUCTURE:
95
  {{
96
  "core_keywords": [
97
  {{"keyword": "...", "search_volume": "...", "competition": "...", "relevance": 90}}, ... (Total 30 items)
98
  ],
99
+ "related_phrases": ["..."],
100
+ "viral_hashtags": [{{"tag": "#...", "post_count": "..."}}],
101
+ "content_titles": ["..."],
102
+ "strategy_tips": ["..."],
103
  "target_audience": "..."
104
  }}
105
  """
 
110
  # Qwen/HuggingFace pipeline handles chat templates automatically
111
  outputs = pipe(
112
  messages,
113
+ max_new_tokens=1500,
114
  do_sample=True,
115
+ temperature=0.3, # Low temp for strict JSON
116
  top_p=0.9
117
  )
118
 
 
119
  content_text = outputs[0]["generated_text"][-1]["content"]
 
 
120
  cleaned_json = clean_json_string(content_text)
121
 
122
  try:
 
124
  except json.JSONDecodeError:
125
  print("⚠️ JSON Parse Error. Attempting repair...")
126
  cleaned_json = repair_json(cleaned_json)
127
+ try:
128
+ data = json.loads(cleaned_json)
129
+ except json.JSONDecodeError as final_err:
130
+ print(f"❌ Repair Failed: {final_err}")
131
+ return {
132
+ "error": "Optimization Failed. The model generated invalid JSON.",
133
+ "raw_output": content_text[:500] + "..." # Debug info
134
+ }
135
 
136
  return data
137