Hammad712 commited on
Commit
54aea1b
·
verified ·
1 Parent(s): 6954723

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -2
main.py CHANGED
@@ -8,11 +8,13 @@ from fastapi.responses import JSONResponse
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from pydantic import BaseModel
10
  from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
11
- from librosa.sequence import dtw
12
  import tempfile
13
  import uuid
14
  import shutil
15
 
 
 
 
16
  # Initialize FastAPI app
17
  app = FastAPI(
18
  title="Quran Recitation Comparison API",
@@ -114,11 +116,60 @@ def get_deep_embedding(audio, sr=16000):
114
  except Exception as e:
115
  raise HTTPException(status_code=500, detail=f"Error extracting embeddings: {e}")
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Compute DTW distance
118
  def compute_dtw_distance(features1, features2):
119
  """Compute the DTW distance between two sequences of features."""
120
  try:
121
- D, wp = dtw(X=features1, Y=features2, metric='euclidean')
 
122
  distance = D[-1, -1]
123
  normalized_distance = distance / len(wp)
124
  return normalized_distance
 
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from pydantic import BaseModel
10
  from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
 
11
  import tempfile
12
  import uuid
13
  import shutil
14
 
15
+ # Disable numba JIT to avoid caching issues
16
+ os.environ["NUMBA_DISABLE_JIT"] = "1"
17
+
18
  # Initialize FastAPI app
19
  app = FastAPI(
20
  title="Quran Recitation Comparison API",
 
116
  except Exception as e:
117
  raise HTTPException(status_code=500, detail=f"Error extracting embeddings: {e}")
118
 
119
+ # Custom DTW implementation to avoid librosa.sequence.dtw issues
120
+ def custom_dtw(X, Y, metric='euclidean'):
121
+ """
122
+ Custom implementation of DTW to avoid librosa.sequence.dtw issues.
123
+
124
+ Parameters:
125
+ X, Y : numpy.ndarray
126
+ The two sequences to be aligned
127
+ metric : str, optional
128
+ The distance metric to use
129
+
130
+ Returns:
131
+ D : numpy.ndarray
132
+ The accumulated cost matrix
133
+ wp : list
134
+ The warping path
135
+ """
136
+ # Initialize cost matrix
137
+ n, m = len(X[0]), len(Y[0])
138
+ D = np.zeros((n+1, m+1))
139
+ D[0, :] = np.inf
140
+ D[:, 0] = np.inf
141
+ D[0, 0] = 0
142
+
143
+ # Fill cost matrix
144
+ for i in range(1, n+1):
145
+ for j in range(1, m+1):
146
+ if metric == 'euclidean':
147
+ cost = np.sqrt(np.sum((X[:, i-1] - Y[:, j-1])**2))
148
+ elif metric == 'cosine':
149
+ cost = 1 - np.dot(X[:, i-1], Y[:, j-1]) / (np.linalg.norm(X[:, i-1]) * np.linalg.norm(Y[:, j-1]))
150
+ else:
151
+ cost = np.sum(np.abs(X[:, i-1] - Y[:, j-1])) # Manhattan by default
152
+
153
+ D[i, j] = cost + min(D[i-1, j], D[i, j-1], D[i-1, j-1])
154
+
155
+ # Backtrack to find warping path
156
+ i, j = n, m
157
+ wp = [(i, j)]
158
+ while i > 1 or j > 1:
159
+ candidates = [(i-1, j-1), (i-1, j), (i, j-1)]
160
+ valid_candidates = [(ii, jj) for ii, jj in candidates if ii > 0 and jj > 0]
161
+ i, j = min(valid_candidates, key=lambda x: D[x[0], x[1]])
162
+ wp.append((i, j))
163
+
164
+ wp.reverse()
165
+ return D, wp
166
+
167
  # Compute DTW distance
168
  def compute_dtw_distance(features1, features2):
169
  """Compute the DTW distance between two sequences of features."""
170
  try:
171
+ # Use custom DTW implementation instead of librosa's
172
+ D, wp = custom_dtw(features1, features2, metric='euclidean')
173
  distance = D[-1, -1]
174
  normalized_distance = distance / len(wp)
175
  return normalized_distance