File size: 5,789 Bytes
da890b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import gradio as gr
from transformers import pipeline

"""
Psychotherapy Session Summarizer (Open-Source Implementation)

This is a Hugging Face Spaces-compatible Gradio app that:
- Takes psychotherapy session transcripts (as text input).
- Summarizes key themes, emotional tones, and patterns.
- Optionally allows custom instructions or focus areas (e.g., "Focus on client's progress since last session").
- Utilizes open-source models only.

Modular:
- Summarization model can be swapped easily.
- Sentiment analysis model can be changed if desired.

Scalable:
- Can be extended to handle multiple transcripts and clustering by topic.

Note: 
All used models are open-source and freely available on Hugging Face:
- Summarization model: "google/flan-t5-small" (FLAN-T5 is open-sourced by Google)
- Sentiment model: "cardiffnlp/twitter-roberta-base-sentiment-latest" (open-source sentiment analysis)

You can adapt these to different models if needed, as long as they are open-source.

Instructions:
1. Paste or upload a session transcript (preferably a few paragraphs of conversation).
2. (Optional) Provide a custom instruction or focus question, e.g., "Highlight moments of cognitive reframing."
3. Click "Summarize" to generate a concise summary with themes and emotional insights.

"""

# Initialize pipelines
summarizer = pipeline("text2text-generation", model="google/flan-t5-small", tokenizer="google/flan-t5-small")
# Note: "text2text-generation" pipeline for FLAN-T5 also works for summarization if we prompt it properly.
sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")

def analyze_session(transcript, custom_instruction):
    # Basic input validation
    if not transcript.strip():
        return "Please provide a transcript."
    
    # Construct a prompt for the summarization model
    # FLAN-T5 is instruction tuned, so we can give it instructions directly.
    # For example:
    prompt = "Summarize the following psychotherapy session transcript, focusing on key themes, emotional shifts, and patterns."
    if custom_instruction.strip():
        prompt += " Additionally, " + custom_instruction.strip()
    prompt += "\n\nTranscript:\n" + transcript.strip()
    
    # Generate summary
    summary_output = summarizer(prompt, max_length=200, do_sample=False)
    summary = summary_output[0]['generated_text'].strip()
    
    # Sentiment analysis: We'll run it on the entire transcript to gauge the overall emotional tone.
    sentiment_results = sentiment_analyzer(transcript)
    # The sentiment model returns something like: [{'label': 'positive', 'score': ...}]
    # We'll aggregate the results (though it's a single input) and just pick the top.
    main_sentiment = sentiment_results[0]['label']
    
    # Construct a more informative result
    # You could elaborate this logic: detect recurring concerns by simple keyword frequency analysis, etc.
    # For a simple first iteration, just provide summary and sentiment.
    
    # Optional: Identify recurring concerns (simple keyword extraction)
    # We'll do a naive keyword frequency approach just as a demonstration:
    words = transcript.lower().split()
    # Common therapy-related words (just a naive approach, could be replaced by a proper keyword extraction model)
    # This is a placeholder for demonstration
    keywords_of_interest = ["anxiety", "depression", "relationship", "stress", "fear", "goals", "progress", "cognitive", "behavior"]
    recurring_concerns = [word for word in words if word in keywords_of_interest]
    recurring_concerns = list(set(recurring_concerns))  # unique
    if not recurring_concerns:
        recurring_concerns_str = "No specific recurring concerns identified from the predefined list."
    else:
        recurring_concerns_str = "Recurring concerns include: " + ", ".join(recurring_concerns)
    
    # Recommended follow-up topics (just a heuristic based on summary)
    # If certain keywords appear in summary, we can suggest follow-up:
    follow_up_suggestions = []
    if "progress" in summary.lower():
        follow_up_suggestions.append("Explore client's perception of progress in more detail.")
    if "relationship" in summary.lower():
        follow_up_suggestions.append("Discuss client's relationship dynamics further.")
    if not follow_up_suggestions:
        follow_up_suggestions.append("Consider following up on the emotional themes identified in the summary.")
    
    follow_up_suggestions_str = " ".join(follow_up_suggestions)
    
    # Combine results into a final output
    final_output = f"**Summary of Session:**\n{summary}\n\n**Overall Sentiment:** {main_sentiment}\n\n**{recurring_concerns_str}**\n\n**Suggested Follow-Up Topics:** {follow_up_suggestions_str}"
    
    return final_output

# Build Gradio UI
description = """# Psychotherapy Session Summarizer

Upload or paste your psychotherapy session transcript and optionally provide a custom instruction (e.g., "Focus on anxiety and coping strategies.").  
Click 'Summarize' to generate a concise summary of key themes, emotional tones, recurring concerns, and suggested follow-up topics.
"""

with gr.Blocks() as demo:
    gr.Markdown(description)
    with gr.Row():
        transcript_input = gr.Textbox(label="Session Transcript", lines=10, placeholder="Paste the session transcript here...")
    custom_instruction_input = gr.Textbox(label="Custom Instruction (Optional)", placeholder="e.g., Focus on how the client describes their feelings about progress.")
    summarize_button = gr.Button("Summarize")
    output_box = gr.Markdown()
    
    summarize_button.click(fn=analyze_session, inputs=[transcript_input, custom_instruction_input], outputs=output_box)

if __name__ == "__main__":
    demo.launch()