Spaces:
Sleeping
Sleeping
Upload casl_analysis.py
Browse files- casl_analysis.py +279 -106
casl_analysis.py
CHANGED
|
@@ -730,62 +730,168 @@ def create_casl_radar_chart(speech_factors):
|
|
| 730 |
def analyze_transcript(transcript, age, gender):
|
| 731 |
"""Analyze a speech transcript using the CASL framework"""
|
| 732 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
# Instructions for the LLM analysis
|
| 734 |
instructions = """
|
| 735 |
-
You
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
1. Difficulty producing fluent, grammatical speech -
|
| 740 |
-
2. Word retrieval issues -
|
| 741 |
-
3. Grammatical errors -
|
| 742 |
-
4. Repetitions and revisions -
|
| 743 |
-
5. Neologisms -
|
| 744 |
-
6. Perseveration -
|
| 745 |
-
7. Comprehension issues -
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
|
| 776 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
"""
|
| 778 |
|
| 779 |
-
# Prepare prompt for Claude
|
| 780 |
prompt = f"""
|
| 781 |
-
|
|
|
|
|
|
|
| 782 |
|
| 783 |
TRANSCRIPT:
|
| 784 |
{transcript}
|
| 785 |
|
|
|
|
|
|
|
| 786 |
{instructions}
|
| 787 |
|
| 788 |
-
|
| 789 |
"""
|
| 790 |
|
| 791 |
# Call the appropriate API or fallback to demo mode
|
|
@@ -1179,34 +1285,14 @@ def create_interface():
|
|
| 1179 |
with gr.Tabs() as results_tabs:
|
| 1180 |
# Summary tab
|
| 1181 |
with gr.TabItem("Summary", id=0, elem_classes="tab-content"):
|
| 1182 |
-
with gr.Row():
|
| 1183 |
-
output_image = gr.Image(
|
| 1184 |
-
label="Speech Factors & CASL-2 Scores",
|
| 1185 |
-
show_label=True,
|
| 1186 |
-
elem_classes="viz-container"
|
| 1187 |
-
)
|
| 1188 |
-
|
| 1189 |
-
with gr.Row():
|
| 1190 |
-
radar_chart = gr.Image(
|
| 1191 |
-
label="Severity Profile",
|
| 1192 |
-
show_label=True,
|
| 1193 |
-
elem_classes="viz-container"
|
| 1194 |
-
)
|
| 1195 |
-
|
| 1196 |
with gr.Group():
|
| 1197 |
gr.Markdown("### Key Findings", elem_classes="markdown-text")
|
| 1198 |
-
|
| 1199 |
-
label="Speech Factors Analysis",
|
| 1200 |
-
headers=["Factor", "Occurrences", "Severity (Percentile)", "Example Errors"],
|
| 1201 |
-
interactive=False
|
| 1202 |
-
)
|
| 1203 |
-
casl_table = gr.DataFrame(
|
| 1204 |
-
label="CASL-2 Assessment",
|
| 1205 |
-
headers=["Domain", "Standard Score", "Percentile", "Performance Level", "Example"],
|
| 1206 |
-
interactive=False
|
| 1207 |
-
)
|
| 1208 |
|
| 1209 |
-
with gr.Accordion("
|
|
|
|
|
|
|
|
|
|
| 1210 |
specific_errors_md = gr.Markdown(elem_classes="markdown-text")
|
| 1211 |
|
| 1212 |
# Treatment tab
|
|
@@ -1529,36 +1615,133 @@ def create_interface():
|
|
| 1529 |
def on_analyze_click(transcript_text, age_val, gender_val, patient_name_val, record_id_val, clinician_val, assessment_date_val):
|
| 1530 |
if not transcript_text or len(transcript_text.strip()) < 50:
|
| 1531 |
return (
|
| 1532 |
-
pd.DataFrame(),
|
| 1533 |
-
pd.DataFrame(),
|
| 1534 |
-
None,
|
| 1535 |
-
None,
|
| 1536 |
"Error: Please provide a longer transcript for analysis.",
|
| 1537 |
"The transcript is too short for meaningful analysis.",
|
| 1538 |
"Please provide a speech sample with at least 50 characters.",
|
|
|
|
|
|
|
| 1539 |
"",
|
| 1540 |
"",
|
| 1541 |
""
|
| 1542 |
)
|
| 1543 |
|
| 1544 |
try:
|
| 1545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1546 |
|
| 1547 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1548 |
treatment_text = ""
|
| 1549 |
-
|
| 1550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1551 |
|
| 1552 |
-
#
|
| 1553 |
-
|
| 1554 |
-
|
| 1555 |
-
|
| 1556 |
-
|
| 1557 |
-
|
| 1558 |
-
else:
|
| 1559 |
-
specific_errors_text += f"**{factor}:** No specific example found\n\n"
|
| 1560 |
else:
|
| 1561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1562 |
|
| 1563 |
# Save the record to storage
|
| 1564 |
patient_info = {
|
|
@@ -1608,20 +1791,14 @@ def create_interface():
|
|
| 1608 |
else:
|
| 1609 |
full_report = f"## Complete Analysis Report\n\n{full_text}"
|
| 1610 |
|
| 1611 |
-
# Get the raw LLM response
|
| 1612 |
-
raw_output =
|
| 1613 |
-
|
| 1614 |
-
# Convert image buffers to PIL images
|
| 1615 |
-
plot_img_pil = Image.open(plot_img)
|
| 1616 |
-
radar_img_pil = Image.open(radar_img)
|
| 1617 |
|
| 1618 |
return (
|
| 1619 |
-
|
| 1620 |
-
|
| 1621 |
-
plot_img_pil,
|
| 1622 |
-
radar_img_pil,
|
| 1623 |
treatment_text,
|
| 1624 |
-
|
| 1625 |
full_report,
|
| 1626 |
save_message,
|
| 1627 |
specific_errors_text,
|
|
@@ -1631,11 +1808,9 @@ def create_interface():
|
|
| 1631 |
logger.exception("Error during analysis")
|
| 1632 |
error_message = f"Error during analysis: {str(e)}"
|
| 1633 |
return (
|
| 1634 |
-
|
| 1635 |
-
|
| 1636 |
-
|
| 1637 |
-
None,
|
| 1638 |
-
f"Error during analysis: {str(e)}",
|
| 1639 |
"An error occurred while processing the transcript.",
|
| 1640 |
f"Error details: {str(e)}",
|
| 1641 |
"",
|
|
@@ -1650,10 +1825,8 @@ def create_interface():
|
|
| 1650 |
patient_name, record_id, clinician_name, assessment_date
|
| 1651 |
],
|
| 1652 |
outputs=[
|
| 1653 |
-
|
| 1654 |
-
|
| 1655 |
-
output_image,
|
| 1656 |
-
radar_chart,
|
| 1657 |
treatment_md,
|
| 1658 |
explanation_md,
|
| 1659 |
full_analysis,
|
|
|
|
| 730 |
def analyze_transcript(transcript, age, gender):
|
| 731 |
"""Analyze a speech transcript using the CASL framework"""
|
| 732 |
|
| 733 |
+
# CHAT transcription symbol cheat sheet
|
| 734 |
+
cheat_sheet = """
|
| 735 |
+
CHAT TRANSCRIPTION SYMBOL SUMMARY -- Abridged for AphasiaBank
|
| 736 |
+
|
| 737 |
+
Basic Utterance Terminators
|
| 738 |
+
. period
|
| 739 |
+
? question
|
| 740 |
+
! exclamation
|
| 741 |
+
|
| 742 |
+
Special Utterance Terminators
|
| 743 |
+
+… trailing off
|
| 744 |
+
+..? trailing off of a question
|
| 745 |
+
+/. interruption by another speaker
|
| 746 |
+
+/? interruption of a question by another speaker
|
| 747 |
+
+//. self-interruption
|
| 748 |
+
+//? self-interruption of a question
|
| 749 |
+
+"/. quotation follows on next line
|
| 750 |
+
+" quoted utterance occurs on this line (use at beginning of utterance
|
| 751 |
+
as link, not a terminator)
|
| 752 |
+
+< lazy overlap marking (at beginning of utterance that overlapped the
|
| 753 |
+
the previous utterance)
|
| 754 |
+
|
| 755 |
+
@n neologism (e.g., sakov@n)
|
| 756 |
+
exclamations common ones: ah, aw, haha, ow, oy, sh, ugh, uhoh
|
| 757 |
+
interjections common ones: mhm, uhhuh, hm, uhuh
|
| 758 |
+
fillers common ones: &-um, &-uh
|
| 759 |
+
letters s@l
|
| 760 |
+
letter sequence abcdefg@k
|
| 761 |
+
xxx unintelligible speech, not treated as a word
|
| 762 |
+
www untranscribed material (e.g., looking through pictures, talking with
|
| 763 |
+
spouse), must be followed by %exp tier (see below)
|
| 764 |
+
&+sounds phonological fragment (&+sh &+w we came home)
|
| 765 |
+
|
| 766 |
+
Scoped Symbols
|
| 767 |
+
[: text] target/intended word for errors (e.g., tried [: cried])
|
| 768 |
+
[*] error (e.g., paraphasia -- wɛk@u [: wet] [*])
|
| 769 |
+
[/] retracing without correction (e.g., simple repetition)
|
| 770 |
+
put repeated items between <> unless only one word was repeated
|
| 771 |
+
[//] retracing with correction (e.g., simple word or grammar change)
|
| 772 |
+
put changed items between <> unless only one word was changed
|
| 773 |
+
"""
|
| 774 |
+
|
| 775 |
# Instructions for the LLM analysis
|
| 776 |
instructions = """
|
| 777 |
+
You are a speech pathologist analyzing this transcription sample. Provide a detailed analysis focused on specific quotes from the transcript.
|
| 778 |
+
|
| 779 |
+
The factors of speech that you need to count are:
|
| 780 |
+
|
| 781 |
+
1. Difficulty producing fluent, grammatical speech - speech that is slow, halting, with pauses while searching for words
|
| 782 |
+
2. Word retrieval issues - trouble thinking of specific words, use of filler words like um, circumlocution, semantically similar word substitutions
|
| 783 |
+
3. Grammatical errors - missing/incorrect function words, problems with verb tenses, conjugation, agreement, simplified sentences
|
| 784 |
+
4. Repetitions and revisions - repeating or restating words, phrases or sentences due to trouble finding the right words
|
| 785 |
+
5. Neologisms - creating nonexistent "new" words
|
| 786 |
+
6. Perseveration - unintentionally repeating words or phrases over and over
|
| 787 |
+
7. Comprehension issues - trouble understanding complex sentences, fast speech, relying more on context and cues
|
| 788 |
+
|
| 789 |
+
For each factor, provide:
|
| 790 |
+
- Number of occurrences
|
| 791 |
+
- Severity percentile (estimate based on your clinical judgment)
|
| 792 |
+
- At least 2-3 specific quotes from the transcript as examples
|
| 793 |
+
|
| 794 |
+
Then evaluate using the CASL-2 Speech and Language Analysis Framework across these domains:
|
| 795 |
+
|
| 796 |
+
1. Lexical/Semantic Skills:
|
| 797 |
+
- Assess vocabulary diversity, word-finding abilities, semantic precision
|
| 798 |
+
- Provide Standard Score (mean=100, SD=15), percentile rank, and performance level
|
| 799 |
+
- Include SPECIFIC QUOTES as evidence
|
| 800 |
+
|
| 801 |
+
2. Syntactic Skills:
|
| 802 |
+
- Evaluate grammatical accuracy, sentence complexity, morphological skills
|
| 803 |
+
- Provide Standard Score, percentile rank, and performance level
|
| 804 |
+
- Include SPECIFIC QUOTES as evidence
|
| 805 |
+
|
| 806 |
+
3. Supralinguistic Skills:
|
| 807 |
+
- Assess figurative language use, inferencing, and abstract reasoning
|
| 808 |
+
- Provide Standard Score, percentile rank, and performance level
|
| 809 |
+
- Include SPECIFIC QUOTES as evidence
|
| 810 |
+
|
| 811 |
+
YOUR RESPONSE MUST USE THESE EXACT SECTION MARKERS FOR PARSING:
|
| 812 |
+
|
| 813 |
+
<SPEECH_FACTORS_START>
|
| 814 |
+
Difficulty producing fluent, grammatical speech: (occurrences), (percentile)
|
| 815 |
+
Examples:
|
| 816 |
+
- "(direct quote from transcript)"
|
| 817 |
+
- "(direct quote from transcript)"
|
| 818 |
+
|
| 819 |
+
Word retrieval issues: (occurrences), (percentile)
|
| 820 |
+
Examples:
|
| 821 |
+
- "(direct quote from transcript)"
|
| 822 |
+
- "(direct quote from transcript)"
|
| 823 |
+
|
| 824 |
+
(And so on for each factor)
|
| 825 |
+
<SPEECH_FACTORS_END>
|
| 826 |
+
|
| 827 |
+
<CASL_SKILLS_START>
|
| 828 |
+
Lexical/Semantic Skills: Standard Score (X), Percentile Rank (X%), Performance Level
|
| 829 |
+
Examples:
|
| 830 |
+
- "(direct quote showing strength or weakness)"
|
| 831 |
+
- "(direct quote showing strength or weakness)"
|
| 832 |
+
|
| 833 |
+
Syntactic Skills: Standard Score (X), Percentile Rank (X%), Performance Level
|
| 834 |
+
Examples:
|
| 835 |
+
- "(direct quote showing strength or weakness)"
|
| 836 |
+
- "(direct quote showing strength or weakness)"
|
| 837 |
+
|
| 838 |
+
Supralinguistic Skills: Standard Score (X), Percentile Rank (X%), Performance Level
|
| 839 |
+
Examples:
|
| 840 |
+
- "(direct quote showing strength or weakness)"
|
| 841 |
+
- "(direct quote showing strength or weakness)"
|
| 842 |
+
<CASL_SKILLS_END>
|
| 843 |
+
|
| 844 |
+
<TREATMENT_RECOMMENDATIONS_START>
|
| 845 |
+
- (treatment recommendation)
|
| 846 |
+
- (treatment recommendation)
|
| 847 |
+
- (treatment recommendation)
|
| 848 |
+
<TREATMENT_RECOMMENDATIONS_END>
|
| 849 |
+
|
| 850 |
+
<EXPLANATION_START>
|
| 851 |
+
(brief diagnostic rationale based on findings)
|
| 852 |
+
<EXPLANATION_END>
|
| 853 |
+
|
| 854 |
+
<ADDITIONAL_ANALYSIS_START>
|
| 855 |
+
(specific insights that would be helpful for treatment planning)
|
| 856 |
+
<ADDITIONAL_ANALYSIS_END>
|
| 857 |
+
|
| 858 |
+
<DIAGNOSTIC_IMPRESSIONS_START>
|
| 859 |
+
(summarize findings across domains using specific examples and clear explanations)
|
| 860 |
+
<DIAGNOSTIC_IMPRESSIONS_END>
|
| 861 |
+
|
| 862 |
+
<ERROR_EXAMPLES_START>
|
| 863 |
+
(Copy all the specific quote examples here again, organized by error type or skill domain)
|
| 864 |
+
<ERROR_EXAMPLES_END>
|
| 865 |
+
|
| 866 |
+
MOST IMPORTANT:
|
| 867 |
+
1. Use EXACTLY the section markers provided (like <SPEECH_FACTORS_START>) to make parsing reliable
|
| 868 |
+
2. For EVERY factor and domain you analyze, you MUST provide direct quotes from the transcript as evidence
|
| 869 |
+
3. Be very specific and cite the exact text
|
| 870 |
+
4. Do not omit any of the required sections
|
| 871 |
+
"""
|
| 872 |
|
| 873 |
+
# Prepare prompt for Claude with the user's role context
|
| 874 |
+
role_context = """
|
| 875 |
+
You are a speech pathologist, a healthcare professional who specializes in evaluating, diagnosing, and treating communication disorders, including speech, language, cognitive-communication, voice, swallowing, and fluency disorders. Your role is to help patients improve their speech and communication skills through various therapeutic techniques and exercises.
|
| 876 |
+
|
| 877 |
+
You are working with a student with speech impediments.
|
| 878 |
+
|
| 879 |
+
The most important thing is that you stay kind to the child. Be constructive and helpful rather than critical.
|
| 880 |
"""
|
| 881 |
|
|
|
|
| 882 |
prompt = f"""
|
| 883 |
+
{role_context}
|
| 884 |
+
|
| 885 |
+
You are analyzing a transcript for a patient who is {age} years old and {gender}.
|
| 886 |
|
| 887 |
TRANSCRIPT:
|
| 888 |
{transcript}
|
| 889 |
|
| 890 |
+
{cheat_sheet}
|
| 891 |
+
|
| 892 |
{instructions}
|
| 893 |
|
| 894 |
+
Remember to be precise but compassionate in your analysis. Use direct quotes from the transcript for every factor and domain you analyze.
|
| 895 |
"""
|
| 896 |
|
| 897 |
# Call the appropriate API or fallback to demo mode
|
|
|
|
| 1285 |
with gr.Tabs() as results_tabs:
|
| 1286 |
# Summary tab
|
| 1287 |
with gr.TabItem("Summary", id=0, elem_classes="tab-content"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1288 |
with gr.Group():
|
| 1289 |
gr.Markdown("### Key Findings", elem_classes="markdown-text")
|
| 1290 |
+
speech_factors_md = gr.Markdown(elem_classes="markdown-text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1291 |
|
| 1292 |
+
with gr.Accordion("CASL Assessment Results", open=True):
|
| 1293 |
+
casl_results_md = gr.Markdown(elem_classes="markdown-text")
|
| 1294 |
+
|
| 1295 |
+
with gr.Accordion("Detailed Error Examples", open=True):
|
| 1296 |
specific_errors_md = gr.Markdown(elem_classes="markdown-text")
|
| 1297 |
|
| 1298 |
# Treatment tab
|
|
|
|
| 1615 |
def on_analyze_click(transcript_text, age_val, gender_val, patient_name_val, record_id_val, clinician_val, assessment_date_val):
|
| 1616 |
if not transcript_text or len(transcript_text.strip()) < 50:
|
| 1617 |
return (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1618 |
"Error: Please provide a longer transcript for analysis.",
|
| 1619 |
"The transcript is too short for meaningful analysis.",
|
| 1620 |
"Please provide a speech sample with at least 50 characters.",
|
| 1621 |
+
"Error: Insufficient data",
|
| 1622 |
+
"Please provide a speech sample with at least 50 characters.",
|
| 1623 |
"",
|
| 1624 |
"",
|
| 1625 |
""
|
| 1626 |
)
|
| 1627 |
|
| 1628 |
try:
|
| 1629 |
+
# Get the raw analysis response
|
| 1630 |
+
results, _, _, full_text = analyze_transcript(transcript_text, age_val, gender_val)
|
| 1631 |
+
|
| 1632 |
+
# Extract speech factors section using section markers
|
| 1633 |
+
speech_factors_section = ""
|
| 1634 |
+
factors_pattern = re.compile(r"<SPEECH_FACTORS_START>(.*?)<SPEECH_FACTORS_END>", re.DOTALL)
|
| 1635 |
+
factors_match = factors_pattern.search(full_text)
|
| 1636 |
+
|
| 1637 |
+
if factors_match:
|
| 1638 |
+
speech_factors_section = factors_match.group(1).strip()
|
| 1639 |
+
else:
|
| 1640 |
+
# Fallback to old pattern if markers aren't found
|
| 1641 |
+
old_factors_pattern = re.compile(r"(Difficulty producing fluent.*?)(?:Evaluation of CASL Skills|<CASL_SKILLS_START>)", re.DOTALL)
|
| 1642 |
+
old_factors_match = old_factors_pattern.search(full_text)
|
| 1643 |
+
if old_factors_match:
|
| 1644 |
+
speech_factors_section = old_factors_match.group(1).strip()
|
| 1645 |
+
else:
|
| 1646 |
+
speech_factors_section = "Error extracting speech factors from analysis."
|
| 1647 |
|
| 1648 |
+
# Extract CASL skills section
|
| 1649 |
+
casl_section = ""
|
| 1650 |
+
casl_pattern = re.compile(r"<CASL_SKILLS_START>(.*?)<CASL_SKILLS_END>", re.DOTALL)
|
| 1651 |
+
casl_match = casl_pattern.search(full_text)
|
| 1652 |
+
|
| 1653 |
+
if casl_match:
|
| 1654 |
+
casl_section = casl_match.group(1).strip()
|
| 1655 |
+
else:
|
| 1656 |
+
# Fallback pattern
|
| 1657 |
+
old_casl_pattern = re.compile(r"(?:Evaluation of CASL Skills:|Lexical/Semantic Skills:)(.*?)(?:Other analysis/Best plans of action:|<TREATMENT_RECOMMENDATIONS_START>)", re.DOTALL)
|
| 1658 |
+
old_casl_match = old_casl_pattern.search(full_text)
|
| 1659 |
+
if old_casl_match:
|
| 1660 |
+
casl_section = old_casl_match.group(1).strip()
|
| 1661 |
+
# Add a header if it's missing
|
| 1662 |
+
if not casl_section.startswith("Lexical"):
|
| 1663 |
+
casl_section = "Evaluation of CASL Skills:\n\n" + casl_section
|
| 1664 |
+
else:
|
| 1665 |
+
casl_section = "Error extracting CASL skills from analysis."
|
| 1666 |
+
|
| 1667 |
+
# Extract treatment recommendations
|
| 1668 |
treatment_text = ""
|
| 1669 |
+
treatment_pattern = re.compile(r"<TREATMENT_RECOMMENDATIONS_START>(.*?)<TREATMENT_RECOMMENDATIONS_END>", re.DOTALL)
|
| 1670 |
+
treatment_match = treatment_pattern.search(full_text)
|
| 1671 |
+
|
| 1672 |
+
if treatment_match:
|
| 1673 |
+
treatment_text = "### Treatment Recommendations\n\n" + treatment_match.group(1).strip()
|
| 1674 |
+
else:
|
| 1675 |
+
# Fallback pattern
|
| 1676 |
+
old_treatment_pattern = re.compile(r"(?:Other analysis/Best plans of action:)(.*?)(?:Explanation:|<EXPLANATION_START>)", re.DOTALL)
|
| 1677 |
+
old_treatment_match = old_treatment_pattern.search(full_text)
|
| 1678 |
+
if old_treatment_match:
|
| 1679 |
+
treatment_text = "### Treatment Recommendations\n\n" + old_treatment_match.group(1).strip()
|
| 1680 |
+
elif 'treatment_suggestions' in results:
|
| 1681 |
+
treatment_text = "### Treatment Recommendations\n\n"
|
| 1682 |
+
for suggestion in results['treatment_suggestions']:
|
| 1683 |
+
treatment_text += f"- {suggestion}\n"
|
| 1684 |
+
|
| 1685 |
+
# Extract explanation section
|
| 1686 |
+
explanation_text = "### Clinical Rationale\n\n"
|
| 1687 |
+
explanation_pattern = re.compile(r"<EXPLANATION_START>(.*?)<EXPLANATION_END>", re.DOTALL)
|
| 1688 |
+
explanation_match = explanation_pattern.search(full_text)
|
| 1689 |
+
|
| 1690 |
+
if explanation_match:
|
| 1691 |
+
explanation_text += explanation_match.group(1).strip()
|
| 1692 |
+
else:
|
| 1693 |
+
# Fallback pattern
|
| 1694 |
+
old_explanation_pattern = re.compile(r"(?:Explanation:)(.*?)(?:Additional Analysis:|<ADDITIONAL_ANALYSIS_START>)", re.DOTALL)
|
| 1695 |
+
old_explanation_match = old_explanation_pattern.search(full_text)
|
| 1696 |
+
if old_explanation_match:
|
| 1697 |
+
explanation_text += old_explanation_match.group(1).strip()
|
| 1698 |
+
else:
|
| 1699 |
+
explanation_text += results.get('explanation', "No explanation provided.")
|
| 1700 |
+
|
| 1701 |
+
# Extract additional analysis
|
| 1702 |
+
additional_analysis = ""
|
| 1703 |
+
additional_pattern = re.compile(r"<ADDITIONAL_ANALYSIS_START>(.*?)<ADDITIONAL_ANALYSIS_END>", re.DOTALL)
|
| 1704 |
+
additional_match = additional_pattern.search(full_text)
|
| 1705 |
+
|
| 1706 |
+
if additional_match:
|
| 1707 |
+
additional_analysis = additional_match.group(1).strip()
|
| 1708 |
+
explanation_text += "\n\n### Additional Analysis\n\n" + additional_analysis
|
| 1709 |
+
else:
|
| 1710 |
+
# Fallback pattern
|
| 1711 |
+
old_additional_pattern = re.compile(r"(?:Additional Analysis:)(.*?)(?:Diagnostic Impressions:|<DIAGNOSTIC_IMPRESSIONS_START>)", re.DOTALL)
|
| 1712 |
+
old_additional_match = old_additional_pattern.search(full_text)
|
| 1713 |
+
if old_additional_match:
|
| 1714 |
+
explanation_text += "\n\n### Additional Analysis\n\n" + old_additional_match.group(1).strip()
|
| 1715 |
+
elif 'additional_analysis' in results:
|
| 1716 |
+
explanation_text += "\n\n### Additional Analysis\n\n" + results.get('additional_analysis', "")
|
| 1717 |
+
|
| 1718 |
+
# Extract diagnostic impressions
|
| 1719 |
+
diagnostic_impressions = ""
|
| 1720 |
+
diagnostic_pattern = re.compile(r"<DIAGNOSTIC_IMPRESSIONS_START>(.*?)<DIAGNOSTIC_IMPRESSIONS_END>", re.DOTALL)
|
| 1721 |
+
diagnostic_match = diagnostic_pattern.search(full_text)
|
| 1722 |
+
|
| 1723 |
+
if diagnostic_match:
|
| 1724 |
+
diagnostic_impressions = diagnostic_match.group(1).strip()
|
| 1725 |
+
# Add to the explanation section
|
| 1726 |
+
explanation_text += "\n\n### Diagnostic Impressions\n\n" + diagnostic_impressions
|
| 1727 |
+
|
| 1728 |
+
# Extract specific error examples
|
| 1729 |
+
specific_errors_text = "## Detailed Error Examples\n\n"
|
| 1730 |
|
| 1731 |
+
# First try the dedicated section
|
| 1732 |
+
errors_pattern = re.compile(r"<ERROR_EXAMPLES_START>(.*?)<ERROR_EXAMPLES_END>", re.DOTALL)
|
| 1733 |
+
errors_match = errors_pattern.search(full_text)
|
| 1734 |
+
|
| 1735 |
+
if errors_match:
|
| 1736 |
+
specific_errors_text += errors_match.group(1).strip()
|
|
|
|
|
|
|
| 1737 |
else:
|
| 1738 |
+
# Fallback to extracting examples from the text
|
| 1739 |
+
example_sections = re.findall(r"Examples:\s*\n((?:- \".*\"\s*\n)+)", full_text)
|
| 1740 |
+
for section in example_sections:
|
| 1741 |
+
specific_errors_text += section + "\n"
|
| 1742 |
+
|
| 1743 |
+
if not example_sections:
|
| 1744 |
+
specific_errors_text += "No specific error examples were found in the analysis."
|
| 1745 |
|
| 1746 |
# Save the record to storage
|
| 1747 |
patient_info = {
|
|
|
|
| 1791 |
else:
|
| 1792 |
full_report = f"## Complete Analysis Report\n\n{full_text}"
|
| 1793 |
|
| 1794 |
+
# Get the raw LLM response for debugging
|
| 1795 |
+
raw_output = full_text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1796 |
|
| 1797 |
return (
|
| 1798 |
+
speech_factors_section,
|
| 1799 |
+
casl_section,
|
|
|
|
|
|
|
| 1800 |
treatment_text,
|
| 1801 |
+
explanation_text,
|
| 1802 |
full_report,
|
| 1803 |
save_message,
|
| 1804 |
specific_errors_text,
|
|
|
|
| 1808 |
logger.exception("Error during analysis")
|
| 1809 |
error_message = f"Error during analysis: {str(e)}"
|
| 1810 |
return (
|
| 1811 |
+
f"Error: {str(e)}",
|
| 1812 |
+
"Error: Analysis failed. Please check input data.",
|
| 1813 |
+
"Error: Treatment analysis not available.",
|
|
|
|
|
|
|
| 1814 |
"An error occurred while processing the transcript.",
|
| 1815 |
f"Error details: {str(e)}",
|
| 1816 |
"",
|
|
|
|
| 1825 |
patient_name, record_id, clinician_name, assessment_date
|
| 1826 |
],
|
| 1827 |
outputs=[
|
| 1828 |
+
speech_factors_md,
|
| 1829 |
+
casl_results_md,
|
|
|
|
|
|
|
| 1830 |
treatment_md,
|
| 1831 |
explanation_md,
|
| 1832 |
full_analysis,
|