Jiaaaaaaax commited on
Commit
7d996ca
·
verified ·
1 Parent(s): daf586d

Update session_analysis.py

Browse files
Files changed (1) hide show
  1. session_analysis.py +142 -218
session_analysis.py CHANGED
@@ -317,66 +317,76 @@ def show_manual_input_form():
317
  analyze_session_content(st.session_state.current_transcript)
318
 
319
 
320
- def analyze_session_content(content):
 
321
  try:
322
- # Configure Gemini model
 
 
 
 
323
  model = genai.GenerativeModel('gemini-pro')
324
 
325
- # Prepare analysis prompt
326
- analysis_prompt = f"""
327
- Analyze the following therapy session using MI principles and provide a comprehensive evaluation:
328
-
329
- Session Content:
330
- {content}
331
-
332
- Please provide detailed analysis including:
333
- 1. MI Adherence Assessment:
334
- - OARS implementation
335
- - Change talk identification
336
- - Resistance management
337
- - MI spirit adherence
338
-
339
- 2. Technical Skills Evaluation:
340
- - Reflection quality and frequency
341
- - Question-to-reflection ratio
342
- - Open vs. closed questions
343
- - Affirmations and summaries
344
-
345
- 3. Client Language Analysis:
346
- - Change talk instances
347
- - Sustain talk patterns
348
- - Commitment language
349
- - Resistance patterns
350
-
351
- 4. Session Flow Analysis:
352
- - Engagement level
353
- - Focus maintenance
354
- - Evocation quality
355
- - Planning effectiveness
356
-
357
- 5. Recommendations:
358
- - Strength areas
359
- - Growth opportunities
360
- - Suggested interventions
361
- - Next session planning
362
-
363
- Format the analysis with clear sections and specific examples from the session.
 
 
 
 
 
 
 
 
364
  """
 
 
 
365
 
366
- # Generate analysis
367
- response = model.generate_content(analysis_prompt)
368
-
369
- # Process and structure the analysis results
370
- analysis_results = process_analysis_results(response.text)
371
-
372
- # Store results in session state
373
- st.session_state.analysis_results = analysis_results
374
-
375
- # Show success message
376
- st.success("Analysis completed successfully!")
377
 
 
 
378
  except Exception as e:
379
- st.error(f"Error during analysis: {str(e)}")
 
380
 
381
 
382
  def generate_transcript(audio_content):
