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 
 
d76b08e
d60565b
d23c5ad
 
d60565b
 
 
a9de5f0
d76b08e
d60565b
 
d0f2f4f
d60565b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d76b08e
 
 
 
 
d60565b
d76b08e
 
 
 
 
 
 
 
 
 
 
d60565b
a9de5f0
 
 
d76b08e
d60565b
 
 
 
29b30f3
 
 
 
 
 
a9de5f0
d60565b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9de5f0
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
import gradio as gr
import json
import os
import logging
import requests
import re
import tempfile
import numpy as np

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Anthropic API key - can be set as HuggingFace secret or environment variable
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "")

# Try to import transcription libraries
try:
    from speechbrain.pretrained import EncoderDecoderASR
    import torch
    SPEECHBRAIN_AVAILABLE = True
    logger.info("SpeechBrain available for transcription")
except ImportError as e:
    logger.warning(f"SpeechBrain not available: {e}")
    SPEECHBRAIN_AVAILABLE = False

# Try to import video processing
try:
    import moviepy.editor as mp
    MOVIEPY_AVAILABLE = True
    logger.info("MoviePy available for video processing")
except ImportError as e:
    logger.warning(f"MoviePy not available: {e}")
    MOVIEPY_AVAILABLE = False

# Try to import speaker diarization
try:
    from pyannote.audio import Pipeline
    from pyannote.audio.pipelines.utils.hook import ProgressHook
    DIARIZATION_AVAILABLE = True
    logger.info("Pyannote.audio available for speaker diarization")
except ImportError as e:
    logger.warning(f"Pyannote.audio not available: {e}")
    DIARIZATION_AVAILABLE = False

# Try to import sentiment and emotion analysis
try:
    from transformers import pipeline
    SENTIMENT_AVAILABLE = True
    logger.info("Transformers available for sentiment analysis")
except ImportError as e:
    logger.warning(f"Transformers not available: {e}")
    SENTIMENT_AVAILABLE = False

# Initialize models if available
asr_model = None
sentiment_model = None
emotion_model = None
diarization_pipeline = None

if SPEECHBRAIN_AVAILABLE:
    try:
        asr_model = EncoderDecoderASR.from_hparams(
            source="speechbrain/asr-crdnn-rnnlm-librispeech",
            savedir="pretrained_models/asr-crdnn-rnnlm-librispeech"
        )
        logger.info("ASR model loaded successfully")
    except Exception as e:
        logger.error(f"Error loading ASR model: {e}")
        SPEECHBRAIN_AVAILABLE = False

if SENTIMENT_AVAILABLE:
    try:
        sentiment_model = pipeline(
            "sentiment-analysis",
            model="cardiffnlp/twitter-roberta-base-sentiment-latest",
            top_k=None
        )
        emotion_model = pipeline(
            "text-classification",
            model="j-hartmann/emotion-english-distilroberta-base",
            top_k=None
        )
        logger.info("Sentiment and emotion models loaded")
    except Exception as e:
        logger.error(f"Error loading sentiment models: {e}")
        SENTIMENT_AVAILABLE = False

if DIARIZATION_AVAILABLE:
    try:
        HF_TOKEN = os.getenv("HF_TOKEN", "")
        if HF_TOKEN:
            diarization_pipeline = Pipeline.from_pretrained(
                "pyannote/speaker-diarization@2.1",
                use_auth_token=HF_TOKEN
            )
            logger.info("Speaker diarization pipeline loaded")
        else:
            logger.warning("HF_TOKEN not set - speaker diarization will be disabled")
    except Exception as e:
        logger.error(f"Error loading diarization pipeline: {e}")

# Check if API key is available
if ANTHROPIC_API_KEY:
    logger.info("Claude API key found")
else:
    logger.warning("Claude API key not found - using demo mode")

def validate_analysis_completeness(response_text):
    """Validate that all 12 sections are present in the analysis"""
    required_sections = [
        "1. SPEECH FACTORS",
        "2. LANGUAGE SKILLS ASSESSMENT", 
        "3. COMPLEX SENTENCE ANALYSIS",
        "4. FIGURATIVE LANGUAGE ANALYSIS",
        "5. PRAGMATIC LANGUAGE ASSESSMENT",
        "6. VOCABULARY AND SEMANTIC ANALYSIS",
        "7. MORPHOLOGICAL AND PHONOLOGICAL ANALYSIS",
        "8. COGNITIVE-LINGUISTIC FACTORS",
        "9. FLUENCY AND RHYTHM ANALYSIS",
        "10. QUANTITATIVE METRICS",
        "11. CLINICAL IMPLICATIONS",
        "12. PROGNOSIS AND SUMMARY"
    ]
    
    missing_sections = []
    for section in required_sections:
        if section not in response_text:
            missing_sections.append(section)
    
    if missing_sections:
        print(f"\n⚠️  MISSING SECTIONS: {missing_sections}")
        return False
    else:
        print(f"\nβœ… ALL 12 SECTIONS PRESENT")
        return True

