SreekarB commited on
Commit
f16b6cd
·
verified ·
1 Parent(s): 2c479d5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +197 -548
app.py CHANGED
@@ -10,7 +10,7 @@ import os
10
  from PIL import Image
11
  import io
12
  import PyPDF2
13
- from datetime import datetime
14
 
15
  # Configure logging
16
  logging.basicConfig(level=logging.INFO)
@@ -22,6 +22,10 @@ AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY", "")
22
  AWS_SECRET_KEY = os.getenv("AWS_SECRET_KEY", "")
23
  AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
24
 
 
 
 
 
25
  # Initialize Bedrock client if credentials are available
26
  bedrock_client = None
27
  if AWS_ACCESS_KEY and AWS_SECRET_KEY:
@@ -52,10 +56,6 @@ SAMPLE_TRANSCRIPT = """*PAR: today I would &-um like to talk about &-um a fun tr
52
  *PAR: my mom said to &-um that I could have &-um two scoops next time.
53
  *PAR: I want to go back to the beach [/] beach next year."""
54
 
55
- # ===============================
56
- # Utility Functions
57
- # ===============================
58
-
59
  def read_pdf(file_path):
60
  """Read text from a PDF file"""
61
  try:
@@ -69,31 +69,15 @@ def read_pdf(file_path):
69
  logger.error(f"Error reading PDF: {str(e)}")
70
  return ""
71
 
72
- def process_upload(file):
73
- """Process an uploaded file (PDF or text)"""
74
- if file is None:
75
- return ""
76
-
77
- file_path = file.name
78
- if file_path.endswith('.pdf'):
79
- return read_pdf(file_path)
80
- else:
81
- with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
82
- return f.read()
83
-
84
- # ===============================
85
- # AI Model Interface Functions
86
- # ===============================
87
-
88
- def call_bedrock(prompt, max_tokens=4096):
89
  """Call the AWS Bedrock API to analyze text using Claude"""
90
  if not bedrock_client:
91
- return "AWS credentials not configured. Please set your AWS credentials as secrets in the Space settings."
92
 
93
  try:
