File size: 45,468 Bytes
34ba0b7
 
 
 
 
 
 
 
 
6df8e6a
34ba0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6df8e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ba0b7
 
 
89c7c75
6df8e6a
89c7c75
34ba0b7
 
6df8e6a
 
 
34ba0b7
 
 
6df8e6a
34ba0b7
 
fcd28fa
 
89c7c75
fcd28fa
 
 
34ba0b7
fcd28fa
 
 
 
 
 
 
 
 
 
 
 
 
 
34ba0b7
fcd28fa
 
 
34ba0b7
fcd28fa
34ba0b7
fcd28fa
34ba0b7
fcd28fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ba0b7
 
 
6df8e6a
 
 
fcd28fa
 
 
 
 
6df8e6a
 
 
 
fcd28fa
6df8e6a
 
 
 
 
 
 
 
 
 
 
 
 
34ba0b7
fcd28fa
 
 
 
 
 
 
34ba0b7
fcd28fa
 
 
 
 
 
34ba0b7
fcd28fa
34ba0b7
fcd28fa
34ba0b7
fcd28fa
 
 
34ba0b7
fcd28fa
 
 
 
 
 
 
 
 
34ba0b7
fcd28fa
 
34ba0b7
 
fcd28fa
 
 
 
 
34ba0b7
fcd28fa
 
 
 
34ba0b7
 
 
 
e6b274c
34ba0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcd28fa
34ba0b7
 
 
 
 
 
 
 
fcd28fa
 
 
 
 
34ba0b7
fcd28fa
 
34ba0b7
fcd28fa
 
 
 
 
 
 
 
34ba0b7
 
fcd28fa
 
 
34ba0b7
 
 
fcd28fa
34ba0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6df8e6a
34ba0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6df8e6a
34ba0b7
 
 
 
 
 
 
6df8e6a
34ba0b7
 
 
 
 
 
6df8e6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ba0b7
 
 
 
 
 
 
 
 
 
 
 
 
e6b274c
 
34ba0b7
 
 
 
6df8e6a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
"""
Visual Storybook / Animated Audiobook Generator
Powered by Qwen3.5-Omni-Plus (narration) + HappyHorse 1.0 (video)

Features:
  - English text input (paste, .txt, .pdf, .docx)
  - Translates + narrates into 36 languages (preset or cloned voice)
  - Generates AI video per scene via HappyHorse 1.0
  - Three video modes: text-to-video, image-to-video, auto scene prompts
  - Two HappyHorse API providers: happyhorse.app, DashScope (Wan 2.1 fallback)
  - Combines narrated audio + video scenes into final MP4

Deploy as a Hugging Face Space:
  1. Create a new Space (SDK: Gradio)
  2. Upload app.py and requirements.txt
  3. Add secrets: DASHSCOPE_API_KEY (required for audio)
     Plus one of: FAL_API_KEY, HAPPYHORSE_API_KEY, or use DashScope for video too
"""

import os
import base64
import json
import pathlib
import shutil
import struct
import subprocess
import tempfile
import time
import re

import gradio as gr
import requests as http_requests
from openai import OpenAI

# Optional document parsers
try:
    import pypdf
    HAS_PYPDF = True
except ImportError:
    HAS_PYPDF = False

try:
    import docx
    HAS_DOCX = True
except ImportError:
    HAS_DOCX = False


# ==========================================
# CONFIGURATION
# ==========================================
OMNI_MODEL = "qwen3.5-omni-plus"
TTS_VC_MODEL = "qwen3-tts-vc-2026-01-22"
VOICE_CLONE_MODEL = "qwen-voice-enrollment"

DASHSCOPE_BASE_URL = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
DASHSCOPE_API_URL = "https://dashscope-intl.aliyuncs.com/api/v1"
VOICE_CLONE_URL = f"{DASHSCOPE_API_URL}/services/audio/tts/customization"
TTS_SYNTHESIS_URL = f"{DASHSCOPE_API_URL}/services/aigc/multimodal-generation/generation"

# HappyHorse API endpoints per provider
HAPPYHORSE_PROVIDERS = {
    "happyhorse.app": {
        "generate": "https://happyhorse.app/api/generate",
        "status": "https://happyhorse.app/api/status",
        "key_env": "HAPPYHORSE_API_KEY",
        "auth_header": "Authorization",
        "auth_prefix": "Bearer ",
    },
    "DashScope (Bailian)": {
        "generate": f"{DASHSCOPE_API_URL}/services/aigc/video-generation/generation",
        "status": f"{DASHSCOPE_API_URL}/tasks",
        "key_env": "DASHSCOPE_API_KEY",
        "auth_header": "Authorization",
        "auth_prefix": "Bearer ",
    },
}

MAX_CHARS_PER_CHUNK = 1200  # Slightly shorter for scene-based splitting

LANGUAGES = {
    "English": {"code": "en", "native": "English", "tier": "core"},
    "Chinese (Mandarin)": {"code": "zh", "native": "Chinese", "tier": "core"},
    "Japanese": {"code": "ja", "native": "Japanese", "tier": "core"},
    "Korean": {"code": "ko", "native": "Korean", "tier": "core"},
    "German": {"code": "de", "native": "Deutsch", "tier": "core"},
    "French": {"code": "fr", "native": "Francais", "tier": "core"},
    "Russian": {"code": "ru", "native": "Russian", "tier": "core"},
    "Portuguese": {"code": "pt", "native": "Portugues", "tier": "core"},
    "Spanish": {"code": "es", "native": "Espanol", "tier": "core"},
    "Italian": {"code": "it", "native": "Italiano", "tier": "core"},
    "Arabic": {"code": "ar", "native": "Arabic", "tier": "extended"},
    "Dutch": {"code": "nl", "native": "Nederlands", "tier": "extended"},
    "Polish": {"code": "pl", "native": "Polski", "tier": "extended"},
    "Turkish": {"code": "tr", "native": "Turkce", "tier": "extended"},
    "Vietnamese": {"code": "vi", "native": "Tieng Viet", "tier": "extended"},
    "Thai": {"code": "th", "native": "Thai", "tier": "extended"},
    "Indonesian": {"code": "id", "native": "Bahasa Indonesia", "tier": "extended"},
    "Hindi": {"code": "hi", "native": "Hindi", "tier": "extended"},
    "Swahili": {"code": "sw", "native": "Kiswahili", "tier": "extended"},
    "Tamil": {"code": "ta", "native": "Tamil", "tier": "extended"},
}

