File size: 27,558 Bytes
d74863e
d31990f
1d88d91
 
 
d74863e
d31990f
3c66ec8
d74863e
dc249d2
3c66ec8
d74863e
97312dd
d74863e
 
4eadcae
 
1d88d91
 
 
 
 
 
74d6673
 
 
1d88d91
74d6673
 
 
 
 
 
 
1d88d91
 
 
 
4eadcae
 
 
3c66ec8
 
4eadcae
 
 
3c66ec8
a5568e2
74fb373
4eadcae
3c66ec8
dc249d2
3c66ec8
 
dc249d2
3c66ec8
 
 
 
 
 
 
 
 
 
 
74fb373
 
 
 
 
 
3c66ec8
dc249d2
a5568e2
3c66ec8
a5568e2
3c66ec8
dc249d2
a5568e2
3c66ec8
 
 
 
a5568e2
 
3c66ec8
74fb373
 
a5568e2
 
 
4eadcae
 
 
 
 
 
 
 
dc249d2
3c66ec8
 
dc249d2
4eadcae
 
 
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74fb373
1d88d91
 
 
 
 
 
 
74fb373
 
 
 
 
 
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
74fb373
 
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eadcae
3c66ec8
4eadcae
 
 
 
3c66ec8
 
 
 
 
 
 
4eadcae
 
3c66ec8
 
 
 
 
 
 
4eadcae
 
 
 
 
 
 
1d88d91
 
 
 
 
 
 
 
74d6673
 
 
1d88d91
 
74d6673
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eadcae
 
 
 
d74863e
1d88d91
 
 
 
 
 
74d6673
 
 
 
1d88d91
d74863e
147d620
d31990f
 
74d6673
1d88d91
 
 
 
74d6673
 
1d88d91
d74863e
1d88d91
 
 
 
 
 
 
 
 
d31990f
 
74d6673
4eadcae
3c66ec8
d31990f
4eadcae
 
 
 
d74863e
4eadcae
 
74d6673
4eadcae
d74863e
1d88d91
 
4eadcae
 
 
 
3c66ec8
 
 
 
74fb373
4eadcae
d74863e
1d88d91
 
 
 
74d6673
d74863e
4eadcae
 
1d88d91
4eadcae
 
 
 
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74d6673
1d88d91
 
 
 
 
 
 
 
 
74d6673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d88d91
 
 
 
 
 
 
 
 
 
 
74d6673
 
 
 
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74d6673
 
 
 
 
 
 
 
 
 
 
1d88d91
 
74d6673
1d88d91
 
 
 
d74863e
1d88d91
 
147d620
1d88d91
4eadcae
 
 
1d88d91
a5568e2
147d620
1d88d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d74863e
3c66ec8
4eadcae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c66ec8
 
 
dc249d2
 
3c66ec8
dc249d2
 
3c66ec8
dc249d2
 
3c66ec8
4eadcae
dc249d2
3c66ec8
 
 
 
4eadcae
dc249d2
4eadcae
3c66ec8
a5568e2
3c66ec8
 
 
 
 
 
a5568e2
4eadcae
3c66ec8
 
 
 
4eadcae
3c66ec8
4eadcae
 
 
d74863e
 
4eadcae
 
 
 
 
c9146f3
d74863e
4eadcae
3c66ec8
4eadcae
c9146f3
3c66ec8
a5568e2
b27b1ac
 
 
 
 
 
 
a5568e2
 
d74863e
4eadcae
 
 
3c66ec8
 
4eadcae
 
 
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
import json
import os
import re
import time
from typing import Dict, List, Optional

from groq import Groq
from pydantic import ValidationError

from ..utils.logger import setup_logger
from .schemas import SummarySchema


logger = setup_logger(__name__)


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# CONFIGURATION
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

# Token threshold: below this, a single API call is used.
_SINGLE_PASS_TOKEN_LIMIT = 8_000

# Target chunk size for MAP phase (tokens).  Kept small so that
# prompt + chunk + response stays well under the 12K TPM free-tier limit.
_CHUNK_TARGET_TOKENS = 2_500

# Model โ€” unified for both MAP and REDUCE phases.
# llama-3.3-70b-versatile has 12K TPM on the free tier (the highest).
_MODEL_PRIMARY = "llama-3.3-70b-versatile"

# Maximum retries when a rate-limit (413 / 429) is hit.
_RATE_LIMIT_MAX_RETRIES = 3
_RATE_LIMIT_SLEEP_SECONDS = 60


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# PROMPT TEMPLATES โ€” SINGLE-PASS (unchanged)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

