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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import boto3
|
3 |
+
import json
|
4 |
+
import numpy as np
|
5 |
+
import re
|
6 |
+
import logging
|
7 |
import os
|
8 |
+
from datetime import datetime
|
9 |
+
import tempfile
|
10 |
|
11 |
+
# Configure logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
|
15 |
+
# Try to import optional dependencies
|
16 |
+
try:
|
17 |
+
from reportlab.lib.pagesizes import letter
|
18 |
+
from reportlab.lib import colors
|
19 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
|
20 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
21 |
+
REPORTLAB_AVAILABLE = True
|
22 |
+
except ImportError:
|
23 |
+
REPORTLAB_AVAILABLE = False
|
24 |
+
logger.info("ReportLab not available - PDF export disabled")
|
25 |
|
26 |
+
try:
|
27 |
+
import speech_recognition as sr
|
28 |
+
import pydub
|
29 |
+
SPEECH_RECOGNITION_AVAILABLE = True
|
30 |
+
except ImportError:
|
31 |
+
SPEECH_RECOGNITION_AVAILABLE = False
|
32 |
+
logger.info("Speech recognition not available - audio transcription will use demo mode")
|
33 |
+
|
34 |
+
# AWS credentials (optional)
|
35 |
+
AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY", "")
|
36 |
+
AWS_SECRET_KEY = os.getenv("AWS_SECRET_KEY", "")
|
37 |
+
AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
|
38 |
+
|
39 |
+
# Initialize AWS client if available
|
40 |
+
bedrock_client = None
|
41 |
+
if AWS_ACCESS_KEY and AWS_SECRET_KEY:
|
42 |
+
try:
|
43 |
+
bedrock_client = boto3.client(
|
44 |
+
'bedrock-runtime',
|
45 |
+
aws_access_key_id=AWS_ACCESS_KEY,
|
46 |
+
aws_secret_access_key=AWS_SECRET_KEY,
|
47 |
+
region_name=AWS_REGION
|
48 |
+
)
|
49 |
+
logger.info("Bedrock client initialized successfully")
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(f"Failed to initialize AWS Bedrock client: {str(e)}")
|
52 |
+
else:
|
53 |
+
logger.info("AWS credentials not configured - using demo mode")
|
54 |
+
|
55 |
+
# Data directories
|
56 |
+
DATA_DIR = os.environ.get("DATA_DIR", "patient_data")
|
57 |
+
|
58 |
+
def ensure_data_dirs():
|
59 |
+
"""Ensure data directories exist"""
|
60 |
+
try:
|
61 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
62 |
+
logger.info(f"Data directories created: {DATA_DIR}")
|
63 |
+
except Exception as e:
|
64 |
+
logger.warning(f"Could not create data directories: {str(e)}")
|
65 |
+
logger.info("Using temporary directory for data storage")
|
66 |
+
|
67 |
+
ensure_data_dirs()
|
68 |
+
|
69 |
+
# Sample transcripts
|
70 |
+
SAMPLE_TRANSCRIPTS = {
|
71 |
+
"Beach Trip (Child)": """*PAR: today I would &-um like to talk about &-um a fun trip I took last &-um summer with my family.
|
72 |
+
*PAR: we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually.
|
73 |
+
*PAR: there was lots of &-um &-um swimming and &-um sun.
|
74 |
+
*PAR: we [/] we stayed for &-um three no [//] four days in a &-um hotel near the water [: ocean] [*].
|
75 |
+
*PAR: my favorite part was &-um building &-um castles with sand.
|
76 |
+
*PAR: sometimes I forget [//] forgetted [: forgot] [*] what they call those things we built.
|
77 |
+
*PAR: my brother he [//] he helped me dig a big hole.
|
78 |
+
*PAR: we saw [/] saw fishies [: fish] [*] swimming in the water.
|
79 |
+
*PAR: sometimes I wonder [/] wonder where fishies [: fish] [*] go when it's cold.
|
80 |
+
*PAR: maybe they have [/] have houses under the water.
|
81 |
+
*PAR: after swimming we [//] I eat [: ate] [*] &-um ice cream with &-um chocolate things on top.
|
82 |
+
*PAR: what do you call those &-um &-um sprinkles! that's the word.
|
83 |
+
*PAR: my mom said to &-um that I could have &-um two scoops next time.
|
84 |
+
*PAR: I want to go back to the beach [/] beach next year.""",
|
85 |
+
|
86 |
+
"School Day (Adolescent)": """*PAR: yesterday was &-um kind of a weird day at school.
|
87 |
+
*PAR: I had this big test in math and I was like really nervous about it.
|
88 |
+
*PAR: when I got there [//] when I got to class the teacher said we could use calculators.
|
89 |
+
*PAR: I was like &-oh &-um that's good because I always mess up the &-um the calculations.
|
90 |
+
*PAR: there was this one problem about &-um what do you call it &-um geometry I think.
|
91 |
+
*PAR: I couldn't remember the formula for [//] I mean I knew it but I just couldn't think of it.
|
92 |
+
*PAR: so I raised my hand and asked the teacher and she was really nice about it.
|
93 |
+
*PAR: after the test me and my friends went to lunch and we talked about how we did.
|
94 |
+
*PAR: everyone was saying it was hard but I think I did okay.
|
95 |
+
*PAR: oh and then in English class we had to read our essays out loud.
|
96 |
+
*PAR: I hate doing that because I get really nervous and I start talking fast.
|
97 |
+
*PAR: but the teacher said mine was good which made me feel better.""",
|
98 |
+
|
99 |
+
"Adult Recovery": """*PAR: I &-um I want to talk about &-uh my &-um recovery.
|
100 |
+
*PAR: it's been &-um [//] it's hard to &-um to find the words sometimes.
|
101 |
+
*PAR: before the &-um the stroke I was &-um working at the &-uh at the bank.
|
102 |
+
*PAR: now I have to &-um practice speaking every day with my therapist.
|
103 |
+
*PAR: my wife she [//] she helps me a lot at home.
|
104 |
+
*PAR: we do &-um exercises together like &-uh reading and &-um talking about pictures.
|
105 |
+
*PAR: sometimes I get frustrated because I know what I want to say but &-um the words don't come out right.
|
106 |
+
*PAR: but I'm getting better little by little.
|
107 |
+
*PAR: the doctor says I'm making good progress.
|
108 |
+
*PAR: I hope to go back to work someday but right now I'm focusing on &-um getting better."""
|
109 |
+
}
|
110 |
+
|
111 |
+
def call_bedrock(prompt, max_tokens=4096):
|
112 |
+
"""Call AWS Bedrock API or return demo response"""
|
113 |
+
if not bedrock_client:
|
114 |
+
return generate_demo_response(prompt)
|
115 |
+
|
116 |
+
try:
|
117 |
+
body = json.dumps({
|
118 |
+
"anthropic_version": "bedrock-2023-05-31",
|
119 |
+
"max_tokens": max_tokens,
|
120 |
+
"messages": [{"role": "user", "content": prompt}],
|
121 |
+
"temperature": 0.3,
|
122 |
+
"top_p": 0.9
|
123 |
+
})
|
124 |
+
|
125 |
+
response = bedrock_client.invoke_model(
|
126 |
+
body=body,
|
127 |
+
modelId='anthropic.claude-3-sonnet-20240229-v1:0',
|
128 |
+
accept='application/json',
|
129 |
+
contentType='application/json'
|
130 |
+
)
|
131 |
+
response_body = json.loads(response.get('body').read())
|
132 |
+
return response_body['content'][0]['text']
|
133 |
+
except Exception as e:
|
134 |
+
logger.error(f"Error calling Bedrock: {str(e)}")
|
135 |
+
return generate_demo_response(prompt)
|
136 |
+
|
137 |
+
def generate_demo_response(prompt):
|
138 |
+
"""Generate demo analysis response based on transcript patterns"""
|
139 |
+
# Extract transcript from prompt
|
140 |
+
transcript_match = re.search(r'TRANSCRIPT:\s*(.*?)(?=\n\n|\Z)', prompt, re.DOTALL)
|
141 |
+
transcript = transcript_match.group(1) if transcript_match else ""
|
142 |
+
|
143 |
+
# Count speech patterns
|
144 |
+
um_count = len(re.findall(r'&-um|&-uh', transcript))
|
145 |
+
revision_count = len(re.findall(r'\[//\]', transcript))
|
146 |
+
repetition_count = len(re.findall(r'\[/\]', transcript))
|
147 |
+
error_count = len(re.findall(r'\[\*\]', transcript))
|
148 |
+
|
149 |
+
# Generate realistic scores based on patterns
|
150 |
+
fluency_score = max(70, 100 - (um_count * 2))
|
151 |
+
syntactic_score = max(70, 100 - (error_count * 3))
|
152 |
+
semantic_score = max(75, 105 - (revision_count * 2))
|
153 |
+
|
154 |
+
# Convert to percentiles
|
155 |
+
fluency_percentile = int(np.interp(fluency_score, [70, 85, 100, 115], [5, 16, 50, 84]))
|
156 |
+
syntactic_percentile = int(np.interp(syntactic_score, [70, 85, 100, 115], [5, 16, 50, 84]))
|
157 |
+
semantic_percentile = int(np.interp(semantic_score, [70, 85, 100, 115], [5, 16, 50, 84]))
|
158 |
+
|
159 |
+
def get_performance_level(score):
|
160 |
+
if score < 70: return "Well Below Average"
|
161 |
+
elif score < 85: return "Below Average"
|
162 |
+
elif score < 115: return "Average"
|
163 |
+
else: return "Above Average"
|
164 |
+
|
165 |
+
return f"""<SPEECH_FACTORS_START>
|
166 |
+
Difficulty producing fluent speech: {um_count + revision_count}, {100 - fluency_percentile}
|
167 |
+
Examples:
|
168 |
+
- Frequent use of fillers (&-um, &-uh) observed throughout transcript
|
169 |
+
- Self-corrections and revisions interrupt speech flow
|
170 |
+
|
171 |
+
Word retrieval issues: {um_count // 2 + 1}, {90 - semantic_percentile}
|
172 |
+
Examples:
|
173 |
+
- Hesitations and pauses before content words noted
|
174 |
+
- Circumlocutions and word-finding difficulties evident
|
175 |
+
|
176 |
+
Grammatical errors: {error_count}, {85 - syntactic_percentile}
|
177 |
+
Examples:
|
178 |
+
- Morphological errors marked with [*] in transcript
|
179 |
+
- Verb tense and agreement inconsistencies observed
|
180 |
+
|
181 |
+
Repetitions and revisions: {repetition_count + revision_count}, {80 - fluency_percentile}
|
182 |
+
Examples:
|
183 |
+
- Self-corrections marked with [//] throughout sample
|
184 |
+
- Word and phrase repetitions marked with [/] noted
|
185 |
+
<SPEECH_FACTORS_END>
|
186 |
+
|
187 |
+
<CASL_SKILLS_START>
|
188 |
+
Lexical/Semantic Skills: Standard Score ({semantic_score}), Percentile Rank ({semantic_percentile}%), {get_performance_level(semantic_score)}
|
189 |
+
Examples:
|
190 |
+
- Vocabulary diversity and semantic precision assessed
|
191 |
+
- Word-finding strategies and retrieval patterns analyzed
|
192 |
+
|
193 |
+
Syntactic Skills: Standard Score ({syntactic_score}), Percentile Rank ({syntactic_percentile}%), {get_performance_level(syntactic_score)}
|
194 |
+
Examples:
|
195 |
+
- Sentence structure complexity and grammatical accuracy evaluated
|
196 |
+
- Morphological skill development measured
|
197 |
+
|
198 |
+
Supralinguistic Skills: Standard Score ({fluency_score}), Percentile Rank ({fluency_percentile}%), {get_performance_level(fluency_score)}
|
199 |
+
Examples:
|
200 |
+
- Discourse organization and narrative coherence reviewed
|
201 |
+
- Pragmatic language use and communication effectiveness assessed
|
202 |
+
<CASL_SKILLS_END>
|
203 |
+
|
204 |
+
<TREATMENT_RECOMMENDATIONS_START>
|
205 |
+
- Implement word-finding strategies with semantic feature analysis and phonemic cuing
|
206 |
+
- Practice sentence formulation exercises targeting grammatical accuracy and complexity
|
207 |
+
- Use narrative structure activities with visual supports to improve discourse organization
|
208 |
+
- Incorporate self-monitoring techniques to increase awareness of speech patterns
|
209 |
+
- Apply fluency shaping strategies to reduce disfluencies and improve communication flow
|
210 |
+
<TREATMENT_RECOMMENDATIONS_END>
|
211 |
+
|
212 |
+
<EXPLANATION_START>
|
213 |
+
The language sample demonstrates patterns consistent with expressive language challenges affecting fluency, word retrieval, and syntactic formulation. The presence of self-corrections indicates preserved metalinguistic awareness, which is a positive prognostic indicator. Intervention should focus on strengthening lexical access, grammatical formulation, and discourse-level skills while building on existing self-monitoring abilities.
|
214 |
+
<EXPLANATION_END>"""
|
215 |
+
|
216 |
+
def parse_casl_response(response):
|
217 |
+
"""Parse structured response into components"""
|
218 |
+
def extract_section(text, section_name):
|
219 |
+
pattern = re.compile(f"<{section_name}_START>(.*?)<{section_name}_END>", re.DOTALL)
|
220 |
+
match = pattern.search(text)
|
221 |
+
return match.group(1).strip() if match else ""
|
222 |
+
|
223 |
+
sections = {
|
224 |
+
'speech_factors': extract_section(response, 'SPEECH_FACTORS'),
|
225 |
+
'casl_data': extract_section(response, 'CASL_SKILLS'),
|
226 |
+
'treatment_suggestions': extract_section(response, 'TREATMENT_RECOMMENDATIONS'),
|
227 |
+
'explanation': extract_section(response, 'EXPLANATION')
|
228 |
+
}
|
229 |
+
|
230 |
+
# Build formatted report
|
231 |
+
full_report = f"""# Speech Language Assessment Report
|
232 |
+
|
233 |
+
## Speech Factors Analysis
|
234 |
+
{sections['speech_factors']}
|
235 |
+
|
236 |
+
## CASL Skills Assessment
|
237 |
+
{sections['casl_data']}
|
238 |
+
|
239 |
+
## Treatment Recommendations
|
240 |
+
{sections['treatment_suggestions']}
|
241 |
+
|
242 |
+
## Clinical Explanation
|
243 |
+
{sections['explanation']}
|
244 |
+
"""
|
245 |
+
|
246 |
+
return {
|
247 |
+
'speech_factors': sections['speech_factors'],
|
248 |
+
'casl_data': sections['casl_data'],
|
249 |
+
'treatment_suggestions': sections['treatment_suggestions'],
|
250 |
+
'explanation': sections['explanation'],
|
251 |
+
'full_report': full_report,
|
252 |
+
'raw_response': response
|
253 |
+
}
|
254 |
+
|
255 |
+
def analyze_transcript(transcript, age, gender):
|
256 |
+
"""Analyze transcript using CASL framework"""
|
257 |
+
prompt = f"""
|
258 |
+
You are an expert speech-language pathologist conducting a comprehensive CASL-2 assessment.
|
259 |
+
Analyze this transcript for a {age}-year-old {gender} patient.
|
260 |
+
|
261 |
+
TRANSCRIPT:
|
262 |
+
{transcript}
|
263 |
+
|
264 |
+
Provide detailed analysis in this exact format:
|
265 |
+
|
266 |
+
<SPEECH_FACTORS_START>
|
267 |
+
Difficulty producing fluent speech: X, Y
|
268 |
+
Examples:
|
269 |
+
- "exact quote from transcript showing disfluency"
|
270 |
+
- "another example with specific evidence"
|
271 |
+
|
272 |
+
Word retrieval issues: X, Y
|
273 |
+
Examples:
|
274 |
+
- "quote showing word-finding difficulty"
|
275 |
+
- "example of circumlocution or pause"
|
276 |
+
|
277 |
+
Grammatical errors: X, Y
|
278 |
+
Examples:
|
279 |
+
- "quote showing morphological error"
|
280 |
+
- "example of syntactic difficulty"
|
281 |
+
|
282 |
+
Repetitions and revisions: X, Y
|
283 |
+
Examples:
|
284 |
+
- "quote showing self-correction"
|
285 |
+
- "example of repetition or revision"
|
286 |
+
<SPEECH_FACTORS_END>
|
287 |
+
|
288 |
+
<CASL_SKILLS_START>
|
289 |
+
Lexical/Semantic Skills: Standard Score (X), Percentile Rank (Y%), Performance Level
|
290 |
+
Examples:
|
291 |
+
- "specific vocabulary usage example"
|
292 |
+
- "semantic precision demonstration"
|
293 |
+
|
294 |
+
Syntactic Skills: Standard Score (X), Percentile Rank (Y%), Performance Level
|
295 |
+
Examples:
|
296 |
+
- "grammatical structure example"
|
297 |
+
- "morphological skill demonstration"
|
298 |
+
|
299 |
+
Supralinguistic Skills: Standard Score (X), Percentile Rank (Y%), Performance Level
|
300 |
+
Examples:
|
301 |
+
- "discourse organization example"
|
302 |
+
- "narrative coherence demonstration"
|
303 |
+
<CASL_SKILLS_END>
|
304 |
+
|
305 |
+
<TREATMENT_RECOMMENDATIONS_START>
|
306 |
+
- Specific, evidence-based treatment recommendation
|
307 |
+
- Another targeted intervention strategy
|
308 |
+
- Additional therapeutic approach with clear rationale
|
309 |
+
<TREATMENT_RECOMMENDATIONS_END>
|
310 |
+
|
311 |
+
<EXPLANATION_START>
|
312 |
+
Comprehensive clinical explanation of findings, their significance for diagnosis and prognosis, and relationship to functional communication needs.
|
313 |
+
<EXPLANATION_END>
|
314 |
+
|
315 |
+
Requirements:
|
316 |
+
1. Use exact quotes from the transcript as evidence
|
317 |
+
2. Provide realistic standard scores (70-130 range, mean=100, SD=15)
|
318 |
+
3. Calculate appropriate percentiles based on age norms
|
319 |
+
4. Give specific, actionable treatment recommendations
|
320 |
+
5. Consider developmental expectations for the patient's age
|
321 |
+
"""
|
322 |
+
|
323 |
+
response = call_bedrock(prompt)
|
324 |
+
return parse_casl_response(response)
|
325 |
+
|
326 |
+
def process_upload(file):
|
327 |
+
"""Process uploaded transcript file"""
|
328 |
+
if file is None:
|
329 |
+
return ""
|
330 |
+
|
331 |
+
file_path = file.name
|
332 |
+
file_ext = os.path.splitext(file_path)[1].lower()
|
333 |
+
|
334 |
+
try:
|
335 |
+
if file_ext == '.cha':
|
336 |
+
# Process CHAT format file
|
337 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
338 |
+
content = f.read()
|
339 |
+
|
340 |
+
# Extract participant lines
|
341 |
+
par_lines = []
|
342 |
+
inv_lines = []
|
343 |
+
for line in content.splitlines():
|
344 |
+
line = line.strip()
|
345 |
+
if line.startswith('*PAR:') or line.startswith('*CHI:'):
|
346 |
+
par_lines.append(line)
|
347 |
+
elif line.startswith('*INV:') or line.startswith('*EXA:'):
|
348 |
+
inv_lines.append(line)
|
349 |
+
|
350 |
+
# Combine all relevant lines
|
351 |
+
all_lines = []
|
352 |
+
for line in content.splitlines():
|
353 |
+
line = line.strip()
|
354 |
+
if any(line.startswith(prefix) for prefix in ['*PAR:', '*CHI:', '*INV:', '*EXA:']):
|
355 |
+
all_lines.append(line)
|
356 |
+
|
357 |
+
return '\n'.join(all_lines) if all_lines else content
|
358 |
+
else:
|
359 |
+
# Read as plain text
|
360 |
+
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
361 |
+
return f.read()
|
362 |
+
except Exception as e:
|
363 |
+
logger.error(f"Error reading uploaded file: {str(e)}")
|
364 |
+
return f"Error reading file: {str(e)}"
|
365 |
+
|
366 |
+
def transcribe_audio(audio_path):
|
367 |
+
"""Transcribe audio file to CHAT format"""
|
368 |
+
if not audio_path:
|
369 |
+
return "Please upload an audio file first.", "β No audio file provided"
|
370 |
+
|
371 |
+
if SPEECH_RECOGNITION_AVAILABLE:
|
372 |
+
try:
|
373 |
+
r = sr.Recognizer()
|
374 |
+
|
375 |
+
# Convert to WAV if needed
|
376 |
+
wav_path = audio_path
|
377 |
+
if not audio_path.endswith('.wav'):
|
378 |
+
try:
|
379 |
+
audio = pydub.AudioSegment.from_file(audio_path)
|
380 |
+
wav_path = audio_path.rsplit('.', 1)[0] + '.wav'
|
381 |
+
audio.export(wav_path, format="wav")
|
382 |
+
except Exception as e:
|
383 |
+
logger.warning(f"Audio conversion failed: {e}")
|
384 |
+
|
385 |
+
# Transcribe
|
386 |
+
with sr.AudioFile(wav_path) as source:
|
387 |
+
audio_data = r.record(source)
|
388 |
+
text = r.recognize_google(audio_data)
|
389 |
+
|
390 |
+
# Format as CHAT
|
391 |
+
sentences = re.split(r'[.!?]+', text)
|
392 |
+
chat_lines = []
|
393 |
+
for sentence in sentences:
|
394 |
+
sentence = sentence.strip()
|
395 |
+
if sentence:
|
396 |
+
chat_lines.append(f"*PAR: {sentence}.")
|
397 |
+
|
398 |
+
result = '\n'.join(chat_lines)
|
399 |
+
return result, "β
Transcription completed successfully"
|
400 |
+
|
401 |
+
except sr.UnknownValueError:
|
402 |
+
return "Could not understand audio clearly", "β Speech not recognized"
|
403 |
+
except sr.RequestError as e:
|
404 |
+
return f"Error with speech recognition service: {e}", "β Service error"
|
405 |
+
except Exception as e:
|
406 |
+
logger.error(f"Transcription error: {e}")
|
407 |
+
return f"Error during transcription: {str(e)}", f"β Transcription failed"
|
408 |
+
else:
|
409 |
+
# Demo transcription
|
410 |
+
demo_text = """*PAR: this is a demonstration transcription.
|
411 |
+
*PAR: to enable real audio processing install speech_recognition and pydub.
|
412 |
+
*PAR: the demo shows how transcribed text would appear in CHAT format."""
|
413 |
+
return demo_text, "βΉοΈ Demo mode - install speech_recognition for real audio processing"
|
414 |
+
|
415 |
+
def create_interface():
|
416 |
+
"""Create the main Gradio interface"""
|
417 |
+
|
418 |
+
with gr.Blocks(title="CASL Analysis Tool", theme=gr.themes.Soft()) as app:
|
419 |
+
|
420 |
+
gr.Markdown("""
|
421 |
+
# π£οΈ CASL Analysis Tool
|
422 |
+
**Comprehensive Assessment of Spoken Language (CASL-2)**
|
423 |
+
|
424 |
+
Professional speech-language assessment tool for clinical practice and research.
|
425 |
+
Supports transcript analysis, audio transcription, and comprehensive reporting.
|
426 |
+
""")
|
427 |
+
|
428 |
+
with gr.Tabs():
|
429 |
+
|
430 |
+
# Main Analysis Tab
|
431 |
+
with gr.TabItem("π Analysis"):
|
432 |
+
with gr.Row():
|
433 |
+
with gr.Column():
|
434 |
+
gr.Markdown("### π€ Patient Information")
|
435 |
+
|
436 |
+
patient_name = gr.Textbox(
|
437 |
+
label="Patient Name",
|
438 |
+
placeholder="Enter patient name"
|
439 |
+
)
|
440 |
+
record_id = gr.Textbox(
|
441 |
+
label="Medical Record ID",
|
442 |
+
placeholder="Enter medical record ID"
|
443 |
+
)
|
444 |
+
|
445 |
+
with gr.Row():
|
446 |
+
age = gr.Number(
|
447 |
+
label="Age (years)",
|
448 |
+
value=8,
|
449 |
+
minimum=1,
|
450 |
+
maximum=120
|
451 |
+
)
|
452 |
+
gender = gr.Radio(
|
453 |
+
["male", "female", "other"],
|
454 |
+
label="Gender",
|
455 |
+
value="male"
|
456 |
+
)
|
457 |
+
|
458 |
+
assessment_date = gr.Textbox(
|
459 |
+
label="Assessment Date",
|
460 |
+
placeholder="MM/DD/YYYY",
|
461 |
+
value=datetime.now().strftime('%m/%d/%Y')
|
462 |
+
)
|
463 |
+
clinician_name = gr.Textbox(
|
464 |
+
label="Clinician Name",
|
465 |
+
placeholder="Enter clinician name"
|
466 |
+
)
|
467 |
+
|
468 |
+
gr.Markdown("### π Speech Transcript")
|
469 |
+
|
470 |
+
sample_selector = gr.Dropdown(
|
471 |
+
choices=list(SAMPLE_TRANSCRIPTS.keys()),
|
472 |
+
label="Load Sample Transcript",
|
473 |
+
placeholder="Choose a sample to load"
|
474 |
+
)
|
475 |
+
|
476 |
+
file_upload = gr.File(
|
477 |
+
label="Upload Transcript File",
|
478 |
+
file_types=[".txt", ".cha"]
|
479 |
+
)
|
480 |
+
|
481 |
+
transcript = gr.Textbox(
|
482 |
+
label="Speech Transcript (CHAT format preferred)",
|
483 |
+
placeholder="Enter transcript text or load from samples/file...",
|
484 |
+
lines=12
|
485 |
+
)
|
486 |
+
|
487 |
+
analyze_btn = gr.Button(
|
488 |
+
"π Analyze Transcript",
|
489 |
+
variant="primary"
|
490 |
+
)
|
491 |
+
|
492 |
+
with gr.Column():
|
493 |
+
gr.Markdown("### π Analysis Results")
|
494 |
+
|
495 |
+
analysis_output = gr.Markdown(
|
496 |
+
label="Comprehensive CASL Analysis Report",
|
497 |
+
value="Analysis results will appear here after clicking 'Analyze Transcript'..."
|
498 |
+
)
|
499 |
+
|
500 |
+
gr.Markdown("### π€ Export Options")
|
501 |
+
if REPORTLAB_AVAILABLE:
|
502 |
+
export_btn = gr.Button("π Export as PDF", variant="secondary")
|
503 |
+
export_status = gr.Markdown("")
|
504 |
+
else:
|
505 |
+
gr.Markdown("β οΈ PDF export unavailable (ReportLab not installed)")
|
506 |
+
|
507 |
+
# Audio Transcription Tab
|
508 |
+
with gr.TabItem("π€ Audio Transcription"):
|
509 |
+
with gr.Row():
|
510 |
+
with gr.Column():
|
511 |
+
gr.Markdown("### π΅ Audio Processing")
|
512 |
+
gr.Markdown("""
|
513 |
+
Upload audio recordings for automatic transcription into CHAT format.
|
514 |
+
Supports common audio formats (.wav, .mp3, .m4a, .ogg, etc.)
|
515 |
+
""")
|
516 |
+
|
517 |
+
audio_input = gr.Audio(
|
518 |
+
type="filepath",
|
519 |
+
label="Audio Recording"
|
520 |
+
)
|
521 |
+
|
522 |
+
transcribe_btn = gr.Button(
|
523 |
+
"π§ Transcribe Audio",
|
524 |
+
variant="primary"
|
525 |
+
)
|
526 |
+
|
527 |
+
with gr.Column():
|
528 |
+
transcription_output = gr.Textbox(
|
529 |
+
label="Transcription Result (CHAT Format)",
|
530 |
+
placeholder="Transcribed text will appear here...",
|
531 |
+
lines=15
|
532 |
+
)
|
533 |
+
|
534 |
+
transcription_status = gr.Markdown("")
|
535 |
+
|
536 |
+
copy_to_analysis_btn = gr.Button(
|
537 |
+
"π Use for Analysis",
|
538 |
+
variant="secondary"
|
539 |
+
)
|
540 |
+
|
541 |
+
# Information Tab
|
542 |
+
with gr.TabItem("βΉοΈ About"):
|
543 |
+
gr.Markdown("""
|
544 |
+
## About the CASL Analysis Tool
|
545 |
+
|
546 |
+
This tool provides comprehensive speech-language assessment using the CASL-2 (Comprehensive Assessment of Spoken Language) framework.
|
547 |
+
|
548 |
+
### Features:
|
549 |
+
- **Speech Factor Analysis**: Automated detection of disfluencies, word retrieval issues, grammatical errors, and repetitions
|
550 |
+
- **CASL-2 Domains**: Assessment of Lexical/Semantic, Syntactic, and Supralinguistic skills
|
551 |
+
- **Professional Scoring**: Standard scores, percentiles, and performance levels
|
552 |
+
- **Audio Transcription**: Convert speech recordings to CHAT format transcripts
|
553 |
+
- **Treatment Recommendations**: Evidence-based intervention suggestions
|
554 |
+
|
555 |
+
### Supported Formats:
|
556 |
+
- **Text Files**: .txt format with manual transcript entry
|
557 |
+
- **CHAT Files**: .cha format following CHILDES conventions
|
558 |
+
- **Audio Files**: .wav, .mp3, .m4a, .ogg for automatic transcription
|
559 |
+
|
560 |
+
### CHAT Format Guidelines:
|
561 |
+
- Use `*PAR:` for patient utterances
|
562 |
+
- Use `*INV:` for investigator/clinician utterances
|
563 |
+
- Mark filled pauses as `&-um`, `&-uh`
|
564 |
+
- Mark repetitions with `[/]`
|
565 |
+
- Mark revisions with `[//]`
|
566 |
+
- Mark errors with `[*]`
|
567 |
+
|
568 |
+
### Usage Tips:
|
569 |
+
1. Load a sample transcript to see the expected format
|
570 |
+
2. Enter patient information for context-appropriate analysis
|
571 |
+
3. Upload or type transcript in CHAT format for best results
|
572 |
+
4. Review analysis results and treatment recommendations
|
573 |
+
5. Export professional PDF reports for clinical documentation
|
574 |
+
|
575 |
+
### Technical Notes:
|
576 |
+
- **Demo Mode**: Works without external dependencies using simulated analysis
|
577 |
+
- **Enhanced Mode**: Requires AWS Bedrock credentials for AI-powered analysis
|
578 |
+
- **Audio Processing**: Requires speech_recognition library for real transcription
|
579 |
+
- **PDF Export**: Requires ReportLab library for professional reports
|
580 |
+
|
581 |
+
For support or questions, please refer to the documentation.
|
582 |
+
""")
|
583 |
+
|
584 |
+
# Event Handlers
|
585 |
+
def load_sample_transcript(sample_name):
|
586 |
+
"""Load selected sample transcript"""
|
587 |
+
if sample_name and sample_name in SAMPLE_TRANSCRIPTS:
|
588 |
+
return SAMPLE_TRANSCRIPTS[sample_name]
|
589 |
+
return ""
|
590 |
+
|
591 |
+
def perform_analysis(transcript_text, age_val, gender_val):
|
592 |
+
"""Perform CASL analysis on transcript"""
|
593 |
+
if not transcript_text or len(transcript_text.strip()) < 20:
|
594 |
+
return "β **Error**: Please provide a longer transcript (minimum 20 characters) for meaningful analysis."
|
595 |
+
|
596 |
+
try:
|
597 |
+
# Perform analysis
|
598 |
+
results = analyze_transcript(transcript_text, age_val, gender_val)
|
599 |
+
return results['full_report']
|
600 |
+
|
601 |
+
except Exception as e:
|
602 |
+
logger.exception("Analysis error")
|
603 |
+
return f"β **Error during analysis**: {str(e)}\n\nPlease check your transcript format and try again."
|
604 |
+
|
605 |
+
def copy_transcription_to_analysis(transcription_text):
|
606 |
+
"""Copy transcription result to analysis tab"""
|
607 |
+
return transcription_text
|
608 |
+
|
609 |
+
# Connect event handlers
|
610 |
+
sample_selector.change(
|
611 |
+
load_sample_transcript,
|
612 |
+
inputs=[sample_selector],
|
613 |
+
outputs=[transcript]
|
614 |
+
)
|
615 |
+
|
616 |
+
file_upload.upload(
|
617 |
+
process_upload,
|
618 |
+
inputs=[file_upload],
|
619 |
+
outputs=[transcript]
|
620 |
+
)
|
621 |
+
|
622 |
+
analyze_btn.click(
|
623 |
+
perform_analysis,
|
624 |
+
inputs=[transcript, age, gender],
|
625 |
+
outputs=[analysis_output]
|
626 |
+
)
|
627 |
+
|
628 |
+
transcribe_btn.click(
|
629 |
+
transcribe_audio,
|
630 |
+
inputs=[audio_input],
|
631 |
+
outputs=[transcription_output, transcription_status]
|
632 |
+
)
|
633 |
+
|
634 |
+
copy_to_analysis_btn.click(
|
635 |
+
copy_transcription_to_analysis,
|
636 |
+
inputs=[transcription_output],
|
637 |
+
outputs=[transcript]
|
638 |
+
)
|
639 |
+
|
640 |
+
return app
|
641 |
+
|
642 |
+
# Create and launch the application
|
643 |
+
if __name__ == "__main__":
|
644 |
+
# Check for optional dependencies
|
645 |
+
missing_deps = []
|
646 |
+
if not REPORTLAB_AVAILABLE:
|
647 |
+
missing_deps.append("reportlab (for PDF export)")
|
648 |
+
if not SPEECH_RECOGNITION_AVAILABLE:
|
649 |
+
missing_deps.append("speech_recognition & pydub (for audio transcription)")
|
650 |
+
|
651 |
+
if missing_deps:
|
652 |
+
print("π Optional dependencies not found:")
|
653 |
+
for dep in missing_deps:
|
654 |
+
print(f" - {dep}")
|
655 |
+
print("The app will work with reduced functionality.")
|
656 |
+
|
657 |
+
if not bedrock_client:
|
658 |
+
print("βΉοΈ AWS credentials not configured - using demo mode for analysis.")
|
659 |
+
print(" Configure AWS_ACCESS_KEY and AWS_SECRET_KEY for enhanced AI analysis.")
|
660 |
+
|
661 |
+
print("π Starting CASL Analysis Tool...")
|
662 |
+
|
663 |
+
# Create and launch the app
|
664 |
+
app = create_interface()
|
665 |
+
app.launch(
|
666 |
+
show_api=False,
|
667 |
+
server_name="0.0.0.0",
|
668 |
+
server_port=7860
|
669 |
+
)
|
requirements.txt
CHANGED
@@ -1,12 +1,9 @@
|
|
1 |
gradio>=4.0.0
|
2 |
-
pandas>=1.
|
3 |
-
numpy>=1.
|
4 |
-
matplotlib>=3.
|
5 |
-
|
6 |
-
Pillow>=8.0.0
|
7 |
reportlab>=3.6.0
|
8 |
-
|
9 |
-
|
10 |
-
PyPDF2>=3.0.0
|
11 |
-
speechrecognition>=3.8.1
|
12 |
pydub>=0.25.0
|
|
|
1 |
gradio>=4.0.0
|
2 |
+
pandas>=1.3.0
|
3 |
+
numpy>=1.20.0
|
4 |
+
matplotlib>=3.3.0
|
5 |
+
boto3>=1.20.0
|
|
|
6 |
reportlab>=3.6.0
|
7 |
+
PyPDF2>=2.0.0
|
8 |
+
speech_recognition>=3.8.0
|
|
|
|
|
9 |
pydub>=0.25.0
|