Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,65 +2,38 @@ import gradio as gr
|
|
2 |
import librosa
|
3 |
import numpy as np
|
4 |
import torch
|
5 |
-
import torch.nn.functional as F
|
6 |
import logging
|
7 |
from transformers import AutoModelForAudioClassification
|
8 |
-
import random
|
9 |
|
10 |
-
# Configure logging
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
13 |
-
# Load the model
|
14 |
model_path = "./"
|
15 |
model = AutoModelForAudioClassification.from_pretrained(model_path)
|
16 |
|
17 |
-
def
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
S = librosa.feature.melspectrogram(y=waveform, sr=sr, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length)
|
20 |
S_DB = librosa.power_to_db(S, ref=np.max)
|
21 |
-
|
22 |
-
# Pitch feature (using piptrack to estimate pitches and then averaging)
|
23 |
-
pitches, _ = librosa.piptrack(y=waveform, sr=sr, n_fft=n_fft, hop_length=hop_length)
|
24 |
-
pitch_mean = np.mean(pitches, axis=0, keepdims=True)
|
25 |
-
|
26 |
-
# Spectral centroid
|
27 |
-
spectral_centroids = librosa.feature.spectral_centroid(y=waveform, sr=sr, n_fft=n_fft, hop_length=hop_length)
|
28 |
-
|
29 |
-
# Concatenate features and normalize
|
30 |
-
features = np.concatenate([S_DB, pitch_mean, spectral_centroids], axis=0)
|
31 |
-
features_tensor = torch.tensor(features).float()
|
32 |
-
|
33 |
-
# Adjust the tensor's length
|
34 |
-
if features_tensor.shape[1] > target_length:
|
35 |
-
features_tensor = features_tensor[:, :target_length]
|
36 |
-
elif features_tensor.shape[1] < target_length:
|
37 |
-
padding = target_length - features_tensor.shape[1]
|
38 |
-
features_tensor = F.pad(features_tensor, (0, padding), 'constant', 0)
|
39 |
-
|
40 |
-
return features_tensor.unsqueeze(0)
|
41 |
|
42 |
-
def
|
43 |
-
shift_amount = int(max_shift_fraction * len(waveform))
|
44 |
-
shift = random.randint(-shift_amount, shift_amount)
|
45 |
-
return np.roll(waveform, shift)
|
46 |
-
|
47 |
-
def predict_voice(audio_file):
|
48 |
try:
|
49 |
-
waveform
|
50 |
-
|
51 |
-
|
52 |
-
original_features = custom_feature_extraction(waveform, sample_rate)
|
53 |
-
augmented_features = custom_feature_extraction(augmented_waveform, sample_rate)
|
54 |
-
|
55 |
with torch.no_grad():
|
56 |
-
|
57 |
-
|
58 |
-
logits = (outputs_original.logits + outputs_augmented.logits) / 2
|
59 |
predicted_index = logits.argmax()
|
60 |
label = model.config.id2label[predicted_index.item()]
|
61 |
confidence = torch.softmax(logits, dim=1).max().item() * 100
|
62 |
-
|
63 |
result = f"The voice is classified as '{label}' with a confidence of {confidence:.2f}%."
|
|
|
64 |
except Exception as e:
|
65 |
result = f"Error during processing: {e}"
|
66 |
logging.error(result)
|
@@ -69,8 +42,8 @@ def predict_voice(audio_file):
|
|
69 |
|
70 |
iface = gr.Interface(
|
71 |
fn=predict_voice,
|
72 |
-
inputs=gr.Audio(label="Upload Audio File", type="
|
73 |
-
outputs=gr.
|
74 |
title="Voice Authenticity Detection",
|
75 |
description="Detects whether a voice is real or AI-generated. Upload an audio file to see the results."
|
76 |
)
|
|
|
2 |
import librosa
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
5 |
import logging
|
6 |
from transformers import AutoModelForAudioClassification
|
|
|
7 |
|
|
|
8 |
logging.basicConfig(level=logging.INFO)
|
9 |
|
|
|
10 |
model_path = "./"
|
11 |
model = AutoModelForAudioClassification.from_pretrained(model_path)
|
12 |
|
13 |
+
def preprocess_audio(audio_file_path, sr=16000):
|
14 |
+
waveform, _ = librosa.load(audio_file_path, sr=sr)
|
15 |
+
waveform = librosa.effects.trim(waveform)[0] # Trim silence
|
16 |
+
return waveform
|
17 |
+
|
18 |
+
def extract_features(waveform, sr=16000, n_mels=128, n_fft=2048, hop_length=512):
|
19 |
S = librosa.feature.melspectrogram(y=waveform, sr=sr, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length)
|
20 |
S_DB = librosa.power_to_db(S, ref=np.max)
|
21 |
+
return torch.tensor(S_DB).float().unsqueeze(0) # Add batch dimension
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
def predict_voice(audio_file_path):
|
|
|
|
|
|
|
|
|
|
|
24 |
try:
|
25 |
+
waveform = preprocess_audio(audio_file_path)
|
26 |
+
features = extract_features(waveform)
|
27 |
+
|
|
|
|
|
|
|
28 |
with torch.no_grad():
|
29 |
+
outputs = model(features)
|
30 |
+
logits = outputs.logits
|
|
|
31 |
predicted_index = logits.argmax()
|
32 |
label = model.config.id2label[predicted_index.item()]
|
33 |
confidence = torch.softmax(logits, dim=1).max().item() * 100
|
34 |
+
|
35 |
result = f"The voice is classified as '{label}' with a confidence of {confidence:.2f}%."
|
36 |
+
logging.info("Prediction successful.")
|
37 |
except Exception as e:
|
38 |
result = f"Error during processing: {e}"
|
39 |
logging.error(result)
|
|
|
42 |
|
43 |
iface = gr.Interface(
|
44 |
fn=predict_voice,
|
45 |
+
inputs=gr.Audio(label="Upload Audio File", type="file"),
|
46 |
+
outputs=gr.Text(label="Prediction"),
|
47 |
title="Voice Authenticity Detection",
|
48 |
description="Detects whether a voice is real or AI-generated. Upload an audio file to see the results."
|
49 |
)
|