VOICE_CLONE_LANGUAGES = {
    "English", "Chinese (Mandarin)", "Japanese", "Korean", "German",
    "French", "Russian", "Portuguese", "Spanish", "Italian",
}

PRESET_VOICES = [
    "Cherry -- Sunny, friendly",
    "Serena -- Gentle, soft",
    "Jennifer -- Cinematic narrator",
    "Katerina -- Mature, rich rhythm",
    "Ethan -- Warm, energetic",
    "Ryan -- Dramatic, rhythmic",
    "Kai -- Soothing, calm",
    "Neil -- Precise, clear",
    "Lenn -- Rational, steady",
    "Eldric Sage -- Authoritative narrator",
    "Arthur -- Classic, mature",
    "Bella -- Elegant, warm",
]


def get_voice_name(label):
    return label.split("--")[0].strip()


# ==========================================
# AUDIO HELPERS
# ==========================================
def base64_to_wav(b64_data, output_path):
    audio_bytes = base64.b64decode(b64_data)
    sr, nc, bps = 24000, 1, 16
    br = sr * nc * bps // 8
    ba = nc * bps // 8
    ds = len(audio_bytes)
    with open(output_path, "wb") as f:
        f.write(b"RIFF")
        f.write(struct.pack("<I", 36 + ds))
        f.write(b"WAVE")
        f.write(b"fmt ")
        f.write(struct.pack("<I", 16))
        f.write(struct.pack("<H", 1))
        f.write(struct.pack("<H", nc))
        f.write(struct.pack("<I", sr))
        f.write(struct.pack("<I", br))
        f.write(struct.pack("<H", ba))
        f.write(struct.pack("<H", bps))
        f.write(b"data")
        f.write(struct.pack("<I", ds))
        f.write(audio_bytes)


def get_audio_duration(filepath):
    result = subprocess.run(
        ["ffprobe", "-v", "quiet", "-show_entries", "format=duration",
         "-of", "default=noprint_wrappers=1:nokey=1", filepath],
        capture_output=True, text=True,
    )
    return float(result.stdout.strip())


def concatenate_media(files, output_path, media_type="audio"):
    if not files:
        return
    if len(files) == 1:
        shutil.copy2(files[0], output_path)
        return
    list_file = output_path + ".txt"
    with open(list_file, "w") as f:
        for fp in files:
            f.write(f"file '{fp}'\n")
    subprocess.run(
        ["ffmpeg", "-y", "-f", "concat", "-safe", "0",
         "-i", list_file, "-c", "copy", output_path],
        capture_output=True, check=True,
    )
    os.remove(list_file)


def generate_silence(duration_sec, output_path):
    subprocess.run(
        ["ffmpeg", "-y", "-f", "lavfi", "-i", "anullsrc=r=24000:cl=mono",
         "-t", str(duration_sec), "-acodec", "pcm_s16le", output_path],
        capture_output=True, check=True,
    )


# ==========================================
# DOCUMENT EXTRACTION
# ==========================================
def extract_text_from_file(filepath):
    ext = os.path.splitext(filepath)[1].lower()
    if ext == ".pdf":
        if not HAS_PYPDF:
            raise gr.Error("pypdf not installed.")
        reader = pypdf.PdfReader(filepath)
        return "\n\n".join(p.extract_text().strip() for p in reader.pages if p.extract_text())
    elif ext in (".docx", ".doc"):
        if ext == ".doc":
            raise gr.Error("Please save as .docx or .pdf.")
        if not HAS_DOCX:
            raise gr.Error("python-docx not installed.")
        doc = docx.Document(filepath)
        return "\n\n".join(p.text.strip() for p in doc.paragraphs if p.text.strip())
    else:
        with open(filepath, "r", encoding="utf-8", errors="replace") as f:
            return f.read()


# ==========================================
# TEXT SPLITTING INTO SCENES
# ==========================================
def split_into_scenes(text, max_chars=MAX_CHARS_PER_CHUNK):
    text = text.strip()
    if not text:
        return []
    if len(text) <= max_chars:
        return [text]

    chunks = []
    paragraphs = re.split(r"\n\s*\n", text)
    current = ""

    for para in paragraphs:
        para = para.strip()
        if not para:
            continue
        if len(current) + len(para) + 2 <= max_chars:
            current = (current + "\n\n" + para).strip()
        else:
            if current:
                chunks.append(current)
            if len(para) > max_chars:
                sentences = re.split(r"(?<=[.!?])\s+", para)
                current = ""
                for s in sentences:
                    if len(current) + len(s) + 1 <= max_chars:
                        current = (current + " " + s).strip()
                    else:
                        if current:
                            chunks.append(current)
                        current = s
            else:
                current = para

    if current:
        chunks.append(current)
    return chunks