94
  body = json.dumps({
95
  "anthropic_version": "bedrock-2023-05-31",
96
- "max_tokens": max_tokens,
97
  "messages": [
98
  {
99
  "role": "user",
@@ -117,8 +101,8 @@ def call_bedrock(prompt, max_tokens=4096):
117
  logger.error(f"Error in call_bedrock: {str(e)}")
118
  return f"Error: {str(e)}"
119
 
120
- def generate_demo_response(prompt):
121
- """Generate a simulated response for demo purposes"""
122
  # This function generates a realistic but fake response for demo purposes
123
  # In a real deployment, you would call an actual LLM API
124
 
@@ -180,34 +164,8 @@ def generate_demo_response(prompt):
180
 
181
  return response
182
 
183
- def generate_demo_transcription(audio_path):
184
- """Generate a simulated transcription response"""
185
- # In a real app, this would process an audio file
186
- return "*PAR: today I want to tell you about my favorite toy.\n*PAR: it's a &-um teddy bear that I got for my birthday.\n*PAR: he has &-um brown fur and a red bow.\n*PAR: I like to sleep with him every night.\n*PAR: sometimes I take him to school in my backpack."
187
-
188
- def generate_demo_qa_response(question):
189
- """Generate a simulated Q&A response"""
190
- qa_responses = {
191
- "what is casl": "CASL-2 (Comprehensive Assessment of Spoken Language, Second Edition) is a standardized assessment tool used by Speech-Language Pathologists to evaluate a child's oral language abilities across multiple domains including lexical/semantic, syntactic, and supralinguistic skills. It helps identify language disorders and guides intervention planning.",
192
- "how do i interpret scores": "CASL-2 scores include standard scores (mean=100, SD=15), percentile ranks, and performance levels. Standard scores below 85 indicate below average performance, 85-115 is average, and above 115 is above average. Percentile ranks show how a child performs relative to same-age peers.",
193
- "what activities help word finding": "Activities to improve word-finding skills include semantic feature analysis (describing attributes of objects), categorization tasks, word association games, rapid naming practice, and structured conversation with gentle cueing. Visual supports and semantic mapping can also be helpful.",
194
- "how often should therapy occur": "The recommended frequency for speech-language therapy typically ranges from 1-3 sessions per week, depending on the severity of the impairment. For moderate difficulties, twice weekly sessions of 30-45 minutes are common. Consistency is important for progress.",
195
- "when should i reassess": "Reassessment is typically recommended every 3-6 months to track progress and adjust treatment goals. For educational settings, annual reassessment is common. More frequent informal assessments can help guide ongoing intervention.",
196
- }
197
-
198
- # Simple keyword matching for demo purposes
199
- for key, response in qa_responses.items():
200
- if key in question.lower():
201
- return response
202
-
203
- return "I don't have specific information about that topic. For detailed professional guidance, consult with a licensed Speech-Language Pathologist who can provide advice specific to your situation."
204
-
205
- # ===============================
206
- # Analysis Functions
207
- # ===============================
208
-
209
- def parse_casl_response(response):
210
- """Parse the LLM response for CASL analysis into structured data"""
211
  lines = response.split('\n')
212
  data = {
213
  'Factor': [],
@@ -310,8 +268,8 @@ def parse_casl_response(response):
310
  'additional_analysis': additional_analysis
311
  }
312
 
313
- def create_casl_plots(speech_factors, casl_data):
314
- """Create visualizations for the CASL analysis results"""
315
 
316
  # Set a professional style for the plots
317
  plt.style.use('seaborn-v0_8-pastel')
@@ -389,7 +347,7 @@ def create_casl_plots(speech_factors, casl_data):
389
 
390
  return buf
391
 
392
- def create_casl_radar_chart(speech_factors):
393
  """Create a radar chart for speech factors (percentiles)"""
394
 
395
  if speech_factors.empty or 'Severity' not in speech_factors.columns:
@@ -444,7 +402,7 @@ def create_casl_radar_chart(speech_factors):
444
  return buf
445
 
446
  def analyze_transcript(transcript, age, gender):
447
- """Analyze a speech transcript using the CASL framework"""
448
 
449
  # Instructions for the LLM analysis
450
  instructions = """
@@ -498,164 +456,19 @@ def analyze_transcript(transcript, age, gender):
498
  if bedrock_client:
499
  response = call_bedrock(prompt)
500
  else:
501
- response = generate_demo_response(prompt)
502
 
503
  # Parse the response
504
- results = parse_casl_response(response)
505
 
506
  # Create visualizations
507
- plot_image = create_casl_plots(results['speech_factors'], results['casl_data'])
508
- radar_image = create_casl_radar_chart(results['speech_factors'])
509
 
510
  return results, plot_image, radar_image, response
511
 
512
- def generate_report(patient_info, analysis_results, report_type="formal"):
513
- """Generate a professional report based on analysis results"""
514
-
515
- patient_name = patient_info.get("name", "")
516
- record_id = patient_info.get("record_id", "")
517
- age = patient_info.get("age", "")
518
- gender = patient_info.get("gender", "")
519
- assessment_date = patient_info.get("assessment_date", datetime.now().strftime('%m/%d/%Y'))
520
- clinician = patient_info.get("clinician", "")
521
-
522
- prompt = f"""
523
- You are a professional Speech-Language Pathologist creating a {report_type} report based on an assessment.
524
-
525
- PATIENT INFORMATION:
526
- Name: {patient_name}
527
- Record ID: {record_id}
528
- Age: {age}
529
- Gender: {gender}
530
- Assessment Date: {assessment_date}
531
- Clinician: {clinician}
532
-
533
- ASSESSMENT RESULTS:
534
- {analysis_results}
535
-
536
- Please create a professional {report_type} report that includes:
537
- 1. Patient information and assessment details
538
- 2. Summary of findings (strengths and areas of concern)
539
- 3. Detailed analysis of language domains
540
- 4. Specific recommendations for therapy
541
- 5. Recommendation for frequency and duration of services
542
-
543
- Use clear, professional language appropriate for {'educational professionals' if report_type == 'formal' else 'parents and caregivers'}.
544
- Format the report with proper headings and sections.
545
- """
546
-
547
- # Call the API or use demo mode
548
- if bedrock_client:
549
- report = call_bedrock(prompt, max_tokens=6000)
550
- else:
551
- # For demo, create a simulated report
552
- report = f"""
553
- # {'FORMAL LANGUAGE ASSESSMENT REPORT' if report_type == 'formal' else 'PARENT-FRIENDLY LANGUAGE ASSESSMENT SUMMARY'}
554
-
555
- **Date of Assessment:** {assessment_date}
556
- **Clinician:** {clinician}
557
-
558
- ## PATIENT INFORMATION
559
- **Name:** {patient_name}
560
- **Record ID:** {record_id}
561
- **Age:** {age}
562
- **Gender:** {gender}
563
-
564
- ## ASSESSMENT SUMMARY
565
-
566
- {'The patient was assessed using the Comprehensive Assessment of Spoken Language, Second Edition (CASL-2) to evaluate language skills across multiple domains. The assessment involved language sample analysis and standardized testing.' if report_type == 'formal' else 'We completed a language assessment to better understand your child\'s communication strengths and challenges. This helps us create a plan to support their development.'}
567
-
568
- ## KEY FINDINGS
569
-
570
- **Areas of Strength:**
571
- - Ability to maintain conversational topics
572
- - Good vocabulary for everyday topics
573
- - Strong nonverbal communication skills
574
-
575
- **Areas of Challenge:**
576
- - Word-finding difficulties during conversation
577
- - Grammatical errors in complex sentences
578
- - Difficulty with abstract language concepts
579
-
580
- ## DETAILED ANALYSIS
581
-
582
- **Lexical/Semantic Skills:** Standard Score 91 (27th percentile) - Low Average Range
583
- The student demonstrates adequate vocabulary but struggles with retrieving specific words during conversation. Word-finding pauses were noted throughout the language sample.
584
-
585
- **Syntactic Skills:** Standard Score 85 (16th percentile) - Low Average Range
586
- The student shows difficulty with complex grammatical structures, particularly verb tense consistency and complex sentence formation.
587
-
588
- **Supralinguistic Skills:** Standard Score 83 (13th percentile) - Below Average Range
589
- The student struggles with understanding figurative language, making inferences, and comprehending abstract concepts.
590
-
591
- ## RECOMMENDATIONS
592
-
593
- {'1. Speech-Language Therapy focused on:' if report_type == 'formal' else 'We recommend:'}
594
- - Word-finding strategies using semantic feature analysis
595
- - Structured grammatical exercises to improve sentence complexity
596
- - Explicit instruction in figurative language comprehension
597
- - Narrative language development using visual supports
598
-
599
- {'2. Frequency of service: Twice weekly sessions of 30 minutes each for 12 weeks, followed by a reassessment to measure progress.' if report_type == 'formal' else '2. We recommend therapy twice a week for 30 minutes. This consistency will help your child make better progress.'}
600
-
601
- {'3. Classroom accommodations including:' if report_type == 'formal' else '3. In school, your child may benefit from:'}
602
- - Extended time for verbal responses
603
- - Visual supports for complex instructions
604
- - Pre-teaching of vocabulary for academic units
605
-
606
- ## PROGNOSIS
607
-
608
- {'The prognosis for improvement is good with consistent therapeutic intervention and support. Regular reassessment is recommended to monitor progress.' if report_type == 'formal' else 'With regular therapy and support at home, we expect your child to make good progress in these areas.'}
609
-
610
- {'Respectfully submitted,' if report_type == 'formal' else 'Please reach out with any questions!'}
611
-
612
- {clinician}
613
- Speech-Language Pathologist
614
- """
615
-
616
- return report
617
-
618
- def transcribe_audio(audio_path, patient_age):
619
- """Transcribe an audio recording using CHAT format"""
620
- # In a real implementation, this would use a speech-to-text service
621
- # For demo purposes, we'll return a simulated transcription
622
-
623
- if bedrock_client:
624
- # In a real implementation, you would process the audio file and send it to a transcription service
625
- # Here we just simulate the result
626
- transcription = generate_demo_transcription(audio_path)
627
- else:
628
- transcription = generate_demo_transcription(audio_path)
629
-
630
- return transcription
631
-
632
- def answer_slp_question(question):
633
- """Answer a question about SLP practice or CASL assessment"""
634
-
635
- prompt = f"""
636
- You are an experienced Speech-Language Pathologist answering a question from a colleague.
637
-
638
- QUESTION:
639
- {question}
640
-
641
- Please provide a clear, evidence-based answer focused specifically on the question asked.
642
- Reference best practices and current research where appropriate.
643
- Keep your answer concise but comprehensive.
644
- """
645
-
646
- if bedrock_client:
647
- answer = call_bedrock(prompt)
648
- else:
649
- answer = generate_demo_qa_response(question)
650
-
651
- return answer
652
-
653
- # ===============================
654
- # Gradio Interface
655
- # ===============================
656
-
657
  def create_interface():
658
- """Create the main Gradio interface"""
659
 
660
  # Define custom theme colors
661
  primary_color = "#2C7FB8" # Professional blue
@@ -757,274 +570,189 @@ def create_interface():
757
  gr.HTML(
758
  """
759
  <div class="header">
760
- <h1>SLP Analysis Tool</h1>
761
- <p>A comprehensive assessment tool for Speech-Language Pathologists</p>
762
  </div>
763
  """
764
  )
765
 
766
- # Main tabs
767
- with gr.Tabs() as main_tabs:
768
- # ===============================
769
- # CASL Analysis Tab
770
- # ===============================
771
- with gr.TabItem("CASL Analysis", id=0):
772
- with gr.Row():
773
- # Left column - Input section
774
- with gr.Column(scale=1):
775
- # Patient information panel
776
- with gr.Box(elem_classes="container patient-info"):
777
- gr.Markdown("### Patient Information")
778
-
779
- with gr.Row():
780
- patient_name = gr.Textbox(label="Patient Name", placeholder="Enter patient name")
781
- record_id = gr.Textbox(label="Record ID", placeholder="Enter record ID")
782
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
  with gr.Row():
784
- age = gr.Number(label="Age", value=8, minimum=1, maximum=120)
785
- gender = gr.Radio(["male", "female", "other"], label="Gender", value="male")
786
 
787
  with gr.Row():
788
- assessment_date = gr.Textbox(
789
- label="Assessment Date",
790
- placeholder="MM/DD/YYYY",
791
- value=datetime.now().strftime('%m/%d/%Y')
792
- )
793
- clinician_name = gr.Textbox(
794
- label="Clinician",
795
- placeholder="Enter clinician name"
796
- )
 
 
797
 
798
- # Speech sample panel
799
- with gr.Box(elem_classes="container speech-sample"):
800
- gr.Markdown("### Speech Sample")
 
801
 
802
- # Sample button
803
- sample_btn = gr.Button("Load Sample Transcript", size="sm")
804
 
805
- # Transcript input
806
- transcript = gr.Textbox(
807
- label="Transcript",
808
- placeholder="Paste the speech transcript here...",
809
- lines=10
810
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
811
 
812
- # Add info about transcript format
813
- gr.Markdown(
814
- """
815
- <div class="info-box">
816
- <strong>Transcript Format:</strong> Use CHAT format with *PAR: for patient lines.
817
- Mark word-finding with &-um, paraphasias with [*], and provide intended words with [: word].
818
- </div>
819
- """,
820
- elem_classes="markdown-text"
821
- )
822
 
823
- # File upload
824
- file_upload = gr.File(
825
- label="Or upload a transcript file",
826
- file_types=["text", "txt", "pdf", "rtf"]
827
- )
828
 
829
- # Analysis button
830
- analyze_btn = gr.Button("Analyze Speech Sample", variant="primary", size="lg")
831
-
832
- # Right column - Results section
833
- with gr.Column(scale=1):
834
- with gr.Box(elem_classes="container results-container"):
835
- with gr.Tabs() as results_tabs:
836
- # Summary tab
837
- with gr.TabItem("Summary", id=0, elem_classes="tab-content"):
838
- with gr.Row():
839
- output_image = gr.Image(
840
- label="Speech Factors & CASL-2 Scores",
841
- show_label=True,
842
- elem_classes="viz-container"
843
- )
844
-
845
- with gr.Row():
846
- radar_chart = gr.Image(
847
- label="Severity Profile",
848
- show_label=True,
849
- elem_classes="viz-container"
850
- )
851
-
852
- with gr.Box():
853
- gr.Markdown("### Key Findings", elem_classes="markdown-text")
854
- speech_factors_table = gr.DataFrame(
855
- label="Speech Factors Analysis",
856
- headers=["Factor", "Occurrences", "Severity (Percentile)"],
857
- interactive=False
858
- )
859
- casl_table = gr.DataFrame(
860
- label="CASL-2 Assessment",
861
- headers=["Domain", "Standard Score", "Percentile", "Performance Level"],
862
- interactive=False
863
- )
864
-
865
- # Treatment tab
866
- with gr.TabItem("Treatment Plan", id=1, elem_classes="tab-content"):
867
- gr.Markdown("### Recommended Treatment Approaches", elem_classes="markdown-text")
868
- treatment_md = gr.Markdown(elem_classes="treatment-panel")
869
-
870
- gr.Markdown("### Clinical Rationale", elem_classes="markdown-text")
871
- explanation_md = gr.Markdown(elem_classes="panel")
872
-
873
- with gr.Accordion("Supporting Evidence", open=False):
874
- gr.Markdown("""
875
- <table class="evidence-table">
876
- <tr>
877
- <th>Factor</th>
878
- <th>Evidence-based Approaches</th>
879
- <th>References</th>
880
- </tr>
881
- <tr>
882
- <td>Word Retrieval</td>
883
- <td>Semantic feature analysis, phonological cueing, word generation tasks</td>
884
- <td>Boyle, 2010; Kiran & Thompson, 2003</td>
885
- </tr>
886
- <tr>
887
- <td>Grammatical Errors</td>
888
- <td>Treatment of Underlying Forms (TUF), Morphosyntactic therapy</td>
889
- <td>Thompson et al., 2003; Ebbels, 2014</td>
890
- </tr>
891
- <tr>
892
- <td>Fluency/Prosody</td>
893
- <td>Rate control, rhythmic cueing, contrastive stress exercises</td>
894
- <td>Ballard et al., 2010; Tamplin & Baker, 2017</td>
895
- </tr>
896
- </table>
897
- """, elem_classes="markdown-text")
898
-
899
- # Full report tab
900
- with gr.TabItem("Full Report", id=2, elem_classes="tab-content"):
901
- full_analysis = gr.Markdown()
902
-
903
- # Add PDF export option
904
- export_btn = gr.Button("Export Report as PDF", variant="secondary")
905
- export_status = gr.Markdown("")
906
-
907
- # ===============================
908
- # Report Generator Tab
909
- # ===============================
910
- with gr.TabItem("Report Generator", id=1):
911
- with gr.Row():
912
- with gr.Column(scale=1):
913
- gr.Markdown("### Generate Professional Reports")
914
-
915
- # Patient info
916
- with gr.Box(elem_classes="container patient-info"):
917
- gr.Markdown("#### Patient Information")
918
- report_patient_name = gr.Textbox(label="Patient Name", placeholder="Enter patient name")
919
- report_record_id = gr.Textbox(label="Record ID", placeholder="Enter record ID")
920
- report_age = gr.Number(label="Age", value=8, minimum=1, maximum=120)
921
- report_gender = gr.Radio(["male", "female", "other"], label="Gender", value="male")
922
- report_date = gr.Textbox(
923
- label="Assessment Date",
924
- placeholder="MM/DD/YYYY",
925
- value=datetime.now().strftime('%m/%d/%Y')
926
- )
927
- report_clinician = gr.Textbox(label="Clinician", placeholder="Enter clinician name")
928
-
929
- with gr.Box():
930
- gr.Markdown("#### Assessment Results")
931
- report_results = gr.Textbox(
932
- label="Paste assessment results or notes here",
933
- placeholder="Include key findings, test scores, and observations...",
934
- lines=10
935
- )
936
 
937
- report_type = gr.Radio(
938
- ["Formal (for professionals)", "Parent-friendly"],
939
- label="Report Type",
940
- value="Formal (for professionals)"
941
- )
942
 
943
- generate_report_btn = gr.Button("Generate Report", variant="primary")
944
-
945
- with gr.Column(scale=1):
946
- report_output = gr.Markdown()
947
- report_download_btn = gr.Button("Download Report as PDF", variant="secondary")
948
- report_download_status = gr.Markdown("")
949
-
950
- # ===============================
951
- # Transcription Tool Tab
952
- # ===============================
953
- with gr.TabItem("Transcription Tool", id=2):
954
- with gr.Row():
955
- with gr.Column(scale=1):
956
- gr.Markdown("### Audio Transcription Tool")
957
- gr.Markdown("Upload an audio recording to automatically transcribe it in CHAT format.")
958
-
959
- audio_input = gr.Audio(type="filepath", label="Upload Audio Recording")
960
-
961
- with gr.Row():
962
- transcription_age = gr.Number(label="Patient Age", value=8, minimum=1, maximum=120)
963
- transcribe_btn = gr.Button("Transcribe Audio", variant="primary")
964
-
965
- with gr.Column(scale=1):
966
- transcription_output = gr.Textbox(
967
- label="Transcription Result",
968
- placeholder="Transcription will appear here...",
969
- lines=12
970
- )
971
-
972
- with gr.Row():
973
- copy_to_analysis_btn = gr.Button("Use for Analysis", variant="secondary")
974
- edit_transcription_btn = gr.Button("Edit Transcription", variant="secondary")
975
-
976
- # ===============================
977
- # SLP Assistant Tab
978
- # ===============================
979
- with gr.TabItem("SLP Assistant", id=3):
980
- with gr.Row():
981
- with gr.Column(scale=1):
982
- gr.Markdown("### SLP Knowledge Assistant")
983
- gr.Markdown("Ask questions about CASL assessment, therapy techniques, or SLP best practices.")
984
-
985
- question_input = gr.Textbox(
986
- label="Your Question",
987
- placeholder="e.g., What activities help improve word-finding skills?",
988
- lines=3
989
- )
990
-
991
- ask_question_btn = gr.Button("Ask Question", variant="primary")
992
-
993
- # Quick question buttons
994
- gr.Markdown("#### Common Questions")
995
- with gr.Row():
996
- q1_btn = gr.Button("What is CASL?")
997
- q2_btn = gr.Button("How do I interpret scores?")
998
-
999
- with gr.Row():
1000
- q3_btn = gr.Button("Activities for word finding")
1001
- q4_btn = gr.Button("When to reassess")
1002
-
1003
- with gr.Column(scale=1):
1004
- answer_output = gr.Markdown()
1005
-
1006
- with gr.Accordion("References", open=False):
1007
- gr.Markdown("""
1008
- - American Speech-Language-Hearing Association (ASHA)
1009
- - Comprehensive Assessment of Spoken Language (CASL-2) Manual
1010
- - Evidence-Based Practice in Speech-Language Pathology
1011
- - Current research in pediatric language intervention
1012
- """)
1013
 
1014
- # ===============================
1015
- # Event Handlers
1016
- # ===============================
 
 
 
 
 
 
1017
 
1018
- # Load sample transcript button
 
 
1019
  def load_sample():
1020
  return SAMPLE_TRANSCRIPT
1021
 
1022
- sample_btn.click(load_sample, outputs=[transcript])
1023
-
1024
- # File upload handler
1025
- file_upload.upload(process_upload, file_upload, transcript)
1026
-
1027
- # Analysis button handler
 
 
 
 
 
 
 
1028
  def on_analyze_click(transcript_text, age_val, gender_val, patient_name_val, record_id_val, clinician_val, assessment_date_val):
1029
  if not transcript_text or len(transcript_text.strip()) < 50:
1030
  return (
@@ -1090,6 +818,17 @@ def create_interface():
1090
  f"Error details: {str(e)}"
1091
  )
1092
 
 
 
 
 
 
 
 
 
 
 
 
1093
  analyze_btn.click(
1094
  on_analyze_click,
1095
  inputs=[
@@ -1106,99 +845,9 @@ def create_interface():
1106
  full_analysis
1107
  ]
1108
  )
1109
-
1110
- # Export report button simulation
1111
- def export_pdf_simulation():
1112
- return "Report export initiated. The PDF would be downloaded in a production environment."
1113
-
1114
- export_btn.click(export_pdf_simulation, outputs=[export_status])
1115
- report_download_btn.click(export_pdf_simulation, outputs=[report_download_status])
1116
-
1117
- # Report generator button
1118
- def on_generate_report(name, record_id, age, gender, date, clinician, results, report_type):
1119
- patient_info = {
1120
- "name": name,
1121
- "record_id": record_id,
1122
- "age": age,
1123
- "gender": gender,
1124
- "assessment_date": date,
1125
- "clinician": clinician
1126
- }
1127
-
1128
- report_type_val = "formal" if "Formal" in report_type else "parent-friendly"
1129
-
1130
- try:
1131
- report = generate_report(patient_info, results, report_type_val)
1132
- return report
1133
- except Exception as e:
1134
- logger.exception("Error generating report")
1135
- return f"Error generating report: {str(e)}"
1136
-
1137
- generate_report_btn.click(
1138
- on_generate_report,
1139
- inputs=[
1140
- report_patient_name, report_record_id, report_age,
1141
- report_gender, report_date, report_clinician,
1142
- report_results, report_type
1143
- ],
1144
- outputs=[report_output]
1145
- )
1146
-
1147
- # Transcription button
1148
- def on_transcribe_audio(audio_path, age):
1149
- try:
1150
- if not audio_path:
1151
- return "Please upload an audio file to transcribe."
1152
-
1153
- transcription = transcribe_audio(audio_path, age)
1154
- return transcription
1155
- except Exception as e:
1156
- logger.exception("Error transcribing audio")
1157
- return f"Error transcribing audio: {str(e)}"
1158
-
1159
- transcribe_btn.click(
1160
- on_transcribe_audio,
1161
- inputs=[audio_input, transcription_age],
1162
- outputs=[transcription_output]
1163
- )
1164
-
1165
- # Copy transcription to analysis
1166
- def copy_to_analysis(transcription):
1167
- return transcription, gr.update(selected=0) # Switches to the Analysis tab
1168
-
1169
- copy_to_analysis_btn.click(
1170
- copy_to_analysis,
1171
- inputs=[transcription_output],
1172
- outputs=[transcript, main_tabs]
1173
- )
1174
-
1175
- # SLP Assistant question handling
1176
- def on_ask_question(question):
1177
- try:
1178
- answer = answer_slp_question(question)
1179
- return answer
1180
- except Exception as e:
1181
- logger.exception("Error getting answer")
1182
- return f"Error: {str(e)}"
1183
-
1184
- ask_question_btn.click(
1185
- on_ask_question,
1186
- inputs=[question_input],
1187
- outputs=[answer_output]
1188
- )
1189
-
1190
- # Quick question buttons
1191
- q1_btn.click(lambda: "What is CASL?", outputs=[question_input])
1192
- q2_btn.click(lambda: "How do I interpret CASL scores?", outputs=[question_input])
1193
- q3_btn.click(lambda: "What activities help with word finding difficulties?", outputs=[question_input])
1194
- q4_btn.click(lambda: "When should I reassess a patient?", outputs=[question_input])
1195
-
1196
  return app
1197
 
1198
- # ===============================
1199
- # Main Application
1200
- # ===============================
1201
-
1202
  # Create requirements.txt file for HuggingFace Spaces
1203
  def create_requirements_file():
1204
  requirements = [
@@ -1227,4 +876,4 @@ if __name__ == "__main__":
1227
 
1228
  # Launch the Gradio app
1229
  app = create_interface()
1230
- app.launch()
 
10
  from PIL import Image
11
  import io
12
  import PyPDF2
13
+ import secrets
14
 
15
  # Configure logging
16
  logging.basicConfig(level=logging.INFO)
 
22
  AWS_SECRET_KEY = os.getenv("AWS_SECRET_KEY", "")
23
  AWS_REGION = os.getenv("AWS_REGION", "us-east-1")
24
 
25
+ # If we're on HuggingFace Spaces, use the HF_TOKEN (if available)
26
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
27
+ USE_HF_INFERENCE = bool(HF_TOKEN) and len(HF_TOKEN) > 0
28
+
29
  # Initialize Bedrock client if credentials are available
30
  bedrock_client = None
31
  if AWS_ACCESS_KEY and AWS_SECRET_KEY:
 
56
  *PAR: my mom said to &-um that I could have &-um two scoops next time.
57
  *PAR: I want to go back to the beach [/] beach next year."""
58
 
 
 
 
 
59
  def read_pdf(file_path):
60
  """Read text from a PDF file"""
61
  try:
 
69
  logger.error(f"Error reading PDF: {str(e)}")
70
  return ""
71
 
72
+ def call_bedrock(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  """Call the AWS Bedrock API to analyze text using Claude"""
74
  if not bedrock_client:
75
+ return "AWS credentials not configured. Please set your AWS credentials as secrets in the HuggingFace Space settings."
76
 
77
  try:
78
  body = json.dumps({
79
  "anthropic_version": "bedrock-2023-05-31",
80
+ "max_tokens": 4096,
81
  "messages": [
82
  {
83
  "role": "user",
 
101
  logger.error(f"Error in call_bedrock: {str(e)}")
102
  return f"Error: {str(e)}"
103
 
104
+ def call_hf_inference(prompt):
105
+ """Simulate LLM output for demo purposes when no API credentials are available"""
106
  # This function generates a realistic but fake response for demo purposes
107
  # In a real deployment, you would call an actual LLM API
108
 
 
164
 
165
  return response
166
 
167
+ def parse_analysis_response(response):
168
+ """Parse the LLM response into structured data"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  lines = response.split('\n')
170
  data = {
171
  'Factor': [],
 
268
  'additional_analysis': additional_analysis
269
  }
270
 
271
+ def create_plots(speech_factors, casl_data):
272
+ """Create visualizations for the analysis results"""
273
 
274
  # Set a professional style for the plots
275
  plt.style.use('seaborn-v0_8-pastel')
 
347
 
348
  return buf
349
 
350
+ def create_radar_chart(speech_factors):
351
  """Create a radar chart for speech factors (percentiles)"""
352
 
353
  if speech_factors.empty or 'Severity' not in speech_factors.columns:
 
402
  return buf
403
 
404
  def analyze_transcript(transcript, age, gender):
405
+ """Analyze a speech transcript using the Bedrock API or fallback to demo mode"""
406
 
407
  # Instructions for the LLM analysis
408
  instructions = """
 
456
  if bedrock_client:
457
  response = call_bedrock(prompt)
458
  else:
459
+ response = call_hf_inference(prompt)
460
 
461
  # Parse the response
462
+ results = parse_analysis_response(response)
463
 
464
  # Create visualizations
465
+ plot_image = create_plots(results['speech_factors'], results['casl_data'])
466
+ radar_image = create_radar_chart(results['speech_factors'])
467
 
468
  return results, plot_image, radar_image, response
469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
  def create_interface():
471
+ """Create the Gradio interface"""
472
 
473
  # Define custom theme colors
474
  primary_color = "#2C7FB8" # Professional blue
 
570
  gr.HTML(
571
  """
572
  <div class="header">
573
+ <h1>CASL Speech Analysis Tool</h1>
574
+ <p>A professional assessment tool for Speech-Language Pathologists</p>
575
  </div>
576
  """
577
  )
578
 
579
+ # Create main layout
580
+ with gr.Row():
581
+ # Left column - Input section
582
+ with gr.Column(scale=1):
583
+ with gr.Box(elem_classes="container patient-info"):
584
+ gr.Markdown("### Patient Information")
585
+
586
+ with gr.Row():
587
+ patient_name = gr.Textbox(label="Patient Name", placeholder="Enter patient name")
588
+ record_id = gr.Textbox(label="Record ID", placeholder="Enter record ID")
589
+
590
+ with gr.Row():
591
+ age = gr.Number(label="Age", value=8, minimum=1, maximum=120)
592
+ gender = gr.Radio(["male", "female", "other"], label="Gender", value="male")
593
+
594
+ with gr.Row():
595
+ assessment_date = gr.Textbox(label="Assessment Date",
596
+ placeholder="MM/DD/YYYY",
597
+ value=None)
598
+ clinician_name = gr.Textbox(label="Clinician",
599
+ placeholder="Enter clinician name")
600
+
601
+ with gr.Box(elem_classes="container speech-sample"):
602
+ gr.Markdown("### Speech Sample")
603
+
604
+ # Add sample button
605
+ sample_btn = gr.Button("Load Sample Transcript", size="sm")
606
+
607
+ # Transcript input
608
+ transcript = gr.Textbox(
609
+ label="Transcript",
610
+ placeholder="Paste the speech transcript here...",
611
+ lines=10
612
+ )
613
+
614
+ # Add info about transcript format
615
+ gr.Markdown(
616
+ """
617
+ <div class="info-box">
618
+ <strong>Transcript Format:</strong> Use CHAT format with *PAR: for patient lines.
619
+ Mark word-finding with &-um, paraphasias with [*], and provide intended words with [: word].
620
+ </div>
621
+ """,
622
+ elem_classes="markdown-text"
623
+ )
624
+
625
+ # File upload
626
+ file_upload = gr.File(
627
+ label="Or upload a transcript file",
628
+ file_types=["text", "txt", "pdf", "rtf"]
629
+ )
630
+
631
+ # Analysis button
632
+ analyze_btn = gr.Button("Analyze Speech Sample", variant="primary", size="lg")
633
+
634
+ # Add API credential section (collapsible)
635
+ with gr.Accordion("API Configuration", open=False):
636
+ gr.Markdown("""
637
+ ### AWS Bedrock Credentials
638
+
639
+ For full functionality, add your AWS credentials as environment variables or secrets in your HuggingFace Space:
640
+ - AWS_ACCESS_KEY
641
+ - AWS_SECRET_KEY
642
+ - AWS_REGION (default: us-east-1)
643
+
644
+ Without credentials, the app will run in demo mode with simulated responses.
645
+ """)
646
+
647
+ # Right column - Results section
648
+ with gr.Column(scale=1):
649
+ with gr.Box(elem_classes="container results-container"):
650
+ with gr.Tabs() as tabs:
651
+ # Summary tab
652
+ with gr.TabItem("Summary", id=0, elem_classes="tab-content"):
653
  with gr.Row():
654
+ output_image = gr.Image(label="Speech Factors & CASL-2 Scores",
655
+ show_label=True, elem_classes="viz-container")
656
 
657
  with gr.Row():
658
+ radar_chart = gr.Image(label="Severity Profile",
659
+ show_label=True, elem_classes="viz-container")
660
+
661
+ with gr.Box():
662
+ gr.Markdown("### Key Findings", elem_classes="markdown-text")
663
+ speech_factors_table = gr.DataFrame(label="Speech Factors Analysis",
664
+ headers=["Factor", "Occurrences", "Severity (Percentile)"],
665
+ interactive=False)
666
+ casl_table = gr.DataFrame(label="CASL-2 Assessment",
667
+ headers=["Domain", "Standard Score", "Percentile", "Performance Level"],
668
+ interactive=False)
669
 
670
+ # Treatment tab
671
+ with gr.TabItem("Treatment Plan", id=1, elem_classes="tab-content"):
672
+ gr.Markdown("### Recommended Treatment Approaches", elem_classes="markdown-text")
673
+ treatment_md = gr.Markdown(elem_classes="treatment-panel")
674
 
675
+ gr.Markdown("### Clinical Rationale", elem_classes="markdown-text")
676
+ explanation_md = gr.Markdown(elem_classes="panel")
677
 
678
+ with gr.Accordion("Supporting Evidence", open=False):
679
+ gr.Markdown("""
680
+ <table class="evidence-table">
681
+ <tr>
682
+ <th>Factor</th>
683
+ <th>Evidence-based Approaches</th>
684
+ <th>References</th>
685
+ </tr>
686
+ <tr>
687
+ <td>Word Retrieval</td>
688
+ <td>Semantic feature analysis, phonological cueing, word generation tasks</td>
689
+ <td>Boyle, 2010; Kiran & Thompson, 2003</td>
690
+ </tr>
691
+ <tr>
692
+ <td>Grammatical Errors</td>
693
+ <td>Treatment of Underlying Forms (TUF), Morphosyntactic therapy</td>
694
+ <td>Thompson et al., 2003; Ebbels, 2014</td>
695
+ </tr>
696
+ <tr>
697
+ <td>Fluency/Prosody</td>
698
+ <td>Rate control, rhythmic cueing, contrastive stress exercises</td>
699
+ <td>Ballard et al., 2010; Tamplin & Baker, 2017</td>
700
+ </tr>
701
+ </table>
702
+ """, elem_classes="markdown-text")
703
+
704
+ # Evidence tab
705
+ with gr.TabItem("Language Sample Evidence", id=2, elem_classes="tab-content"):
706
+ gr.Markdown("### Speech Sample Evidence", elem_classes="markdown-text")
707
 
708
+ # Create a collapsible section for each speech factor
709
+ factors = ["Word Retrieval", "Grammatical Errors", "Repetitions/Revisions",
710
+ "Fluency", "Neologisms", "Perseveration"]
 
 
 
 
 
 
 
711
 
712
+ for factor in factors:
713
+ with gr.Accordion(f"{factor} Examples", open=False):
714
+ gr.Markdown(f"Examples of {factor.lower()} will be highlighted here from the transcript.")
 
 
715
 
716
+ gr.Markdown("### Transcript Annotations", elem_classes="markdown-text")
717
+ gr.Markdown("A detailed analysis of the transcript will appear here after processing.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
718
 
719
+ # Full report tab
720
+ with gr.TabItem("Full Report", id=3, elem_classes="tab-content"):
721
+ full_analysis = gr.Markdown()
 
 
722
 
723
+ # Add PDF export option
724
+ export_btn = gr.Button("Export Report as PDF", variant="secondary")
725
+ export_status = gr.Markdown("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
726
 
727
+ # Footer
728
+ gr.HTML(
729
+ """
730
+ <div class="footer">
731
+ <p>CASL Speech Analysis Tool | For professional use by Speech-Language Pathologists</p>
732
+ <p>Results should be interpreted by qualified professionals in conjunction with other assessment methods.</p>
733
+ </div>
734
+ """
735
+ )
736
 
737
+ # Define app functions
738
+
739
+ # Function to load sample transcript
740
  def load_sample():
741
  return SAMPLE_TRANSCRIPT
742
 
743
+ # Handle file upload
744
+ def process_upload(file):
745
+ if file is None:
746
+ return ""
747
+
748
+ file_path = file.name
749
+ if file_path.endswith('.pdf'):
750
+ return read_pdf(file_path)
751
+ else:
752
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
753
+ return f.read()
754
+
755
+ # Handle analysis button click
756
  def on_analyze_click(transcript_text, age_val, gender_val, patient_name_val, record_id_val, clinician_val, assessment_date_val):
757
  if not transcript_text or len(transcript_text.strip()) < 50:
758
  return (
 
818
  f"Error details: {str(e)}"
819
  )
820
 
821
+ # Function to simulate PDF export
822
+ def export_pdf():
823
+ # In a real app, this would generate a PDF
824
+ # For this demo, we'll just return a status message
825
+ return "Report export initiated. The PDF would be downloaded in a production environment."
826
+
827
+ # Connect UI components to functions
828
+ sample_btn.click(load_sample, outputs=[transcript])
829
+ file_upload.upload(process_upload, file_upload, transcript)
830
+ export_btn.click(export_pdf, outputs=[export_status])
831
+
832
  analyze_btn.click(
833
  on_analyze_click,
834
  inputs=[
 
845
  full_analysis
846
  ]
847
  )
848
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849
  return app
850
 
 
 
 
 
851
  # Create requirements.txt file for HuggingFace Spaces
852
  def create_requirements_file():
853
  requirements = [
 
876
 
877
  # Launch the Gradio app
878
  app = create_interface()
879
+ app.launch()