def call_claude_api_with_continuation(prompt, max_continuations=3):
    """Call Claude API with continuation prompting to ensure complete responses"""
    if not ANTHROPIC_API_KEY:
        return "❌ Claude API key not configured. Please set ANTHROPIC_API_KEY environment variable."
    
    try:
        full_response = ""
        continuation_count = 0
        
        # Add continuation instruction to original prompt
        initial_prompt = prompt + "\n\nIMPORTANT: If your response is cut off or incomplete, end with <CONTINUE> to indicate more content is needed. Ensure you complete all sections of the analysis."
        
        while continuation_count <= max_continuations:
            if continuation_count == 0:
                current_prompt = initial_prompt
            else:
                # For continuations, provide context about what was already covered
                current_prompt = prompt + f"\n\nContinue from where you left off (continuation {continuation_count + 1} of {max_continuations}):\n\nIMPORTANT: Do not repeat what you've already written. Continue with the next section or complete any unfinished sections. If you're done, do not include <CONTINUE>. Provide the remaining analysis sections. Make sure to complete ALL 12 sections of the analysis."
            
            headers = {
                "Content-Type": "application/json",
                "x-api-key": ANTHROPIC_API_KEY,
                "anthropic-version": "2023-06-01"
            }
            
            data = {
                "model": "claude-3-5-sonnet-20241022",
                "max_tokens": 4096,
                "messages": [
                    {
                        "role": "user",
                        "content": current_prompt
                    }
                ]
            }
            
            response = requests.post(
                "https://api.anthropic.com/v1/messages",
                headers=headers,
                json=data,
                timeout=90
            )
            
            if response.status_code == 200:
                response_json = response.json()
                response_text = response_json['content'][0]['text']
                
                # Log response for debugging
                print(f"\n=== PART {continuation_count + 1} RESPONSE ===")
                print(f"Length: {len(response_text)} characters")
                print(f"Contains CONTINUE: {'<CONTINUE>' in response_text}")
                print(f"First 200 chars: {response_text[:200]}...")
                print(f"Last 200 chars: {response_text[-200:]}...")
                print("=" * 50)
                
                # Simple string combination - no complex processing
                if continuation_count == 0:
                    full_response = response_text
                else:
                    # Just add a newline and append the continuation
                    full_response += "\n\n" + response_text
                
                # Check if response indicates continuation is needed
                needs_continuation = "<CONTINUE>" in response_text
                
                print(f"Needs continuation: {needs_continuation}")
                print(f"Continuation count: {continuation_count}/{max_continuations}")
                
                # Continue if <CONTINUE> is present and we haven't reached max
                if needs_continuation and continuation_count < max_continuations:
                    # Remove the CONTINUE marker
                    full_response = full_response.replace("<CONTINUE>", "")
                    continuation_count += 1
                    logger.info(f"Continuing analysis (attempt {continuation_count}/{max_continuations})")
                    continue
                else:
                    # Clean up any remaining continuation markers
                    full_response = full_response.replace("<CONTINUE>", "")
                    break
            else:
                logger.error(f"Claude API error: {response.status_code} - {response.text}")
                return f"❌ Claude API Error: {response.status_code}"
                
    except Exception as e:
        logger.error(f"Error calling Claude API: {str(e)}")
        return f"❌ Error: {str(e)}"
    
    # Add completion indicator
    if continuation_count > 0:
        full_response += f"\n\n[Analysis completed in {continuation_count + 1} parts]"
    
    # Log final response for debugging
    print(f"\n=== FINAL COMPLETE RESPONSE ===")
    print(f"Total length: {len(full_response)} characters")
    print(f"Number of parts: {continuation_count + 1}")
    print("=" * 50)
    
    # Print the entire final response for debugging
    print(f"\n=== ENTIRE FINAL RESPONSE ===")
    print(full_response)
    print("=" * 50)
    
    return full_response

def call_claude_api(prompt):
    """Call Claude API directly (legacy function for backward compatibility)"""
    return call_claude_api_with_continuation(prompt, max_continuations=0)

def extract_audio_from_video(video_path):
    """Extract audio from video file"""
    if not MOVIEPY_AVAILABLE:
        return None, "MoviePy not available for video processing"
    
    try:
        temp_audio = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
        temp_audio_path = temp_audio.name
        temp_audio.close()
        
        video = mp.VideoFileClip(video_path)
        audio = video.audio
        
        if audio is None:
            return None, "No audio track found in video file"
        
        audio.write_audiofile(temp_audio_path, verbose=False, logger=None)
        video.close()
        audio.close()
        
        return temp_audio_path, "Audio extracted successfully"
        
    except Exception as e:
        logger.error(f"Error extracting audio: {e}")
        return None, f"Error extracting audio: {str(e)}"

def perform_speaker_diarization(audio_path):
    """Perform speaker diarization on audio file"""
    if not DIARIZATION_AVAILABLE or not diarization_pipeline:
        return None, "Speaker diarization not available"
    
    try:
        with ProgressHook() as hook:
            diarization = diarization_pipeline(audio_path, hook=hook)
        
        speaker_segments = []
        for turn, _, speaker in diarization.itertracks(yield_label=True):
            speaker_segments.append({
                'start': turn.start,
                'end': turn.end,
                'speaker': speaker,
                'duration': turn.end - turn.start
            })
        
        logger.info(f"Diarization completed: {len(speaker_segments)} segments found")
        return speaker_segments, "Diarization completed successfully"
        
    except Exception as e:
        logger.error(f"Error in diarization: {e}")
        return None, f"Diarization error: {str(e)}"

def transcribe_audio_with_metadata(audio_file, enable_diarization=True):
    """Transcribe audio with timestamps, sentiment, and metadata"""
    if not audio_file:
        return None, "No audio file provided"
    
    if not SPEECHBRAIN_AVAILABLE:
        return None, "SpeechBrain not available for transcription"
    
    try:
        # Check if it's a video file
        file_extension = os.path.splitext(audio_file)[1].lower()
        if file_extension in ['.mp4', '.avi', '.mov', '.mkv', '.wmv', '.flv']:
            processed_audio, status = extract_audio_from_video(audio_file)
            if not processed_audio:
                return None, status
        else:
            processed_audio = audio_file
        
        # Perform speaker diarization if enabled
        speaker_segments = None
        diarization_status = ""
        if enable_diarization:
            speaker_segments, diarization_status = perform_speaker_diarization(processed_audio)
        
        # Get transcription
        transcript = asr_model.transcribe_file(processed_audio)
        
        # Clean up temporary file if created
        if processed_audio != audio_file and os.path.exists(processed_audio):
            try:
                os.unlink(processed_audio)
            except:
                pass
        
        # Split into sentences and add metadata
        sentences = re.split(r'[.!?]+', transcript)
        sentences = [s.strip() for s in sentences if s.strip()]
        
        rich_transcript = []
        current_time = 0
        
        for i, sentence in enumerate(sentences):
            timestamp = current_time + (i * 2)
            
            # Determine speaker
            speaker = "UNKNOWN"
            if speaker_segments:
                for segment in speaker_segments:
                    if segment['start'] <= timestamp <= segment['end']:
                        speaker = segment['speaker']
                        break
            
            # Sentiment and emotion analysis
            sentiment = {'label': 'neutral', 'score': 0.5}
            emotion = {'label': 'neutral', 'score': 0.5}
            
            if SENTIMENT_AVAILABLE:
                try:
                    sentiment_result = sentiment_model(sentence)[0] if sentiment_model else None
                    sentiment = max(sentiment_result, key=lambda x: x['score']) if sentiment_result else sentiment
                    
                    emotion_result = emotion_model(sentence)[0] if emotion_model else None
                    emotion = max(emotion_result, key=lambda x: x['score']) if emotion_result else emotion
                except:
                    pass
            
            # Word metrics
            words = sentence.split()
            word_count = len(words)
            avg_word_length = np.mean([len(word) for word in words]) if words else 0
            speech_rate = word_count * 30 / 60
            
            rich_transcript.append({
                'timestamp': timestamp,
                'speaker': speaker,
                'sentence': sentence,
                'word_count': word_count,
                'avg_word_length': round(avg_word_length, 2),
                'speech_rate_wpm': round(speech_rate, 1),
                'sentiment': sentiment['label'],
                'sentiment_score': round(sentiment['score'], 3),
                'emotion': emotion['label'],
                'emotion_score': round(emotion['score'], 3)
            })
            
            current_time = timestamp
        
        status_msg = f"Transcription completed successfully"
        if diarization_status:
            status_msg += f" {diarization_status}"
        
        return rich_transcript, status_msg
        
    except Exception as e:
        logger.error(f"Error in transcription: {e}")
        return None, f"Transcription error: {str(e)}"