# ==========================================
# SCENE PROMPT GENERATION (via Qwen)
# ==========================================
def generate_scene_prompt(client, scene_text, scene_index):
    """Use Qwen to create a cinematic video prompt from story text."""
    try:
        response = client.chat.completions.create(
            model=OMNI_MODEL,
            modalities=["text"],
            messages=[
                {
                    "role": "system",
                    "content": (
                        "You are a cinematic scene director. Given a passage from a story, "
                        "create a single vivid video generation prompt (max 200 words) that "
                        "describes the visual scene. Include: setting, lighting, camera angle, "
                        "character actions, mood. Do NOT include dialogue or text overlays. "
                        "Output ONLY the video prompt, nothing else."
                    ),
                },
                {
                    "role": "user",
                    "content": f"Create a cinematic video prompt for this scene:\n\n{scene_text[:1500]}",
                },
            ],
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        # Fallback: use first 200 chars as prompt
        return scene_text[:200]


# ==========================================
# VOICE CLONING
# ==========================================
def prepare_clone_audio(audio_path):
    result = subprocess.run(
        ["ffprobe", "-v", "quiet", "-show_entries", "format=duration",
         "-of", "default=noprint_wrappers=1:nokey=1", audio_path],
        capture_output=True, text=True,
    )
    duration = float(result.stdout.strip())

    if duration < 10:
        raise ValueError(f"Audio too short ({duration:.1f}s). Need at least 10 seconds.")

    tmp_prepared = audio_path + "_prepared.wav"
    if duration <= 60:
        subprocess.run(
            ["ffmpeg", "-y", "-i", audio_path, "-ar", "24000", "-ac", "1",
             "-acodec", "pcm_s16le", tmp_prepared],
            capture_output=True, check=True,
        )
    else:
        start = min(5, duration - 60)
        subprocess.run(
            ["ffmpeg", "-y", "-ss", str(start), "-t", "60", "-i", audio_path,
             "-ar", "24000", "-ac", "1", "-acodec", "pcm_s16le", tmp_prepared],
            capture_output=True, check=True,
        )
    return tmp_prepared


def clone_voice(audio_path, api_key):
    prepared = prepare_clone_audio(audio_path)
    b64 = base64.b64encode(pathlib.Path(prepared).read_bytes()).decode()
    try:
        os.remove(prepared)
    except OSError:
        pass

    resp = http_requests.post(VOICE_CLONE_URL, json={
        "model": VOICE_CLONE_MODEL,
        "input": {
            "action": "create",
            "target_model": TTS_VC_MODEL,
            "preferred_name": "storybook_voice",
            "audio": {"data": f"data:audio/wav;base64,{b64}"},
        },
    }, headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }, timeout=60)

    if resp.status_code != 200:
        raise RuntimeError(f"Voice clone failed: {resp.text[:300]}")
    return resp.json()["output"]["voice"]


# ==========================================
# AUDIO NARRATION
# ==========================================
def narrate_scene_preset(client, text, voice, language, lang_config, translate):
    """Narrate with preset voice via Qwen3.5-Omni-Plus. Returns (audio_b64_parts, transcript)."""
    if translate and language != "English":
        sys_prompt = (
            f"You are a professional audiobook narrator. "
            f"Translate the English text into {language} ({lang_config['native']}) "
            f"and narrate it expressively. Respond ONLY with the spoken {language} narration."
        )
        user_text = f"Translate into {language} and narrate:\n\n{text}"
    else:
        sys_prompt = "You are a professional audiobook narrator. Read expressively. Respond ONLY with narration."
        user_text = f"Narrate:\n\n{text}"

    completion = client.chat.completions.create(
        model=OMNI_MODEL,
        messages=[
            {"role": "system", "content": sys_prompt},
            {"role": "user", "content": user_text},
        ],
        modalities=["text", "audio"],
        audio={"voice": voice, "format": "wav"},
        stream=True,
        stream_options={"include_usage": True},
    )

    audio_parts, text_parts = [], []
    for event in completion:
        if not event.choices:
            continue
        delta = event.choices[0].delta
        if hasattr(delta, "content") and delta.content:
            text_parts.append(delta.content)
        if hasattr(delta, "audio") and delta.audio:
            if isinstance(delta.audio, dict) and "data" in delta.audio:
                audio_parts.append(delta.audio["data"])
            elif hasattr(delta.audio, "data") and delta.audio.data:
                audio_parts.append(delta.audio.data)

    return "".join(audio_parts), "".join(text_parts)


def narrate_scene_cloned(client, text, voice_id, language, lang_config, translate, api_key):
    """Translate then synthesize with cloned voice. Returns (audio_url, transcript)."""
    final_text = text
    if translate and language != "English":
        resp = client.chat.completions.create(
            model=OMNI_MODEL, modalities=["text"],
            messages=[
                {"role": "system", "content": f"Translate English to {language}. Output ONLY the translation."},
                {"role": "user", "content": f"Translate:\n\n{text}"},
            ],
        )
        final_text = resp.choices[0].message.content.strip()

    lang_map = {
        "English": "English", "Chinese (Mandarin)": "Chinese", "Japanese": "Japanese",
        "Korean": "Korean", "German": "German", "French": "French",
        "Russian": "Russian", "Portuguese": "Portuguese", "Spanish": "Spanish", "Italian": "Italian",
    }

    resp = http_requests.post(TTS_SYNTHESIS_URL, json={
        "model": TTS_VC_MODEL,
        "input": {"text": final_text, "voice": voice_id, "language_type": lang_map.get(language, "English")},
    }, headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}, timeout=120)

    if resp.status_code != 200:
        return None, final_text

    audio_url = resp.json().get("output", {}).get("audio", {}).get("url")
    return audio_url, final_text


