Spaces:
Sleeping
Sleeping
Commit ·
0054399
1
Parent(s): 2eb9ab6
fix: Run cross-map after defaults so LLM-sparse responses still get mapped
Browse files- services/utils.py +10 -4
services/utils.py
CHANGED
|
@@ -242,11 +242,15 @@ def normalize_response(data: dict) -> dict:
|
|
| 242 |
if "target_audience" in data and isinstance(data["target_audience"], dict):
|
| 243 |
data["target_audience"] = str(data["target_audience"])
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
| 246 |
if "video_titles" in data and "content_titles" not in data:
|
| 247 |
data["content_titles"] = [t.get("title", str(t)) for t in data["video_titles"] if isinstance(t, dict)]
|
| 248 |
if "video_titles" in data and "related_phrases" not in data:
|
| 249 |
-
|
|
|
|
| 250 |
if "tags" in data and "hashtags" not in data:
|
| 251 |
data["hashtags"] = data["tags"]
|
| 252 |
if "tags" in data and "core_keywords" not in data:
|
|
@@ -259,7 +263,6 @@ def normalize_response(data: dict) -> dict:
|
|
| 259 |
data["viral_hashtags"] = [{"tag": h, "post_count": "N/A"} for h in data["hashtags"]]
|
| 260 |
if "best_posting_time" in data and "strategy_tips" not in data:
|
| 261 |
data["strategy_tips"] = [f"Post during: {data['best_posting_time']}"]
|
| 262 |
-
|
| 263 |
return data
|
| 264 |
|
| 265 |
|
|
@@ -306,10 +309,13 @@ def run_analysis(messages, defaults=None, temperature=0.3, max_new_tokens=2000):
|
|
| 306 |
|
| 307 |
if defaults:
|
| 308 |
try:
|
| 309 |
-
|
|
|
|
|
|
|
| 310 |
except Exception as e:
|
| 311 |
print(f"[validate error] {e}")
|
| 312 |
base = dict(defaults) if defaults else {}
|
| 313 |
base["error"] = "Failed to validate response. Please try again."
|
| 314 |
return base
|
|
|
|
| 315 |
return data
|
|
|
|
| 242 |
if "target_audience" in data and isinstance(data["target_audience"], dict):
|
| 243 |
data["target_audience"] = str(data["target_audience"])
|
| 244 |
|
| 245 |
+
return data
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def cross_map_fields(data: dict) -> dict:
|
| 249 |
if "video_titles" in data and "content_titles" not in data:
|
| 250 |
data["content_titles"] = [t.get("title", str(t)) for t in data["video_titles"] if isinstance(t, dict)]
|
| 251 |
if "video_titles" in data and "related_phrases" not in data:
|
| 252 |
+
titles = data.get("content_titles") or [t.get("title", str(t)) for t in data["video_titles"] if isinstance(t, dict)]
|
| 253 |
+
data["related_phrases"] = titles[:3]
|
| 254 |
if "tags" in data and "hashtags" not in data:
|
| 255 |
data["hashtags"] = data["tags"]
|
| 256 |
if "tags" in data and "core_keywords" not in data:
|
|
|
|
| 263 |
data["viral_hashtags"] = [{"tag": h, "post_count": "N/A"} for h in data["hashtags"]]
|
| 264 |
if "best_posting_time" in data and "strategy_tips" not in data:
|
| 265 |
data["strategy_tips"] = [f"Post during: {data['best_posting_time']}"]
|
|
|
|
| 266 |
return data
|
| 267 |
|
| 268 |
|
|
|
|
| 309 |
|
| 310 |
if defaults:
|
| 311 |
try:
|
| 312 |
+
data = validate_and_fill_data_defaults(data, defaults)
|
| 313 |
+
data = cross_map_fields(data)
|
| 314 |
+
return data
|
| 315 |
except Exception as e:
|
| 316 |
print(f"[validate error] {e}")
|
| 317 |
base = dict(defaults) if defaults else {}
|
| 318 |
base["error"] = "Failed to validate response. Please try again."
|
| 319 |
return base
|
| 320 |
+
data = cross_map_fields(data)
|
| 321 |
return data
|