@@ -795,78 +805,16 @@ def format_session_data(session_data):
795
  """
796
  return formatted_text
797
 
798
- def analyze_session_content(transcript):
799
- """Analyze the session transcript using Gemini"""
800
- try:
801
- if not transcript:
802
- st.warning("Please provide a transcript for analysis.")
803
- return
804
-
805
- # Configure the model
806
- model = genai.GenerativeModel('gemini-pro')
807
-
808
- # Prepare prompt with structured output request
809
- prompt = """
810
- Analyze this MI (Motivational Interviewing) session transcript and provide analysis in the following structured format:
811
-
812
- MI Adherence Score: [0-100]
813
-
814
- Key Themes:
815
- - [Theme 1]
816
- - [Theme 2]
817
- - [Theme 3]
818
-
819
- Technique Usage:
820
- - Open Questions: [count]
821
- - Reflections: [count]
822
- - Affirmations: [count]
823
- - Summaries: [count]
824
-
825
- Strengths:
826
- - [Strength 1]
827
- - [Strength 2]
828
- - [Strength 3]
829
-
830
- Areas for Improvement:
831
- - [Area 1]
832
- - [Area 2]
833
- - [Area 3]
834
-
835
- Change Talk Instances:
836
- - [Example 1]
837
- - [Example 2]
838
-
839
- Sustain Talk Instances:
840
- - [Example 1]
841
- - [Example 2]
842
-
843
- Session Summary:
844
- [Brief summary of the session flow and key moments]
845
-
846
- Transcript to analyze:
847
- {transcript}
848
- """
849
-
850
- # Generate response
851
- response = model.generate_content(prompt)
852
-
853
- # Store results in session state
854
- st.session_state.analysis_results = response.text
855
-
856
- # Trigger results display
857
- show_analysis_results()
858
-
859
- except Exception as e:
860
- st.error(f"Error analyzing session: {str(e)}")
861
-
862
 
863
  def show_analysis_results():
864
- """Display the analysis results in the dashboard"""
865
  if 'analysis_results' not in st.session_state or not st.session_state.analysis_results:
866
- st.info("No analysis results available. Please analyze a transcript first.")
867
  return
868
 
869
- # Create tabs for different aspects of analysis
 
 
870
  tabs = st.tabs([
871
  "MI Adherence",
872
  "Technical Skills",
@@ -877,10 +825,10 @@ def show_analysis_results():
877
 
878
  # MI Adherence Tab
879
  with tabs[0]:
880
- st.subheader("MI Adherence Score")
881
 
882
- # Extract score from analysis results
883
- score_match = re.search(r'MI Adherence Score:\s*(\d+)', st.session_state.analysis_results)
884
  if score_match:
885
  score = int(score_match.group(1))
886
 
@@ -889,120 +837,96 @@ def show_analysis_results():
889
  mode="gauge+number",
890
  value=score,
891
  domain={'x': [0, 1], 'y': [0, 1]},
892
- gauge={'axis': {'range': [0, 100]},
893
- 'bar': {'color': "rgb(26, 118, 255)"},
894
- 'steps': [
895
- {'range': [0, 33], 'color': "lightgray"},
896
- {'range': [33, 66], 'color': "gray"},
897
- {'range': [66, 100], 'color': "darkgray"}
898
- ]}))
 
 
 
899
  st.plotly_chart(fig)
900
 
901
- # Extract and display strengths
902
- strengths = re.findall(r'Strengths:\n((?:- .*\n)*)', st.session_state.analysis_results)
903
- if strengths:
904
  st.subheader("Strengths")
905
- strength_list = strengths[0].strip().split('\n')
906
- for strength in strength_list:
907
- if strength.startswith('- '):
908
- st.markdown(f"✅ {strength[2:]}")
909
-
910
- # Extract and display areas for improvement
911
- improvements = re.findall(r'Areas for Improvement:\n((?:- .*\n)*)', st.session_state.analysis_results)
912
- if improvements:
913
- st.subheader("Areas for Improvement")
914
- improvement_list = improvements[0].strip().split('\n')
915
- for improvement in improvement_list:
916
- if improvement.startswith('- '):
917
- st.markdown(f"🔄 {improvement[2:]}")
918
-
919
- # Technical Skills Tab
920
- with tabs[1]:
921
- st.subheader("MI Technique Usage")
922
-
923
- # Extract technique counts
924
- techniques = {
925
- 'Open Questions': 0,
926
- 'Reflections': 0,
927
- 'Affirmations': 0,
928
- 'Summaries': 0
929
- }
930
-
931
- for technique in techniques.keys():
932
- count_match = re.search(f'{technique}:\s*(\d+)', st.session_state.analysis_results)
933
- if count_match:
934
- techniques[technique] = int(count_match.group(1))
935
-
936
- # Create bar chart
937
- fig = go.Figure(data=[
938
- go.Bar(
939
- x=list(techniques.keys()),
940
- y=list(techniques.values()),
941
- marker_color='rgb(26, 118, 255)'
942
- )
943
- ])
944
-
945
- fig.update_layout(
946
- title="Technique Usage Frequency",
947
- xaxis_title="Technique",
948
- yaxis_title="Count",
949
- template="plotly_white"
950
- )
951
- st.plotly_chart(fig)
952
 
953
  # Client Language Tab
954
  with tabs[2]:
955
  st.subheader("Client Language Analysis")
956
 
957
- # Extract change talk
958
- change_talk = re.findall(r'Change Talk Instances:\n((?:- .*\n)*)', st.session_state.analysis_results)
959
- if change_talk:
960
  st.markdown("### Change Talk 🌱")
961
- changes = change_talk[0].strip().split('\n')
962
- for change in changes:
963
- if change.startswith('- '):
964
- st.markdown(f"- {change[2:]}")
965
-
966
- # Extract sustain talk
967
- sustain_talk = re.findall(r'Sustain Talk Instances:\n((?:- .*\n)*)', st.session_state.analysis_results)
968
- if sustain_talk:
969
  st.markdown("### Sustain Talk 🔄")
970
- sustains = sustain_talk[0].strip().split('\n')
971
- for sustain in sustains:
972
- if sustain.startswith('- '):
973
- st.markdown(f"- {sustain[2:]}")
 
974
 
975
  # Session Flow Tab
976
  with tabs[3]:
977
  st.subheader("Session Flow Analysis")
978
 
979
- # Extract key themes
980
- themes = re.findall(r'Key Themes:\n((?:- .*\n)*)', st.session_state.analysis_results)
981
- if themes:
982
- st.markdown("### Key Themes 🎯")
983
- theme_list = themes[0].strip().split('\n')
984
- for theme in theme_list:
985
- if theme.startswith('- '):
986
- st.markdown(f"- {theme[2:]}")
987
-
988
- # Extract session summary
989
- summary_match = re.search(r'Session Summary:\n(.*?)(?=\n\n|\Z)', st.session_state.analysis_results, re.DOTALL)
990
- if summary_match:
991
- st.markdown("### Session Summary")
992
- st.write(summary_match.group(1).strip())
 
993
 
994
  # Recommendations Tab
995
  with tabs[4]:
996
- st.subheader("Recommendations for Improvement")
997
 
998
- # Display improvement areas as recommendations
999
- if improvements:
1000
- st.markdown("### Priority Areas 🎯")
1001
- improvement_list = improvements[0].strip().split('\n')
1002
- for i, improvement in enumerate(improvement_list[:3], 1):
1003
- if improvement.startswith('- '):
1004
- st.markdown(f"**{i}. {improvement[2:]}**")
1005
- st.markdown(get_improvement_suggestion(improvement[2:]))
 
 
 
 
 
 
 
1006
 
1007
 
1008
  def get_technique_description(technique):
 
317
  analyze_session_content(st.session_state.current_transcript)
318
 
319
 
320
+ def analyze_session_content(transcript):
321
+ """Analyze the session transcript using Gemini"""
322
  try:
323
+ if not transcript:
324
+ st.warning("Please provide a transcript for analysis.")
325
+ return
326
+
327
+ # Configure the model
328
  model = genai.GenerativeModel('gemini-pro')
329
 
330
+ # Structured prompt for MI analysis
331
+ prompt = f"""
332
+ As an MI (Motivational Interviewing) expert, analyze this therapy session transcript and provide detailed feedback in the following format:
333
+
334
+ === MI Adherence ===
335
+ Score: [Provide a score from 0-100]
336
+ Strengths:
337
+ - [List 3 specific strengths with examples]
338
+ Areas for Growth:
339
+ - [List 3 specific areas needing improvement with examples]
340
+
341
+ === Technical Analysis ===
342
+ OARS Usage Count:
343
+ - Open Questions: [number]
344
+ - Affirmations: [number]
345
+ - Reflections: [number]
346
+ - Summaries: [number]
347
+
348
+ === Client Language Analysis ===
349
+ Change Talk Examples:
350
+ - [List 3-4 specific quotes showing change talk]
351
+ Sustain Talk Examples:
352
+ - [List 2-3 specific quotes showing sustain talk]
353
+ Change Talk/Sustain Talk Ratio: [X:Y]
354
+
355
+ === Session Flow ===
356
+ Key Moments:
357
+ 1. [Describe key moment 1]
358
+ 2. [Describe key moment 2]
359
+ 3. [Describe key moment 3]
360
+
361
+ Therapeutic Process:
362
+ - [Describe how the session progressed]
363
+ - [Note any significant shifts]
364
+
365
+ === Recommendations ===
366
+ Priority Actions:
367
+ 1. [Specific recommendation 1]
368
+ 2. [Specific recommendation 2]
369
+ 3. [Specific recommendation 3]
370
+
371
+ Development Strategies:
372
+ - [Practical strategy 1]
373
+ - [Practical strategy 2]
374
+
375
+ Analyze this transcript:
376
+ {transcript}
377
  """
378
+
379
+ # Generate response
380
+ response = model.generate_content(prompt)
381
 
382
+ # Store results
383
+ st.session_state.analysis_results = response.text
 
 
 
 
 
 
 
 
 
384
 
385
+ return True
386
+
387
  except Exception as e:
388
+ st.error(f"Error in analysis: {str(e)}")
389
+ return False
390
 
391
 
392
  def generate_transcript(audio_content):
 
805
  """
