Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ from fastapi import FastAPI, UploadFile, File, Form, Request
|
|
| 16 |
from fastapi.responses import JSONResponse
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
from datetime import datetime, timedelta
|
| 19 |
-
|
| 20 |
if not hasattr(torchaudio, 'list_audio_backends'):
|
| 21 |
torchaudio.list_audio_backends = lambda: ["soundfile"]
|
| 22 |
|
|
@@ -30,8 +30,8 @@ MAX_SEC = 4
|
|
| 30 |
MAX_LEN = SAMPLE_RATE * MAX_SEC
|
| 31 |
THRESHOLD = 0.4200
|
| 32 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 33 |
-
NUM_CLEAN_SAMPLES =
|
| 34 |
-
NUM_NOISY_COPIES =
|
| 35 |
MAX_ATTEMPTS = 3
|
| 36 |
LOCKOUT_MINUTES = 5
|
| 37 |
COOLDOWN_SECONDS = 3
|
|
@@ -237,6 +237,22 @@ def add_noise(wav_tensor, noise_level=0.005):
|
|
| 237 |
noise = torch.randn_like(wav_tensor) * noise_level
|
| 238 |
return wav_tensor + noise
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
def check_liveness(wav_tensor):
|
| 242 |
wav_np = wav_tensor.numpy()
|
|
@@ -366,8 +382,7 @@ def enroll_sample(audio_input, user_id, full_name, sample_number, total_samples=
|
|
| 366 |
clean_emb = extract_embedding(wav)
|
| 367 |
noisy_embeddings = []
|
| 368 |
for i in range(NUM_NOISY_COPIES):
|
| 369 |
-
|
| 370 |
-
noisy_wav = add_noise(wav, noise_level)
|
| 371 |
noisy_emb = extract_embedding(noisy_wav)
|
| 372 |
noisy_embeddings.append(noisy_emb)
|
| 373 |
db = load_db()
|
|
@@ -747,7 +762,7 @@ with gr.Blocks(title="ATM Voice Authentication System") as demo:
|
|
| 747 |
with gr.Column():
|
| 748 |
enroll_audio = gr.Audio(label="Record Voice Sample", sources=["microphone", "upload"], type="numpy")
|
| 749 |
enroll_user_id = gr.Textbox(label="User ID (e.g., ATM_001)", placeholder="ATM_001")
|
| 750 |
-
enroll_name = gr.Textbox(label="Full Name", placeholder="
|
| 751 |
enroll_sample_num = gr.Number(label="Sample Number", value=1, minimum=1, maximum=6, step=1)
|
| 752 |
enroll_btn = gr.Button("Enroll Sample", variant="primary")
|
| 753 |
with gr.Column():
|
|
|
|
| 16 |
from fastapi.responses import JSONResponse
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
from datetime import datetime, timedelta
|
| 19 |
+
|
| 20 |
if not hasattr(torchaudio, 'list_audio_backends'):
|
| 21 |
torchaudio.list_audio_backends = lambda: ["soundfile"]
|
| 22 |
|
|
|
|
| 30 |
MAX_LEN = SAMPLE_RATE * MAX_SEC
|
| 31 |
THRESHOLD = 0.4200
|
| 32 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 33 |
+
NUM_CLEAN_SAMPLES = 6
|
| 34 |
+
NUM_NOISY_COPIES = 4
|
| 35 |
MAX_ATTEMPTS = 3
|
| 36 |
LOCKOUT_MINUTES = 5
|
| 37 |
COOLDOWN_SECONDS = 3
|
|
|
|
| 237 |
noise = torch.randn_like(wav_tensor) * noise_level
|
| 238 |
return wav_tensor + noise
|
| 239 |
|
| 240 |
+
def add_noise_robust(wav_tensor, augment_type=0):
|
| 241 |
+
if augment_type == 0:
|
| 242 |
+
noise = torch.randn_like(wav_tensor) * 0.005
|
| 243 |
+
return wav_tensor + noise
|
| 244 |
+
elif augment_type == 1:
|
| 245 |
+
noise = torch.randn_like(wav_tensor) * 0.015
|
| 246 |
+
return wav_tensor + noise
|
| 247 |
+
elif augment_type == 2:
|
| 248 |
+
shift = int(0.1 * len(wav_tensor))
|
| 249 |
+
padded = torch.cat([wav_tensor[shift:], torch.zeros(shift)])
|
| 250 |
+
return wav_tensor * 0.7 + padded * 0.3
|
| 251 |
+
elif augment_type == 3:
|
| 252 |
+
factor = 0.7 + torch.rand(1).item() * 0.6
|
| 253 |
+
return wav_tensor * factor
|
| 254 |
+
return wav_tensor
|
| 255 |
+
|
| 256 |
|
| 257 |
def check_liveness(wav_tensor):
|
| 258 |
wav_np = wav_tensor.numpy()
|
|
|
|
| 382 |
clean_emb = extract_embedding(wav)
|
| 383 |
noisy_embeddings = []
|
| 384 |
for i in range(NUM_NOISY_COPIES):
|
| 385 |
+
noisy_wav = add_noise_robust(wav, augment_type=i % 4)
|
|
|
|
| 386 |
noisy_emb = extract_embedding(noisy_wav)
|
| 387 |
noisy_embeddings.append(noisy_emb)
|
| 388 |
db = load_db()
|
|
|
|
| 762 |
with gr.Column():
|
| 763 |
enroll_audio = gr.Audio(label="Record Voice Sample", sources=["microphone", "upload"], type="numpy")
|
| 764 |
enroll_user_id = gr.Textbox(label="User ID (e.g., ATM_001)", placeholder="ATM_001")
|
| 765 |
+
enroll_name = gr.Textbox(label="Full Name", placeholder="John Doe")
|
| 766 |
enroll_sample_num = gr.Number(label="Sample Number", value=1, minimum=1, maximum=6, step=1)
|
| 767 |
enroll_btn = gr.Button("Enroll Sample", variant="primary")
|
| 768 |
with gr.Column():
|