Spaces:
Sleeping
Sleeping
Commit ·
e1cb6ad
1
Parent(s): b0fb0e7
fix: Add normalize_response to handle model output format mismatches
Browse files- Model returns strings where objects expected (e.g., video_titles: ['str'] → [{'title':'str','expected_ctr':'Medium'}])
- Model returns objects where strings expected (e.g., thumbnail_ideas: [{url, caption}] → ['caption text'])
- Normalizes all field types: keyword_list, hashtag_list, caption_list, pin_list, object_list, string_list
- Handles nested structures: hashtag_sets, content_ideas, post_drafts, tweet_threads
- services/utils.py +122 -0
services/utils.py
CHANGED
|
@@ -92,6 +92,126 @@ def repair_json_v2(json_str: str) -> str:
|
|
| 92 |
|
| 93 |
return trimmed
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def run_analysis(messages, defaults=None, temperature=0.3, max_new_tokens=2000):
|
| 96 |
from services.model_loader import generate_text
|
| 97 |
raw = generate_text(messages, temperature, max_new_tokens)
|
|
@@ -106,6 +226,8 @@ def run_analysis(messages, defaults=None, temperature=0.3, max_new_tokens=2000):
|
|
| 106 |
base["error"] = f"JSON parse failed: {err.get('error', 'unknown')}"
|
| 107 |
return base
|
| 108 |
|
|
|
|
|
|
|
| 109 |
if defaults:
|
| 110 |
return validate_and_fill_data_defaults(data, defaults)
|
| 111 |
return data
|
|
|
|
| 92 |
|
| 93 |
return trimmed
|
| 94 |
|
| 95 |
+
def normalize_field(items, expected_type):
|
| 96 |
+
"""Convert model output to match Pydantic expectations."""
|
| 97 |
+
if not isinstance(items, list):
|
| 98 |
+
return items
|
| 99 |
+
|
| 100 |
+
if expected_type == "object_list":
|
| 101 |
+
# Items should be [{key: val, ...}, ...] but model gave ["str1", "str2"]
|
| 102 |
+
if items and isinstance(items[0], str):
|
| 103 |
+
return [{"title": s, "expected_ctr": "Medium"} for s in items]
|
| 104 |
+
return items
|
| 105 |
+
|
| 106 |
+
if expected_type == "string_list":
|
| 107 |
+
# Items should be ["str1", "str2"] but model gave [{...}, {...}]
|
| 108 |
+
if items and isinstance(items[0], dict):
|
| 109 |
+
extracted = []
|
| 110 |
+
for obj in items:
|
| 111 |
+
val = obj.get("caption") or obj.get("description") or obj.get("title") or obj.get("name") or obj.get("url") or str(obj)
|
| 112 |
+
extracted.append(val)
|
| 113 |
+
return extracted
|
| 114 |
+
return items
|
| 115 |
+
|
| 116 |
+
if expected_type == "keyword_list":
|
| 117 |
+
# Items should be [{keyword, search_volume, competition, relevance}]
|
| 118 |
+
if items and isinstance(items[0], str):
|
| 119 |
+
return [{"keyword": s, "search_volume": "Medium", "competition": "Medium", "relevance": 80} for s in items]
|
| 120 |
+
return items
|
| 121 |
+
|
| 122 |
+
if expected_type == "hashtag_list":
|
| 123 |
+
# Items should be [{tag, post_count}] but model gave ["#tag1", "#tag2"]
|
| 124 |
+
if items and isinstance(items[0], str):
|
| 125 |
+
return [{"tag": s, "post_count": "N/A"} for s in items]
|
| 126 |
+
return items
|
| 127 |
+
|
| 128 |
+
if expected_type == "caption_list":
|
| 129 |
+
if items and isinstance(items[0], str):
|
| 130 |
+
return [{"caption": s, "tone": "engaging"} for s in items]
|
| 131 |
+
return items
|
| 132 |
+
|
| 133 |
+
if expected_type == "pin_list":
|
| 134 |
+
if items and isinstance(items[0], str):
|
| 135 |
+
return [{"title": s, "description": s, "keyword_focus": s} for s in items]
|
| 136 |
+
return items
|
| 137 |
+
|
| 138 |
+
return items
|
| 139 |
+
|
| 140 |
+
def normalize_response(data: dict) -> dict:
|
| 141 |
+
"""Post-process model output to match expected Pydantic schemas."""
|
| 142 |
+
# YouTube: video_titles expects list of {title, expected_ctr}
|
| 143 |
+
if "video_titles" in data:
|
| 144 |
+
data["video_titles"] = normalize_field(data["video_titles"], "object_list")
|
| 145 |
+
if "thumbnail_ideas" in data:
|
| 146 |
+
data["thumbnail_ideas"] = normalize_field(data["thumbnail_ideas"], "string_list")
|
| 147 |
+
|
| 148 |
+
# Instagram
|
| 149 |
+
if "captions" in data:
|
| 150 |
+
data["captions"] = normalize_field(data["captions"], "caption_list")
|
| 151 |
+
if "hashtag_sets" in data and isinstance(data["hashtag_sets"], dict):
|
| 152 |
+
for tier in ["small", "medium", "large"]:
|
| 153 |
+
if tier in data["hashtag_sets"] and isinstance(data["hashtag_sets"][tier], list):
|
| 154 |
+
if data["hashtag_sets"][tier] and isinstance(data["hashtag_sets"][tier][0], dict):
|
| 155 |
+
data["hashtag_sets"][tier] = [obj.get("tag", obj.get("name", str(obj))) for obj in data["hashtag_sets"][tier]]
|
| 156 |
+
if "content_ideas" in data and isinstance(data["content_ideas"], dict):
|
| 157 |
+
for key in ["reels", "carousels", "stories"]:
|
| 158 |
+
if key in data["content_ideas"]:
|
| 159 |
+
data["content_ideas"][key] = normalize_field(data["content_ideas"][key], "string_list")
|
| 160 |
+
|
| 161 |
+
# Generic SEO
|
| 162 |
+
if "core_keywords" in data:
|
| 163 |
+
data["core_keywords"] = normalize_field(data["core_keywords"], "keyword_list")
|
| 164 |
+
if "viral_hashtags" in data:
|
| 165 |
+
data["viral_hashtags"] = normalize_field(data["viral_hashtags"], "hashtag_list")
|
| 166 |
+
|
| 167 |
+
# Pinterest
|
| 168 |
+
if "pin_ideas" in data:
|
| 169 |
+
data["pin_ideas"] = normalize_field(data["pin_ideas"], "pin_list")
|
| 170 |
+
if "seo_keywords" in data:
|
| 171 |
+
data["seo_keywords"] = normalize_field(data["seo_keywords"], "string_list")
|
| 172 |
+
|
| 173 |
+
# Generic string lists that might have gotten objects
|
| 174 |
+
for field in ["related_phrases", "strategy_tips", "content_titles",
|
| 175 |
+
"tags", "engagement_strategies", "growth_strategies",
|
| 176 |
+
"article_topics", "thought_leadership_angles",
|
| 177 |
+
"engagement_hooks", "ad_copy_suggestions", "page_growth_tips",
|
| 178 |
+
"viral_hooks", "engagement_tactics",
|
| 179 |
+
"trending_angles", "viral_strategies",
|
| 180 |
+
"board_organization", "traffic_strategies",
|
| 181 |
+
"hashtags"]:
|
| 182 |
+
if field in data:
|
| 183 |
+
data[field] = normalize_field(data[field], "string_list")
|
| 184 |
+
|
| 185 |
+
# LinkedIn post_drafts: should be [{headline, body, hook}]
|
| 186 |
+
if "post_drafts" in data:
|
| 187 |
+
if data["post_drafts"] and isinstance(data["post_drafts"][0], str):
|
| 188 |
+
data["post_drafts"] = [{"headline": s, "body": s, "hook": s} for s in data["post_drafts"]]
|
| 189 |
+
|
| 190 |
+
# Post ideas: should be [{type, content}]
|
| 191 |
+
if "post_ideas" in data:
|
| 192 |
+
if data["post_ideas"] and isinstance(data["post_ideas"][0], str):
|
| 193 |
+
data["post_ideas"] = [{"type": "text", "content": s} for s in data["post_ideas"]]
|
| 194 |
+
|
| 195 |
+
# Tweet threads: should be [{tweets: [str], theme: str}]
|
| 196 |
+
if "tweet_threads" in data:
|
| 197 |
+
if data["tweet_threads"] and isinstance(data["tweet_threads"][0], str):
|
| 198 |
+
data["tweet_threads"] = [{"tweets": [data["tweet_threads"][0]], "theme": "topic"}]
|
| 199 |
+
|
| 200 |
+
# Video concepts: should be [{hook, script_snippet, sound_suggestion}]
|
| 201 |
+
if "video_concepts" in data:
|
| 202 |
+
if data["video_concepts"] and isinstance(data["video_concepts"][0], str):
|
| 203 |
+
data["video_concepts"] = [{"hook": s, "script_snippet": s, "sound_suggestion": "Trending"} for s in data["video_concepts"]]
|
| 204 |
+
|
| 205 |
+
# Ensure descriptions are strings, not objects
|
| 206 |
+
if "description_template" in data and isinstance(data["description_template"], dict):
|
| 207 |
+
data["description_template"] = str(data["description_template"])
|
| 208 |
+
|
| 209 |
+
# Target audience should be string
|
| 210 |
+
if "target_audience" in data and isinstance(data["target_audience"], dict):
|
| 211 |
+
data["target_audience"] = str(data["target_audience"])
|
| 212 |
+
|
| 213 |
+
return data
|
| 214 |
+
|
| 215 |
def run_analysis(messages, defaults=None, temperature=0.3, max_new_tokens=2000):
|
| 216 |
from services.model_loader import generate_text
|
| 217 |
raw = generate_text(messages, temperature, max_new_tokens)
|
|
|
|
| 226 |
base["error"] = f"JSON parse failed: {err.get('error', 'unknown')}"
|
| 227 |
return base
|
| 228 |
|
| 229 |
+
data = normalize_response(data)
|
| 230 |
+
|
| 231 |
if defaults:
|
| 232 |
return validate_and_fill_data_defaults(data, defaults)
|
| 233 |
return data
|