806
  return formatted_text
807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808
 
809
  def show_analysis_results():
810
+ """Display the analysis results in organized tabs"""
811
  if 'analysis_results' not in st.session_state or not st.session_state.analysis_results:
812
+ st.info("Please analyze a transcript first.")
813
  return
814
 
815
+ results = st.session_state.analysis_results
816
+
817
+ # Create tabs
818
  tabs = st.tabs([
819
  "MI Adherence",
820
  "Technical Skills",
 
825
 
826
  # MI Adherence Tab
827
  with tabs[0]:
828
+ st.subheader("MI Adherence Analysis")
829
 
830
+ # Extract score
831
+ score_match = re.search(r'Score:\s*(\d+)', results)
832
  if score_match:
833
  score = int(score_match.group(1))
834
 
 
837
  mode="gauge+number",
838
  value=score,
839
  domain={'x': [0, 1], 'y': [0, 1]},
840
+ gauge={
841
+ 'axis': {'range': [0, 100]},
842
+ 'bar': {'color': "rgb(26, 118, 255)"},
843
+ 'steps': [
844
+ {'range': [0, 33], 'color': "lightgray"},
845
+ {'range': [33, 66], 'color': "gray"},
846
+ {'range': [66, 100], 'color': "darkgray"}
847
+ ]
848
+ }
849
+ ))
850
  st.plotly_chart(fig)