def format_rich_transcript(rich_transcript):
    """Format rich transcript for display"""
    if not rich_transcript:
        return "No transcript data available"
    
    formatted_lines = []
    for entry in rich_transcript:
        timestamp_str = f"{int(entry['timestamp']//60):02d}:{int(entry['timestamp']%60):02d}"
        
        line = f"[{timestamp_str}] *{entry['speaker']}: {entry['sentence']}"
        line += f" [Words: {entry['word_count']}, Rate: {entry['speech_rate_wpm']}wpm]"
        line += f" [Sentiment: {entry['sentiment']} ({entry['sentiment_score']})]"
        line += f" [Emotion: {entry['emotion']} ({entry['emotion_score']})]"
        
        formatted_lines.append(line)
    
    return '\n'.join(formatted_lines)

def calculate_slp_metrics(rich_transcript):
    """Calculate comprehensive SLP metrics"""
    if not rich_transcript:
        return {}
    
    # Basic metrics
    total_sentences = len(rich_transcript)
    total_words = sum(entry['word_count'] for entry in rich_transcript)
    total_duration = rich_transcript[-1]['timestamp'] if rich_transcript else 0
    
    # Speaker analysis
    speakers = {}
    for entry in rich_transcript:
        speaker = entry['speaker']
        if speaker not in speakers:
            speakers[speaker] = {
                'sentences': 0,
                'words': 0,
                'sentiments': [],
                'emotions': []
            }
        speakers[speaker]['sentences'] += 1
        speakers[speaker]['words'] += entry['word_count']
        speakers[speaker]['sentiments'].append(entry['sentiment'])
        speakers[speaker]['emotions'].append(entry['emotion'])
    
    # Word-level analysis
    all_words = []
    for entry in rich_transcript:
        words = entry['sentence'].lower().split()
        all_words.extend(words)
    
    # Word frequency distribution
    word_freq = {}
    for word in all_words:
        word_clean = re.sub(r'[^\w\s]', '', word)
        if word_clean:
            word_freq[word_clean] = word_freq.get(word_clean, 0) + 1
    
    # Vocabulary diversity (Type-Token Ratio)
    unique_words = len(set(all_words))
    ttr = unique_words / total_words if total_words > 0 else 0
    
    # Speech rate analysis
    speech_rates = [entry['speech_rate_wpm'] for entry in rich_transcript]
    avg_speech_rate = np.mean(speech_rates) if speech_rates else 0
    
    # Sentiment analysis
    sentiment_counts = {}
    emotion_counts = {}
    for entry in rich_transcript:
        sentiment_counts[entry['sentiment']] = sentiment_counts.get(entry['sentiment'], 0) + 1
        emotion_counts[entry['emotion']] = emotion_counts.get(entry['emotion'], 0) + 1
    
    # Sentence complexity
    sentence_lengths = [entry['word_count'] for entry in rich_transcript]
    avg_sentence_length = np.mean(sentence_lengths) if sentence_lengths else 0
    
    # Pause analysis
    pauses = []
    for i in range(1, len(rich_transcript)):
        pause = rich_transcript[i]['timestamp'] - rich_transcript[i-1]['timestamp']
        pauses.append(pause)
    
    avg_pause_duration = np.mean(pauses) if pauses else 0
    
    return {
        'total_sentences': total_sentences,
        'total_words': total_words,
        'total_duration_seconds': total_duration,
        'unique_words': unique_words,
        'type_token_ratio': round(ttr, 3),
        'avg_sentence_length': round(avg_sentence_length, 1),
        'avg_speech_rate_wpm': round(avg_speech_rate, 1),
        'avg_pause_duration': round(avg_pause_duration, 1),
        'sentiment_distribution': sentiment_counts,
        'emotion_distribution': emotion_counts,
        'word_frequency': dict(sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:20]),
        'speech_rate_variability': round(np.std(speech_rates), 1) if speech_rates else 0,
        'speakers': speakers,
        'speaker_count': len(speakers)
    }

