Rogerjs commited on
Commit
da890b5
·
verified ·
1 Parent(s): 0d9a11b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ """
5
+ Psychotherapy Session Summarizer (Open-Source Implementation)
6
+
7
+ This is a Hugging Face Spaces-compatible Gradio app that:
8
+ - Takes psychotherapy session transcripts (as text input).
9
+ - Summarizes key themes, emotional tones, and patterns.
10
+ - Optionally allows custom instructions or focus areas (e.g., "Focus on client's progress since last session").
11
+ - Utilizes open-source models only.
12
+
13
+ Modular:
14
+ - Summarization model can be swapped easily.
15
+ - Sentiment analysis model can be changed if desired.
16
+
17
+ Scalable:
18
+ - Can be extended to handle multiple transcripts and clustering by topic.
19
+
20
+ Note:
21
+ All used models are open-source and freely available on Hugging Face:
22
+ - Summarization model: "google/flan-t5-small" (FLAN-T5 is open-sourced by Google)
23
+ - Sentiment model: "cardiffnlp/twitter-roberta-base-sentiment-latest" (open-source sentiment analysis)
24
+
25
+ You can adapt these to different models if needed, as long as they are open-source.
26
+
27
+ Instructions:
28
+ 1. Paste or upload a session transcript (preferably a few paragraphs of conversation).
29
+ 2. (Optional) Provide a custom instruction or focus question, e.g., "Highlight moments of cognitive reframing."
30
+ 3. Click "Summarize" to generate a concise summary with themes and emotional insights.
31
+
32
+ """
33
+
34
+ # Initialize pipelines
35
+ summarizer = pipeline("text2text-generation", model="google/flan-t5-small", tokenizer="google/flan-t5-small")
36
+ # Note: "text2text-generation" pipeline for FLAN-T5 also works for summarization if we prompt it properly.
37
+ sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
38
+
39
+ def analyze_session(transcript, custom_instruction):
40
+ # Basic input validation
41
+ if not transcript.strip():
42
+ return "Please provide a transcript."
43
+
44
+ # Construct a prompt for the summarization model
45
+ # FLAN-T5 is instruction tuned, so we can give it instructions directly.
46
+ # For example:
47
+ prompt = "Summarize the following psychotherapy session transcript, focusing on key themes, emotional shifts, and patterns."
48
+ if custom_instruction.strip():
49
+ prompt += " Additionally, " + custom_instruction.strip()
50
+ prompt += "\n\nTranscript:\n" + transcript.strip()
51
+
52
+ # Generate summary
53
+ summary_output = summarizer(prompt, max_length=200, do_sample=False)
54
+ summary = summary_output[0]['generated_text'].strip()
55
+
56
+ # Sentiment analysis: We'll run it on the entire transcript to gauge the overall emotional tone.
57
+ sentiment_results = sentiment_analyzer(transcript)
58
+ # The sentiment model returns something like: [{'label': 'positive', 'score': ...}]
59
+ # We'll aggregate the results (though it's a single input) and just pick the top.
60
+ main_sentiment = sentiment_results[0]['label']
61
+
62
+ # Construct a more informative result
63
+ # You could elaborate this logic: detect recurring concerns by simple keyword frequency analysis, etc.
64
+ # For a simple first iteration, just provide summary and sentiment.
65
+
66
+ # Optional: Identify recurring concerns (simple keyword extraction)
67
+ # We'll do a naive keyword frequency approach just as a demonstration:
68
+ words = transcript.lower().split()
69
+ # Common therapy-related words (just a naive approach, could be replaced by a proper keyword extraction model)
70
+ # This is a placeholder for demonstration
71
+ keywords_of_interest = ["anxiety", "depression", "relationship", "stress", "fear", "goals", "progress", "cognitive", "behavior"]
72
+ recurring_concerns = [word for word in words if word in keywords_of_interest]
73
+ recurring_concerns = list(set(recurring_concerns)) # unique
74
+ if not recurring_concerns:
75
+ recurring_concerns_str = "No specific recurring concerns identified from the predefined list."
76
+ else:
77
+ recurring_concerns_str = "Recurring concerns include: " + ", ".join(recurring_concerns)
78
+
79
+ # Recommended follow-up topics (just a heuristic based on summary)
80
+ # If certain keywords appear in summary, we can suggest follow-up:
81
+ follow_up_suggestions = []
82
+ if "progress" in summary.lower():
83
+ follow_up_suggestions.append("Explore client's perception of progress in more detail.")
84
+ if "relationship" in summary.lower():
85
+ follow_up_suggestions.append("Discuss client's relationship dynamics further.")
86
+ if not follow_up_suggestions:
87
+ follow_up_suggestions.append("Consider following up on the emotional themes identified in the summary.")
88
+
89
+ follow_up_suggestions_str = " ".join(follow_up_suggestions)
90
+
91
+ # Combine results into a final output
92
+ 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}"
93
+
94
+ return final_output
95
+
96
+ # Build Gradio UI
97
+ description = """# Psychotherapy Session Summarizer
98
+
99
+ Upload or paste your psychotherapy session transcript and optionally provide a custom instruction (e.g., "Focus on anxiety and coping strategies.").
100
+ Click 'Summarize' to generate a concise summary of key themes, emotional tones, recurring concerns, and suggested follow-up topics.
101
+ """
102
+
103
+ with gr.Blocks() as demo:
104
+ gr.Markdown(description)
105
+ with gr.Row():
106
+ transcript_input = gr.Textbox(label="Session Transcript", lines=10, placeholder="Paste the session transcript here...")
107
+ custom_instruction_input = gr.Textbox(label="Custom Instruction (Optional)", placeholder="e.g., Focus on how the client describes their feelings about progress.")
108
+ summarize_button = gr.Button("Summarize")
109
+ output_box = gr.Markdown()
110
+
111
+ summarize_button.click(fn=analyze_session, inputs=[transcript_input, custom_instruction_input], outputs=output_box)
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch()