mkunz7 commited on
Commit
868f0a3
1 Parent(s): 3b41dc9

Update host.py

Browse files
Files changed (1) hide show
  1. host.py +21 -9
host.py CHANGED
@@ -7,6 +7,7 @@ import librosa
7
  from scipy.spatial.distance import cosine
8
  import numpy as np
9
  import os
 
10
  # brew install ffmpeg
11
  # pip install flask transformers librosa torch torchaudio
12
 
@@ -43,6 +44,15 @@ def preprocess_audio(audio_data):
43
  waveform = waveform.squeeze().numpy()
44
  return waveform
45
 
 
 
 
 
 
 
 
 
 
46
  @app.route('/')
47
  def index():
48
  return render_template('index.html')
@@ -58,6 +68,7 @@ def chal2():
58
  @app.route('/compare_audio', methods=['POST'])
59
  def compare_audio():
60
  try:
 
61
  # Get the recorded audio file from the frontend
62
  recorded_audio = request.files['audio_data']
63
 
@@ -68,22 +79,20 @@ def compare_audio():
68
  embeddings_normalized = torch.nn.functional.normalize(embeddings, dim=-1).cpu()
69
 
70
  # Load and preprocess MP3 file for comparison
71
- mp3_audio = preprocess_audio(mp3_file_path)
72
- mp3_inputs = feature_extractor(mp3_audio, return_tensors="pt")
73
- mp3_embeddings = model(**mp3_inputs).embeddings
74
- mp3_embeddings_normalized = torch.nn.functional.normalize(mp3_embeddings, dim=-1).cpu()
75
 
76
  # Calculate cosine similarity
77
  cosine_sim = torch.nn.CosineSimilarity(dim=-1)
78
  similarity = cosine_sim(embeddings_normalized, mp3_embeddings_normalized).item()
79
 
80
  similarity = round(similarity, 3)
 
81
 
82
  threshold = 0.89 # Adjust the threshold as needed
83
  if similarity < threshold:
84
- result = "Authorization Failed! " + str(similarity) + " < 0.890<br>Do your best Terminator impression"
85
  else:
86
- result = "Good job! Match: " + str(similarity) + "<br>" + flag1 + "<br><a href='/chal2'>Click here to open the next challenge</a>"
87
 
88
  return jsonify({'result': result})
89
  except Exception as e:
@@ -118,15 +127,18 @@ def preprocess_audio2(audio_bytes):
118
 
119
  return waveform
120
 
 
 
121
  @app.route('/compare_audio2', methods=['POST'])
122
  def compare_audio2():
123
  try:
124
  recorded_audio = request.files['audio_data'].read()
125
- mp3_audio = open(mp3_file_path2, 'rb').read()
126
 
127
  # Compare similarity between audio
128
  mfcc1 = extract_mfcc(recorded_audio)
129
- mfcc2 = extract_mfcc(mp3_audio)
 
130
  similarity = 1 - cosine(np.mean(mfcc1, axis=1), np.mean(mfcc2, axis=1))
131
  similarity = round(similarity, 3)
132
  if similarity < 0.940:
@@ -140,4 +152,4 @@ def compare_audio2():
140
  return jsonify({'error': 'An error occurred during audio comparison. Im fragile please dont abuse.'})
141
 
142
  if __name__ == '__main__':
143
- app.run(host="0.0.0.0", port=8080, debug=True)
 
7
  from scipy.spatial.distance import cosine
8
  import numpy as np
9
  import os
10
+ import time
11
  # brew install ffmpeg
12
  # pip install flask transformers librosa torch torchaudio
13
 
 
44
  waveform = waveform.squeeze().numpy()
45
  return waveform
46
 
47
+
48
+ # Preload expected audio
49
+ mp3_audio = preprocess_audio(mp3_file_path)
50
+ mp3_inputs = feature_extractor(mp3_audio, return_tensors="pt")
51
+ mp3_embeddings = model(**mp3_inputs).embeddings
52
+ mp3_embeddings_normalized = torch.nn.functional.normalize(mp3_embeddings, dim=-1).cpu()
53
+
54
+ mp3_audio2 = open(mp3_file_path2, 'rb').read()
55
+
56
  @app.route('/')
57
  def index():
58
  return render_template('index.html')
 
68
  @app.route('/compare_audio', methods=['POST'])
69
  def compare_audio():
70
  try:
71
+ start_time = time.time()
72
  # Get the recorded audio file from the frontend
73
  recorded_audio = request.files['audio_data']
74
 
 
79
  embeddings_normalized = torch.nn.functional.normalize(embeddings, dim=-1).cpu()
80
 
81
  # Load and preprocess MP3 file for comparison
82
+ global mp3_embeddings_normalized
 
 
 
83
 
84
  # Calculate cosine similarity
85
  cosine_sim = torch.nn.CosineSimilarity(dim=-1)
86
  similarity = cosine_sim(embeddings_normalized, mp3_embeddings_normalized).item()
87
 
88
  similarity = round(similarity, 3)
89
+ end = time.time()-start_time
90
 
91
  threshold = 0.89 # Adjust the threshold as needed
92
  if similarity < threshold:
93
+ result = "Authorization Failed! " + str(similarity) + " < 0.890"+" in "+str(round(end,3))+"s"+"<br>Do your best Terminator impression"
94
  else:
95
+ result = "Good job! Match: " + str(similarity) + "<br>" + flag1 + "<br><a href='/chal2'>Click here to open the next challenge</a>"+"<br>processed in "+str(round(end,3))+"s"
96
 
97
  return jsonify({'result': result})
98
  except Exception as e:
 
127
 
128
  return waveform
129
 
130
+ mfcc2 = extract_mfcc(mp3_audio2)
131
+
132
  @app.route('/compare_audio2', methods=['POST'])
133
  def compare_audio2():
134
  try:
135
  recorded_audio = request.files['audio_data'].read()
136
+ #mp3_audio = open(mp3_file_path2, 'rb').read()
137
 
138
  # Compare similarity between audio
139
  mfcc1 = extract_mfcc(recorded_audio)
140
+ #mfcc2 = extract_mfcc(mp3_audio)
141
+ global mfcc2
142
  similarity = 1 - cosine(np.mean(mfcc1, axis=1), np.mean(mfcc2, axis=1))
143
  similarity = round(similarity, 3)
144
  if similarity < 0.940:
 
152
  return jsonify({'error': 'An error occurred during audio comparison. Im fragile please dont abuse.'})
153
 
154
  if __name__ == '__main__':
155
+ app.run(host="0.0.0.0", port=8080, debug=True)