Spaces:
Sleeping
Sleeping
Upload simple_casl_app.py
Browse files- simple_casl_app.py +282 -344
simple_casl_app.py
CHANGED
@@ -93,387 +93,325 @@ def analyze_transcript(file, age, gender, slp_notes):
|
|
93 |
SLP CLINICAL NOTES:
|
94 |
{slp_notes.strip()}
|
95 |
"""
|
96 |
-
# Provide the instructions for analyzing the transcript
|
97 |
-
instructions = f"""
|
98 |
|
|
|
|
|
|
|
99 |
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
Lexical/Semantic Skills:
|
104 |
-
|
105 |
-
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.
|
106 |
-
|
107 |
-
Key Subtests:
|
108 |
-
|
109 |
-
Antonyms: Identifying words with opposite meanings.
|
110 |
-
Synonyms: Identifying words with similar meanings.
|
111 |
-
Idiomatic Language: Understanding and interpreting idioms and figurative language.
|
112 |
-
|
113 |
-
Evaluate vocabulary diversity (type-token ratio).
|
114 |
-
Note word-finding difficulties, incorrect word choices, or over-reliance on fillers (e.g., “like,” “stuff”).
|
115 |
-
Assess use of specific vs. vague language (e.g., "car" vs. "sedan").
|
116 |
-
|
117 |
-
Syntactic Skills:
|
118 |
-
|
119 |
-
This category evaluates understanding and use of grammar and sentence structure. It focuses on the ability to comprehend and produce grammatically correct sentences.
|
120 |
-
|
121 |
-
Key Subtests:
|
122 |
-
|
123 |
-
Sentence Expression: Producing grammatically correct sentences based on prompts.
|
124 |
-
Grammaticality Judgment: Identifying whether a sentence is grammatically correct.
|
125 |
-
|
126 |
-
Examine sentence structure for grammatical accuracy.
|
127 |
-
Identify errors in verb tense, subject-verb agreement, or sentence complexity.
|
128 |
-
Note the use of clauses, conjunctions, and varied sentence types.
|
129 |
-
|
130 |
-
|
131 |
-
Supralinguistic Skills:
|
132 |
-
|
133 |
-
This subcategory assesses higher-level language skills that go beyond literal meanings, such as understanding implied meanings, sarcasm, and complex verbal reasoning.
|
134 |
-
|
135 |
-
Key Subtests:
|
136 |
-
|
137 |
-
Inferences: Understanding information that is not explicitly stated.
|
138 |
-
Meaning from Context: Deriving meaning from surrounding text or dialogue.
|
139 |
-
Nonliteral Language: Interpreting figurative language, such as metaphors or irony
|
140 |
-
|
141 |
-
Look for use or understanding of figurative language, idioms, or humor.
|
142 |
-
Assess ability to handle ambiguous or implied meanings in context.
|
143 |
-
Identify advanced language use for abstract or hypothetical ideas.
|
144 |
-
|
145 |
-
|
146 |
-
Pragmatic Skills(focus less on this as it is not typically necessary for the age range you will be dealing with):
|
147 |
-
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.
|
148 |
-
|
149 |
-
Key Subtests:
|
150 |
-
|
151 |
-
Pragmatic Language Test: Assessing appropriateness of responses in social scenarios.
|
152 |
-
|
153 |
-
|
154 |
-
Observe turn-taking, topic maintenance, and conversational appropriateness.
|
155 |
-
Note use of politeness, tone, or adapting language to the listener.
|
156 |
-
Evaluate narrative coherence in storytelling or recounting events.
|
157 |
-
|
158 |
-
|
159 |
-
Quantitative Analysis:
|
160 |
-
Count errors (e.g., grammatical, lexical) and divide by total utterances to calculate error rates.
|
161 |
-
Measure Mean Length of Utterance (MLU) to gauge syntactic complexity.
|
162 |
-
Count the variety of unique words for vocabulary richness.
|
163 |
-
|
164 |
-
|
165 |
-
Qualitative Analysis:
|
166 |
-
Assess the appropriateness and sophistication of responses for the individual’s age.
|
167 |
-
Evaluate overall fluency, coherence, and adaptability in communication.
|
168 |
-
1. Lexical/Semantic Analysis
|
169 |
-
Objective: Evaluate vocabulary diversity, word meaning, and contextual usage.
|
170 |
-
LLM Process:
|
171 |
|
172 |
-
|
173 |
-
Calculate the type-token ratio (TTR): Divide the number of unique words by the total number of words to estimate vocabulary richness.
|
174 |
-
Identify repetition and overuse of basic words.
|
175 |
-
Word Appropriateness:
|
176 |
-
Analyze context to determine if words are used accurately (e.g., "big" for "large" vs. incorrect substitutions).
|
177 |
-
Idiomatic Language:
|
178 |
-
Detect use of idioms, metaphors, or figurative expressions.
|
179 |
-
Assess whether these expressions are used and interpreted correctly within the speech.
|
180 |
-
Scoring:
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
186 |
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
Count clauses, conjunctions, and use of complex structures (e.g., relative clauses).
|
192 |
-
Error Patterns:
|
193 |
-
Note recurring syntactic errors or simplified constructions indicative of developmental or functional delays.
|
194 |
-
Scoring:
|
195 |
|
196 |
-
|
197 |
-
3
|
198 |
-
Objective: Evaluate understanding and use of abstract, implied, or nonliteral language.
|
199 |
-
LLM Process:
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
Nonliteral Language:
|
204 |
-
Detect use or interpretation of figurative expressions, idioms, or sarcasm.
|
205 |
-
Contextual Adaptation:
|
206 |
-
Assess whether the speaker adjusts their language based on the situation or implied meaning.
|
207 |
-
Scoring:
|
208 |
-
|
209 |
-
Assign points for accurate interpretations of implied meanings and use of nonliteral language.
|
210 |
-
4. Pragmatic Analysis
|
211 |
-
Objective: Assess social communication and appropriateness in interactions.
|
212 |
-
|
213 |
-
|
214 |
-
LLM Process:
|
215 |
-
|
216 |
-
Turn-Taking and Topic Maintenance:
|
217 |
-
Evaluate whether the speaker follows conversational rules, including turn-taking and staying on topic.
|
218 |
-
Social Cues:
|
219 |
-
Analyze how well the speaker uses language to match the context (e.g., formal vs. informal speech).
|
220 |
-
Clarity and Politeness:
|
221 |
-
Assess the appropriateness of language for the audience and situation (e.g., clarity, tone, politeness).
|
222 |
-
Scoring:
|
223 |
-
|
224 |
-
Base scores on observed appropriateness and adherence to conversational norms.
|
225 |
-
5. Overall Scoring
|
226 |
-
Objective: Aggregate scores for all categories to estimate a composite language ability.
|
227 |
-
LLM Process:
|
228 |
-
|
229 |
-
Normalize subcategory scores based on predefined thresholds (e.g., raw scores from lexical, syntactic, supralinguistic, and pragmatic categories).
|
230 |
-
Combine normalized scores to calculate an estimated General Language Ability Index (GLAI).
|
231 |
-
Implementation Workflow
|
232 |
-
Input:
|
233 |
-
Provide the LLM with a transcribed spontaneous speech sample.
|
234 |
-
Include metadata (e.g., speaker’s age, context of the conversation).
|
235 |
-
Processing:
|
236 |
-
Use predefined scoring rubrics to analyze lexical, syntactic, supralinguistic, and pragmatic features in the transcript.
|
237 |
-
Output:
|
238 |
-
Generate scores for each subcategory.
|
239 |
-
Offer a detailed breakdown explaining how each score was derived.
|
240 |
-
Summarize results with an estimated composite score and percentile rank.
|
241 |
-
|
242 |
-
|
243 |
-
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:
|
244 |
-
|
245 |
-
Descriptive Score Levels
|
246 |
-
Each subcategory or subtest score typically falls into one of these descriptive ranges based on the standard score:
|
247 |
-
|
248 |
-
Standard Score Range Descriptive Label
|
249 |
-
131 and above Very Superior
|
250 |
-
121–130 Superior
|
251 |
-
111–120 Above Average
|
252 |
-
90–110 Average
|
253 |
-
80–89 Below Average
|
254 |
-
70–79 Poor
|
255 |
-
Below 70 Very Poor
|
256 |
-
Descriptive Scores for Each Subcategory
|
257 |
-
1. Lexical/Semantic
|
258 |
-
Reflects vocabulary knowledge, word usage, and ability to interpret word relationships.
|
259 |
-
Descriptive labels are based on scores from subtests like Antonyms, Synonyms, and Idiomatic Language.
|
260 |
-
2. Syntactic
|
261 |
-
Evaluates grammar and sentence structure understanding.
|
262 |
-
Descriptive scores use results from subtests such as Sentence Expression and Grammaticality Judgment.
|
263 |
-
3. Supralinguistic
|
264 |
-
Represents higher-order language comprehension, such as nonliteral and abstract meanings.
|
265 |
-
Descriptive labels are derived from subtests like Inferences, Meaning from Context, and Nonliteral Language.
|
266 |
-
4. Pragmatic
|
267 |
-
Measures social language use, including conversational appropriateness and adaptability.
|
268 |
-
Descriptive scores come from the Pragmatic Language Test.
|
269 |
-
5. General Language Ability Index (GLAI)
|
270 |
-
Composite score representing overall spoken language competence.
|
271 |
-
Combines scaled scores from various subtests into one standard score, interpreted using the same descriptive range.
|
272 |
-
Percentile Ranks
|
273 |
-
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:
|
274 |
-
|
275 |
-
A standard score of 100 corresponds to the 50th percentile (average performance).
|
276 |
-
A score of 85 corresponds to the 16th percentile (below average).
|
277 |
|
|
|
|
|
|
|
278 |
"""
|
279 |
-
|
280 |
-
# Format the answer
|
281 |
-
answer_format = f"""
|
282 |
-
Template for LLM Output: CASL-2 Analysis from Spontaneous Speech Sample
|
283 |
-
1. Introduction
|
284 |
-
Provide a brief overview of the analysis, including context and objectives.
|
285 |
-
Example:
|
286 |
-
"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."
|
287 |
-
|
288 |
-
2. Lexical/Semantic Analysis
|
289 |
-
Describe findings related to vocabulary, word meanings, and contextual usage.
|
290 |
-
|
291 |
-
Word Diversity (Type-Token Ratio):
|
292 |
-
Example: The speaker used 80 unique words out of 200 total words, resulting in a TTR of 0.4, indicating moderate vocabulary richness.
|
293 |
-
|
294 |
-
Word Appropriateness:
|
295 |
-
Example: Most words were used correctly within context, though advanced vocabulary or synonyms were limited.
|
296 |
-
|
297 |
-
Idiomatic and Figurative Language:
|
298 |
-
Example: No idiomatic expressions or figurative language were observed.
|
299 |
-
|
300 |
-
Score: X/5
|
301 |
-
3. Syntactic Analysis
|
302 |
-
Evaluate grammatical accuracy, sentence complexity, and structure.
|
303 |
-
|
304 |
-
Grammatical Accuracy:
|
305 |
-
Example: The speech contained 3 grammatical errors, such as incorrect verb tense usage ("I goed to the park").
|
306 |
-
|
307 |
-
Sentence Complexity (Mean Length of Utterance):
|
308 |
-
Example: The mean length of utterance (MLU) was 5.2 words, indicating a preference for simple sentences.
|
309 |
-
|
310 |
-
Error Patterns:
|
311 |
-
Example: Frequent omissions of articles and auxiliary verbs.
|
312 |
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
Example: No use or understanding of idioms, metaphors, or sarcasm was observed.
|
322 |
-
|
323 |
-
Contextual Adaptation:
|
324 |
-
Example: The speaker’s responses were appropriate to the conversation but lacked depth.
|
325 |
-
|
326 |
-
Score: X/5
|
327 |
-
5. Pragmatic Analysis
|
328 |
-
Examine social communication skills and conversational appropriateness.
|
329 |
|
330 |
-
|
331 |
-
|
332 |
|
333 |
-
|
334 |
-
|
|
|
|
|
335 |
|
336 |
-
|
337 |
-
|
|
|
338 |
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
|
343 |
-
|
344 |
-
Example: The composite score, derived from subcategory averages, is 3.0/5, indicating below-average language abilities relative to peers.
|
345 |
|
346 |
-
|
347 |
-
|
348 |
|
349 |
-
|
350 |
-
|
351 |
|
352 |
-
|
353 |
-
Example: Vocabulary diversity, sentence complexity, and inference-making skills require development.
|
354 |
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
- Word retrieval issues
|
360 |
-
- Grammatical errors
|
361 |
-
- Repetitions and revisions
|
362 |
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
|
368 |
-
|
369 |
-
|
370 |
|
371 |
-
|
372 |
-
|
|
|
|
|
373 |
|
374 |
-
|
375 |
-
|
376 |
"""
|
377 |
-
|
378 |
-
prompt = f"""
|
379 |
-
|
380 |
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
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.
|
388 |
-
|
389 |
-
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.
|
390 |
-
|
391 |
-
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.
|
392 |
-
|
393 |
-
Here is a sample dialogue between you and the patient:
|
394 |
-
|
395 |
-
<dialogue>
|
396 |
|
397 |
TRANSCRIPT:
|
398 |
-
{transcript}
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
|
|
408 |
"""
|
409 |
|
410 |
-
|
411 |
-
result = call_claude_api(prompt)
|
412 |
-
return result
|
413 |
-
|
414 |
-
|
415 |
-
# Get analysis from Claude API
|
416 |
-
result = call_claude_api(prompt)
|
417 |
-
return result
|
418 |
-
|
419 |
-
# Create simple interface
|
420 |
-
with gr.Blocks(title="Simple CASL Analysis", theme=gr.themes.Soft()) as app:
|
421 |
-
|
422 |
-
gr.Markdown("# 🗣️ Simple CASL Analysis Tool")
|
423 |
-
gr.Markdown("Upload a speech transcript and get instant CASL assessment results.")
|
424 |
|
425 |
-
|
426 |
-
with gr.Column():
|
427 |
-
gr.Markdown("### Upload & Settings")
|
428 |
-
|
429 |
-
file_upload = gr.File(
|
430 |
-
label="Upload Transcript File",
|
431 |
-
file_types=[".txt", ".cha"]
|
432 |
-
)
|
433 |
-
|
434 |
-
age = gr.Number(
|
435 |
-
label="Patient Age",
|
436 |
-
value=8,
|
437 |
-
minimum=1,
|
438 |
-
maximum=120
|
439 |
-
)
|
440 |
-
|
441 |
-
gender = gr.Radio(
|
442 |
-
["male", "female", "other"],
|
443 |
-
label="Gender",
|
444 |
-
value="male"
|
445 |
-
)
|
446 |
-
|
447 |
-
slp_notes = gr.Textbox(
|
448 |
-
label="SLP Clinical Notes (Optional)",
|
449 |
-
placeholder="Enter any additional clinical observations, context, or notes...",
|
450 |
-
lines=3
|
451 |
-
)
|
452 |
-
|
453 |
-
analyze_btn = gr.Button(
|
454 |
-
"🔍 Analyze Transcript",
|
455 |
-
variant="primary"
|
456 |
-
)
|
457 |
-
|
458 |
-
with gr.Column():
|
459 |
-
gr.Markdown("### Analysis Results")
|
460 |
-
|
461 |
-
output = gr.Textbox(
|
462 |
-
label="CASL Analysis Report",
|
463 |
-
placeholder="Analysis results will appear here...",
|
464 |
-
lines=25,
|
465 |
-
max_lines=30
|
466 |
-
)
|
467 |
-
|
468 |
-
# Connect the analyze button
|
469 |
analyze_btn.click(
|
470 |
-
|
471 |
inputs=[file_upload, age, gender, slp_notes],
|
472 |
-
outputs=[output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
473 |
)
|
474 |
|
475 |
if __name__ == "__main__":
|
476 |
-
print("🚀 Starting
|
477 |
if not ANTHROPIC_API_KEY:
|
478 |
print("⚠️ ANTHROPIC_API_KEY not configured - analysis will show error message")
|
479 |
print(" For HuggingFace Spaces: Add ANTHROPIC_API_KEY as a secret in your space settings")
|
|
|
93 |
SLP CLINICAL NOTES:
|
94 |
{slp_notes.strip()}
|
95 |
"""
|
|
|
|
|
96 |
|
97 |
+
# Simple analysis prompt - removing CASL-2 scores as requested
|
98 |
+
prompt = f"""
|
99 |
+
You are a speech-language pathologist analyzing a transcript for CASL assessment.
|
100 |
|
101 |
+
Patient: {age}-year-old {gender}
|
102 |
|
103 |
+
TRANSCRIPT:
|
104 |
+
{transcript}{notes_section}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
Please provide a CASL analysis including:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
1. SPEECH FACTORS (with counts and severity):
|
109 |
+
- Difficulty producing fluent speech
|
110 |
+
- Word retrieval issues
|
111 |
+
- Grammatical errors
|
112 |
+
- Repetitions and revisions
|
113 |
|
114 |
+
2. LANGUAGE SKILLS ASSESSMENT:
|
115 |
+
- Lexical/Semantic Skills (qualitative assessment)
|
116 |
+
- Syntactic Skills (qualitative assessment)
|
117 |
+
- Supralinguistic Skills (qualitative assessment)
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
3. TREATMENT RECOMMENDATIONS:
|
120 |
+
- List 3-5 specific intervention strategies
|
|
|
|
|
121 |
|
122 |
+
4. CLINICAL SUMMARY:
|
123 |
+
- Brief explanation of findings and prognosis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
+
Use exact quotes from the transcript as evidence.
|
126 |
+
Focus on qualitative observations rather than standardized scores.
|
127 |
+
{f"Consider the SLP clinical notes in your analysis." if slp_notes and slp_notes.strip() else ""}
|
128 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
# Get analysis from Claude API
|
131 |
+
result = call_claude_api(prompt)
|
132 |
+
return result
|
133 |
+
|
134 |
+
def targeted_analysis(transcript, custom_question, age, gender, slp_notes):
|
135 |
+
"""Perform targeted analysis based on custom questions"""
|
136 |
+
if not transcript or not transcript.strip():
|
137 |
+
return "Please provide a transcript first."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
if not custom_question or not custom_question.strip():
|
140 |
+
return "Please enter a specific question for analysis."
|
141 |
|
142 |
+
# Add SLP notes to the prompt if provided
|
143 |
+
notes_section = ""
|
144 |
+
if slp_notes and slp_notes.strip():
|
145 |
+
notes_section = f"""
|
146 |
|
147 |
+
SLP CLINICAL NOTES:
|
148 |
+
{slp_notes.strip()}
|
149 |
+
"""
|
150 |
|
151 |
+
# Targeted analysis prompt
|
152 |
+
prompt = f"""
|
153 |
+
You are a speech-language pathologist conducting a targeted analysis of a speech transcript.
|
154 |
|
155 |
+
Patient: {age}-year-old {gender}
|
|
|
156 |
|
157 |
+
TRANSCRIPT:
|
158 |
+
{transcript}{notes_section}
|
159 |
|
160 |
+
SPECIFIC QUESTION FOR ANALYSIS:
|
161 |
+
{custom_question.strip()}
|
162 |
|
163 |
+
Please provide a detailed, evidence-based analysis that directly addresses this specific question.
|
|
|
164 |
|
165 |
+
Your response should:
|
166 |
+
1. Directly answer the question asked
|
167 |
+
2. Provide specific examples from the transcript as evidence
|
168 |
+
3. Include relevant clinical observations
|
169 |
+
4. Offer practical insights for clinical practice
|
170 |
+
5. Be concise but comprehensive
|
171 |
|
172 |
+
Use exact quotes from the transcript to support your analysis.
|
173 |
+
"""
|
|
|
|
|
|
|
174 |
|
175 |
+
# Get targeted analysis from Claude API
|
176 |
+
result = call_claude_api(prompt)
|
177 |
+
return result
|
178 |
+
|
179 |
+
# Create enhanced interface with tabs
|
180 |
+
with gr.Blocks(title="Enhanced CASL Analysis", theme=gr.themes.Soft()) as app:
|
181 |
+
|
182 |
+
gr.Markdown("# 🗣️ Enhanced CASL Analysis Tool")
|
183 |
+
gr.Markdown("Upload a speech transcript and get instant CASL assessment results with targeted analysis options.")
|
184 |
+
|
185 |
+
# Store transcript globally
|
186 |
+
transcript_state = gr.State("")
|
187 |
+
|
188 |
+
with gr.Tabs():
|
189 |
+
# Tab 1: Basic Analysis
|
190 |
+
with gr.Tab("📊 Basic Analysis"):
|
191 |
+
with gr.Row():
|
192 |
+
with gr.Column():
|
193 |
+
gr.Markdown("### Upload & Settings")
|
194 |
+
|
195 |
+
file_upload = gr.File(
|
196 |
+
label="Upload Transcript File",
|
197 |
+
file_types=[".txt", ".cha"]
|
198 |
+
)
|
199 |
+
|
200 |
+
age = gr.Number(
|
201 |
+
label="Patient Age",
|
202 |
+
value=8,
|
203 |
+
minimum=1,
|
204 |
+
maximum=120
|
205 |
+
)
|
206 |
+
|
207 |
+
gender = gr.Radio(
|
208 |
+
["male", "female", "other"],
|
209 |
+
label="Gender",
|
210 |
+
value="male"
|
211 |
+
)
|
212 |
+
|
213 |
+
slp_notes = gr.Textbox(
|
214 |
+
label="SLP Clinical Notes (Optional)",
|
215 |
+
placeholder="Enter any additional clinical observations, context, or notes...",
|
216 |
+
lines=3
|
217 |
+
)
|
218 |
+
|
219 |
+
analyze_btn = gr.Button(
|
220 |
+
"🔍 Analyze Transcript",
|
221 |
+
variant="primary"
|
222 |
+
)
|
223 |
+
|
224 |
+
with gr.Column():
|
225 |
+
gr.Markdown("### Analysis Results")
|
226 |
+
|
227 |
+
output = gr.Textbox(
|
228 |
+
label="CASL Analysis Report",
|
229 |
+
placeholder="Analysis results will appear here...",
|
230 |
+
lines=25,
|
231 |
+
max_lines=30
|
232 |
+
)
|
233 |
+
|
234 |
+
# Tab 2: Targeted Analysis
|
235 |
+
with gr.Tab("🎯 Targeted Analysis"):
|
236 |
+
with gr.Row():
|
237 |
+
with gr.Column():
|
238 |
+
gr.Markdown("### Transcript Input")
|
239 |
+
|
240 |
+
transcript_input = gr.Textbox(
|
241 |
+
label="Paste Transcript Here",
|
242 |
+
placeholder="Paste your transcript text here, or use the transcript from Basic Analysis...",
|
243 |
+
lines=10
|
244 |
+
)
|
245 |
+
|
246 |
+
gr.Markdown("### Custom Analysis Question")
|
247 |
+
|
248 |
+
# Predefined question templates
|
249 |
+
question_templates = gr.Dropdown(
|
250 |
+
choices=[
|
251 |
+
"Select a template or write your own...",
|
252 |
+
"What specific speech patterns indicate word-finding difficulties?",
|
253 |
+
"How does the patient's grammar compare to age expectations?",
|
254 |
+
"What evidence suggests fluency issues in this transcript?",
|
255 |
+
"What pragmatic language skills are demonstrated?",
|
256 |
+
"How does the patient handle complex sentence structures?",
|
257 |
+
"What narrative organization skills are evident?",
|
258 |
+
"What specific intervention targets would you recommend?",
|
259 |
+
"How does this patient's language compare to typical development?",
|
260 |
+
"What evidence suggests cognitive-linguistic strengths/weaknesses?"
|
261 |
+
],
|
262 |
+
label="Question Templates (Optional)",
|
263 |
+
value="Select a template or write your own..."
|
264 |
+
)
|
265 |
+
|
266 |
+
custom_question = gr.Textbox(
|
267 |
+
label="Your Specific Question",
|
268 |
+
placeholder="Enter your specific analysis question here...",
|
269 |
+
lines=3
|
270 |
+
)
|
271 |
+
|
272 |
+
targeted_analyze_btn = gr.Button(
|
273 |
+
"🎯 Analyze Specific Question",
|
274 |
+
variant="primary"
|
275 |
+
)
|
276 |
+
|
277 |
+
with gr.Column():
|
278 |
+
gr.Markdown("### Targeted Analysis Results")
|
279 |
+
|
280 |
+
targeted_output = gr.Textbox(
|
281 |
+
label="Targeted Analysis Report",
|
282 |
+
placeholder="Targeted analysis results will appear here...",
|
283 |
+
lines=25,
|
284 |
+
max_lines=30
|
285 |
+
)
|
286 |
+
|
287 |
+
# Tab 3: Quick Questions
|
288 |
+
with gr.Tab("⚡ Quick Questions"):
|
289 |
+
with gr.Row():
|
290 |
+
with gr.Column():
|
291 |
+
gr.Markdown("### Quick Analysis Questions")
|
292 |
+
|
293 |
+
quick_transcript = gr.Textbox(
|
294 |
+
label="Transcript",
|
295 |
+
placeholder="Paste transcript here...",
|
296 |
+
lines=8
|
297 |
+
)
|
298 |
+
|
299 |
+
gr.Markdown("### Select Quick Questions")
|
300 |
+
|
301 |
+
quick_questions = gr.CheckboxGroup(
|
302 |
+
choices=[
|
303 |
+
"Word-finding difficulties",
|
304 |
+
"Grammar errors",
|
305 |
+
"Fluency issues",
|
306 |
+
"Pragmatic skills",
|
307 |
+
"Narrative structure",
|
308 |
+
"Vocabulary level",
|
309 |
+
"Sentence complexity",
|
310 |
+
"Speech rate patterns"
|
311 |
+
],
|
312 |
+
label="Select questions to analyze:",
|
313 |
+
value=[]
|
314 |
+
)
|
315 |
+
|
316 |
+
quick_analyze_btn = gr.Button(
|
317 |
+
"⚡ Quick Analysis",
|
318 |
+
variant="primary"
|
319 |
+
)
|
320 |
+
|
321 |
+
with gr.Column():
|
322 |
+
gr.Markdown("### Quick Analysis Results")
|
323 |
+
|
324 |
+
quick_output = gr.Textbox(
|
325 |
+
label="Quick Analysis Report",
|
326 |
+
placeholder="Quick analysis results will appear here...",
|
327 |
+
lines=25,
|
328 |
+
max_lines=30
|
329 |
+
)
|
330 |
+
|
331 |
+
# Event handlers
|
332 |
+
def on_analyze(file, age_val, gender_val, notes):
|
333 |
+
"""Handle basic analysis and store transcript"""
|
334 |
+
result = analyze_transcript(file, age_val, gender_val, notes)
|
335 |
+
transcript = process_file(file) if file else ""
|
336 |
+
return result, transcript
|
337 |
+
|
338 |
+
def on_targeted_analyze(transcript, question, age_val, gender_val, notes):
|
339 |
+
"""Handle targeted analysis"""
|
340 |
+
return targeted_analysis(transcript, question, age_val, gender_val, notes)
|
341 |
+
|
342 |
+
def on_question_template_change(template):
|
343 |
+
"""Handle question template selection"""
|
344 |
+
if template and template != "Select a template or write your own...":
|
345 |
+
return template
|
346 |
+
return ""
|
347 |
+
|
348 |
+
def on_quick_analyze(transcript, questions, age_val, gender_val, notes):
|
349 |
+
"""Handle quick analysis with multiple questions"""
|
350 |
+
if not transcript or not transcript.strip():
|
351 |
+
return "Please provide a transcript first."
|
352 |
|
353 |
+
if not questions:
|
354 |
+
return "Please select at least one question to analyze."
|
355 |
|
356 |
+
# Add SLP notes to the prompt if provided
|
357 |
+
notes_section = ""
|
358 |
+
if notes and notes.strip():
|
359 |
+
notes_section = f"""
|
360 |
|
361 |
+
SLP CLINICAL NOTES:
|
362 |
+
{notes.strip()}
|
363 |
"""
|
|
|
|
|
|
|
364 |
|
365 |
+
# Create quick analysis prompt
|
366 |
+
questions_text = "\n".join([f"- {q}" for q in questions])
|
367 |
+
prompt = f"""
|
368 |
+
You are a speech-language pathologist conducting a quick analysis of a speech transcript.
|
369 |
+
|
370 |
+
Patient: {age_val}-year-old {gender_val}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
|
372 |
TRANSCRIPT:
|
373 |
+
{transcript}{notes_section}
|
374 |
+
|
375 |
+
Please provide a brief analysis addressing these specific areas:
|
376 |
+
{questions_text}
|
377 |
+
|
378 |
+
For each area, provide:
|
379 |
+
1. Brief observations
|
380 |
+
2. Specific examples from the transcript
|
381 |
+
3. Clinical significance
|
382 |
+
|
383 |
+
Keep each section concise but informative.
|
384 |
"""
|
385 |
|
386 |
+
return call_claude_api(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
+
# Connect event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
analyze_btn.click(
|
390 |
+
on_analyze,
|
391 |
inputs=[file_upload, age, gender, slp_notes],
|
392 |
+
outputs=[output, transcript_input]
|
393 |
+
)
|
394 |
+
|
395 |
+
targeted_analyze_btn.click(
|
396 |
+
on_targeted_analyze,
|
397 |
+
inputs=[transcript_input, custom_question, age, gender, slp_notes],
|
398 |
+
outputs=[targeted_output]
|
399 |
+
)
|
400 |
+
|
401 |
+
question_templates.change(
|
402 |
+
on_question_template_change,
|
403 |
+
inputs=[question_templates],
|
404 |
+
outputs=[custom_question]
|
405 |
+
)
|
406 |
+
|
407 |
+
quick_analyze_btn.click(
|
408 |
+
on_quick_analyze,
|
409 |
+
inputs=[quick_transcript, quick_questions, age, gender, slp_notes],
|
410 |
+
outputs=[quick_output]
|
411 |
)
|
412 |
|
413 |
if __name__ == "__main__":
|
414 |
+
print("🚀 Starting Enhanced CASL Analysis Tool...")
|
415 |
if not ANTHROPIC_API_KEY:
|
416 |
print("⚠️ ANTHROPIC_API_KEY not configured - analysis will show error message")
|
417 |
print(" For HuggingFace Spaces: Add ANTHROPIC_API_KEY as a secret in your space settings")
|