karlhajal commited on
Commit
99bb2c7
·
verified ·
1 Parent(s): 0926452

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -6
app.py CHANGED
@@ -11,9 +11,10 @@ from src.pronunciation_checker import PronunciationChecker
11
  from src.audio_preprocessing import assess_pronunciation_quality
12
  from datetime import datetime
13
  from pathlib import Path
 
14
 
15
  @spaces.GPU
16
- def check_pronunciation(reference_audio, input_audio, threshold, wavlm_layer):
17
  wavlm_layer = int(wavlm_layer)
18
 
19
  timing_results = []
@@ -54,7 +55,7 @@ def check_pronunciation(reference_audio, input_audio, threshold, wavlm_layer):
54
  raise ValueError("DTW computation failed.")
55
 
56
 
57
- PronunciationChecker.plot_waveform_with_overlay(ref_wav, sr, dist_matrix, path, "ref", threshold)
58
  log_timing("Visualization Generation")
59
 
60
  # Save the visualization to a temporary image file
@@ -71,6 +72,19 @@ def check_pronunciation(reference_audio, input_audio, threshold, wavlm_layer):
71
  # Return the image file path for Gradio to display
72
  return tmp_path, quality_score, needs_repeat, timing_analysis
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  @spaces.GPU
75
  def run_tests(threshold=0.4, wavlm_layer=24):
76
  # Create a directory for storing plots if it doesn't exist
@@ -88,15 +102,18 @@ def run_tests(threshold=0.4, wavlm_layer=24):
88
  if test_word_dir.is_dir():
89
  word = test_word_dir.name
90
  ref_file = test_word_dir / "ref.wav"
 
91
 
92
- if not ref_file.exists():
93
  test_cases.append({
94
- "heading": f"Error: Missing reference file for '{word}'",
95
  "plot": None,
96
- "text": f"Reference file not found for test word '{word}'."
97
  })
98
  continue
99
 
 
 
100
  for input_file in test_word_dir.glob("input_*.wav"):
101
  if not input_file.exists():
102
  test_cases.append({
@@ -111,9 +128,12 @@ def run_tests(threshold=0.4, wavlm_layer=24):
111
  plot_filename = f"{word}_{input_file.stem}_{datetime.now().strftime('%H%M%S')}.png"
112
  plot_path = plots_dir / plot_filename
113
 
 
 
 
114
  # Run the pronunciation checker
115
  tmp_path, quality_score, needs_repeat, timing_analysis = check_pronunciation(
116
- ref_file, input_file, threshold, wavlm_layer
117
  )
118
 
119
  # Move the plot to our persistent directory
 
11
  from src.audio_preprocessing import assess_pronunciation_quality
12
  from datetime import datetime
13
  from pathlib import Path
14
+ import re
15
 
16
  @spaces.GPU
17
+ def check_pronunciation(reference_audio, input_audio, threshold, wavlm_layer, labels_data=None, input_number=None):
18
  wavlm_layer = int(wavlm_layer)
19
 
20
  timing_results = []
 
55
  raise ValueError("DTW computation failed.")
56
 
57
 
58
+ PronunciationChecker.plot_waveform_with_overlay(ref_wav, sr, dist_matrix, path, "ref", threshold, labels_data, input_number)
59
  log_timing("Visualization Generation")
60
 
61
  # Save the visualization to a temporary image file
 
72
  # Return the image file path for Gradio to display
73
  return tmp_path, quality_score, needs_repeat, timing_analysis
74
 
75
+ def parse_labels_file(labels_file):
76
+ labels_data = []
77
+ with open(labels_file, 'r') as f:
78
+ for line in f:
79
+ parts = re.split(r'[\t ]+', line)
80
+ if len(parts) < 3:
81
+ continue
82
+ start, end = map(float, parts[:2])
83
+ grapheme = parts[2]
84
+ boolean_labels = list(map(int, parts[3:]))
85
+ labels_data.append((start, end, grapheme, *boolean_labels))
86
+ return labels_data
87
+
88
  @spaces.GPU
89
  def run_tests(threshold=0.4, wavlm_layer=24):
90
  # Create a directory for storing plots if it doesn't exist
 
102
  if test_word_dir.is_dir():
103
  word = test_word_dir.name
104
  ref_file = test_word_dir / "ref.wav"
105
+ labels_file = test_word_dir / "labels.txt"
106
 
107
+ if not ref_file.exists() or not labels_file.exists():
108
  test_cases.append({
109
+ "heading": f"Error: Missing files for '{word}'",
110
  "plot": None,
111
+ "text": f"Missing required files (ref.wav or labels.txt) for test word '{word}'."
112
  })
113
  continue
114
 
115
+ labels_data = parse_labels_file(labels_file)
116
+
117
  for input_file in test_word_dir.glob("input_*.wav"):
118
  if not input_file.exists():
119
  test_cases.append({
 
128
  plot_filename = f"{word}_{input_file.stem}_{datetime.now().strftime('%H%M%S')}.png"
129
  plot_path = plots_dir / plot_filename
130
 
131
+ input_number = str(input_file.stem).split('_')[1]
132
+ input_number = int(input_number)-1
133
+
134
  # Run the pronunciation checker
135
  tmp_path, quality_score, needs_repeat, timing_analysis = check_pronunciation(
136
+ ref_file, input_file, threshold, wavlm_layer, labels_data, input_number
137
  )
138
 
139
  # Move the plot to our persistent directory