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 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0eefc3
29b30f3
 
a9de5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0f2f4f
 
 
 
 
 
a9de5f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0f2f4f
a9de5f0
 
 
 
 
29b30f3
 
 
 
 
 
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
import gradio as gr
import json
import os
import logging
import requests

# 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", "")

# 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 call_claude_api(prompt):
    """Call Claude API directly"""
    if not ANTHROPIC_API_KEY:
        return "❌ Claude API key not configured. Please set ANTHROPIC_API_KEY environment variable."
    
    try:
        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": prompt
                }
            ]
        }
        
        response = requests.post(
            "https://api.anthropic.com/v1/messages",
            headers=headers,
            json=data,
            timeout=60
        )
        
        if response.status_code == 200:
            response_json = response.json()
            return response_json['content'][0]['text']
        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)}"

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(file, age, gender, slp_notes):
    """Simple CASL analysis"""
    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
    
    # 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()}
    """
    
    # Simple analysis prompt
    def analyze_transcript(file, age, gender):
    """Simple CASL analysis"""
    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
    
# Provide the instructions for analyzing the transcript
    instructions = f"""
    
    
    
    Each domain from the CASL-2 framework can be analyzed using the sample:
    
    Lexical/Semantic Skills:
    
    This category focuses on vocabulary knowledge, word meanings, and the ability to use words contextually. It measures both receptive and expressive language abilities related to word use.
    
    Key Subtests:
    
    Antonyms: Identifying words with opposite meanings.
    Synonyms: Identifying words with similar meanings.
    Idiomatic Language: Understanding and interpreting idioms and figurative language.
    
    Evaluate vocabulary diversity (type-token ratio).
    Note word-finding difficulties, incorrect word choices, or over-reliance on fillers (e.g., “like,” “stuff”).
    Assess use of specific vs. vague language (e.g., "car" vs. "sedan").
    
    Syntactic Skills:
    
    This category evaluates understanding and use of grammar and sentence structure. It focuses on the ability to comprehend and produce grammatically correct sentences.
    
    Key Subtests:
    
    Sentence Expression: Producing grammatically correct sentences based on prompts.
    Grammaticality Judgment: Identifying whether a sentence is grammatically correct.
    
    Examine sentence structure for grammatical accuracy.
    Identify errors in verb tense, subject-verb agreement, or sentence complexity.
    Note the use of clauses, conjunctions, and varied sentence types.
    
    
    Supralinguistic Skills:
    
    This subcategory assesses higher-level language skills that go beyond literal meanings, such as understanding implied meanings, sarcasm, and complex verbal reasoning.
    
    Key Subtests:
    
    Inferences: Understanding information that is not explicitly stated.
    Meaning from Context: Deriving meaning from surrounding text or dialogue.
    Nonliteral Language: Interpreting figurative language, such as metaphors or irony
    
    Look for use or understanding of figurative language, idioms, or humor.
    Assess ability to handle ambiguous or implied meanings in context.
    Identify advanced language use for abstract or hypothetical ideas.
    
    
    Pragmatic Skills(focus less on this as it is not typically necessary for the age range you will be dealing with):
    This category measures the ability to use language effectively in social contexts. It evaluates understanding of conversational rules, turn-taking, and adapting communication to different social situations.
    
    Key Subtests:
    
    Pragmatic Language Test: Assessing appropriateness of responses in social scenarios.
    
    
    Observe turn-taking, topic maintenance, and conversational appropriateness.
    Note use of politeness, tone, or adapting language to the listener.
    Evaluate narrative coherence in storytelling or recounting events.
    
    
    Quantitative Analysis:
    Count errors (e.g., grammatical, lexical) and divide by total utterances to calculate error rates.
    Measure Mean Length of Utterance (MLU) to gauge syntactic complexity.
    Count the variety of unique words for vocabulary richness.
    
    
    Qualitative Analysis:
    Assess the appropriateness and sophistication of responses for the individual’s age.
    Evaluate overall fluency, coherence, and adaptability in communication.
    1. Lexical/Semantic Analysis
    Objective: Evaluate vocabulary diversity, word meaning, and contextual usage.
    LLM Process:
    
    Word Diversity:
    Calculate the type-token ratio (TTR): Divide the number of unique words by the total number of words to estimate vocabulary richness.
    Identify repetition and overuse of basic words.
    Word Appropriateness:
    Analyze context to determine if words are used accurately (e.g., "big" for "large" vs. incorrect substitutions).
    Idiomatic Language:
    Detect use of idioms, metaphors, or figurative expressions.
    Assess whether these expressions are used and interpreted correctly within the speech.
    Scoring:
    
    Assign scores based on TTR thresholds and the presence of appropriately used advanced vocabulary or idiomatic language.
    2. Syntactic Analysis
    Objective: Assess grammatical accuracy, sentence complexity, and variety.
    LLM Process:
    
    Grammar Checking:
    Identify grammatical errors (e.g., incorrect verb tense, subject-verb disagreement, missing articles).
    Sentence Complexity:
    Calculate the mean length of utterance (MLU): Average the number of morphemes per sentence.
    Count clauses, conjunctions, and use of complex structures (e.g., relative clauses).
    Error Patterns:
    Note recurring syntactic errors or simplified constructions indicative of developmental or functional delays.
    Scoring:
    
    Base scores on error frequency, MLU, and the proportion of complex vs. simple sentences.
    3. Supralinguistic Analysis
    Objective: Evaluate understanding and use of abstract, implied, or nonliteral language.
    LLM Process:
    
    Inference Detection:
    Analyze whether the speaker makes logical inferences or responds appropriately to indirect prompts (e.g., "Why do you think they did that?" requiring contextual reasoning).
    Nonliteral Language:
    Detect use or interpretation of figurative expressions, idioms, or sarcasm.
    Contextual Adaptation:
    Assess whether the speaker adjusts their language based on the situation or implied meaning.
    Scoring:
    
    Assign points for accurate interpretations of implied meanings and use of nonliteral language.
    4. Pragmatic Analysis
    Objective: Assess social communication and appropriateness in interactions.
    
    
    LLM Process:
    
    Turn-Taking and Topic Maintenance:
    Evaluate whether the speaker follows conversational rules, including turn-taking and staying on topic.
    Social Cues:
    Analyze how well the speaker uses language to match the context (e.g., formal vs. informal speech).
    Clarity and Politeness:
    Assess the appropriateness of language for the audience and situation (e.g., clarity, tone, politeness).
    Scoring:
    
    Base scores on observed appropriateness and adherence to conversational norms.
    5. Overall Scoring
    Objective: Aggregate scores for all categories to estimate a composite language ability.
    LLM Process:
    
    Normalize subcategory scores based on predefined thresholds (e.g., raw scores from lexical, syntactic, supralinguistic, and pragmatic categories).
    Combine normalized scores to calculate an estimated General Language Ability Index (GLAI).
    Implementation Workflow
    Input:
    Provide the LLM with a transcribed spontaneous speech sample.
    Include metadata (e.g., speaker’s age, context of the conversation).
    Processing:
    Use predefined scoring rubrics to analyze lexical, syntactic, supralinguistic, and pragmatic features in the transcript.
    Output:
    Generate scores for each subcategory.
    Offer a detailed breakdown explaining how each score was derived.
    Summarize results with an estimated composite score and percentile rank.
    
    
    The CASL-2 provides descriptive scores for each subcategory and subtest to help interpret an individual's performance relative to age-based norms. These descriptive scores are based on the standard scores and are typically categorized into qualitative performance levels. Here’s an overview of how the descriptive scores are generally aligned for each subcategory and subtest:
    
    Descriptive Score Levels
    Each subcategory or subtest score typically falls into one of these descriptive ranges based on the standard score:
    
    Standard Score Range	Descriptive Label
    131 and above	Very Superior
    121–130	Superior
    111–120	Above Average
    90–110	Average
    80–89	Below Average
    70–79	Poor
    Below 70	Very Poor
    Descriptive Scores for Each Subcategory
    1. Lexical/Semantic
    Reflects vocabulary knowledge, word usage, and ability to interpret word relationships.
    Descriptive labels are based on scores from subtests like Antonyms, Synonyms, and Idiomatic Language.
    2. Syntactic
    Evaluates grammar and sentence structure understanding.
    Descriptive scores use results from subtests such as Sentence Expression and Grammaticality Judgment.
    3. Supralinguistic
    Represents higher-order language comprehension, such as nonliteral and abstract meanings.
    Descriptive labels are derived from subtests like Inferences, Meaning from Context, and Nonliteral Language.
    4. Pragmatic
    Measures social language use, including conversational appropriateness and adaptability.
    Descriptive scores come from the Pragmatic Language Test.
    5. General Language Ability Index (GLAI)
    Composite score representing overall spoken language competence.
    Combines scaled scores from various subtests into one standard score, interpreted using the same descriptive range.
    Percentile Ranks
    In addition to descriptive scores, CASL-2 provides percentile ranks to indicate the proportion of individuals in the normative sample who scored lower than the test-taker. For example:
    
    A standard score of 100 corresponds to the 50th percentile (average performance).
    A score of 85 corresponds to the 16th percentile (below average).
    
    """

# Format the answer
    answer_format = f"""
