norhan12 commited on
Commit
bbfad32
·
verified ·
1 Parent(s): 9eb83da

Update process_interview.py

Browse files
Files changed (1) hide show
  1. process_interview.py +14 -16
process_interview.py CHANGED
@@ -25,10 +25,9 @@ from reportlab.lib.units import inch
25
  from reportlab.lib import colors
26
  import matplotlib.pyplot as plt
27
  import matplotlib
28
-
29
- matplotlib.use('Agg') # --- FIX: تحديد backend لـ matplotlib ---
30
  from reportlab.platypus import Image
31
- import io # --- FIX: إضافة import io لـ BytesIO ---
32
  # --- End Imports for enhanced PDF ---
33
  from transformers import AutoTokenizer, AutoModel
34
  import spacy
@@ -216,14 +215,14 @@ def process_utterance(utterance, full_audio, wav_file):
216
  segment.export(temp_path, format="wav")
217
 
218
  with torch.no_grad():
219
- embedding = speaker_model.get_embedding(temp_path).cpu().numpy() # Ensure numpy array
220
-
221
  # --- FIX: Convert embedding to a flat list for Pinecone query ---
222
  embedding_list = embedding.flatten().tolist()
223
  # --- End FIX ---
224
 
225
  query_result = index.query(
226
- vector=embedding_list, # Use the corrected flat list
227
  top_k=1,
228
  include_metadata=True
229
  )
@@ -234,7 +233,7 @@ def process_utterance(utterance, full_audio, wav_file):
234
  else:
235
  speaker_id = f"unknown_{uuid.uuid4().hex[:6]}"
236
  speaker_name = f"Speaker_{speaker_id[-4:]}"
237
- index.upsert([(speaker_id, embedding_list, {"speaker_name": speaker_name})]) # Use corrected list
238
 
239
  os.remove(temp_path)
240
 
@@ -242,7 +241,7 @@ def process_utterance(utterance, full_audio, wav_file):
242
  **utterance,
243
  'speaker': speaker_name,
244
  'speaker_id': speaker_id,
245
- 'embedding': embedding_list # Store the corrected list
246
  }
247
  except Exception as e:
248
  logger.error(f"Utterance processing failed: {str(e)}", exc_info=True)
@@ -580,9 +579,9 @@ def calculate_acceptance_probability(analysis_data: Dict) -> float:
580
  # Normalize to 0-1 and then to percentage
581
  # These max/min values are rough estimates and should be calibrated with real data
582
  min_possible_score = (0 * w_confidence) + (0 * abs(w_anxiety)) + (0 * w_fluency) + (0 * w_speaking_rate) + (
583
- 0 * abs(w_filler_repetition)) + (0 * w_content_strengths)
584
  max_possible_score = (1 * w_confidence) + (1 * abs(w_anxiety)) + (1 * w_fluency) + (1 * w_speaking_rate) + (
585
- 1 * abs(w_filler_repetition)) + (1 * w_content_strengths)
586
 
587
  # Prevent division by zero if all weights are zero or min/max are same
588
  if max_possible_score == min_possible_score:
