Rogerjs commited on
Commit
a1ee826
·
verified ·
1 Parent(s): fcfe145

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -42
app.py CHANGED
@@ -3,25 +3,14 @@ from transformers import pipeline
3
  import re
4
 
5
  # Initialize pipelines
6
- # Summarization pipeline with FLAN-T5
7
  summarizer = pipeline("text2text-generation", model="google/flan-t5-small", tokenizer="google/flan-t5-small")
8
-
9
- # Sentiment analysis pipeline
10
  sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
11
-
12
- # Automatic speech recognition pipeline for audio
13
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small")
14
 
15
  def convert_to_json(transcript_text):
16
- """
17
- Convert the transcript into a structured JSON format.
18
- Attempts to identify speaker turns based on lines starting with 'Therapist:' or 'Client:'.
19
- If no clear pattern is found, the entire transcript is considered one turn.
20
- """
21
  lines = transcript_text.strip().split("\n")
22
  session_data = []
23
 
24
- # Regex patterns to identify lines with a speaker
25
  therapist_pattern = re.compile(r"^\s*(Therapist|T):", re.IGNORECASE)
26
  client_pattern = re.compile(r"^\s*(Client|C):", re.IGNORECASE)
27
 
@@ -31,61 +20,47 @@ def convert_to_json(transcript_text):
31
  for line in lines:
32
  line = line.strip()
33
  if therapist_pattern.match(line):
34
- # If we have accumulated text from previous speaker, store it
35
  if current_speaker and current_text:
36
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
37
  current_text = []
38
-
39
  current_speaker = "Therapist"
40
- # Remove the speaker prefix
41
  text_part = therapist_pattern.sub("", line).strip()
42
  current_text.append(text_part)
43
-
44
  elif client_pattern.match(line):
45
  if current_speaker and current_text:
46
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
47
  current_text = []
48
-
49
  current_speaker = "Client"
50
  text_part = client_pattern.sub("", line).strip()
51
  current_text.append(text_part)
52
-
53
  else:
54
- # Just text, append to current speaker's segment if identified
55
  if current_speaker is None:
56
- # No speaker identified yet, assume unknown
57
  current_speaker = "Unknown"
58
  current_text.append(line)
59
 
60
- # Append the last collected segment
61
  if current_speaker and current_text:
62
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
63
 
64
- # If no speakers identified at all and just one big chunk, still return it as JSON
65
  if not session_data:
66
  session_data = [{"speaker": "Unknown", "text": transcript_text.strip()}]
67
 
68
- # Create a final JSON structure
69
  json_data = {"session": session_data}
70
  return json_data
71
 
72
  def analyze_session(transcript, custom_instruction, audio):
73
- # If audio is provided, we transcribe it and ignore the text transcript field
74
  if audio is not None:
75
- # Transcribe audio
76
  asr_result = asr_pipeline(audio)
77
  transcript_text = asr_result['text']
78
  else:
79
- # Use the provided transcript text
80
  transcript_text = transcript
81
-
82
  if not transcript_text.strip():
83
  return "Please provide a transcript or an audio file."
84
-
85
- # Convert transcript to JSON
86
  json_data = convert_to_json(transcript_text)
87
 
88
- # Prepare the prompt for summarization
89
  prompt = (
90
  "You are a helpful assistant that summarizes psychotherapy sessions. "
91
  "The session is provided in JSON format with speaker turns. "
@@ -95,25 +70,20 @@ def analyze_session(transcript, custom_instruction, audio):
95
  prompt += f" Additionally, {custom_instruction.strip()}"
96
  prompt += "\n\nJSON data:\n" + str(json_data)
97
 
98
- # Summarize using the LLM
99
  summary_output = summarizer(prompt, max_length=200, do_sample=False)
100
  summary = summary_output[0]['generated_text'].strip()
101
-
102
- # Sentiment analysis of the entire transcript
103
  sentiment_results = sentiment_analyzer(transcript_text)
104
  main_sentiment = sentiment_results[0]['label']
105
-
106
- # Simple keyword-based recurring concerns
107
  words = transcript_text.lower().split()
108
  keywords_of_interest = ["anxiety", "depression", "relationship", "stress", "fear", "goals", "progress", "cognitive", "behavior"]
109
- recurring_concerns = [word for word in words if word in keywords_of_interest]
110
- recurring_concerns = list(set(recurring_concerns))
111
  if not recurring_concerns:
112
  recurring_concerns_str = "No specific recurring concerns identified from the predefined list."
113
  else:
114
  recurring_concerns_str = "Recurring concerns include: " + ", ".join(recurring_concerns)
115
-
116
- # Suggest follow-up topics based on summary
117
  follow_up_suggestions = []
118
  if "progress" in summary.lower():
119
  follow_up_suggestions.append("Explore client's perception of progress in more detail.")
@@ -122,12 +92,10 @@ def analyze_session(transcript, custom_instruction, audio):
122
  if not follow_up_suggestions:
123
  follow_up_suggestions.append("Consider following up on the emotional themes identified in the summary.")
124
  follow_up_suggestions_str = " ".join(follow_up_suggestions)
125
-
126
  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}"
127
-
128
  return final_output
129
 
