Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import streamlit as st
|
|
2 |
import librosa
|
3 |
import numpy as np
|
4 |
import onnxruntime as ort
|
|
|
|
|
5 |
|
6 |
# Audio padding function
|
7 |
def pad(x, max_len=64600):
|
@@ -24,13 +26,29 @@ def preprocess_audio_segment(segment, cut=64600):
|
|
24 |
segment = pad(segment, max_len=cut)
|
25 |
return np.expand_dims(np.array(segment, dtype=np.float32), axis=0) # Add batch dimension
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Sliding window prediction function
|
28 |
-
def predict_with_sliding_window(audio_path,
|
29 |
"""
|
30 |
Use a sliding window to predict if the audio is real or fake over the entire audio.
|
31 |
"""
|
32 |
# Load ONNX runtime session
|
33 |
-
ort_session = ort.InferenceSession(
|
34 |
|
35 |
# Load audio file
|
36 |
waveform, _ = librosa.load(audio_path, sr=sample_rate)
|
@@ -69,22 +87,25 @@ st.write("Upload an audio file to detect if it is Real or Fake.")
|
|
69 |
# File uploader
|
70 |
uploaded_file = st.file_uploader("Upload your audio file (WAV or MP3)", type=["wav", "mp3"])
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
onnx_model_url = "https://huggingface.co/Mrkomiljon/DeepVoiceGuard/resolve/main/RawNet_model.onnx"
|
75 |
|
|
|
|
|
|
|
|
|
76 |
# Save uploaded file temporarily
|
77 |
with open("temp_audio_file.wav", "wb") as f:
|
78 |
f.write(uploaded_file.read())
|
79 |
|
80 |
# Perform prediction
|
81 |
with st.spinner("Processing..."):
|
82 |
-
result, avg_probability = predict_with_sliding_window("temp_audio_file.wav",
|
83 |
|
84 |
# Display results
|
85 |
st.success(f"Prediction: {result}")
|
86 |
st.info(f"Confidence: {avg_probability:.2f}%")
|
87 |
|
88 |
# Clean up temporary file
|
89 |
-
import os
|
90 |
os.remove("temp_audio_file.wav")
|
|
|
|
2 |
import librosa
|
3 |
import numpy as np
|
4 |
import onnxruntime as ort
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
|
8 |
# Audio padding function
|
9 |
def pad(x, max_len=64600):
|
|
|
26 |
segment = pad(segment, max_len=cut)
|
27 |
return np.expand_dims(np.array(segment, dtype=np.float32), axis=0) # Add batch dimension
|
28 |
|
29 |
+
# Download ONNX model from Hugging Face
|
30 |
+
def download_model(url, local_path="RawNet_model.onnx"):
|
31 |
+
"""
|
32 |
+
Download the ONNX model from a URL if it doesn't already exist locally.
|
33 |
+
"""
|
34 |
+
if not os.path.exists(local_path):
|
35 |
+
with st.spinner("Downloading ONNX model..."):
|
36 |
+
response = requests.get(url)
|
37 |
+
if response.status_code == 200:
|
38 |
+
with open(local_path, "wb") as f:
|
39 |
+
f.write(response.content)
|
40 |
+
st.success("Model downloaded successfully!")
|
41 |
+
else:
|
42 |
+
raise Exception("Failed to download ONNX model")
|
43 |
+
return local_path
|
44 |
+
|
45 |
# Sliding window prediction function
|
46 |
+
def predict_with_sliding_window(audio_path, onnx_model_path, window_size=64600, step_size=64600, sample_rate=16000):
|
47 |
"""
|
48 |
Use a sliding window to predict if the audio is real or fake over the entire audio.
|
49 |
"""
|
50 |
# Load ONNX runtime session
|
51 |
+
ort_session = ort.InferenceSession(onnx_model_path)
|
52 |
|
53 |
# Load audio file
|
54 |
waveform, _ = librosa.load(audio_path, sr=sample_rate)
|
|
|
87 |
# File uploader
|
88 |
uploaded_file = st.file_uploader("Upload your audio file (WAV or MP3)", type=["wav", "mp3"])
|
89 |
|
90 |
+
# ONNX model URL (replace with your actual Hugging Face model URL)
|
91 |
+
onnx_model_url = "https://huggingface.co/Mrkomiljon/DeepVoiceGuard/resolve/main/RawNet_model.onnx"
|
|
|
92 |
|
93 |
+
# Ensure ONNX model is downloaded locally
|
94 |
+
onnx_model_path = download_model(onnx_model_url)
|
95 |
+
|
96 |
+
if uploaded_file is not None:
|
97 |
# Save uploaded file temporarily
|
98 |
with open("temp_audio_file.wav", "wb") as f:
|
99 |
f.write(uploaded_file.read())
|
100 |
|
101 |
# Perform prediction
|
102 |
with st.spinner("Processing..."):
|
103 |
+
result, avg_probability = predict_with_sliding_window("temp_audio_file.wav", onnx_model_path)
|
104 |
|
105 |
# Display results
|
106 |
st.success(f"Prediction: {result}")
|
107 |
st.info(f"Confidence: {avg_probability:.2f}%")
|
108 |
|
109 |
# Clean up temporary file
|
|
|
110 |
os.remove("temp_audio_file.wav")
|
111 |
+
|