Template for LLM Output: CASL-2 Analysis from Spontaneous Speech Sample
    1. Introduction
    Provide a brief overview of the analysis, including context and objectives.
    Example:
    "This analysis evaluates the spoken language abilities of the individual based on a transcribed spontaneous speech sample. The results are categorized into the CASL-2 subcategories: Lexical/Semantic, Syntactic, Supralinguistic, Pragmatic, and Overall Language Ability."
    
    2. Lexical/Semantic Analysis
    Describe findings related to vocabulary, word meanings, and contextual usage.
    
    Word Diversity (Type-Token Ratio):
    Example: The speaker used 80 unique words out of 200 total words, resulting in a TTR of 0.4, indicating moderate vocabulary richness.
    
    Word Appropriateness:
    Example: Most words were used correctly within context, though advanced vocabulary or synonyms were limited.
    
    Idiomatic and Figurative Language:
    Example: No idiomatic expressions or figurative language were observed.
    
    Score: X/5
    3. Syntactic Analysis
    Evaluate grammatical accuracy, sentence complexity, and structure.
    
    Grammatical Accuracy:
    Example: The speech contained 3 grammatical errors, such as incorrect verb tense usage ("I goed to the park").
    
    Sentence Complexity (Mean Length of Utterance):
    Example: The mean length of utterance (MLU) was 5.2 words, indicating a preference for simple sentences.
    
    Error Patterns:
    Example: Frequent omissions of articles and auxiliary verbs.
    
    Score: X/5
    4. Supralinguistic Analysis
    Assess abstract and nonliteral language skills.
    
    Inference Making:
    Example: The speaker demonstrated minimal ability to infer meaning, as responses were literal and lacked contextual reasoning.
    
    Nonliteral Language:
    Example: No use or understanding of idioms, metaphors, or sarcasm was observed.
    
    Contextual Adaptation:
    Example: The speaker’s responses were appropriate to the conversation but lacked depth.
    
    Score: X/5
    5. Pragmatic Analysis
    Examine social communication skills and conversational appropriateness.
    
    Turn-Taking:
    Example: The speaker effectively took turns but occasionally interrupted the conversational flow.
    
    Topic Maintenance:
    Example: The speaker maintained the topic but struggled with cohesive transitions.
    
    Social Appropriateness:
    Example: Responses were contextually appropriate but lacked expressive variation.
    
    Score: X/5
    6. Overall Language Ability
    Summarize the composite score and provide insights.
    
    Composite Score (Estimated):
    Example: The composite score, derived from subcategory averages, is 3.0/5, indicating below-average language abilities relative to peers.
    
    Percentile Rank:
    Example: Based on performance, the speaker falls in the 30th percentile for their age group.
    
    Key Strengths:
    Example: The speaker exhibited strong topic maintenance and grammatical accuracy for simple sentences.
    
    Areas for Improvement:
    Example: Vocabulary diversity, sentence complexity, and inference-making skills require development.
    
    Please provide a CASL analysis including:
    
    1. SPEECH FACTORS (with counts and severity):
    - Difficulty producing fluent speech
    - Word retrieval issues  
    - Grammatical errors
    - Repetitions and revisions
    
    2. CASL SKILLS ASSESSMENT:
    - Lexical/Semantic Skills (Standard Score, Percentile, Level)
        - Syntactic Skills (Standard Score, Percentile, Level)
        - Supralinguistic Skills (Standard Score, Percentile, Level)
        
        3. TREATMENT RECOMMENDATIONS:
        - List 3-5 specific intervention strategies
        
        4. CLINICAL SUMMARY:
        - Brief explanation of findings and prognosis
        
        Use exact quotes from the transcript as evidence.
        Provide realistic standard scores (70-130 range, mean=100).
        """
    
    prompt = f"""
    
        
    You are a speech pathologist, a healthcare professional who specializes in evaluating, diagnosing, and treating communication disorders, including speech, language, cognitive-communication, voice, swallowing, and fluency disorders. Your role is to help patients improve their speech and communication skills through various therapeutic techniques and exercises.
    
    In this scenario, you will be working with a patient who has the following speech disorder or issue:
    
    Autism Spectrum Disorder - Demographics  Patient: {age}-year-old {gender}
    
    A speech-language pathologist (SLP) working in schools plays a vital role in supporting students with autism spectrum disorder (ASD). These professionals focus on helping students develop the communication skills they need to succeed academically, socially, and emotionally. Communication challenges are a common characteristic of ASD, and SLPs are uniquely equipped to address them with specialized knowledge and strategies.
    
    SLPs working with students with ASD begin by assessing their communication abilities, including their use of verbal and nonverbal language. This evaluation may involve observing how the student interacts with peers and teachers, understanding their use of gestures, facial expressions, or other forms of communication, and identifying areas for improvement. Based on the assessment, the SLP creates an individualized treatment plan tailored to the student’s specific needs.
    
    Interventions often focus on improving social communication skills, such as taking turns in conversation, understanding and using eye contact, recognizing emotions in others, and maintaining appropriate topics during discussions. For students with limited verbal abilities, the SLP may introduce augmentative and alternative communication (AAC) tools, such as picture exchange systems or speech-generating devices, to help them express themselves effectively.
    
    Here is a sample dialogue between you and the patient:
    
    <dialogue>
        
        TRANSCRIPT:
        {transcript}
    </dialogue>
    
    Based on the information provided, please do the following:
    
    {instructions}
    Do this using {answer_format}
    
    Remember, your goal is to provide a comprehensive and supportive therapy experience for the patient, helping them to improve their speech and communication skills and build their confidence in the process. Use your expertise and empathy to guide them through this journey.
    
        """
        
    # Get analysis from Claude API
    result = call_claude_api(prompt)
    return result

    
    # Get analysis from Claude API
    result = call_claude_api(prompt)
    return result

# Create simple interface
with gr.Blocks(title="Simple CASL Analysis", theme=gr.themes.Soft()) as app:
    
    gr.Markdown("# 🗣️ Simple CASL Analysis Tool")
    gr.Markdown("Upload a speech transcript and get instant CASL assessment results.")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### Upload & Settings")
            
            file_upload = gr.File(
                label="Upload Transcript File",
                file_types=[".txt", ".cha"]
            )
            
            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
            )
            
            analyze_btn = gr.Button(
                "🔍 Analyze Transcript", 
                variant="primary"
            )
        
        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
            )
    
    # Connect the analyze button
    analyze_btn.click(
        analyze_transcript,
        inputs=[file_upload, age, gender, slp_notes],
        outputs=[output]
    )

if __name__ == "__main__":
    print("🚀 Starting Simple CASL Analysis Tool...")
    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")
    
    app.launch(show_api=False)