_SUMMARY_SYSTEM = """
You are an expert educational content analyst and structured note-taking specialist.
Transform raw video transcripts into clean, structured chronological JSON summaries.

LANGUAGE RULE โ€” CRITICAL, NEVER VIOLATE:
- Detect the primary language of the transcript.
- Every content field (title, summary, segments, conclusion) MUST be written entirely in that SAME detected language.
- Do NOT mix languages. Arabic transcript -> everything in Arabic.
- Only the "detected_language" and "suggested_category" fields are stated in English.

TIMELINE RULES โ€” STRICTLY ENFORCED:
- Divide the transcript into chronological segments that follow its natural progression.
- Produce a MINIMUM of 3 and a MAXIMUM of 7 segments.
- Each segment MUST cover a distinct phase or theme; do NOT repeat the same topic.
- Segments must be ordered chronologically as they appear in the transcript.
- Each segment must include:
  * title: a short descriptive title
  * summary: concise summary of that section (2-3 sentences)
  * key_insight: the single most important takeaway from that section
  * why_it_matters: brief explanation of value/importance (1-2 sentences)

TOPICS RULE:
- Extract the actual topics discussed in the video dynamically.
- Topics should be specific and descriptive (e.g. "Python", "Machine Learning", "Neural Networks").
- Do NOT use generic fixed categories.

CATEGORY RULE:
- Provide a single, concise category label (1-2 words max) in English.
- This should be the most accurate high-level category for the video content.
- Examples: "Programming", "Finance", "History", "Psychology", "Mathematics", "Cooking".
- The suggested_category MUST always be in English regardless of the transcript language.

CRITICAL: RETURN A JSON OBJECT EXACTLY MATCHING THIS STRUCTURE.
DO NOT CHANGE, OMIT, OR RENAME ANY KEYS.
{
    "title": "Inferred video title in transcript language",
    "detected_language": "English (or Arabic, etc.)",
    "summary": "Concise overall summary (3-5 sentences)",
    "segments": [
        {
            "title": "Segment title",
            "summary": "What this section covers (2-3 sentences)",
            "key_insight": "Most important point from this section",
            "why_it_matters": "Why this is valuable (1-2 sentences)"
        }
    ],
    "conclusion": "Final overall takeaway / closing conclusion",
    "topics": ["Topic1", "Topic2", "Topic3"],
    "suggested_category": "Programming"
}

OUTPUT: Return ONLY a valid JSON object. No markdown fences, no extra text.
""".strip()

_SUMMARY_USER = """
Video Title: {video_title}

TRANSCRIPT:
{transcript}

Analyze thoroughly. Detect the language.
Divide the content into 3-7 chronological segments.
For each segment provide: title, summary, key_insight, why_it_matters.
Return ONLY the exact JSON structure requested.
""".strip()


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# PROMPT TEMPLATES โ€” MAP PHASE
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

_MAP_SYSTEM = """
You are an expert educational content analyst.
You will receive ONE CHUNK of a longer video transcript.
Extract the key information from this chunk ONLY.

LANGUAGE RULE โ€” CRITICAL:
- Detect the primary language of the text.
- Write ALL content fields in that SAME detected language.
- Only "detected_language" is stated in English.

Return a JSON object with this EXACT structure:
{
    "detected_language": "English (or Arabic, etc.)",
    "chunk_summary": "Concise summary of this chunk (3-5 sentences)",
    "key_points": [
        {
            "title": "Short title for this point",
            "detail": "1-2 sentence explanation",
            "insight": "Key takeaway"
        }
    ],
    "topics": ["Topic1", "Topic2"]
}

RULES:
- Extract 2-4 key points from this chunk.
- Topics should be specific (e.g. "Python", "Neural Networks"), not generic.
- OUTPUT: Return ONLY a valid JSON object. No markdown fences, no extra text.
""".strip()

_MAP_USER = """
Video Title: {video_title}
Chunk {chunk_index} of {total_chunks}:

{chunk_text}

Extract the key information from this chunk. Return ONLY the JSON.
""".strip()


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# PROMPT TEMPLATES โ€” REDUCE PHASE
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