def process_file(file):
    """Process uploaded file"""
    if file is None:
        return "Please upload a file first."
    
    try:
        # Read file content
        with open(file.name, 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read()
        
        if not content.strip():
            return "File appears to be empty."
            
        return content
    except Exception as e:
        return f"Error reading file: {str(e)}"

def analyze_transcript_content(transcript_content, age, gender, slp_notes):
    """Analyze transcript content with comprehensive quantification and detailed citations"""
    if not transcript_content or len(transcript_content.strip()) < 50:
        return "Error: Please provide a longer transcript for analysis."
    
    # Add SLP notes to the prompt if provided
    notes_section = ""
    if slp_notes and slp_notes.strip():
        notes_section = f"""
    
    SLP CLINICAL NOTES:
    {slp_notes.strip()}
    """
    
    # Enhanced comprehensive analysis prompt with detailed quantification
    prompt = f"""
    You are a speech-language pathologist conducting a COMPREHENSIVE CASL assessment. Provide a SINGLE, DETAILED analysis that quantifies EVERY occurrence and cites specific examples.

    Patient: {age}-year-old {gender}
    
    TRANSCRIPT:
    {transcript_content}{notes_section}
    
    INSTRUCTIONS: Provide ONE comprehensive analysis covering ALL areas below. QUANTIFY EVERYTHING with exact counts and cite SPECIFIC examples from the transcript. Be thorough and detailed. COMPLETE ALL 12 SECTIONS.

    COMPREHENSIVE CASL ANALYSIS:

    1. SPEECH FACTORS (with EXACT counts and specific citations):

    A. Fluency Issues:
    - Count and cite EVERY filler word ("um", "uh", "like", "you know", etc.)
    - Count and cite EVERY false start/self-correction
    - Count and cite EVERY repetition of words/phrases
    - Count and cite EVERY revision/restart
    - Calculate percentage of disfluent speech

    B. Word Retrieval Issues:
    - Count and cite EVERY instance of circumlocution
    - Count and cite EVERY incomplete thought/abandoned utterance
    - Count and cite EVERY word-finding pause
    - Count and cite EVERY use of generic terms ("thing", "stuff", etc.)

    C. Grammatical Errors:
    - Count and cite EVERY grammatical error (verb tense, subject-verb agreement, etc.)
    - Count and cite EVERY syntactic error
    - Count and cite EVERY morphological error
    - Count and cite EVERY run-on sentence

    2. LANGUAGE SKILLS ASSESSMENT (with specific evidence):

    A. Lexical/Semantic Skills:
    - Count total unique words vs. total words (Type-Token Ratio)
    - List and categorize vocabulary by sophistication level
    - Identify semantic relationships demonstrated
    - Assess word retrieval strategies used
    - Evaluate semantic precision

    B. Syntactic Skills:
    - Count sentence types (simple, compound, complex, compound-complex)
    - Calculate average sentence length
    - Identify syntactic patterns and errors
    - Assess clause complexity and embedding

    C. Supralinguistic Skills:
    - Identify and cite examples of:
        * Cause-effect relationships
        * Inferences made
        * Non-literal language use
        * Problem-solving language
        * Metalinguistic awareness

    3. COMPLEX SENTENCE ANALYSIS (with exact counts):

    A. Coordinating Conjunctions:
    - Count and cite EVERY use of: and, but, or, so, yet, for, nor
    - Analyze patterns of use
    - Assess age-appropriateness

    B. Subordinating Conjunctions:
    - Count and cite EVERY use of: because, although, while, since, if, when, where, that, which, who, whom, whose
    - Analyze clause complexity
    - Assess embedding depth

    C. Sentence Structure Analysis:
    - Count each sentence type with examples
    - Calculate complexity ratios
    - Assess developmental appropriateness

    4. FIGURATIVE LANGUAGE ANALYSIS (with exact counts):

    A. Similes:
    - Count and cite EVERY simile (comparisons using "like" or "as")
    - Analyze creativity and appropriateness

    B. Metaphors:
    - Count and cite EVERY metaphor (direct comparisons)
    - Assess comprehension and use

    C. Idioms:
    - Count and cite EVERY idiom used
    - Assess comprehension and appropriate use

    D. Non-literal Language:
    - Count and cite EVERY instance of sarcasm, humor, irony
    - Assess comprehension level

    5. PRAGMATIC LANGUAGE ASSESSMENT (with specific examples):

    A. Turn-taking:
    - Analyze conversational flow
    - Count interruptions or overlaps
    - Assess reciprocity

    B. Topic Management:
    - Count topic shifts
    - Assess topic maintenance
    - Evaluate topic introduction

    C. Social Communication:
    - Assess register appropriateness
    - Evaluate politeness markers
    - Analyze social awareness

    6. VOCABULARY AND SEMANTIC ANALYSIS (with quantification):

    A. Vocabulary Diversity:
    - Calculate Type-Token Ratio
    - List most frequent words
    - Assess vocabulary sophistication

    B. Semantic Relationships:
    - Count and cite examples of:
        * Synonyms/antonyms
        * Categories/hierarchies
        * Part-whole relationships
        * Cause-effect vocabulary

    7. MORPHOLOGICAL AND PHONOLOGICAL ANALYSIS (with counts):

    A. Morphological Markers:
    - Count and cite use of:
        * Plurals (-s, -es)
        * Possessives
        * Verb tenses
        * Derivational morphemes

    B. Phonological Patterns:
    - Identify speech sound errors
    - Count phonological processes
    - Assess syllable structure

    8. COGNITIVE-LINGUISTIC FACTORS (with evidence):

    A. Working Memory:
    - Assess sentence length complexity
    - Analyze information retention
    - Evaluate processing demands

    B. Processing Speed:
    - Analyze speech rate
    - Assess response time
    - Evaluate efficiency

    C. Executive Function:
    - Assess planning and organization
    - Evaluate self-monitoring
    - Analyze cognitive flexibility

    9. FLUENCY AND RHYTHM ANALYSIS (with quantification):

    A. Speech Rate:
    - Calculate words per minute
    - Analyze rate variability
    - Assess naturalness

    B. Pause Patterns:
    - Count and analyze pauses
    - Assess pause function
    - Evaluate rhythm

    10. QUANTITATIVE METRICS:

    - Total words: [count]
    - Total sentences: [count]
    - Average sentence length: [calculation]
    - Type-Token Ratio: [calculation]
    - Disfluency rate: [percentage]
    - Error rate: [percentage]
    - Vocabulary diversity score: [calculation]

    11. CLINICAL IMPLICATIONS:

    A. Strengths:
    - List specific strengths with evidence
    - Identify areas of competence

    B. Areas of Need:
    - Prioritize intervention targets
    - Provide specific examples

    C. Treatment Recommendations:
    - List 5-7 specific intervention strategies
    - Include intensity and frequency recommendations
    - Address all identified areas of need

    12. PROGNOSIS AND SUMMARY:

    - Overall communication profile
    - Developmental appropriateness
    - Impact on academic/social functioning
    - Expected progress with intervention

    FORMAT REQUIREMENTS:
    - Use bullet points for organization
    - Include exact counts for everything
    - Cite specific quotes from transcript
    - Use clear headings and subheadings
    - Provide percentages and ratios where applicable
    - Be comprehensive but organized
    - Focus on clinical relevance
    - COMPLETE ALL 12 SECTIONS

    SECTION CHECKLIST - COMPLETE ALL:
    β–‘ 1. SPEECH FACTORS (A, B, C)
    β–‘ 2. LANGUAGE SKILLS ASSESSMENT (A, B, C)
    β–‘ 3. COMPLEX SENTENCE ANALYSIS (A, B, C)
    β–‘ 4. FIGURATIVE LANGUAGE ANALYSIS (A, B, C, D)
    β–‘ 5. PRAGMATIC LANGUAGE ASSESSMENT (A, B, C)
    β–‘ 6. VOCABULARY AND SEMANTIC ANALYSIS (A, B)
    β–‘ 7. MORPHOLOGICAL AND PHONOLOGICAL ANALYSIS (A, B)
    β–‘ 8. COGNITIVE-LINGUISTIC FACTORS (A, B, C)
    β–‘ 9. FLUENCY AND RHYTHM ANALYSIS (A, B)
    β–‘ 10. QUANTITATIVE METRICS
    β–‘ 11. CLINICAL IMPLICATIONS (A, B, C)
    β–‘ 12. PROGNOSIS AND SUMMARY

    CRITICAL: If you cannot complete all 12 sections in one response, end with <CONTINUE> and continue with the remaining sections. Do not skip any sections. Use the checklist to ensure all sections are completed.
    """
    
    # Get analysis from Claude API
    result = call_claude_api_with_continuation(prompt, max_continuations=5)
    return result

def analyze_transcript(file, age, gender, slp_notes):
    """Analyze transcript from file upload"""
    if file is None:
        return "Please upload a transcript file first."
    
    # Get transcript content
    transcript = process_file(file)
    if transcript.startswith("Error") or transcript.startswith("Please"):
        return transcript
    
    return analyze_transcript_content(transcript, age, gender, slp_notes)

def targeted_analysis(transcript, custom_question, age, gender, slp_notes):
    """Perform targeted analysis based on custom questions with comprehensive detail"""
    if not transcript or not transcript.strip():
        return "Please provide a transcript first."
    
    if not custom_question or not custom_question.strip():
        return "Please enter a specific question for analysis."
    
    # Add SLP notes to the prompt if provided
    notes_section = ""
    if slp_notes and slp_notes.strip():
        notes_section = f"""
    
    SLP CLINICAL NOTES:
    {slp_notes.strip()}
    """
    
    # Enhanced targeted analysis prompt with comprehensive detail
    prompt = f"""
    You are a speech-language pathologist conducting a DETAILED targeted analysis of a speech transcript.
    
    Patient: {age}-year-old {gender}
    
    TRANSCRIPT:
    {transcript}{notes_section}
    
    SPECIFIC QUESTION FOR ANALYSIS:
    {custom_question.strip()}
    
    INSTRUCTIONS: Provide a COMPREHENSIVE, DETAILED analysis that directly addresses this specific question. Include:
    - EXACT counts and quantification
    - SPECIFIC citations from the transcript
    - DETAILED examples for every observation
    - PERCENTAGES and ratios where applicable
    - CLINICAL significance of findings
    - AGE-APPROPRIATE assessment
    
    ANALYSIS REQUIREMENTS:
    
    1. QUANTIFICATION:
    - Count every relevant occurrence
    - Calculate percentages and ratios
    - Provide specific numbers for all observations
    
    2. EVIDENCE:
    - Cite exact quotes from the transcript
    - Provide line-by-line examples
    - Include specific timestamps or context
    
    3. DETAILED EXAMPLES:
    - Give multiple examples for each pattern
    - Show variations in the pattern
    - Demonstrate the range of severity
    
    4. CLINICAL ASSESSMENT:
    - Assess severity level
    - Compare to age expectations
    - Identify clinical significance
    - Suggest intervention implications
    
    5. COMPREHENSIVE COVERAGE:
    - Address all aspects of the question
    - Consider related language areas
    - Include both strengths and weaknesses
    - Provide developmental context
    
    ANALYSIS STRUCTURE:
    
    A. DIRECT ANSWER TO QUESTION:
    - Provide a clear, direct answer
    - Include quantification and severity assessment
    
    B. DETAILED EVIDENCE:
    - List every relevant example with exact quotes
    - Provide counts and percentages
    - Show patterns and variations
    
    C. PATTERN ANALYSIS:
    - Identify underlying patterns
    - Analyze frequency and consistency
    - Assess variability across the transcript
    
    D. DEVELOPMENTAL ASSESSMENT:
    - Compare to age-appropriate expectations
    - Identify developmental level
    - Assess progress and challenges
    
    E. CLINICAL IMPLICATIONS:
    - Impact on communication
    - Effect on academic/social functioning
    - Priority for intervention
    
    F. INTERVENTION CONSIDERATIONS:
    - Specific strategies to address the issue
    - Intensity and frequency recommendations
    - Expected outcomes and timeline
    
    FORMAT REQUIREMENTS:
    - Use clear headings and subheadings
    - Include bullet points for organization
    - Provide exact counts and percentages
    - Cite specific quotes with context
    - Be thorough and comprehensive
    - Focus on clinical relevance and utility
    
    Remember: This should be a DETAILED, COMPREHENSIVE analysis that thoroughly addresses the specific question with quantification, evidence, and clinical implications.
    """
    
    # Get targeted analysis from Claude API
    result = call_claude_api_with_continuation(prompt, max_continuations=3)
    return result

# Create enhanced interface with tabs
with gr.Blocks(title="Enhanced CASL Analysis", theme=gr.themes.Soft()) as app:
    
    gr.Markdown("# πŸ—£οΈ Enhanced CASL Analysis Tool")
    gr.Markdown("Upload a speech transcript, paste text, or transcribe audio/video and get instant CASL assessment results with targeted analysis options.")
    
    # Store transcript globally
    transcript_state = gr.State("")
    
    with gr.Tabs():
        # Tab 1: Basic Analysis
        with gr.Tab("πŸ“Š Basic Analysis"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Input Options")
                    
                    with gr.Tabs():
                        with gr.Tab("πŸ“ File Upload"):
                            file_upload = gr.File(
                                label="Upload Transcript File",
                                file_types=[".txt", ".cha"]
                            )
                            
                            analyze_file_btn = gr.Button(
                                "πŸ” Analyze File", 
                                variant="primary"
                            )
                        
                        with gr.Tab("πŸ“ Text Input"):
                            text_input = gr.Textbox(
                                label="Paste Transcript Here",
                                placeholder="Paste your transcript text here...",
                                lines=10
                            )
                            
                            analyze_text_btn = gr.Button(
                                "πŸ” Analyze Text", 
                                variant="primary"
                            )
                        
                        with gr.Tab("🎀 Audio/Video Transcription"):
                            audio_input = gr.File(
                                label="Upload Audio/Video File",
                                file_types=["audio", "video"]
                            )
                            
                            transcribe_btn = gr.Button(
                                "🎀 Transcribe & Analyze", 
                                variant="primary"
                            )
                            
                            transcription_status = gr.Markdown("")
                    
                    gr.Markdown("### Patient Information")
                    
                    age = gr.Number(
                        label="Patient Age", 
                        value=8, 
                        minimum=1, 
                        maximum=120
                    )
                    
                    gender = gr.Radio(
                        ["male", "female", "other"], 
                        label="Gender", 
                        value="male"
                    )
                    
                    slp_notes = gr.Textbox(
                        label="SLP Clinical Notes (Optional)",
                        placeholder="Enter any additional clinical observations, context, or notes...",
                        lines=3
                    )
                
                with gr.Column():
                    gr.Markdown("### Analysis Results")
                    
                    output = gr.Textbox(
                        label="CASL Analysis Report",
                        placeholder="Analysis results will appear here...",
                        lines=25,
                        max_lines=30
                    )
                    
                    analysis_progress = gr.Markdown("")
        
        # Tab 2: Targeted Analysis
        with gr.Tab("🎯 Targeted Analysis"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Transcript Input")
                    
                    transcript_input = gr.Textbox(
                        label="Paste Transcript Here",
                        placeholder="Paste your transcript text here, or use the transcript from Basic Analysis...",
                        lines=10
                    )
                    
                    gr.Markdown("### Custom Analysis Question")
                    
                    # Predefined question templates
                    question_templates = gr.Dropdown(
                        choices=[
                            "Select a template or write your own...",
                            "What specific speech patterns indicate word-finding difficulties?",
                            "How does the patient's grammar compare to age expectations?",
                            "What evidence suggests fluency issues in this transcript?",
                            "What pragmatic language skills are demonstrated?",
                            "How does the patient handle complex sentence structures?",
                            "What narrative organization skills are evident?",
                            "What specific intervention targets would you recommend?",
                            "How does this patient's language compare to typical development?",
                            "What evidence suggests cognitive-linguistic strengths/weaknesses?",
                            "Analyze the use of conjunctions and complex sentences",
                            "Identify and analyze figurative language use"
                        ],
                        label="Question Templates (Optional)",
                        value="Select a template or write your own..."
                    )
                    
                    custom_question = gr.Textbox(
                        label="Your Specific Question",
                        placeholder="Enter your specific analysis question here...",
                        lines=3
                    )
                    
                    targeted_analyze_btn = gr.Button(
                        "🎯 Analyze Specific Question", 
                        variant="primary"
                    )
                
                with gr.Column():
                    gr.Markdown("### Targeted Analysis Results")
                    
                    targeted_output = gr.Textbox(
                        label="Targeted Analysis Report",
                        placeholder="Targeted analysis results will appear here...",
                        lines=25,
                        max_lines=30
                    )
                    
                    targeted_progress = gr.Markdown("")
        
        # Tab 3: Quick Questions
        with gr.Tab("⚑ Quick Questions"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Quick Analysis Questions")
                    
                    quick_transcript = gr.Textbox(
                        label="Transcript",
                        placeholder="Paste transcript here...",
                        lines=8
                    )
                    
                    gr.Markdown("### Select Quick Questions")
                    
                    quick_questions = gr.CheckboxGroup(
                        choices=[
                            "Word-finding difficulties",
                            "Grammar errors",
                            "Fluency issues", 
                            "Pragmatic skills",
                            "Narrative structure",
                            "Vocabulary level",
                            "Sentence complexity",
                            "Speech rate patterns",
                            "Complex sentence analysis",
                            "Figurative language use",
                            "Morphological markers",
                            "Phonological patterns",
                            "Turn-taking skills",
                            "Topic maintenance",
                            "Social communication",
                            "Cognitive-linguistic factors",
                            "Working memory demands",
                            "Executive function skills",
                            "Metalinguistic awareness",
                            "Academic language use"
                        ],
                        label="Select questions to analyze:",
                        value=[]
                    )
                    
                    quick_analyze_btn = gr.Button(
                        "⚑ Quick Analysis", 
                        variant="primary"
                    )
                
                with gr.Column():
                    gr.Markdown("### Quick Analysis Results")
                    
                    quick_output = gr.Textbox(
                        label="Quick Analysis Report",
                        placeholder="Quick analysis results will appear here...",
                        lines=25,
                        max_lines=30
                    )
                    
                    quick_progress = gr.Markdown("")
        
        # Tab 4: Advanced Transcription
        with gr.Tab("🎀 Advanced Transcription"):
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### Audio/Video Upload")
                    gr.Markdown("**Supported formats:** MP4, AVI, MOV, MKV, WMV, FLV, WAV, MP3, M4A, FLAC, OGG")
                    
                    transcription_file_input = gr.File(
                        label="Upload Audio or Video File",
                        file_types=["audio", "video"]
                    )
                    
                    enable_diarization = gr.Checkbox(
                        label="Enable Speaker Diarization",
                        value=True,
                        info="Identify different speakers in the audio"
                    )
                    
                    transcribe_advanced_btn = gr.Button(
                        "🎀 Transcribe with Metadata", 
                        variant="primary",
                        size="lg"
                    )
                    
                    transcription_status = gr.Markdown("")
                
                with gr.Column(scale=2):
                    gr.Markdown("### Rich Transcript with Metadata")
                    
                    rich_transcript_display = gr.Textbox(
                        label="Transcription with Speakers, Timestamps, Sentiment & Emotion",
                        lines=15,
                        max_lines=20
                    )
            
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Speech Metrics")
                    
                    transcription_metrics_display = gr.Textbox(
                        label="SLP Metrics",
                        lines=10,
                        max_lines=15
                    )
                
                with gr.Column():
                    gr.Markdown("### Word Frequency")
                    
                    transcription_word_freq_display = gr.Dataframe(
                        headers=["Word", "Frequency"],
                        label="Most Frequent Words",
                        interactive=False
                    )
    
    # Event handlers
    def on_analyze_file(file, age_val, gender_val, notes):
        """Handle file analysis"""
        result = analyze_transcript(file, age_val, gender_val, notes)
        transcript = process_file(file) if file else ""
        progress_msg = "βœ… Analysis completed" if "[Analysis completed in" in result else "πŸ”„ Analysis in progress..."
        return result, transcript, progress_msg
    
    def on_analyze_text(text, age_val, gender_val, notes):
        """Handle text analysis"""
        result = analyze_transcript_content(text, age_val, gender_val, notes)
        progress_msg = "βœ… Analysis completed" if "[Analysis completed in" in result else "πŸ”„ Analysis in progress..."
        return result, text, progress_msg
    
    def on_transcribe_and_analyze(audio_file, age_val, gender_val, notes):
        """Handle transcription and analysis"""
        if not audio_file:
            return "Please upload an audio/video file first.", "", "No file provided"
        
        transcript, status = transcribe_audio(audio_file.name)
        if transcript:
            result = analyze_transcript_content(transcript, age_val, gender_val, notes)
            progress_msg = "βœ… Analysis completed" if "[Analysis completed in" in result else "πŸ”„ Analysis in progress..."
            return result, transcript, status
        else:
            return f"Transcription failed: {status}", "", status
    
    def on_transcribe_advanced(audio_file, enable_diarization):
        """Handle advanced transcription"""
        if not audio_file:
            return "Please upload an audio/video file first.", "", "No file provided"
        
        transcript, status = transcribe_audio_with_metadata(audio_file.name, enable_diarization)
        if transcript:
            metrics = calculate_slp_metrics(transcript)
            word_freq_data = metrics.get('word_frequency', {})
            return transcript, status, metrics, word_freq_data
        else:
            return f"Transcription failed: {status}", "", {}, {}
    
    def on_targeted_analyze(transcript, question, age_val, gender_val, notes):
        """Handle targeted analysis"""
        result = targeted_analysis(transcript, question, age_val, gender_val, notes)
        progress_msg = "βœ… Targeted analysis completed" if "[Analysis completed in" in result else "πŸ”„ Targeted analysis in progress..."
        return result, progress_msg
    
    def on_question_template_change(template):
        """Handle question template selection"""
        if template and template != "Select a template or write your own...":
            return template
        return ""
    
    def on_quick_analyze(transcript, questions, age_val, gender_val, notes):
        """Handle quick analysis with multiple questions"""
        if not transcript or not transcript.strip():
            return "Please provide a transcript first.", "❌ No transcript provided"
        
        if not questions:
            return "Please select at least one question to analyze.", "❌ No questions selected"
        
        # Add SLP notes to the prompt if provided
        notes_section = ""
        if notes and notes.strip():
            notes_section = f"""
        
        SLP CLINICAL NOTES:
        {notes.strip()}
        """
        
        # Create enhanced quick analysis prompt with comprehensive SLP analysis
        questions_text = "\n".join([f"- {q}" for q in questions])
        prompt = f"""
        You are a speech-language pathologist conducting a COMPREHENSIVE quick analysis of a speech transcript.
        
        Patient: {age_val}-year-old {gender_val}
        
        TRANSCRIPT:
        {transcript}{notes_section}
        
        Please provide a DETAILED analysis addressing these specific areas:
        {questions_text}
        
        ANALYSIS REQUIREMENTS:
        
        For each selected area, provide:
        1. EXACT COUNTS and quantification
        2. SPECIFIC EXAMPLES with exact quotes from transcript
        3. PERCENTAGES and ratios where applicable
        4. SEVERITY assessment
        5. AGE-APPROPRIATE evaluation
        6. CLINICAL significance
        7. INTERVENTION considerations
        
        DETAILED ANALYSIS GUIDELINES:
        
        For SYNTAX and COMPLEX SENTENCE analysis:
        - Count and cite EVERY coordinating conjunction (and, but, or, so, yet, for, nor)
        - Count and cite EVERY subordinating conjunction (because, although, while, since, if, when, where, that, which, who, whom, whose)
        - Identify and count each sentence type (simple, compound, complex, compound-complex)
        - Calculate complexity ratios and percentages
        - Assess embedding depth and clause complexity
        - Provide specific examples for each pattern
        
        For FIGURATIVE LANGUAGE analysis:
        - Count and cite EVERY simile (comparisons using "like" or "as")
        - Count and cite EVERY metaphor (direct comparisons without "like" or "as")
        - Count and cite EVERY idiom and non-literal expression
        - Assess creativity and age-appropriate use
        - Provide specific examples with context
        
        For PRAGMATIC and SOCIAL COMMUNICATION:
        - Count and analyze turn-taking patterns
        - Assess topic maintenance and shifting abilities
        - Evaluate social appropriateness and register use
        - Count interruptions or conversational breakdowns
        - Analyze non-literal language comprehension
        - Provide specific examples of pragmatic behaviors
        
        For VOCABULARY and SEMANTIC analysis:
        - Calculate Type-Token Ratio
        - Count and categorize vocabulary by sophistication level
        - Analyze word retrieval strategies and circumlocution
        - Assess semantic precision and relationships
        - Count academic vs. everyday vocabulary use
        - Provide specific examples of vocabulary patterns
        
        For MORPHOLOGICAL and PHONOLOGICAL analysis:
        - Count and cite EVERY morphological marker (plurals, possessives, verb tenses)
        - Count and cite EVERY derivational morpheme (prefixes, suffixes)
        - Identify and count phonological patterns and errors
        - Assess syllable structure and stress patterns
        - Provide specific examples of morphological use
        
        For COGNITIVE-LINGUISTIC factors:
        - Assess working memory demands in language production
        - Analyze processing speed and efficiency
        - Count and evaluate attention and focus patterns
        - Assess executive function skills and self-monitoring
        - Provide specific examples of cognitive-linguistic patterns
        
        For FLUENCY and SPEECH RATE:
        - Count and cite EVERY disfluency (fillers, repetitions, revisions)
        - Calculate speech rate and variability
        - Analyze pause patterns and their function
        - Assess overall speech naturalness
        - Provide specific examples of fluency patterns
        
        For GRAMMAR and LANGUAGE ERRORS:
        - Count and cite EVERY grammatical error
        - Count and cite EVERY syntactic error
        - Count and cite EVERY morphological error
        - Calculate error rates and percentages
        - Provide specific examples of error patterns
        
        For WORD-FINDING and RETRIEVAL:
        - Count and cite EVERY instance of circumlocution
        - Count and cite EVERY incomplete thought
        - Count and cite EVERY word-finding pause
        - Analyze word retrieval strategies used
        - Provide specific examples of retrieval patterns
        
        For NARRATIVE and DISCOURSE:
        - Assess narrative organization and coherence
        - Count topic shifts and maintenance
        - Analyze discourse markers and transitions
        - Evaluate story structure and completeness
        - Provide specific examples of narrative patterns
        
        FORMAT REQUIREMENTS:
        - Use clear headings for each area analyzed
        - Include bullet points for organization
        - Provide exact counts and percentages
        - Cite specific quotes from transcript
        - Include severity assessments
        - Provide clinical implications
        - Be comprehensive but focused on selected areas
        
        Remember: This should be a DETAILED analysis that thoroughly addresses each selected area with quantification, evidence, and clinical relevance.
        """
        
        result = call_claude_api_with_continuation(prompt, max_continuations=2)
        progress_msg = "βœ… Quick analysis completed" if "[Analysis completed in" in result else "πŸ”„ Quick analysis in progress..."
        return result, progress_msg
    
    # Connect event handlers
    analyze_file_btn.click(
        on_analyze_file,
        inputs=[file_upload, age, gender, slp_notes],
        outputs=[output, transcript_input, analysis_progress]
    )
    
    analyze_text_btn.click(
        on_analyze_text,
        inputs=[text_input, age, gender, slp_notes],
        outputs=[output, transcript_input, analysis_progress]
    )
    
    transcribe_btn.click(
        on_transcribe_and_analyze,
        inputs=[audio_input, age, gender, slp_notes],
        outputs=[output, transcript_input, transcription_status]
    )
    
    transcribe_advanced_btn.click(
        on_transcribe_advanced,
        inputs=[transcription_file_input, enable_diarization],
        outputs=[rich_transcript_display, transcription_status, transcription_metrics_display, transcription_word_freq_display]
    )
    
    targeted_analyze_btn.click(
        on_targeted_analyze,
        inputs=[transcript_input, custom_question, age, gender, slp_notes],
        outputs=[targeted_output, targeted_progress]
    )
    
    question_templates.change(
        on_question_template_change,
        inputs=[question_templates],
        outputs=[custom_question]
    )
    
    quick_analyze_btn.click(
        on_quick_analyze,
        inputs=[quick_transcript, quick_questions, age, gender, slp_notes],
        outputs=[quick_output, quick_progress]
    )

if __name__ == "__main__":
    print("πŸš€ Starting Enhanced CASL Analysis Tool...")
    print("πŸ“Š Features: Basic Analysis, Targeted Questions, Quick Multi-Analysis, Advanced Transcription")
    print("🎀 Transcription: Audio/Video support with speaker diarization, sentiment, and emotion analysis")
    print("πŸ“ˆ Analysis: Complex sentences, figurative language, pragmatic skills, cognitive-linguistic factors")
    
    if not ANTHROPIC_API_KEY:
        print("⚠️  ANTHROPIC_API_KEY not configured - analysis will show error message")
        print("   For HuggingFace Spaces: Add ANTHROPIC_API_KEY as a secret in your space settings")
        print("   For local use: export ANTHROPIC_API_KEY='your-key-here'")
    else:
        print("βœ… Claude API configured")
    
    if not SPEECHBRAIN_AVAILABLE:
        print("⚠️  SpeechBrain not available - transcription will be disabled")
        print("   Install with: pip install speechbrain transformers torch")
    else:
        print("βœ… SpeechBrain available for transcription")
    
    if not MOVIEPY_AVAILABLE:
        print("⚠️  MoviePy not available - video processing will be limited")
        print("   Install with: pip install moviepy")
    else:
        print("βœ… MoviePy available for video processing")
    
    if not DIARIZATION_AVAILABLE:
        print("⚠️  Pyannote.audio not available - speaker diarization will be disabled")
        print("   Install with: pip install pyannote.audio")
        print("   Note: Requires HuggingFace token for model access")
    else:
        print("βœ… Pyannote.audio available for speaker diarization")
    
    if not SENTIMENT_AVAILABLE:
        print("⚠️  Transformers not available - sentiment/emotion analysis will be disabled")
        print("   Install with: pip install transformers torch")
    else:
        print("βœ… Transformers available for sentiment and emotion analysis")
    
    app.launch(show_api=False)