@@ -787,13 +786,12 @@ def create_pdf_report(analysis_data: Dict, output_path: str, gemini_report_text:
787
  # It relies on matplotlib and Image (from reportlab.platypus)
788
  generate_anxiety_confidence_chart(voice_analysis['composite_scores'], chart_path)
789
  if os.path.exists(chart_path):
790
- img = Image(chart_path, width=3.5 * inch, height=2.0 * inch)
791
  story.append(img)
792
  story.append(Spacer(1, 0.1 * inch))
793
  os.remove(chart_path)
794
- except NameError: # Catch NameError if function is truly not defined
795
- logger.warning(
796
- "Chart generation function 'generate_anxiety_confidence_chart' is not defined. Skipping chart.")
797
  except Exception as chart_e:
798
  logger.warning(f"Could not add chart image to PDF: {chart_e}. Please check matplotlib installation.")
799
  # --- End FIX ---
@@ -823,7 +821,7 @@ def create_pdf_report(analysis_data: Dict, output_path: str, gemini_report_text:
823
  if line.strip():
824
  # Handle bullet points from Gemini
825
  if line.strip().startswith('-'):
826
- story.append(Paragraph(line.strip()[1:].strip(), bullet_style)) # Remove the '-' and strip
827
  else:
828
  story.append(Paragraph(line.strip(), body_text))
829
  story.append(Spacer(1, 0.2 * inch))
@@ -846,7 +844,7 @@ def create_pdf_report(analysis_data: Dict, output_path: str, gemini_report_text:
846
  if line.strip():
847
  # Handle bullet points from Gemini
848
  if line.strip().startswith('-'):
849
- story.append(Paragraph(line.strip()[1:].strip(), bullet_style)) # Remove the '-' and strip
850
  else:
851
  story.append(Paragraph(line.strip(), body_text))
852
  story.append(Spacer(1, 0.2 * inch))
 
25
  from reportlab.lib import colors
26
  import matplotlib.pyplot as plt
27
  import matplotlib
28
+ matplotlib.use('Agg') # --- FIX: تحديد backend لـ matplotlib ---
 
29
  from reportlab.platypus import Image
30
+ import io # --- FIX: إضافة import io لـ BytesIO ---
31
  # --- End Imports for enhanced PDF ---
32
  from transformers import AutoTokenizer, AutoModel
33
  import spacy
 
215
  segment.export(temp_path, format="wav")
216
 
217
  with torch.no_grad():
218
+ embedding = speaker_model.get_embedding(temp_path).cpu().numpy() # Ensure numpy array
219
+
220
  # --- FIX: Convert embedding to a flat list for Pinecone query ---
221
  embedding_list = embedding.flatten().tolist()
222
  # --- End FIX ---
223
 
224
  query_result = index.query(
225
+ vector=embedding_list, # Use the corrected flat list
226
  top_k=1,
227
  include_metadata=True
228
  )
 
233
  else:
234
  speaker_id = f"unknown_{uuid.uuid4().hex[:6]}"
235
  speaker_name = f"Speaker_{speaker_id[-4:]}"
236
+ index.upsert([(speaker_id, embedding_list, {"speaker_name": speaker_name})]) # Use corrected list
237
 
238
  os.remove(temp_path)
239
 
 
241
  **utterance,
242
  'speaker': speaker_name,
243
  'speaker_id': speaker_id,
244
+ 'embedding': embedding_list # Store the corrected list
245
  }
246
  except Exception as e:
247
  logger.error(f"Utterance processing failed: {str(e)}", exc_info=True)
 
579
  # Normalize to 0-1 and then to percentage
580
  # These max/min values are rough estimates and should be calibrated with real data
581
  min_possible_score = (0 * w_confidence) + (0 * abs(w_anxiety)) + (0 * w_fluency) + (0 * w_speaking_rate) + (
582
+ 0 * abs(w_filler_repetition)) + (0 * w_content_strengths)
583
  max_possible_score = (1 * w_confidence) + (1 * abs(w_anxiety)) + (1 * w_fluency) + (1 * w_speaking_rate) + (
584
+ 1 * abs(w_filler_repetition)) + (1 * w_content_strengths)
585
 
586
  # Prevent division by zero if all weights are zero or min/max are same
587
  if max_possible_score == min_possible_score:
 
786
  # It relies on matplotlib and Image (from reportlab.platypus)
787
  generate_anxiety_confidence_chart(voice_analysis['composite_scores'], chart_path)
788
  if os.path.exists(chart_path):
789
+ img = Image(chart_path, width=3.5*inch, height=2.0*inch)
790
  story.append(img)
791
  story.append(Spacer(1, 0.1 * inch))
792
  os.remove(chart_path)
793
+ except NameError: # Catch NameError if function is truly not defined
794
+ logger.warning("Chart generation function 'generate_anxiety_confidence_chart' is not defined. Skipping chart.")
 
795
  except Exception as chart_e:
796
  logger.warning(f"Could not add chart image to PDF: {chart_e}. Please check matplotlib installation.")
797
  # --- End FIX ---
 
821
  if line.strip():
822
  # Handle bullet points from Gemini
823
  if line.strip().startswith('-'):
824
+ story.append(Paragraph(line.strip()[1:].strip(), bullet_style)) # Remove the '-' and strip
825
  else:
826
  story.append(Paragraph(line.strip(), body_text))
827
  story.append(Spacer(1, 0.2 * inch))
 
844
  if line.strip():
845
  # Handle bullet points from Gemini
846
  if line.strip().startswith('-'):
847
+ story.append(Paragraph(line.strip()[1:].strip(), bullet_style)) # Remove the '-' and strip
848
  else:
849
  story.append(Paragraph(line.strip(), body_text))
850
  story.append(Spacer(1, 0.2 * inch))