Spaces:
Sleeping
Sleeping
Update live_session.py
Browse files- live_session.py +176 -21
live_session.py
CHANGED
@@ -1,41 +1,196 @@
|
|
1 |
import streamlit as st
|
2 |
from audio_recorder_streamlit import audio_recorder
|
3 |
import time
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def show_live_session():
|
6 |
-
st.title("Live Session Recording")
|
7 |
|
8 |
-
#
|
9 |
if "recording" not in st.session_state:
|
10 |
st.session_state.recording = False
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
col1, col2 = st.columns(2)
|
14 |
|
15 |
with col1:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
with col2:
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
# Recording
|
24 |
if st.session_state.recording:
|
|
|
|
|
|
|
25 |
audio_bytes = audio_recorder()
|
26 |
if audio_bytes:
|
27 |
st.audio(audio_bytes, format="audio/wav")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
1 |
import streamlit as st
|
2 |
from audio_recorder_streamlit import audio_recorder
|
3 |
import time
|
4 |
+
import google.generativeai as genai
|
5 |
+
from datetime import datetime
|
6 |
+
import json
|
7 |
+
from prompts import REAL_TIME_ANALYSIS_PROMPT, MI_SYSTEM_PROMPT
|
8 |
|
9 |
def show_live_session():
|
10 |
+
st.title("Live Therapy Session Recording & Analysis")
|
11 |
|
12 |
+
# Initialize session state
|
13 |
if "recording" not in st.session_state:
|
14 |
st.session_state.recording = False
|
15 |
+
if "session_transcript" not in st.session_state:
|
16 |
+
st.session_state.session_transcript = []
|
17 |
+
if "session_start_time" not in st.session_state:
|
18 |
+
st.session_state.session_start_time = None
|
19 |
|
20 |
+
# Layout
|
21 |
+
col1, col2 = st.columns([2, 3])
|
22 |
|
23 |
with col1:
|
24 |
+
show_recording_controls()
|
25 |
+
show_session_info()
|
26 |
+
|
27 |
with col2:
|
28 |
+
show_real_time_analysis()
|
29 |
+
|
30 |
+
def show_recording_controls():
|
31 |
+
st.subheader("Recording Controls")
|
32 |
+
|
33 |
+
# Start/Stop Recording button
|
34 |
+
if st.button("Start Recording" if not st.session_state.recording else "Stop Recording"):
|
35 |
+
if not st.session_state.recording:
|
36 |
+
start_session()
|
37 |
+
else:
|
38 |
+
end_session()
|
39 |
|
40 |
+
# Recording indicator
|
41 |
if st.session_state.recording:
|
42 |
+
st.markdown("🔴 **Recording in progress...**")
|
43 |
+
|
44 |
+
# Audio recorder
|
45 |
audio_bytes = audio_recorder()
|
46 |
if audio_bytes:
|
47 |
st.audio(audio_bytes, format="audio/wav")
|
48 |
+
process_audio(audio_bytes)
|
49 |
+
|
50 |
+
def start_session():
|
51 |
+
st.session_state.recording = True
|
52 |
+
st.session_state.session_start_time = datetime.now()
|
53 |
+
st.session_state.session_transcript = []
|
54 |
+
|
55 |
+
def end_session():
|
56 |
+
st.session_state.recording = False
|
57 |
+
save_session()
|
58 |
+
|
59 |
+
def show_session_info():
|
60 |
+
if st.session_state.recording and st.session_state.session_start_time:
|
61 |
+
duration = datetime.now() - st.session_state.session_start_time
|
62 |
+
st.info(f"Session Duration: {str(duration).split('.')[0]}")
|
63 |
+
|
64 |
+
def show_real_time_analysis():
|
65 |
+
st.subheader("Real-time Analysis")
|
66 |
+
|
67 |
+
# Display transcript and analysis
|
68 |
+
for entry in st.session_state.session_transcript:
|
69 |
+
with st.expander(f"Entry at {entry['timestamp']}"):
|
70 |
+
st.markdown(f"**Speaker:** {entry['speaker']}")
|
71 |
+
st.markdown(entry['text'])
|
72 |
+
|
73 |
+
if 'analysis' in entry:
|
74 |
+
st.markdown("### Analysis")
|
75 |
+
st.markdown(entry['analysis'])
|
76 |
+
|
77 |
+
def process_audio(audio_bytes):
|
78 |
+
"""Process recorded audio"""
|
79 |
+
try:
|
80 |
+
# Here you would typically:
|
81 |
+
# 1. Convert audio_bytes to text using a speech-to-text service
|
82 |
+
# 2. Analyze the text using Gemini
|
83 |
+
# For now, we'll use a placeholder text
|
84 |
+
|
85 |
+
transcript = "Example transcription" # Replace with actual transcription
|
86 |
+
|
87 |
+
# Add to session transcript
|
88 |
+
entry = {
|
89 |
+
"speaker": "Client",
|
90 |
+
"text": transcript,
|
91 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
92 |
+
}
|
93 |
+
|
94 |
+
# Generate analysis
|
95 |
+
analysis = analyze_real_time(transcript)
|
96 |
+
if analysis:
|
97 |
+
entry["analysis"] = analysis
|
98 |
+
|
99 |
+
st.session_state.session_transcript.append(entry)
|
100 |
+
|
101 |
+
except Exception as e:
|
102 |
+
st.error(f"Error processing audio: {str(e)}")
|
103 |
+
|
104 |
+
def analyze_real_time(transcript):
|
105 |
+
try:
|
106 |
+
# Configure Gemini model
|
107 |
+
model = genai.GenerativeModel('gemini-pro')
|
108 |
+
|
109 |
+
# Prepare context
|
110 |
+
context = {
|
111 |
+
"transcript": transcript,
|
112 |
+
"session_history": str(st.session_state.session_transcript[-5:]), # Last 5 entries
|
113 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
114 |
+
}
|
115 |
+
|
116 |
+
# Generate analysis
|
117 |
+
prompt = f"""
|
118 |
+
Analyze the following therapy session segment using MI principles:
|
119 |
+
|
120 |
+
Transcript: {context['transcript']}
|
121 |
+
|
122 |
+
Recent Context: {context['session_history']}
|
123 |
+
|
124 |
+
Please provide:
|
125 |
+
1. Identification of MI techniques used or missed opportunities
|
126 |
+
2. Analysis of change talk vs sustain talk
|
127 |
+
3. Suggestions for next interventions
|
128 |
+
4. Overall MI adherence assessment
|
129 |
+
"""
|
130 |
+
|
131 |
+
response = model.generate_content(prompt)
|
132 |
+
return response.text
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
st.error(f"Error generating analysis: {str(e)}")
|
136 |
+
return None
|
137 |
+
|
138 |
+
def save_session():
|
139 |
+
"""Save session data to file"""
|
140 |
+
if st.session_state.session_transcript:
|
141 |
+
try:
|
142 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
143 |
+
filename = f"session_{timestamp}.json"
|
144 |
+
|
145 |
+
session_data = {
|
146 |
+
"start_time": st.session_state.session_start_time.isoformat(),
|
147 |
+
"end_time": datetime.now().isoformat(),
|
148 |
+
"transcript": st.session_state.session_transcript
|
149 |
+
}
|
150 |
+
|
151 |
+
with open(filename, "w") as f:
|
152 |
+
json.dump(session_data, f, indent=4)
|
153 |
+
|
154 |
+
st.success(f"Session saved to {filename}")
|
155 |
|
156 |
+
except Exception as e:
|
157 |
+
st.error(f"Error saving session: {str(e)}")
|
158 |
+
|
159 |
+
# Add session controls
|
160 |
+
def show_session_controls():
|
161 |
+
st.sidebar.subheader("Session Controls")
|
162 |
+
|
163 |
+
# Session settings
|
164 |
+
st.sidebar.text_input("Client ID (optional)")
|
165 |
+
st.sidebar.text_input("Session Notes (optional)")
|
166 |
+
|
167 |
+
# Timer controls
|
168 |
+
if st.session_state.recording:
|
169 |
+
if st.sidebar.button("Add Marker"):
|
170 |
+
add_session_marker()
|
171 |
|
172 |
+
def add_session_marker():
|
173 |
+
"""Add a marker/note to the session transcript"""
|
174 |
+
marker_text = st.text_input("Marker note:")
|
175 |
+
if marker_text:
|
176 |
+
st.session_state.session_transcript.append({
|
177 |
+
"speaker": "System",
|
178 |
+
"text": f"MARKER: {marker_text}",
|
179 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
180 |
+
})
|
181 |
+
|
182 |
+
# Add visualization features
|
183 |
+
def show_session_visualizations():
|
184 |
+
if st.session_state.session_transcript:
|
185 |
+
st.subheader("Session Analytics")
|
186 |
+
|
187 |
+
# Add visualizations here (e.g., using plotly)
|
188 |
+
# - Speaking time distribution
|
189 |
+
# - Change talk vs sustain talk ratio
|
190 |
+
# - MI adherence scores
|
191 |
+
pass
|
192 |
|
193 |
+
def show_live_session_main():
|
194 |
+
show_live_session()
|
195 |
+
show_session_controls()
|
196 |
+
show_session_visualizations()
|