# ==========================================
# HAPPYHORSE VIDEO GENERATION
# ==========================================
def sanitize_prompt(prompt, max_length=2000):
    """Clean and truncate prompt for HappyHorse API."""
    # Replace smart quotes and special chars
    prompt = prompt.replace("\u2018", "'").replace("\u2019", "'")
    prompt = prompt.replace("\u201c", '"').replace("\u201d", '"')
    prompt = prompt.replace("\u2014", " -- ").replace("\u2013", " - ")
    # Remove any control characters
    prompt = re.sub(r'[\x00-\x1f\x7f-\x9f]', ' ', prompt)
    # Collapse multiple spaces
    prompt = re.sub(r'\s+', ' ', prompt).strip()
    # Truncate
    if len(prompt) > max_length:
        prompt = prompt[:max_length].rsplit(' ', 1)[0]
    return prompt


def generate_video_happyhorse_app(prompt, api_key, duration=5, aspect="16:9", image_url=None):
    """Generate video via happyhorse.app."""
    headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}

    clean_prompt = sanitize_prompt(prompt, max_length=2000)

    payload = {
        "model": "happyhorse-1.0/video",
        "prompt": clean_prompt,
        "mode": "pro",
        "duration": int(duration),
        "aspect_ratio": aspect,
        "sound": False,
    }
    if image_url and not image_url.startswith("data:"):
        payload["image_urls"] = [image_url]

    generate_url = HAPPYHORSE_PROVIDERS["happyhorse.app"]["generate"]
    print(f"[HappyHorse] Submitting to {generate_url}")
    print(f"[HappyHorse] Payload: {json.dumps(payload)[:300]}")

    resp = http_requests.post(generate_url, json=payload, headers=headers, timeout=60)
    print(f"[HappyHorse] Submit response: {resp.status_code}")

    if resp.status_code != 200:
        resp_text = resp.text[:500]
        print(f"[HappyHorse] Error: {resp_text}")
        raise RuntimeError(f"happyhorse.app submit failed ({resp.status_code}): {resp_text}")

    resp_data = resp.json()
    print(f"[HappyHorse] Response data: {json.dumps(resp_data)[:300]}")

    # Handle different response structures
    task_id = (
        resp_data.get("data", {}).get("task_id")
        or resp_data.get("task_id")
        or resp_data.get("id")
    )
    if not task_id:
        raise RuntimeError(f"No task_id in response: {json.dumps(resp_data)[:300]}")

    print(f"[HappyHorse] Task ID: {task_id}")

    # Poll for completion
    status_url = HAPPYHORSE_PROVIDERS["happyhorse.app"]["status"]
    for attempt in range(90):  # ~15 minutes max
        time.sleep(10)
        try:
            s = http_requests.get(
                f"{status_url}?task_id={task_id}",
                headers=headers, timeout=30,
            ).json()
        except Exception as e:
            print(f"[HappyHorse] Poll error (attempt {attempt}): {e}")
            continue

        # Handle different status response structures
        status_data = s.get("data", s)
        status = (
            status_data.get("status", "")
            or s.get("status", "")
        )
        print(f"[HappyHorse] Poll {attempt}: status={status}")

        if status in ("SUCCESS", "COMPLETED", "completed", "succeeded"):
            # Try different result URL paths
            urls = (
                status_data.get("response", {}).get("resultUrls", [])
                or status_data.get("resultUrls", [])
                or [status_data.get("video_url")]
                or [status_data.get("output", {}).get("video_url")]
            )
            urls = [u for u in urls if u]  # Filter None
            if urls:
                print(f"[HappyHorse] Video URL: {urls[0][:100]}")
                return urls[0]
            raise RuntimeError(f"Status SUCCESS but no video URL found in: {json.dumps(s)[:300]}")

        elif status in ("FAILED", "failed", "error"):
            error_msg = (
                status_data.get("error_message", "")
                or status_data.get("error", "")
                or "Unknown error"
            )
            raise RuntimeError(f"happyhorse.app failed: {error_msg}")

    raise RuntimeError("happyhorse.app timeout after 15 minutes")


def generate_video_dashscope(prompt, api_key, duration=5, aspect="16:9", image_url=None):
    """Generate video via DashScope Bailian (async task API).
    Uses wan2.1-t2v-plus since happyhorse-1.0 is not yet available on DashScope.
    """
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
        "X-DashScope-Async": "enable",
    }

    clean_prompt = sanitize_prompt(prompt, max_length=2000)

    # Use Wan 2.1 T2V Plus (available on DashScope) as HappyHorse is not live yet
    if image_url and not image_url.startswith("data:"):
        model = "wan2.1-i2v-plus"
        payload = {
            "model": model,
            "input": {"prompt": clean_prompt, "img_url": image_url},
            "parameters": {"resolution": "1280*720"},
        }
    else:
        model = "wan2.1-t2v-plus"
        payload = {
            "model": model,
            "input": {"prompt": clean_prompt},
            "parameters": {"size": "1280*720"},
        }

    generate_url = HAPPYHORSE_PROVIDERS["DashScope (Bailian)"]["generate"]
    print(f"[DashScope] Submitting to {generate_url}")
    print(f"[DashScope] Prompt: {prompt[:100]}...")

    resp = http_requests.post(generate_url, json=payload, headers=headers, timeout=60)
    print(f"[DashScope] Submit response: {resp.status_code}")

    if resp.status_code != 200:
        resp_text = resp.text[:500]
        print(f"[DashScope] Error: {resp_text}")
        raise RuntimeError(f"DashScope submit failed ({resp.status_code}): {resp_text}")

    resp_data = resp.json()
    print(f"[DashScope] Response: {json.dumps(resp_data)[:300]}")

    task_id = resp_data.get("output", {}).get("task_id")
    if not task_id:
        raise RuntimeError(f"No task_id from DashScope: {json.dumps(resp_data)[:300]}")

    print(f"[DashScope] Task ID: {task_id}")

    for attempt in range(90):
        time.sleep(10)
        try:
            s = http_requests.get(
                f"{HAPPYHORSE_PROVIDERS['DashScope (Bailian)']['status']}/{task_id}",
                headers={"Authorization": f"Bearer {api_key}"}, timeout=30,
            ).json()
        except Exception as e:
            print(f"[DashScope] Poll error (attempt {attempt}): {e}")
            continue

        status = s.get("output", {}).get("task_status", "")
        print(f"[DashScope] Poll {attempt}: status={status}")

        if status == "SUCCEEDED":
            results = s.get("output", {}).get("results", [])
            if results:
                video_url = results[0].get("url")
                print(f"[DashScope] Video URL: {video_url[:100] if video_url else 'None'}")
                return video_url
            raise RuntimeError(f"SUCCEEDED but no results: {json.dumps(s)[:300]}")
        elif status == "FAILED":
            msg = s.get("output", {}).get("message", "Unknown error")
            raise RuntimeError(f"DashScope failed: {msg}")

    raise RuntimeError("DashScope timeout after 15 minutes")