_REDUCE_SYSTEM = """
You are an expert educational content analyst and structured note-taking specialist.
You will receive INTERMEDIATE SUMMARIES from multiple chunks of a single video transcript.
Your job is to MERGE them into ONE final, cohesive, structured summary.

LANGUAGE RULE โ€” CRITICAL, NEVER VIOLATE:
- Use the detected language from the intermediate summaries.
- Every content field MUST be in that SAME language.
- Only "detected_language" and "suggested_category" are stated in English.

TIMELINE RULES โ€” STRICTLY ENFORCED:
- Merge the chunk summaries into 3-7 chronological segments.
- Each segment MUST cover a distinct phase or theme; do NOT repeat topics.
- Segments must follow the natural progression of the video.
- Each segment must include: title, summary, key_insight, why_it_matters.

CATEGORY RULE:
- Provide a single, concise category label (1-2 words max) in English.
- This should be the most accurate high-level category for the video content.
- Examples: "Programming", "Finance", "History", "Psychology", "Mathematics", "Cooking".
- The suggested_category MUST always be in English regardless of the transcript language.

CRITICAL: RETURN A JSON OBJECT EXACTLY MATCHING THIS STRUCTURE.
{
    "title": "Inferred video title in transcript language",
    "detected_language": "English (or Arabic, etc.)",
    "summary": "Concise overall summary (3-5 sentences)",
    "segments": [
        {
            "title": "Segment title",
            "summary": "What this section covers (2-3 sentences)",
            "key_insight": "Most important point from this section",
            "why_it_matters": "Why this is valuable (1-2 sentences)"
        }
    ],
    "conclusion": "Final overall takeaway / closing conclusion",
    "topics": ["Topic1", "Topic2", "Topic3"],
    "suggested_category": "Programming"
}

OUTPUT: Return ONLY a valid JSON object. No markdown fences, no extra text.
""".strip()

_REDUCE_USER = """
Video Title: {video_title}

The following are intermediate summaries extracted from {total_chunks} consecutive chunks
of the video transcript. Merge them into ONE cohesive final summary.

{merged_summaries}

Merge into 3-7 chronological segments. Return ONLY the final JSON structure.
""".strip()


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# LANGUAGE LABELS (simplified)
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

_LABELS = {
    "Arabic": {
        "source":     "ุงู„ู…ุตุฏุฑ",
        "duration":   "ุงู„ู…ุฏุฉ",
        "summary":    "ุงู„ู…ู„ุฎุต ุงู„ุนุงู…",
        "timeline":   "ุงู„ุชุณู„ุณู„ ุงู„ุฒู…ู†ูŠ",
        "insight":    "ุฃู‡ู… ู†ู‚ุทุฉ",
        "why":        "ู„ู…ุงุฐุง ูŠู‡ู…ุŸ",
        "conclusion": "ุงู„ุฎู„ุงุตุฉ",
    },
    "English": {
        "source":     "Source",
        "duration":   "Duration",
        "summary":    "Overall Summary",
        "timeline":   "Timeline",
        "insight":    "Key Insight",
        "why":        "Why It Matters",
        "conclusion": "Conclusion",
    },
}

def _labels(language: str) -> dict:
    return _LABELS.get(language, _LABELS["English"])


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# TOKEN UTILITIES
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def _estimate_tokens(text: str) -> int:
    """
    Lightweight token estimation using a word-count heuristic.

    Production logs show that Groq's tokenizer produces ~2.5 tokens per
    whitespace-delimited word for Arabic / mixed-script transcripts.
    Using 2.5ร— as a conservative multiplier to avoid underestimation.
    """
    word_count = len(text.split())
    return int(word_count * 2.5)


def _split_into_chunks(text: str, target_tokens: int = _CHUNK_TARGET_TOKENS) -> List[str]:
    """
    Split text into chunks of approximately `target_tokens` tokens each.

    Splits on sentence boundaries (period + space, newline) to avoid
    cutting mid-sentence. Falls back to word-level splitting if no
    sentence boundaries are found within a chunk.
    """
    # Split into sentences (on ". " or newline)
    sentences = re.split(r'(?<=[.!?])\s+|\n+', text)
    sentences = [s.strip() for s in sentences if s.strip()]

    chunks: List[str] = []
    current_chunk: List[str] = []
    current_tokens = 0

    for sentence in sentences:
        sentence_tokens = _estimate_tokens(sentence)

        # If a single sentence exceeds the target, split by words
        if sentence_tokens > target_tokens:
            # Flush current chunk first
            if current_chunk:
                chunks.append(" ".join(current_chunk))
                current_chunk = []
                current_tokens = 0

            words = sentence.split()
            word_buffer: List[str] = []
            buffer_tokens = 0
            for word in words:
                wt = _estimate_tokens(word)
                if buffer_tokens + wt > target_tokens and word_buffer:
                    chunks.append(" ".join(word_buffer))
                    word_buffer = [word]
                    buffer_tokens = wt
                else:
                    word_buffer.append(word)
                    buffer_tokens += wt
            if word_buffer:
                chunks.append(" ".join(word_buffer))
            continue

        if current_tokens + sentence_tokens > target_tokens and current_chunk:
            chunks.append(" ".join(current_chunk))
            current_chunk = [sentence]
            current_tokens = sentence_tokens
        else:
            current_chunk.append(sentence)
            current_tokens += sentence_tokens

    # Don't forget the last chunk
    if current_chunk:
        chunks.append(" ".join(current_chunk))

    return chunks


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# NOTE GENERATOR
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