851
 
852
+ # Display strengths and areas for growth
853
+ col1, col2 = st.columns(2)
854
+ with col1:
855
  st.subheader("Strengths")
856
+ strengths = re.findall(r'Strengths:\n((?:- .*\n)*)', results)
857
+ if strengths:
858
+ for strength in strengths[0].strip().split('\n'):
859
+ if strength.startswith('- '):
860
+ st.markdown(f"✅ {strength[2:]}")
861
+
862
+ with col2:
863
+ st.subheader("Areas for Growth")
864
+ growth = re.findall(r'Areas for Growth:\n((?:- .*\n)*)', results)
865
+ if growth:
866
+ for area in growth[0].strip().split('\n'):
867
+ if area.startswith('- '):
868
+ st.markdown(f"🔄 {area[2:]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
869
 
870
  # Client Language Tab
871
  with tabs[2]:
872
  st.subheader("Client Language Analysis")
873
 
874
+ col1, col2 = st.columns(2)
875
+ with col1:
 
876
  st.markdown("### Change Talk 🌱")
877
+ change_talk = re.findall(r'Change Talk Examples:\n((?:- .*\n)*)', results)
878
+ if change_talk:
879
+ for talk in change_talk[0].strip().split('\n'):
880
+ if talk.startswith('- '):
881
+ st.markdown(f"- {talk[2:]}")
882
+
883
+ with col2:
 
884
  st.markdown("### Sustain Talk 🔄")
885
+ sustain_talk = re.findall(r'Sustain Talk Examples:\n((?:- .*\n)*)', results)
886
+ if sustain_talk:
887
+ for talk in sustain_talk[0].strip().split('\n'):
888
+ if talk.startswith('- '):
889
+ st.markdown(f"- {talk[2:]}")
890
 
891
  # Session Flow Tab
892
  with tabs[3]:
893
  st.subheader("Session Flow Analysis")
894
 
895
+ # Key Moments
896
+ st.markdown("### Key Moments")
897
+ key_moments = re.findall(r'Key Moments:\n((?:\d\. .*\n)*)', results)
898
+ if key_moments:
899
+ for moment in key_moments[0].strip().split('\n'):
900
+ if moment.strip():
901
+ st.markdown(f"{moment}")
902
+
903
+ # Therapeutic Process
904
+ st.markdown("### Therapeutic Process")
905
+ process = re.findall(r'Therapeutic Process:\n((?:- .*\n)*)', results)
906
+ if process:
907
+ for item in process[0].strip().split('\n'):
908
+ if item.startswith('- '):
909
+ st.markdown(f"- {item[2:]}")
910
 
911
  # Recommendations Tab
912
  with tabs[4]:
913
+ st.subheader("Recommendations")
914
 
915
+ # Priority Actions
916
+ st.markdown("### Priority Actions 🎯")
917
+ priorities = re.findall(r'Priority Actions:\n((?:\d\. .*\n)*)', results)
918
+ if priorities:
919
+ for priority in priorities[0].strip().split('\n'):
920
+ if priority.strip():
921
+ st.markdown(f"{priority}")
922
+
923
+ # Development Strategies
924
+ st.markdown("### Development Strategies 📈")
925
+ strategies = re.findall(r'Development Strategies:\n((?:- .*\n)*)', results)
926
+ if strategies:
927
+ for strategy in strategies[0].strip().split('\n'):
928
+ if strategy.startswith('- '):
929
+ st.markdown(f"- {strategy[2:]}")
930
 
931
 
932
  def get_technique_description(technique):