def generate_video(prompt, provider, api_key, duration=5, aspect="16:9", image_url=None):
    """Route to the correct provider."""
    if provider == "happyhorse.app":
        return generate_video_happyhorse_app(prompt, api_key, duration, aspect, image_url)
    elif provider == "DashScope (Bailian)":
        return generate_video_dashscope(prompt, api_key, duration, aspect, image_url)
    else:
        raise ValueError(f"Unknown provider: {provider}")


def download_video(url, output_path):
    """Download a video from URL."""
    resp = http_requests.get(url, timeout=120, stream=True)
    if resp.status_code != 200:
        raise RuntimeError(f"Failed to download video: {resp.status_code}")
    with open(output_path, "wb") as f:
        for chunk in resp.iter_content(chunk_size=8192):
            f.write(chunk)
    return output_path


# ==========================================
# VIDEO + AUDIO COMPOSITING
# ==========================================
def create_scene_video(video_path, audio_path, output_path):
    """Combine a video clip with audio. Loop/trim video to match audio duration."""
    audio_dur = get_audio_duration(audio_path)

    # Create a video that matches audio length (loop if needed, trim if longer)
    subprocess.run([
        "ffmpeg", "-y",
        "-stream_loop", "-1", "-i", video_path,
        "-i", audio_path,
        "-map", "0:v:0", "-map", "1:a:0",
        "-c:v", "libx264", "-preset", "fast", "-crf", "23",
        "-c:a", "aac", "-b:a", "128k",
        "-t", str(audio_dur),
        "-shortest",
        output_path,
    ], capture_output=True, check=True)
    return output_path


def concatenate_videos(video_files, output_path):
    """Concatenate multiple MP4 files."""
    if len(video_files) == 1:
        shutil.copy2(video_files[0], output_path)
        return

    # Re-encode to ensure compatible streams
    normalized = []
    for i, vf in enumerate(video_files):
        norm = vf.replace(".mp4", f"_norm_{i}.mp4")
        subprocess.run([
            "ffmpeg", "-y", "-i", vf,
            "-c:v", "libx264", "-preset", "fast", "-crf", "23",
            "-c:a", "aac", "-b:a", "128k",
            "-ar", "24000", "-ac", "1",
            "-r", "24",
            "-s", "1280x720",
            norm,
        ], capture_output=True, check=True)
        normalized.append(norm)

    list_file = output_path + ".txt"
    with open(list_file, "w") as f:
        for vf in normalized:
            f.write(f"file '{vf}'\n")

    subprocess.run([
        "ffmpeg", "-y", "-f", "concat", "-safe", "0",
        "-i", list_file, "-c", "copy", output_path,
    ], capture_output=True, check=True)
    os.remove(list_file)


