Spaces:
Sleeping
Sleeping
import gradio as gr | |
import boto3 | |
import json | |
import re | |
import logging | |
import os | |
import tempfile | |
import shutil | |
from datetime import datetime | |
# Try to import ReportLab (needed for PDF generation) | |
try: | |
from reportlab.lib.pagesizes import letter | |
from reportlab.lib import colors | |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle | |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle | |
REPORTLAB_AVAILABLE = True | |
except ImportError: | |
logger.warning("ReportLab library not available - PDF export will be disabled") | |
REPORTLAB_AVAILABLE = False | |
# Configure logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# AWS credentials for Bedrock API | |
AWS_ACCESS_KEY = os.getenv("AWS_ACCESS_KEY", "") | |
AWS_SECRET_KEY = os.getenv("AWS_SECRET_KEY", "") | |
AWS_REGION = os.getenv("AWS_REGION", "us-east-1") | |
# Initialize Bedrock client if credentials are available | |
bedrock_client = None | |
if AWS_ACCESS_KEY and AWS_SECRET_KEY: | |
try: | |
bedrock_client = boto3.client( | |
'bedrock-runtime', | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_KEY, | |
region_name=AWS_REGION | |
) | |
logger.info("Bedrock client initialized successfully") | |
except Exception as e: | |
logger.error(f"Failed to initialize Bedrock client: {str(e)}") | |
# Create data directories if they don't exist | |
DATA_DIR = os.environ.get("DATA_DIR", "patient_data") | |
DOWNLOADS_DIR = os.path.join(DATA_DIR, "downloads") | |
def ensure_data_dirs(): | |
"""Ensure data directories exist""" | |
try: | |
os.makedirs(DATA_DIR, exist_ok=True) | |
os.makedirs(DOWNLOADS_DIR, exist_ok=True) | |
logger.info(f"Data directories created: {DATA_DIR}, {DOWNLOADS_DIR}") | |
except Exception as e: | |
logger.warning(f"Could not create data directories: {str(e)}") | |
# Fallback to tmp directory on HF Spaces | |
global DOWNLOADS_DIR | |
DOWNLOADS_DIR = os.path.join(tempfile.gettempdir(), "casl_downloads") | |
os.makedirs(DOWNLOADS_DIR, exist_ok=True) | |
logger.info(f"Using fallback directory: {DOWNLOADS_DIR}") | |
# Initialize data directories | |
ensure_data_dirs() | |
# Sample transcript for the demo | |
SAMPLE_TRANSCRIPT = """*PAR: today I would &-um like to talk about &-um a fun trip I took last &-um summer with my family. | |
*PAR: we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually. | |
*PAR: there was lots of &-um &-um swimming and &-um sun. | |
*PAR: we [/] we stayed for &-um three no [//] four days in a &-um hotel near the water [: ocean] [*]. | |
*PAR: my favorite part was &-um building &-um castles with sand. | |
*PAR: sometimes I forget [//] forgetted [: forgot] [*] what they call those things we built. | |
*PAR: my brother he [//] he helped me dig a big hole. | |
*PAR: we saw [/] saw fishies [: fish] [*] swimming in the water. | |
*PAR: sometimes I wonder [/] wonder where fishies [: fish] [*] go when it's cold. | |
*PAR: maybe they have [/] have houses under the water. | |
*PAR: after swimming we [//] I eat [: ate] [*] &-um ice cream with &-um chocolate things on top. | |
*PAR: what do you call those &-um &-um sprinkles! that's the word. | |
*PAR: my mom said to &-um that I could have &-um two scoops next time. | |
*PAR: I want to go back to the beach [/] beach next year.""" | |
def read_cha_file(file_path): | |
"""Read and parse a .cha transcript file""" | |
try: | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
content = f.read() | |
# Extract participant lines (starting with *PAR:) | |
par_lines = [] | |
for line in content.splitlines(): | |
if line.startswith('*PAR:'): | |
par_lines.append(line) | |
# If no PAR lines found, just return the whole content | |
if not par_lines: | |
return content | |
return '\n'.join(par_lines) | |
except Exception as e: | |
logger.error(f"Error reading CHA file: {str(e)}") | |
return "" | |
def process_upload(file): | |
"""Process an uploaded file (PDF, text, or CHA)""" | |
if file is None: | |
return "" | |
file_path = file.name | |
if file_path.endswith('.pdf'): | |
# For PDF, we would need PyPDF2 or similar | |
return "PDF upload not supported in this simple version" | |
elif file_path.endswith('.cha'): | |
return read_cha_file(file_path) | |
else: | |
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
return f.read() | |
def call_bedrock(prompt, max_tokens=4096): | |
"""Call the AWS Bedrock API to analyze text using Claude""" | |
if not bedrock_client: | |
return "AWS credentials not configured. Using demo response instead." | |
try: | |
body = json.dumps({ | |
"anthropic_version": "bedrock-2023-05-31", | |
"max_tokens": max_tokens, | |
"messages": [ | |
{ | |
"role": "user", | |
"content": prompt | |
} | |
], | |
"temperature": 0.3, | |
"top_p": 0.9 | |
}) | |
modelId = 'anthropic.claude-3-sonnet-20240229-v1:0' | |
response = bedrock_client.invoke_model( | |
body=body, | |
modelId=modelId, | |
accept='application/json', | |
contentType='application/json' | |
) | |
response_body = json.loads(response.get('body').read()) | |
return response_body['content'][0]['text'] | |
except Exception as e: | |
logger.error(f"Error in call_bedrock: {str(e)}") | |
return f"Error: {str(e)}" | |
def generate_demo_response(prompt): | |
"""Generate a simulated response for demo purposes""" | |
# This function generates a realistic but fake response for demo purposes | |
# In a real deployment, you would call an actual LLM API | |
return """<SPEECH_FACTORS_START> | |
Difficulty producing fluent speech: 8, 65 | |
Examples: | |
- "today I would &-um like to talk about &-um a fun trip I took last &-um summer with my family" | |
- "we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually" | |
Word retrieval issues: 6, 72 | |
Examples: | |
- "what do you call those &-um &-um sprinkles! that's the word" | |
- "sometimes I forget [//] forgetted [: forgot] [*] what they call those things we built" | |
Grammatical errors: 4, 58 | |
Examples: | |
- "after swimming we [//] I eat [: ate] [*] &-um ice cream" | |
- "sometimes I forget [//] forgetted [: forgot] [*] what they call those things we built" | |
Repetitions and revisions: 5, 62 | |
Examples: | |
- "we [/] we stayed for &-um three no [//] four days" | |
- "we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually" | |
<SPEECH_FACTORS_END> | |
<CASL_SKILLS_START> | |
Lexical/Semantic Skills: Standard Score (92), Percentile Rank (30%), Average Performance | |
Examples: | |
- "what do you call those &-um &-um sprinkles! that's the word" | |
- "we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually" | |
Syntactic Skills: Standard Score (87), Percentile Rank (19%), Low Average Performance | |
Examples: | |
- "my brother he [//] he helped me dig a big hole" | |
- "after swimming we [//] I eat [: ate] [*] &-um ice cream with &-um chocolate things on top" | |
Supralinguistic Skills: Standard Score (90), Percentile Rank (25%), Average Performance | |
Examples: | |
- "sometimes I wonder [/] wonder where fishies [: fish] [*] go when it's cold" | |
- "maybe they have [/] have houses under the water" | |
<CASL_SKILLS_END> | |
<TREATMENT_RECOMMENDATIONS_START> | |
- Implement word-finding strategies with semantic cuing focused on everyday objects and activities, using the patient's beach experience as a context (e.g., "sprinkles," "castles") | |
- Practice structured narrative tasks with visual supports to reduce revisions and improve sequencing | |
- Use sentence formulation exercises focusing on verb tense consistency (addressing errors like "forgetted" and "eat" for "ate") | |
- Incorporate self-monitoring techniques to help identify and correct grammatical errors | |
- Work on increasing vocabulary specificity (e.g., "things on top" to "sprinkles") | |
<TREATMENT_RECOMMENDATIONS_END> | |
<EXPLANATION_START> | |
This child demonstrates moderate word-finding difficulties with compensatory strategies including fillers ("&-um") and repetitions. The frequent use of self-corrections shows good metalinguistic awareness, but the pauses and repairs impact conversational fluency. Syntactic errors primarily involve verb tense inconsistency. Overall, the pattern suggests a mild-to-moderate language disorder with stronger receptive than expressive skills. | |
<EXPLANATION_END> | |
<ADDITIONAL_ANALYSIS_START> | |
The child shows relative strengths in maintaining topic coherence and conveying a complete narrative structure despite the language challenges. The pattern of errors suggests that word-finding difficulties and processing speed are primary concerns rather than conceptual or cognitive issues. Semantic network activities that strengthen word associations would likely be beneficial, particularly when paired with visual supports. | |
<ADDITIONAL_ANALYSIS_END> | |
<DIAGNOSTIC_IMPRESSIONS_START> | |
Based on the language sample, this child presents with a profile consistent with a mild-to-moderate expressive language disorder. The most prominent features include: | |
1. Word-finding difficulties characterized by fillers, pauses, and self-corrections when attempting to retrieve specific vocabulary | |
2. Grammatical challenges primarily affecting verb tense consistency and morphological markers | |
3. Relatively intact narrative structure and topic maintenance | |
These findings suggest intervention should focus on word retrieval strategies, grammatical form practice, and continued support for narrative development, with an emphasis on fluency and self-monitoring. | |
<DIAGNOSTIC_IMPRESSIONS_END> | |
<ERROR_EXAMPLES_START> | |
Word-finding difficulties: | |
- "what do you call those &-um &-um sprinkles! that's the word" | |
- "we went to the &-um &-um beach [//] no to the mountains [//] I mean the beach actually" | |
- "there was lots of &-um &-um swimming and &-um sun" | |
Grammatical errors: | |
- "after swimming we [//] I eat [: ate] [*] &-um ice cream" | |
- "sometimes I forget [//] forgetted [: forgot] [*] what they call those things we built" | |
- "we saw [/] saw fishies [: fish] [*] swimming in the water" | |
Repetitions and revisions: | |
- "we [/] we stayed for &-um three no [//] four days" | |
- "I want to go back to the beach [/] beach next year" | |
- "sometimes I wonder [/] wonder where fishies [: fish] [*] go when it's cold" | |
<ERROR_EXAMPLES_END>""" | |
def parse_casl_response(response): | |
"""Parse the LLM response for CASL analysis into structured data""" | |
# Extract speech factors section using section markers | |
speech_factors_section = "" | |
factors_pattern = re.compile(r"<SPEECH_FACTORS_START>(.*?)<SPEECH_FACTORS_END>", re.DOTALL) | |
factors_match = factors_pattern.search(response) | |
if factors_match: | |
speech_factors_section = factors_match.group(1).strip() | |
else: | |
speech_factors_section = "Error extracting speech factors from analysis." | |
# Extract CASL skills section | |
casl_section = "" | |
casl_pattern = re.compile(r"<CASL_SKILLS_START>(.*?)<CASL_SKILLS_END>", re.DOTALL) | |
casl_match = casl_pattern.search(response) | |
if casl_match: | |
casl_section = casl_match.group(1).strip() | |
else: | |
casl_section = "Error extracting CASL skills from analysis." | |
# Extract treatment recommendations | |
treatment_text = "" | |
treatment_pattern = re.compile(r"<TREATMENT_RECOMMENDATIONS_START>(.*?)<TREATMENT_RECOMMENDATIONS_END>", re.DOTALL) | |
treatment_match = treatment_pattern.search(response) | |
if treatment_match: | |
treatment_text = treatment_match.group(1).strip() | |
else: | |
treatment_text = "Error extracting treatment recommendations from analysis." | |
# Extract explanation section | |
explanation_text = "" | |
explanation_pattern = re.compile(r"<EXPLANATION_START>(.*?)<EXPLANATION_END>", re.DOTALL) | |
explanation_match = explanation_pattern.search(response) | |
if explanation_match: | |
explanation_text = explanation_match.group(1).strip() | |
else: | |
explanation_text = "Error extracting clinical explanation from analysis." | |
# Extract additional analysis | |
additional_analysis = "" | |
additional_pattern = re.compile(r"<ADDITIONAL_ANALYSIS_START>(.*?)<ADDITIONAL_ANALYSIS_END>", re.DOTALL) | |
additional_match = additional_pattern.search(response) | |
if additional_match: | |
additional_analysis = additional_match.group(1).strip() | |
# Extract diagnostic impressions | |
diagnostic_impressions = "" | |
diagnostic_pattern = re.compile(r"<DIAGNOSTIC_IMPRESSIONS_START>(.*?)<DIAGNOSTIC_IMPRESSIONS_END>", re.DOTALL) | |
diagnostic_match = diagnostic_pattern.search(response) | |
if diagnostic_match: | |
diagnostic_impressions = diagnostic_match.group(1).strip() | |
# Extract specific error examples | |
specific_errors_text = "" | |
errors_pattern = re.compile(r"<ERROR_EXAMPLES_START>(.*?)<ERROR_EXAMPLES_END>", re.DOTALL) | |
errors_match = errors_pattern.search(response) | |
if errors_match: | |
specific_errors_text = errors_match.group(1).strip() | |
# Create full report text | |
full_report = f""" | |
## Speech Factors Analysis | |
{speech_factors_section} | |
## CASL Skills Assessment | |
{casl_section} | |
## Treatment Recommendations | |
{treatment_text} | |
## Clinical Explanation | |
{explanation_text} | |
""" | |
if additional_analysis: | |
full_report += f"\n## Additional Analysis\n\n{additional_analysis}" | |
if diagnostic_impressions: | |
full_report += f"\n## Diagnostic Impressions\n\n{diagnostic_impressions}" | |
if specific_errors_text: | |
full_report += f"\n## Detailed Error Examples\n\n{specific_errors_text}" | |
return { | |
'speech_factors': speech_factors_section, | |
'casl_data': casl_section, | |
'treatment_suggestions': treatment_text, | |
'explanation': explanation_text, | |
'additional_analysis': additional_analysis, | |
'diagnostic_impressions': diagnostic_impressions, | |
'specific_errors': specific_errors_text, | |
'full_report': full_report, | |
'raw_response': response | |
} | |
def analyze_transcript(transcript, age, gender): | |
"""Analyze a speech transcript using Claude""" | |
# CASL-2 assessment cheat sheet | |
cheat_sheet = """ | |
# Speech-Language Pathologist Analysis Cheat Sheet | |
## Types of Speech Patterns to Identify: | |
1. Difficulty producing fluent, grammatical speech | |
- Fillers (um, uh) and pauses | |
- False starts and revisions | |
- Incomplete sentences | |
2. Word retrieval issues | |
- Pauses before content words | |
- Circumlocutions (talking around a word) | |
- Word substitutions | |
3. Grammatical errors | |
- Verb tense inconsistencies | |
- Subject-verb agreement errors | |
- Morphological errors (plurals, possessives) | |
4. Repetitions and revisions | |
- Word or phrase repetitions [/] | |
- Self-corrections [//] | |
- Retracing | |
5. Neologisms | |
- Made-up words | |
- Word blends | |
6. Perseveration | |
- Inappropriate repetition of ideas | |
- Recurring themes | |
7. Comprehension issues | |
- Topic maintenance difficulties | |
- Non-sequiturs | |
- Inappropriate responses | |
""" | |
# Instructions for the analysis | |
instructions = """ | |
Analyze this speech transcript to identify specific patterns and provide a detailed CASL-2 (Comprehensive Assessment of Spoken Language) assessment. | |
For each speech pattern you identify: | |
1. Count the occurrences in the transcript | |
2. Estimate a percentile (how typical/atypical this is for the age) | |
3. Provide DIRECT QUOTES from the transcript as evidence | |
Then assess the following CASL-2 domains: | |
1. Lexical/Semantic Skills: | |
- Assess vocabulary diversity, word-finding abilities, semantic precision | |
- Provide Standard Score (mean=100, SD=15), percentile rank, and performance level | |
- Include SPECIFIC QUOTES as evidence | |
2. Syntactic Skills: | |
- Evaluate grammatical accuracy, sentence complexity, morphological skills | |
- Provide Standard Score, percentile rank, and performance level | |
- Include SPECIFIC QUOTES as evidence | |
3. Supralinguistic Skills: | |
- Assess figurative language use, inferencing, and abstract reasoning | |
- Provide Standard Score, percentile rank, and performance level | |
- Include SPECIFIC QUOTES as evidence | |
YOUR RESPONSE MUST USE THESE EXACT SECTION MARKERS FOR PARSING: | |
<SPEECH_FACTORS_START> | |
Difficulty producing fluent, grammatical speech: (occurrences), (percentile) | |
Examples: | |
- "(direct quote from transcript)" | |
- "(direct quote from transcript)" | |
Word retrieval issues: (occurrences), (percentile) | |
Examples: | |
- "(direct quote from transcript)" | |
- "(direct quote from transcript)" | |
(And so on for each factor) | |
<SPEECH_FACTORS_END> | |
<CASL_SKILLS_START> | |
Lexical/Semantic Skills: Standard Score (X), Percentile Rank (X%), Performance Level | |
Examples: | |
- "(direct quote showing strength or weakness)" | |
- "(direct quote showing strength or weakness)" | |
Syntactic Skills: Standard Score (X), Percentile Rank (X%), Performance Level | |
Examples: | |
- "(direct quote showing strength or weakness)" | |
- "(direct quote showing strength or weakness)" | |
Supralinguistic Skills: Standard Score (X), Percentile Rank (X%), Performance Level | |
Examples: | |
- "(direct quote showing strength or weakness)" | |
- "(direct quote showing strength or weakness)" | |
<CASL_SKILLS_END> | |
<TREATMENT_RECOMMENDATIONS_START> | |
- (treatment recommendation) | |
- (treatment recommendation) | |
- (treatment recommendation) | |
<TREATMENT_RECOMMENDATIONS_END> | |
<EXPLANATION_START> | |
(brief diagnostic rationale based on findings) | |
<EXPLANATION_END> | |
<ADDITIONAL_ANALYSIS_START> | |
(specific insights that would be helpful for treatment planning) | |
<ADDITIONAL_ANALYSIS_END> | |
<DIAGNOSTIC_IMPRESSIONS_START> | |
(summarize findings across domains using specific examples and clear explanations) | |
<DIAGNOSTIC_IMPRESSIONS_END> | |
<ERROR_EXAMPLES_START> | |
(Copy all the specific quote examples here again, organized by error type or skill domain) | |
<ERROR_EXAMPLES_END> | |
MOST IMPORTANT: | |
1. Use EXACTLY the section markers provided (like <SPEECH_FACTORS_START>) to make parsing reliable | |
2. For EVERY factor and domain you analyze, you MUST provide direct quotes from the transcript as evidence | |
3. Be very specific and cite the exact text | |
4. Do not omit any of the required sections | |
""" | |
# Prepare prompt for Claude with the user's role context | |
role_context = """ | |
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. | |
You are working with a student with speech impediments. | |
The most important thing is that you stay kind to the child. Be constructive and helpful rather than critical. | |
""" | |
prompt = f""" | |
{role_context} | |
You are analyzing a transcript for a patient who is {age} years old and {gender}. | |
TRANSCRIPT: | |
{transcript} | |
{cheat_sheet} | |
{instructions} | |
Remember to be precise but compassionate in your analysis. Use direct quotes from the transcript for every factor and domain you analyze. | |
""" | |
# Call the appropriate API or fallback to demo mode | |
if bedrock_client: | |
response = call_bedrock(prompt) | |
else: | |
response = generate_demo_response(prompt) | |
# Parse the response | |
results = parse_casl_response(response) | |
return results | |
def export_pdf(results, patient_name="", record_id="", age="", gender="", assessment_date="", clinician=""): | |
"""Export analysis results to a PDF report""" | |
# Check if ReportLab is available | |
if not REPORTLAB_AVAILABLE: | |
return "ERROR: PDF export is not available - ReportLab library is not installed. Please run 'pip install reportlab'." | |
try: | |
# Generate a safe filename | |
if patient_name: | |
safe_name = f"{patient_name.replace(' ', '_')}" | |
else: | |
safe_name = f"speech_analysis_{datetime.now().strftime('%Y%m%d%H%M%S')}" | |
# Make sure the downloads directory exists | |
try: | |
os.makedirs(DOWNLOADS_DIR, exist_ok=True) | |
except Exception as e: | |
logger.warning(f"Could not access downloads directory: {str(e)}") | |
# Fallback to temp directory | |
global DOWNLOADS_DIR | |
DOWNLOADS_DIR = os.path.join(tempfile.gettempdir(), "casl_downloads") | |
os.makedirs(DOWNLOADS_DIR, exist_ok=True) | |
# Create the PDF path in our downloads directory | |
pdf_path = os.path.join(DOWNLOADS_DIR, f"{safe_name}.pdf") | |
# Create the PDF document | |
doc = SimpleDocTemplate(pdf_path, pagesize=letter) | |
styles = getSampleStyleSheet() | |
# Create enhanced custom styles | |
styles.add(ParagraphStyle( | |
name='Heading1', | |
parent=styles['Heading1'], | |
fontSize=16, | |
spaceAfter=12, | |
textColor=colors.navy | |
)) | |
styles.add(ParagraphStyle( | |
name='Heading2', | |
parent=styles['Heading2'], | |
fontSize=14, | |
spaceAfter=10, | |
spaceBefore=10, | |
textColor=colors.darkblue | |
)) | |
styles.add(ParagraphStyle( | |
name='Heading3', | |
parent=styles['Heading2'], | |
fontSize=12, | |
spaceAfter=8, | |
spaceBefore=8, | |
textColor=colors.darkblue | |
)) | |
styles.add(ParagraphStyle( | |
name='BodyText', | |
parent=styles['BodyText'], | |
fontSize=11, | |
spaceAfter=8, | |
leading=14 | |
)) | |
styles.add(ParagraphStyle( | |
name='BulletPoint', | |
parent=styles['BodyText'], | |
fontSize=11, | |
leftIndent=20, | |
firstLineIndent=-15, | |
spaceAfter=4, | |
leading=14 | |
)) | |
# Convert markdown to PDF elements | |
story = [] | |
# Add title and date | |
story.append(Paragraph("Speech Language Assessment Report", styles['Title'])) | |
story.append(Spacer(1, 12)) | |
# Add patient information table | |
if patient_name or record_id or age or gender: | |
# Prepare patient info data | |
data = [] | |
if patient_name: | |
data.append(["Patient Name:", patient_name]) | |
if record_id: | |
data.append(["Record ID:", record_id]) | |
if age: | |
data.append(["Age:", f"{age} years"]) | |
if gender: | |
data.append(["Gender:", gender]) | |
if assessment_date: | |
data.append(["Assessment Date:", assessment_date]) | |
if clinician: | |
data.append(["Clinician:", clinician]) | |
if data: | |
# Create a table with the data | |
patient_table = Table(data, colWidths=[120, 350]) | |
patient_table.setStyle(TableStyle([ | |
('BACKGROUND', (0, 0), (0, -1), colors.lightgrey), | |
('TEXTCOLOR', (0, 0), (0, -1), colors.darkblue), | |
('ALIGN', (0, 0), (0, -1), 'RIGHT'), | |
('ALIGN', (1, 0), (1, -1), 'LEFT'), | |
('FONTNAME', (0, 0), (0, -1), 'Helvetica-Bold'), | |
('BOTTOMPADDING', (0, 0), (-1, -1), 6), | |
('TOPPADDING', (0, 0), (-1, -1), 6), | |
('GRID', (0, 0), (-1, -1), 0.5, colors.lightgrey), | |
])) | |
story.append(patient_table) | |
story.append(Spacer(1, 12)) | |
# Add clinical analysis sections | |
story.append(Paragraph("Speech Factors Analysis", styles['Heading1'])) | |
speech_factors_paragraphs = [] | |
for line in results['speech_factors'].split('\n'): | |
line = line.strip() | |
if not line: | |
continue | |
if line.startswith('- '): | |
story.append(Paragraph(f"• {line[2:]}", styles['BulletPoint'])) | |
else: | |
story.append(Paragraph(line, styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
story.append(Paragraph("CASL Skills Assessment", styles['Heading1'])) | |
for line in results['casl_data'].split('\n'): | |
line = line.strip() | |
if not line: | |
continue | |
if line.startswith('- '): | |
story.append(Paragraph(f"• {line[2:]}", styles['BulletPoint'])) | |
else: | |
story.append(Paragraph(line, styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
story.append(Paragraph("Treatment Recommendations", styles['Heading1'])) | |
# Process treatment recommendations as bullet points | |
for line in results['treatment_suggestions'].split('\n'): | |
line = line.strip() | |
if not line: | |
continue | |
if line.startswith('- '): | |
story.append(Paragraph(f"• {line[2:]}", styles['BulletPoint'])) | |
else: | |
story.append(Paragraph(line, styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
story.append(Paragraph("Clinical Explanation", styles['Heading1'])) | |
story.append(Paragraph(results['explanation'], styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
if results['additional_analysis']: | |
story.append(Paragraph("Additional Analysis", styles['Heading1'])) | |
story.append(Paragraph(results['additional_analysis'], styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
if results['diagnostic_impressions']: | |
story.append(Paragraph("Diagnostic Impressions", styles['Heading1'])) | |
story.append(Paragraph(results['diagnostic_impressions'], styles['BodyText'])) | |
story.append(Spacer(1, 12)) | |
# Add footer with date | |
footer_text = f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" | |
story.append(Spacer(1, 20)) | |
story.append(Paragraph(footer_text, ParagraphStyle( | |
name='Footer', | |
parent=styles['Normal'], | |
fontSize=8, | |
textColor=colors.grey | |
))) | |
# Build the PDF | |
doc.build(story) | |
logger.info(f"Report saved as PDF: {pdf_path}") | |
return pdf_path | |
except Exception as e: | |
logger.exception("Error creating PDF") | |
return f"Error creating PDF: {str(e)}" | |
def create_interface(): | |
"""Create the Gradio interface""" | |
# Set a theme compatible with Hugging Face Spaces | |
theme = gr.themes.Soft( | |
primary_hue="blue", | |
secondary_hue="indigo", | |
) | |
with gr.Blocks(title="Simple CASL Analysis Tool", theme=theme) as app: | |
gr.Markdown("# CASL Analysis Tool") | |
gr.Markdown("A simplified tool for analyzing speech transcripts using CASL framework") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
# Patient info | |
gr.Markdown("### Patient Information") | |
patient_name = gr.Textbox(label="Patient Name", placeholder="Enter patient name") | |
record_id = gr.Textbox(label="Record ID", placeholder="Enter record ID") | |
with gr.Row(): | |
age = gr.Number(label="Age", value=8, minimum=1, maximum=120) | |
gender = gr.Radio(["male", "female", "other"], label="Gender", value="male") | |
assessment_date = gr.Textbox( | |
label="Assessment Date", | |
placeholder="MM/DD/YYYY", | |
value=datetime.now().strftime('%m/%d/%Y') | |
) | |
clinician_name = gr.Textbox(label="Clinician", placeholder="Enter clinician name") | |
# Transcript input | |
gr.Markdown("### Transcript") | |
sample_btn = gr.Button("Load Sample Transcript") | |
file_upload = gr.File(label="Upload transcript file (.txt or .cha)") | |
transcript = gr.Textbox( | |
label="Speech transcript (CHAT format preferred)", | |
placeholder="Enter transcript text or upload a file...", | |
lines=10 | |
) | |
# Analysis button | |
analyze_btn = gr.Button("Analyze Transcript", variant="primary") | |
with gr.Column(scale=1): | |
# Results display | |
gr.Markdown("### Analysis Results") | |
analysis_output = gr.Markdown(label="Full Analysis") | |
# PDF export (only shown if ReportLab is available) | |
export_status = gr.Markdown("") | |
if REPORTLAB_AVAILABLE: | |
export_btn = gr.Button("Export as PDF", variant="secondary") | |
else: | |
gr.Markdown("⚠️ PDF export is disabled - ReportLab library is not installed") | |
# Load sample transcript button | |
def load_sample(): | |
return SAMPLE_TRANSCRIPT | |
sample_btn.click(load_sample, outputs=[transcript]) | |
# File upload handler | |
file_upload.upload(process_upload, file_upload, transcript) | |
# Analysis button handler | |
def on_analyze_click(transcript_text, age_val, gender_val, patient_name_val, record_id_val, clinician_val, assessment_date_val): | |
if not transcript_text or len(transcript_text.strip()) < 50: | |
return "Error: Please provide a longer transcript for analysis." | |
try: | |
# Get the analysis results | |
results = analyze_transcript(transcript_text, age_val, gender_val) | |
# Return the full report | |
return results['full_report'] | |
except Exception as e: | |
logger.exception("Error during analysis") | |
return f"Error during analysis: {str(e)}" | |
analyze_btn.click( | |
on_analyze_click, | |
inputs=[ | |
transcript, age, gender, | |
patient_name, record_id, clinician_name, assessment_date | |
], | |
outputs=[analysis_output] | |
) | |
# PDF export function | |
def on_export_pdf(report_text, p_name, p_record_id, p_age, p_gender, p_date, p_clinician): | |
# Check if ReportLab is available | |
if not REPORTLAB_AVAILABLE: | |
return "ERROR: PDF export is not available because the ReportLab library is not installed. Please install it with 'pip install reportlab'." | |
if not report_text or len(report_text.strip()) < 50: | |
return "Error: Please run the analysis first before exporting to PDF." | |
try: | |
# Parse the report text back into sections | |
results = { | |
'speech_factors': '', | |
'casl_data': '', | |
'treatment_suggestions': '', | |
'explanation': '', | |
'additional_analysis': '', | |
'diagnostic_impressions': '', | |
} | |
sections = report_text.split('##') | |
for section in sections: | |
section = section.strip() | |
if not section: | |
continue | |
title_content = section.split('\n', 1) | |
if len(title_content) < 2: | |
continue | |
title = title_content[0].strip() | |
content = title_content[1].strip() | |
if "Speech Factors Analysis" in title: | |
results['speech_factors'] = content | |
elif "CASL Skills Assessment" in title: | |
results['casl_data'] = content | |
elif "Treatment Recommendations" in title: | |
results['treatment_suggestions'] = content | |
elif "Clinical Explanation" in title: | |
results['explanation'] = content | |
elif "Additional Analysis" in title: | |
results['additional_analysis'] = content | |
elif "Diagnostic Impressions" in title: | |
results['diagnostic_impressions'] = content | |
pdf_path = export_pdf( | |
results, | |
patient_name=p_name, | |
record_id=p_record_id, | |
age=p_age, | |
gender=p_gender, | |
assessment_date=p_date, | |
clinician=p_clinician | |
) | |
# Check if the export was successful | |
if pdf_path.startswith("ERROR:"): | |
return pdf_path | |
# Make it downloadable in Hugging Face Spaces | |
download_link = f'<a href="file={pdf_path}" download="{os.path.basename(pdf_path)}">Download PDF Report</a>' | |
return f"Report saved as PDF: {pdf_path}<br>{download_link}" | |
except Exception as e: | |
logger.exception("Error exporting to PDF") | |
return f"Error creating PDF: {str(e)}" | |
# Only set up the PDF export button if ReportLab is available | |
if REPORTLAB_AVAILABLE: | |
export_btn.click( | |
on_export_pdf, | |
inputs=[ | |
analysis_output, | |
patient_name, | |
record_id, | |
age, | |
gender, | |
assessment_date, | |
clinician_name | |
], | |
outputs=[export_status] | |
) | |
return app | |
# Create requirements.txt file for HuggingFace Spaces | |
def create_requirements_file(): | |
requirements = [ | |
"gradio>=4.0.0", | |
"pandas", | |
"numpy", | |
"Pillow", | |
"reportlab>=3.6.0", # Required for PDF exports | |
"boto3" | |
] | |
with open("requirements.txt", "w") as f: | |
for req in requirements: | |
f.write(f"{req}\n") | |
if __name__ == "__main__": | |
# Create requirements.txt for HuggingFace Spaces | |
create_requirements_file() | |
# Check for AWS credentials | |
if not AWS_ACCESS_KEY or not AWS_SECRET_KEY: | |
print("NOTE: AWS credentials not found. The app will run in demo mode with simulated responses.") | |
print("To enable full functionality, set AWS_ACCESS_KEY and AWS_SECRET_KEY environment variables.") | |
app = create_interface() | |
app.launch(show_api=False) # Disable API tab for security |