130
- # Build Gradio UI
131
  description = """# Psychotherapy Session Summarizer
132
 
133
  This tool summarizes psychotherapy session transcripts (text or audio) into key themes, emotional shifts, and patterns.
@@ -142,7 +110,7 @@ with gr.Blocks() as demo:
142
  gr.Markdown(description)
143
  with gr.Row():
144
  transcript_input = gr.Textbox(label="Session Transcript (Text)", lines=10, placeholder="Paste the session transcript here...")
145
- audio_input = gr.Audio(source="upload", type="file", label="Session Audio (Optional)")
146
  custom_instruction_input = gr.Textbox(label="Custom Instruction (Optional)", placeholder="e.g., Focus on anxiety and coping strategies.")
147
  summarize_button = gr.Button("Summarize")
148
  output_box = gr.Markdown()
 
3
  import re
4
 
5
  # Initialize pipelines
 
6
  summarizer = pipeline("text2text-generation", model="google/flan-t5-small", tokenizer="google/flan-t5-small")
 
 
7
  sentiment_analyzer = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
 
 
8
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small")
9
 
10
  def convert_to_json(transcript_text):
 
 
 
 
 
11
  lines = transcript_text.strip().split("\n")
12
  session_data = []
13
 
 
14
  therapist_pattern = re.compile(r"^\s*(Therapist|T):", re.IGNORECASE)
15
  client_pattern = re.compile(r"^\s*(Client|C):", re.IGNORECASE)
16
 
 
20
  for line in lines:
21
  line = line.strip()
22
  if therapist_pattern.match(line):
 
23
  if current_speaker and current_text:
24
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
25
  current_text = []
 
26
  current_speaker = "Therapist"
 
27
  text_part = therapist_pattern.sub("", line).strip()
28
  current_text.append(text_part)
 
29
  elif client_pattern.match(line):
30
  if current_speaker and current_text:
31
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
32
  current_text = []
 
33
  current_speaker = "Client"
34
  text_part = client_pattern.sub("", line).strip()
35
  current_text.append(text_part)
 
36
  else:
 
37
  if current_speaker is None:
 
38
  current_speaker = "Unknown"
39
  current_text.append(line)
40
 
 
41
  if current_speaker and current_text:
42
  session_data.append({"speaker": current_speaker, "text": " ".join(current_text).strip()})
43
 
 
44
  if not session_data:
45
  session_data = [{"speaker": "Unknown", "text": transcript_text.strip()}]
46
 
 
47
  json_data = {"session": session_data}
48
  return json_data
49
 
50
  def analyze_session(transcript, custom_instruction, audio):
51
+ # If an audio file is provided, transcribe it
52
  if audio is not None:
53
+ # 'audio' will be the file path if type="filepath"
54
  asr_result = asr_pipeline(audio)
55
  transcript_text = asr_result['text']
56
  else:
 
57
  transcript_text = transcript
58
+
59
  if not transcript_text.strip():
60
  return "Please provide a transcript or an audio file."
61
+
 
62
  json_data = convert_to_json(transcript_text)
63
 
 
64
  prompt = (
65
  "You are a helpful assistant that summarizes psychotherapy sessions. "
66
  "The session is provided in JSON format with speaker turns. "
 
70
  prompt += f" Additionally, {custom_instruction.strip()}"
71
  prompt += "\n\nJSON data:\n" + str(json_data)
72
 
 
73
  summary_output = summarizer(prompt, max_length=200, do_sample=False)
74
  summary = summary_output[0]['generated_text'].strip()
75
+
 
76
  sentiment_results = sentiment_analyzer(transcript_text)
77
  main_sentiment = sentiment_results[0]['label']
78
+
 
79
  words = transcript_text.lower().split()
80
  keywords_of_interest = ["anxiety", "depression", "relationship", "stress", "fear", "goals", "progress", "cognitive", "behavior"]
81
+ recurring_concerns = list(set([word for word in words if word in keywords_of_interest]))
 
82
  if not recurring_concerns:
83
  recurring_concerns_str = "No specific recurring concerns identified from the predefined list."
84
  else:
85
  recurring_concerns_str = "Recurring concerns include: " + ", ".join(recurring_concerns)
86
+
 
87
  follow_up_suggestions = []
88
  if "progress" in summary.lower():
89
  follow_up_suggestions.append("Explore client's perception of progress in more detail.")
 
92
  if not follow_up_suggestions:
93
  follow_up_suggestions.append("Consider following up on the emotional themes identified in the summary.")
94
  follow_up_suggestions_str = " ".join(follow_up_suggestions)
95
+
96
  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}"
 
97
  return final_output
98
 
 
99
  description = """# Psychotherapy Session Summarizer
100
 
101
  This tool summarizes psychotherapy session transcripts (text or audio) into key themes, emotional shifts, and patterns.
 
110
  gr.Markdown(description)
111
  with gr.Row():
112
  transcript_input = gr.Textbox(label="Session Transcript (Text)", lines=10, placeholder="Paste the session transcript here...")
113
+ audio_input = gr.Audio(type="filepath", label="Session Audio (Optional)")
114
  custom_instruction_input = gr.Textbox(label="Custom Instruction (Optional)", placeholder="e.g., Focus on anxiety and coping strategies.")
115
  summarize_button = gr.Button("Summarize")
116
  output_box = gr.Markdown()