# ==========================================
# MAIN PIPELINE
# ==========================================
def generate_storybook(
    text_input, file_input, target_language, voice_mode, preset_voice_label,
    clone_audio, video_provider, video_mode, scene_images, aspect_ratio,
    video_duration, progress=gr.Progress(),
):
    # -- Resolve text --
    if file_input is not None:
        progress(0.01, desc="Extracting text...")
        text = extract_text_from_file(file_input)
    elif text_input and text_input.strip():
        text = text_input.strip()
    else:
        raise gr.Error("Please provide text or upload a file.")

    if len(text) < 20:
        raise gr.Error("Text is too short for a storybook.")

    # -- API keys --
    ds_key = os.environ.get("DASHSCOPE_API_KEY", "")
    if not ds_key:
        raise gr.Error("DASHSCOPE_API_KEY not set (needed for audio narration).")

    provider_config = HAPPYHORSE_PROVIDERS[video_provider]
    video_key = os.environ.get(provider_config["key_env"], "")
    if not video_key:
        raise gr.Error(f"{provider_config['key_env']} not set. Add it in Settings > Secrets.")

    lang_config = LANGUAGES[target_language]
    use_clone = voice_mode == "Clone a Voice"
    translate = target_language != "English"
    client = OpenAI(api_key=ds_key, base_url=DASHSCOPE_BASE_URL)
    tmp_dir = tempfile.mkdtemp(prefix="storybook_")

    # -- Voice cloning --
    cloned_voice_id = None
    if use_clone:
        if clone_audio is None:
            raise gr.Error("Upload a voice sample for cloning.")
        if target_language not in VOICE_CLONE_LANGUAGES:
            raise gr.Error(f"Voice cloning only supports: {', '.join(sorted(VOICE_CLONE_LANGUAGES))}")
        progress(0.03, desc="Cloning voice...")
        cloned_voice_id = clone_voice(clone_audio, ds_key)

    # -- Parse scene images if provided --
    image_list = []
    if scene_images:
        image_list = scene_images if isinstance(scene_images, list) else [scene_images]

    try:
        # -- Split into scenes --
        progress(0.05, desc="Splitting text into scenes...")
        scenes = split_into_scenes(text)
        total = len(scenes)
        scene_videos = []
        all_transcripts = []

        for i, scene_text in enumerate(scenes):
            base_frac = 0.08 + 0.85 * (i / total)

            # --- Step 1: Generate video prompt ---
            progress(base_frac, desc=f"Scene {i+1}/{total}: Creating video prompt...")
            if video_mode == "Text-to-Video (auto scene prompts)":
                video_prompt = generate_scene_prompt(client, scene_text, i)
                image_url = None
            elif video_mode == "Image-to-Video (animate uploaded images)":
                video_prompt = generate_scene_prompt(client, scene_text, i)
                # Use matching image if available, otherwise cycle through
                if image_list:
                    img_index = i % len(image_list)
                    img_path = image_list[img_index]
                    # Upload image and get URL -- for now, encode as data URI
                    # Note: some providers need a public URL; we base64 encode
                    img_b64 = base64.b64encode(pathlib.Path(img_path).read_bytes()).decode()
                    ext = pathlib.Path(img_path).suffix.lower()
                    mime = {".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp"}.get(ext, "image/jpeg")
                    image_url = f"data:{mime};base64,{img_b64}"
                else:
                    image_url = None
            else:  # Both
                video_prompt = generate_scene_prompt(client, scene_text, i)
                if image_list and i < len(image_list):
                    img_path = image_list[i]
                    img_b64 = base64.b64encode(pathlib.Path(img_path).read_bytes()).decode()
                    ext = pathlib.Path(img_path).suffix.lower()
                    mime = {".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png"}.get(ext, "image/jpeg")
                    image_url = f"data:{mime};base64,{img_b64}"
                else:
                    image_url = None

            # --- Step 2: Generate video ---
            progress(base_frac + 0.02, desc=f"Scene {i+1}/{total}: Generating video...")
            video_error = None
            try:
                video_url = generate_video(
                    video_prompt, video_provider, video_key,
                    duration=video_duration, aspect=aspect_ratio, image_url=image_url,
                )
                scene_video_path = os.path.join(tmp_dir, f"scene_{i:03d}_video.mp4")
                if video_url:
                    download_video(video_url, scene_video_path)
                    # Verify the downloaded file is a valid video
                    file_size = os.path.getsize(scene_video_path)
                    if file_size < 1000:
                        video_error = f"Downloaded file too small ({file_size} bytes) - likely not a video"
                        scene_video_path = None
                else:
                    video_error = "No video URL returned from API"
                    scene_video_path = None
            except Exception as e:
                video_error = str(e)
                scene_video_path = None

            if scene_video_path is None:
                # Create a placeholder with text overlay showing the error
                scene_video_path = os.path.join(tmp_dir, f"scene_{i:03d}_placeholder.mp4")
                # Escape special chars for ffmpeg drawtext
                safe_prompt = video_prompt[:80].replace("'", "").replace('"', '').replace(':', ' ')
                subprocess.run([
                    "ffmpeg", "-y", "-f", "lavfi", "-i",
                    f"color=c=0x1a1a2e:s=1280x720:d={video_duration}:r=24",
                    "-vf", f"drawtext=text='Scene {i+1}':fontsize=48:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2-40,"
                           f"drawtext=text='{safe_prompt[:60]}':fontsize=20:fontcolor=0xaaaaaa:x=(w-text_w)/2:y=(h-text_h)/2+30",
                    "-c:v", "libx264", "-preset", "fast",
                    scene_video_path,
                ], capture_output=True, check=True)
                all_transcripts.append(f"**Scene {i+1} VIDEO ERROR:** {video_error}")

            # --- Step 3: Generate narration audio ---
            progress(base_frac + 0.04, desc=f"Scene {i+1}/{total}: Narrating...")
            scene_audio_path = os.path.join(tmp_dir, f"scene_{i:03d}_audio.wav")

            try:
                if use_clone:
                    audio_url, transcript = narrate_scene_cloned(
                        client, scene_text, cloned_voice_id, target_language, lang_config, translate, ds_key,
                    )
                    if audio_url:
                        download_video(audio_url, scene_audio_path)
                    else:
                        generate_silence(5, scene_audio_path)
                else:
                    voice = get_voice_name(preset_voice_label)
                    audio_b64, transcript = narrate_scene_preset(
                        client, scene_text, voice, target_language, lang_config, translate,
                    )
                    if audio_b64:
                        base64_to_wav(audio_b64, scene_audio_path)
                    else:
                        generate_silence(5, scene_audio_path)
                        transcript = "No audio generated"

                if transcript:
                    all_transcripts.append(f"**Scene {i+1}:** {transcript[:200]}...")
            except Exception as e:
                generate_silence(5, scene_audio_path)
                all_transcripts.append(f"Scene {i+1} narration failed: {str(e)[:100]}")

            # --- Step 4: Combine video + audio for this scene ---
            progress(base_frac + 0.06, desc=f"Scene {i+1}/{total}: Compositing...")
            scene_final = os.path.join(tmp_dir, f"scene_{i:03d}_final.mp4")
            try:
                create_scene_video(scene_video_path, scene_audio_path, scene_final)
                scene_videos.append(scene_final)
            except Exception as e:
                all_transcripts.append(f"Scene {i+1} compositing failed: {str(e)[:100]}")

        if not scene_videos:
            raise gr.Error("No scenes were successfully generated.")

        # -- Final assembly --
        progress(0.95, desc="Assembling final storybook video...")
        final_video = os.path.join(tmp_dir, "storybook.mp4")
        concatenate_videos(scene_videos, final_video)

        progress(1.0, desc="Done!")

        # Stats
        video_size = os.path.getsize(final_video) / (1024 * 1024)
        stats = (
            f"**Visual Storybook Generated!**\n\n"
            f"- **Scenes:** {len(scene_videos)} / {total}\n"
            f"- **Language:** {target_language} ({lang_config['native']})\n"
            f"- **Voice:** {'Cloned' if use_clone else preset_voice_label}\n"
            f"- **Video Provider:** {video_provider}\n"
            f"- **File size:** {video_size:.1f} MB\n"
        )

        transcript_text = "\n\n---\n\n".join(all_transcripts) if all_transcripts else ""

        return final_video, stats, transcript_text

    except gr.Error:
        raise
    except Exception as e:
        raise gr.Error(f"Pipeline error: {str(e)}")


# ==========================================
# GRADIO UI
# ==========================================
SAMPLE_TEXT = """Chapter 1: The Lighthouse

The old lighthouse stood at the edge of the world. Each morning, Elena climbed one hundred and forty-seven iron steps to the lamp room and watched the sun rise from the sea like a great golden coin.

"One day," she whispered to the seagulls, "I'll follow that sun to wherever it goes."

Her grandfather was a man of few words but many stories. He kept them locked away like treasures, only bringing them out on winter nights when storms howled outside.

Chapter 2: The Storm

The storm came without warning. Dark clouds swallowed the horizon and the sea rose in great heaving walls of grey-green water. Elena pressed her face to the glass of the lamp room and watched lightning split the sky.

Through the rain, she saw something impossible - a ship, its sails torn to ribbons, riding the crest of a monstrous wave. And on its deck, a figure stood perfectly still, as if the fury of the ocean meant nothing at all.

Chapter 3: The Stranger

He arrived at dawn, walking up the rocky path as if he had always known the way. His clothes were dry despite the storm, and his eyes held the same changing colors as the sea itself.

"I've been looking for this lighthouse," he said simply. "I've been looking for a very long time."

Elena's grandfather stood in the doorway, his weathered face unreadable. Then slowly, he stepped aside and let the stranger in."""

DESCRIPTION = """
# Visual Storybook / Animated Audiobook
### English Text to Multi-Language Narrated Video
**Powered by Qwen3.5-Omni-Plus (narration) + HappyHorse 1.0 (video)**

Upload English text and generate a **narrated video storybook**:
- AI generates cinematic video scenes from your story
- Professional narration in 36 languages (preset or cloned voice)
- Each scene gets its own video + synchronized audio
- All scenes assembled into one final MP4

| Component | Model | What it does |
|-----------|-------|-------------|
| **Narration** | Qwen3.5-Omni-Plus | Translates + speaks the story |
| **Video** | HappyHorse 1.0 | Generates cinematic scene videos |
| **Scene Prompts** | Qwen3.5-Omni-Plus | Auto-creates video prompts from text |
"""

lang_choices = []
for name, cfg in LANGUAGES.items():
    if cfg["tier"] == "core":
        lang_choices.append(f"* {name}")
for name, cfg in LANGUAGES.items():
    if cfg["tier"] == "extended":
        lang_choices.append(name)


def clean_lang(choice):
    return choice.replace("* ", "").strip()


def on_voice_mode_change(mode):
    if mode == "Clone a Voice":
        return gr.update(visible=False), gr.update(visible=True)
    return gr.update(visible=True), gr.update(visible=False)


def on_video_mode_change(mode):
    show_images = "Image" in mode or "Both" in mode
    return gr.update(visible=show_images)


def generate_wrapper(text_input, file_input, lang, voice_mode, preset_voice,
                     clone_audio, video_provider, video_mode, scene_images,
                     aspect, duration, progress=gr.Progress()):
    language = clean_lang(lang)
    imgs = None
    if scene_images:
        imgs = [f.name if hasattr(f, 'name') else f for f in scene_images] if isinstance(scene_images, list) else scene_images
    return generate_storybook(
        text_input, file_input, language, voice_mode, preset_voice,
        clone_audio, video_provider, video_mode, imgs, aspect, duration, progress,
    )


