Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import librosa
|
|
8 |
# Path to the local directory where the model files are stored within the Space
|
9 |
local_model_path = "./"
|
10 |
|
11 |
-
#
|
12 |
extractor = AutoFeatureExtractor.from_pretrained(local_model_path)
|
13 |
model = AutoModelForAudioClassification.from_pretrained(local_model_path)
|
14 |
|
@@ -22,44 +22,41 @@ def predict_voice(audio_file_path):
|
|
22 |
Returns:
|
23 |
A string with the prediction and confidence level.
|
24 |
"""
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
waveform =
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# Prepare the output string.
|
51 |
-
result = f"The voice is classified as '{label}' with a confidence of {confidence:.2f}%."
|
52 |
return result
|
53 |
|
54 |
# Setting up the Gradio interface
|
55 |
iface = gr.Interface(
|
56 |
fn=predict_voice,
|
57 |
-
inputs=gr.Audio(label="Upload Audio File", type="filepath"),
|
58 |
outputs=gr.Textbox(label="Prediction"),
|
59 |
title="Voice Authenticity Detection",
|
60 |
description="Detects whether a voice is real or AI-generated. Upload an audio file to see the results.",
|
61 |
theme="huggingface"
|
62 |
)
|
63 |
|
64 |
-
# Run the Gradio interface
|
65 |
-
iface.launch(share=True)
|
|
|
8 |
# Path to the local directory where the model files are stored within the Space
|
9 |
local_model_path = "./"
|
10 |
|
11 |
+
# Load the model and feature extractor outside the function to improve performance
|
12 |
extractor = AutoFeatureExtractor.from_pretrained(local_model_path)
|
13 |
model = AutoModelForAudioClassification.from_pretrained(local_model_path)
|
14 |
|
|
|
22 |
Returns:
|
23 |
A string with the prediction and confidence level.
|
24 |
"""
|
25 |
+
try:
|
26 |
+
# Ensure the file path does not lead to unintended directories
|
27 |
+
if not audio_file_path.startswith("/expected/path/for/safety"):
|
28 |
+
return "Error: Invalid file path."
|
29 |
+
|
30 |
+
# Load and preprocess the audio file
|
31 |
+
waveform, sample_rate = librosa.load(audio_file_path, sr=16000, mono=True)
|
32 |
+
|
33 |
+
# Convert the input audio file to model's expected format
|
34 |
+
inputs = extractor(waveform, return_tensors="pt", sampling_rate=sample_rate)
|
35 |
+
|
36 |
+
# Generate predictions from the model
|
37 |
+
with torch.no_grad(): # Ensure no gradients are calculated
|
38 |
+
outputs = model(**inputs)
|
39 |
+
|
40 |
+
# Extract logits, compute the class with the highest score, and calculate confidence
|
41 |
+
logits = outputs.logits
|
42 |
+
predicted_index = logits.argmax()
|
43 |
+
label = model.config.id2label[predicted_index.item()]
|
44 |
+
confidence = softmax(logits, dim=1).max().item() * 100
|
45 |
+
|
46 |
+
result = f"The voice is classified as '{label}' with a confidence of {confidence:.2f}%."
|
47 |
+
except Exception as e:
|
48 |
+
result = f"An error occurred during processing: {str(e)}"
|
|
|
|
|
|
|
49 |
return result
|
50 |
|
51 |
# Setting up the Gradio interface
|
52 |
iface = gr.Interface(
|
53 |
fn=predict_voice,
|
54 |
+
inputs=gr.Audio(label="Upload Audio File", type="filepath"),
|
55 |
outputs=gr.Textbox(label="Prediction"),
|
56 |
title="Voice Authenticity Detection",
|
57 |
description="Detects whether a voice is real or AI-generated. Upload an audio file to see the results.",
|
58 |
theme="huggingface"
|
59 |
)
|
60 |
|
61 |
+
# Run the Gradio interface, consider using enable_queue=True if processing is expected to be long or the app faces high traffic
|
62 |
+
iface.launch(share=True, enable_queue=True)
|