class NoteGenerator:
    """
    Generates structured study notes using Groq.

    Automatically selects between:
    - **Single-pass**: for short transcripts (< 8K tokens)
    - **Map-Reduce**: for long transcripts (โ‰ฅ 8K tokens), splitting into
      chunks, summarizing each individually, then merging in a REDUCE pass.

    Uses a single model (llama-3.3-70b-versatile) for all phases and
    includes adaptive rate-limit retry (60s backoff on 413/429).
    """

    def __init__(self):
        self.api_key = os.environ.get("GROQ_API_KEY", "").strip()
        self.client = Groq(api_key=self.api_key) if self.api_key else None
        self.model = _MODEL_PRIMARY
        self.chunk_delay = float(
            os.environ.get("GROQ_CHUNK_DELAY_SECONDS", "3")
        )
        logger.info(
            "๐Ÿš€ NoteGenerator v5.1 initialized โ€” model: %s, delay: %.1fs",
            self.model, self.chunk_delay,
        )

    # โ”€โ”€ Low-level API call โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def _chat(
        self,
        system: str,
        user: str,
        max_tokens: int = 4096,
    ) -> Optional[str]:
        """Send a chat completion request to Groq."""
        try:
            response = self.client.chat.completions.create(
                model=self.model,
                max_tokens=max_tokens,
                temperature=0.3,
                response_format={"type": "json_object"},
                messages=[
                    {"role": "system", "content": system},
                    {"role": "user",   "content": user},
                ],
            )
            return response.choices[0].message.content
        except Exception as e:
            logger.error("โŒ Groq API call failed (model=%s): %s", self.model, e)
            return None

    # โ”€โ”€ Error fallback โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def _get_error_json(self, error_msg: str) -> Dict:
        return {
            "title": "Error in Generation",
            "detected_language": "English",
            "summary": f"Could not generate notes: {error_msg}",
            "segments": [],
            "conclusion": "",
            "topics": [],
            "suggested_category": "",
        }

    # โ”€โ”€ Single-pass summarization (short transcripts) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def _single_pass(self, transcript_text: str, video_title: str) -> Dict:
        """Process the entire transcript in one API call."""
        logger.info("๐Ÿ“ Single-pass summarization via %s", self.model)

        user_prompt = _SUMMARY_USER.format(
            video_title=video_title,
            transcript=transcript_text,
        )

        raw = self._chat(_SUMMARY_SYSTEM, user_prompt, max_tokens=4096)
        if raw is None:
            return self._get_error_json("Groq API call failed (single-pass).")

        return self._parse_and_validate(raw)

    # โ”€โ”€ Map-Reduce summarization (long transcripts) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def _map_reduce(self, transcript_text: str, video_title: str) -> Dict:
        """
        Split transcript into chunks, summarize each (MAP), then merge (REDUCE).
        """
        chunks = _split_into_chunks(transcript_text)
        total = len(chunks)
        logger.info(
            "๐Ÿ—บ๏ธ  Map-Reduce activated: %d chunks (delay=%.1fs between calls)",
            total, self.chunk_delay,
        )

        # โ”€โ”€ MAP PHASE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        intermediate_results: List[Dict] = []

        for i, chunk in enumerate(chunks, start=1):
            chunk_tokens = _estimate_tokens(chunk)
            logger.info(
                "  ๐Ÿ“ฆ MAP chunk %d/%d (~%d est. tokens)...", i, total, chunk_tokens,
            )

            user_prompt = _MAP_USER.format(
                video_title=video_title,
                chunk_index=i,
                total_chunks=total,
                chunk_text=chunk,
            )

            # Retry loop with adaptive backoff on rate-limit errors
            raw = None
            for attempt in range(1, _RATE_LIMIT_MAX_RETRIES + 1):
                raw = self._chat(
                    _MAP_SYSTEM, user_prompt,
                    max_tokens=2048,
                )

                if raw is not None:
                    break  # success

                # _chat() returns None on any exception. Check if it was a
                # rate-limit error (413 / 429) by inspecting the last
                # exception.  We re-try with a 60s sleep.
                logger.warning(
                    "  โš ๏ธ MAP chunk %d/%d attempt %d/%d failed. "
                    "Sleeping %ds for TPM window reset...",
                    i, total, attempt, _RATE_LIMIT_MAX_RETRIES,
                    _RATE_LIMIT_SLEEP_SECONDS,
                )
                time.sleep(_RATE_LIMIT_SLEEP_SECONDS)

            if raw:
                try:
                    parsed = json.loads(raw)
                    intermediate_results.append(parsed)
                    logger.info("  โœ… MAP chunk %d/%d done.", i, total)
                except json.JSONDecodeError as e:
                    logger.warning(
                        "  โš ๏ธ MAP chunk %d/%d returned invalid JSON: %s", i, total, e,
                    )
            else:
                logger.error(
                    "  โŒ MAP chunk %d/%d failed after %d retries. Skipping.",
                    i, total, _RATE_LIMIT_MAX_RETRIES,
                )

            # Respect TPM limits โ€” delay between consecutive API calls
            if i < total and self.chunk_delay > 0:
                logger.info("  โณ Sleeping %.1fs (TPM cooldown)...", self.chunk_delay)
                time.sleep(self.chunk_delay)

        if not intermediate_results:
            return self._get_error_json(
                "Map-Reduce failed: no chunks were successfully summarized."
            )

        # โ”€โ”€ REDUCE PHASE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        logger.info("๐Ÿ”— REDUCE phase: merging %d intermediate summaries...", len(intermediate_results))

        # Build a readable merged text for the reduce prompt
        merged_parts: List[str] = []
        all_topics: List[str] = []
        detected_lang = "English"

        for idx, result in enumerate(intermediate_results, start=1):
            detected_lang = result.get("detected_language", detected_lang)
            chunk_summary = result.get("chunk_summary", "")
            key_points = result.get("key_points", [])
            topics = result.get("topics", [])
            all_topics.extend(topics)

            part = f"--- Chunk {idx} ---\n"
            part += f"Summary: {chunk_summary}\n"
            for kp in key_points:
                if isinstance(kp, dict):
                    part += f"- {kp.get('title', '')}: {kp.get('detail', '')} "
                    part += f"(Insight: {kp.get('insight', '')})\n"
            part += f"Topics: {', '.join(topics)}\n"
            merged_parts.append(part)

        merged_text = "\n".join(merged_parts)

        # Check if the merged text itself is within single-pass limits
        reduce_tokens = _estimate_tokens(merged_text)
        logger.info("๐Ÿ”— REDUCE input: ~%d tokens", reduce_tokens)

        user_prompt = _REDUCE_USER.format(
            video_title=video_title,
            total_chunks=len(intermediate_results),
            merged_summaries=merged_text,
        )

        # Sleep before REDUCE to ensure TPM cooldown from last MAP call
        if self.chunk_delay > 0:
            logger.info("  โณ Sleeping %.1fs before REDUCE call...", self.chunk_delay)
            time.sleep(self.chunk_delay)

        # REDUCE with retry on rate-limit
        raw = None
        for attempt in range(1, _RATE_LIMIT_MAX_RETRIES + 1):
            raw = self._chat(_REDUCE_SYSTEM, user_prompt, max_tokens=4096)
            if raw is not None:
                break
            logger.warning(
                "  โš ๏ธ REDUCE attempt %d/%d failed. Sleeping %ds...",
                attempt, _RATE_LIMIT_MAX_RETRIES, _RATE_LIMIT_SLEEP_SECONDS,
            )
            time.sleep(_RATE_LIMIT_SLEEP_SECONDS)

        if raw is None:
            return self._get_error_json("Groq API call failed (REDUCE phase after retries).")

        return self._parse_and_validate(raw)

    # โ”€โ”€ JSON parsing + schema validation โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def _parse_and_validate(self, raw_json: str) -> Dict:
        """Parse raw JSON string and validate against SummarySchema."""
        try:
            data = json.loads(raw_json)
            validated = SummarySchema(**data)
            return validated.model_dump()
        except (json.JSONDecodeError, ValidationError) as e:
            logger.error("โŒ Schema validation failed: %s", e)
            return self._get_error_json(f"Validation Error: {str(e)}")

    # โ”€โ”€ Public API (unchanged signature) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def generateSummary(self, transcript_text: str, video_title: str) -> Dict:
        """
        Generate structured JSON summary from transcript.

        Automatically selects single-pass or Map-Reduce based on estimated
        token count. The return type is always a Dict matching SummarySchema.
        """
        if not self.client:
            return self._get_error_json("Groq API Key missing.")

        # Estimate total tokens for the full prompt
        full_prompt = _SUMMARY_USER.format(
            video_title=video_title,
            transcript=transcript_text,
        )
        total_tokens = _estimate_tokens(_SUMMARY_SYSTEM + full_prompt)

        logger.info(
            "๐Ÿ“Š Token estimate: ~%d tokens (threshold: %d)",
            total_tokens, _SINGLE_PASS_TOKEN_LIMIT,
        )

        if total_tokens < _SINGLE_PASS_TOKEN_LIMIT:
            return self._single_pass(transcript_text, video_title)
        else:
            logger.info(
                "โšก Transcript too large for single-pass (%d โ‰ฅ %d). "
                "Activating Map-Reduce pipeline...",
                total_tokens, _SINGLE_PASS_TOKEN_LIMIT,
            )
            return self._map_reduce(transcript_text, video_title)

    # โ”€โ”€ Markdown formatting (unchanged) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    def format_notes_to_markdown(self, json_notes: Dict) -> str:
        """Convert JSON notes to clean Markdown โ€” Summary โ†’ Timeline โ†’ Conclusion."""
        lang = json_notes.get("detected_language", "English")
        L = _labels(lang)
        lines: list[str] = []

        def add(text: str = ""):
            lines.append(text)

        def blank():
            lines.append("")

        def divider():
            lines.append("")
            lines.append("---")
            lines.append("")

        # โ”€โ”€ OVERALL SUMMARY โ”€โ”€
        summary = json_notes.get("summary", "")
        if summary:
            add(f"## ๐Ÿ“‹ {L['summary']}")
            blank()
            add(summary)
            divider()

        # โ”€โ”€ TIMELINE โ”€โ”€
        segments = json_notes.get("segments", [])
        if segments:
            add(f"## ๐Ÿ• {L['timeline']}")
            blank()
            for i, seg in enumerate(segments, start=1):
                s_title   = seg.get("title", "")   if isinstance(seg, dict) else seg.title
                s_summary = seg.get("summary", "")  if isinstance(seg, dict) else seg.summary
                s_insight = seg.get("key_insight", "") if isinstance(seg, dict) else seg.key_insight
                s_why     = seg.get("why_it_matters", "") if isinstance(seg, dict) else seg.why_it_matters

                add(f"### {i}. {s_title}")
                blank()
                add(s_summary)
                blank()
                if s_insight:
                    add(f"> **๐Ÿ’Ž {L['insight']}:** {s_insight}")
                    blank()
                if s_why:
                    add(f"> **{L['why']}** {s_why}")
                    blank()
            divider()

        # โ”€โ”€ CONCLUSION โ”€โ”€
        conclusion = json_notes.get("conclusion", "")
        if conclusion:
            add(f"## ๐Ÿ”– {L['conclusion']}")
            blank()
            add(f"> {conclusion}")
            blank()

        return "\n".join(lines)

    def format_final_notes(
        self,
        notes: str,
        video_title: str,
        video_url: str,
        duration: int,
        detected_language: str = "English",
    ) -> str:
        """
        Wrap the formatted Markdown body with Source + Duration header.
        """
        L = _labels(detected_language)

        if duration and duration > 0:
            hours   = int(duration // 3600)
            minutes = int((duration % 3600) // 60)
            secs    = int(duration % 60)
            if hours > 0:
                duration_str = f"{hours}:{minutes:02d}:{secs:02d}"
            else:
                duration_str = f"{minutes:02d}:{secs:02d}"
        else:
            duration_str = "N/A (Auto-generated)"

        header = (
            f"# {video_title}\n\n"
            f"---\n\n"
            f"> **{L['source']}:** {video_url}  \n"
            f"> **{L['duration']}:** {duration_str}\n\n"
            f"---\n\n"
        )
        return header + notes