with gr.Blocks(
    title="Visual Storybook Generator",
) as demo:

    gr.Markdown(DESCRIPTION)

    with gr.Row():
        # -- LEFT: Inputs --
        with gr.Column(scale=1):
            with gr.Tab("Story"):
                text_input = gr.Textbox(label="English Text", placeholder="Paste your story...", lines=8, max_lines=20)
                file_input = gr.File(
                    label="Or Upload (.txt, .md, .pdf, .docx)",
                    file_types=[".txt", ".md", ".pdf", ".docx"],
                    type="filepath",
                )
                sample_btn = gr.Button("Load Sample Story", variant="secondary", size="sm")

            with gr.Tab("Voice"):
                target_lang = gr.Dropdown(choices=lang_choices, value="* English", label="Target Language")
                voice_mode = gr.Radio(choices=["Preset Voice", "Clone a Voice"], value="Preset Voice", label="Voice Mode")
                preset_voice = gr.Dropdown(choices=PRESET_VOICES, value="Jennifer -- Cinematic narrator", label="Narrator Voice", visible=True)
                clone_audio = gr.Audio(label="Voice Sample (10s-3min)", type="filepath", visible=False)

            with gr.Tab("Video"):
                video_provider = gr.Dropdown(
                    choices=list(HAPPYHORSE_PROVIDERS.keys()),
                    value="happyhorse.app",
                    label="HappyHorse API Provider",
                    info="Each provider needs its own API key in Secrets",
                )
                video_mode = gr.Dropdown(
                    choices=[
                        "Text-to-Video (auto scene prompts)",
                        "Image-to-Video (animate uploaded images)",
                        "Both (images where available, text for rest)",
                    ],
                    value="Text-to-Video (auto scene prompts)",
                    label="Video Generation Mode",
                )
                scene_images = gr.File(
                    label="Scene Images (one per scene, optional)",
                    file_types=["image"],
                    file_count="multiple",
                    visible=False,
                )
                with gr.Row():
                    aspect_ratio = gr.Dropdown(choices=["16:9", "9:16", "1:1"], value="16:9", label="Aspect Ratio")
                    video_duration = gr.Slider(minimum=3, maximum=10, value=5, step=1, label="Video Duration (sec/scene)")

            generate_btn = gr.Button("Generate Visual Storybook", variant="primary", size="lg")
            test_btn = gr.Button("Test Video API Connection", variant="secondary", size="sm")

        # -- RIGHT: Output --
        with gr.Column(scale=1):
            video_output = gr.Video(label="Generated Storybook Video")
            stats_output = gr.Markdown(label="Stats")
            with gr.Accordion("Scene Transcripts", open=False):
                transcript_output = gr.Markdown()
            test_output = gr.Markdown(label="API Test Result", visible=True)

    # Events
    sample_btn.click(fn=lambda: SAMPLE_TEXT, outputs=text_input)
    voice_mode.change(fn=on_voice_mode_change, inputs=voice_mode, outputs=[preset_voice, clone_audio])
    video_mode.change(fn=on_video_mode_change, inputs=video_mode, outputs=[scene_images])

    def test_video_api(provider):
        """Test the video API connection with a simple request."""
        config = HAPPYHORSE_PROVIDERS[provider]
        api_key = os.environ.get(config["key_env"], "")

        if not api_key:
            return f"**FAILED:** `{config['key_env']}` is not set in Secrets."

        results = []
        results.append(f"**Provider:** {provider}")
        results.append(f"**API Key:** `{api_key[:8]}...{api_key[-4:]}`")

        # Step 1: Submit a short test generation
        if provider == "happyhorse.app":
            headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
            payload = {
                "model": "happyhorse-1.0/video",
                "prompt": "A golden sunset over calm ocean waves",
                "mode": "std",
                "duration": 3,
                "aspect_ratio": "16:9",
                "sound": False,
            }
            url = config["generate"]
        else:
            headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json", "X-DashScope-Async": "enable"}
            payload = {
                "model": "wan2.1-t2v-plus",
                "input": {"prompt": "A golden sunset over calm ocean waves"},
                "parameters": {"size": "1280*720"},
            }
            url = config["generate"]

        results.append(f"**URL:** `{url}`")
        results.append(f"**Payload:** `{json.dumps(payload)}`")

        try:
            resp = http_requests.post(url, json=payload, headers=headers, timeout=30)
            results.append(f"**HTTP Status:** {resp.status_code}")
            results.append(f"**Response:** ```{resp.text[:500]}```")

            if resp.status_code == 200:
                data = resp.json()
                task_id = (
                    data.get("data", {}).get("task_id")
                    or data.get("task_id")
                    or data.get("output", {}).get("task_id")
                )
                if task_id:
                    results.append(f"**Task ID:** `{task_id}` -- API is working! Task submitted successfully.")

                    # Quick poll once
                    time.sleep(5)
                    if provider == "happyhorse.app":
                        s_resp = http_requests.get(
                            f"{config['status']}?task_id={task_id}",
                            headers=headers, timeout=15,
                        )
                    else:
                        s_resp = http_requests.get(
                            f"{config['status']}/{task_id}",
                            headers={"Authorization": f"Bearer {api_key}"}, timeout=15,
                        )
                    results.append(f"**Status poll:** {s_resp.status_code}")
                    results.append(f"**Status body:** ```{s_resp.text[:500]}```")
                else:
                    results.append(f"**WARNING:** 200 OK but no task_id found in response")
            elif resp.status_code == 401:
                results.append("**ERROR:** Invalid API key (401 Unauthorized)")
            elif resp.status_code == 402:
                results.append("**ERROR:** Insufficient credits (402 Payment Required)")
            else:
                results.append(f"**ERROR:** Unexpected status {resp.status_code}")

        except Exception as e:
            results.append(f"**EXCEPTION:** {str(e)}")

        return "\n\n".join(results)

    test_btn.click(fn=test_video_api, inputs=[video_provider], outputs=[test_output])

    generate_btn.click(
        fn=generate_wrapper,
        inputs=[text_input, file_input, target_lang, voice_mode, preset_voice,
                clone_audio, video_provider, video_mode, scene_images,
                aspect_ratio, video_duration],
        outputs=[video_output, stats_output, transcript_output],
    )

    gr.Markdown(
        "---\n"
        "**Pipeline:** Text > Split into scenes > Qwen generates video prompts > "
        "HappyHorse generates video per scene > Qwen narrates audio per scene > "
        "FFmpeg composites video+audio > Concatenates all scenes into final MP4\n\n"
        "**API Keys needed:** DASHSCOPE_API_KEY (audio) + HAPPYHORSE_API_KEY (if using happyhorse.app) "
        "or just DASHSCOPE_API_KEY for both audio and video (DashScope provider)\n\n"
        "Built with Gradio | Narration by Qwen | Video by HappyHorse 1.0"
    )

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Soft(primary_hue="amber", secondary_hue="orange", neutral_hue="stone"))