Update app.py
Browse files
app.py
CHANGED
|
@@ -237,9 +237,71 @@ def generate_video():
|
|
| 237 |
return jsonify({"error": "Invalid API key"}), 401
|
| 238 |
|
| 239 |
# Get JSON data
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# Validate input
|
| 245 |
if "slides" not in data or not data["slides"]:
|
|
|
|
| 237 |
return jsonify({"error": "Invalid API key"}), 401
|
| 238 |
|
| 239 |
# Get JSON data
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
# Try reading raw body text
|
| 243 |
+
raw_body = request.data.decode("utf-8").strip()
|
| 244 |
+
data = None
|
| 245 |
+
|
| 246 |
+
if not raw_body:
|
| 247 |
+
return jsonify({"error": "No input data provided"}), 400
|
| 248 |
+
|
| 249 |
+
# Try to detect if input is JSON or plain string
|
| 250 |
+
if raw_body.startswith("{") or raw_body.startswith("["):
|
| 251 |
+
# Likely JSON, try parsing
|
| 252 |
+
try:
|
| 253 |
+
data = json.loads(raw_body)
|
| 254 |
+
print("✅ Detected valid JSON input.")
|
| 255 |
+
except json.JSONDecodeError:
|
| 256 |
+
# Not valid JSON, fallback to manual parse
|
| 257 |
+
data = None
|
| 258 |
+
|
| 259 |
+
if data is None:
|
| 260 |
+
print("⚙️ Detected raw string input (non-JSON). Parsing manually...")
|
| 261 |
+
|
| 262 |
+
# Handle format like: [ [...], [...]] &&& Tamil explanation
|
| 263 |
+
parts = raw_body.split("&&&")
|
| 264 |
+
slides_part = parts[0].strip()
|
| 265 |
+
extra_info = parts[1].strip() if len(parts) > 1 else ""
|
| 266 |
+
|
| 267 |
+
try:
|
| 268 |
+
slides = json.loads(slides_part)
|
| 269 |
+
except json.JSONDecodeError as e:
|
| 270 |
+
return jsonify({
|
| 271 |
+
"error": "Failed to parse slides JSON from string input",
|
| 272 |
+
"details": str(e),
|
| 273 |
+
"raw_snippet": slides_part[:200]
|
| 274 |
+
}), 400
|
| 275 |
+
|
| 276 |
+
# Convert list of lists → list of dicts
|
| 277 |
+
slides_json = []
|
| 278 |
+
for s in slides:
|
| 279 |
+
if len(s) >= 4:
|
| 280 |
+
slide_type, content, animation, duration = s
|
| 281 |
+
slides_json.append({
|
| 282 |
+
"type": slide_type,
|
| 283 |
+
"content": content,
|
| 284 |
+
"animation": animation,
|
| 285 |
+
"duration": duration
|
| 286 |
+
})
|
| 287 |
+
|
| 288 |
+
data = {
|
| 289 |
+
"slides": slides_json,
|
| 290 |
+
"language": "Tamil" if "Tamil" in extra_info else "English",
|
| 291 |
+
"explanation": extra_info,
|
| 292 |
+
"video_settings": {
|
| 293 |
+
"background_color": "#0f0f23",
|
| 294 |
+
"text_color": "WHITE",
|
| 295 |
+
"highlight_color": "YELLOW",
|
| 296 |
+
"font": "CMU Serif"
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
# ✅ Final validation
|
| 301 |
+
if "slides" not in data or not data["slides"]:
|
| 302 |
+
return jsonify({"error": "No slides provided in request"}), 400
|
| 303 |
+
|
| 304 |
+
print(f"✅ Parsed {len(data['slides'])} slides successfully.")
|
| 305 |
|
| 306 |
# Validate input
|
| 307 |
if "slides" not in data or not data["slides"]:
|