Spaces:
Runtime error
Runtime error
Ajay Karthick Senthil Kumar
commited on
Commit
·
416dc49
1
Parent(s):
381c43b
add streamlit
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import librosa
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
from src.features.extraction.low_level_features_extractor import LowLevelFeatureExtractor
|
| 9 |
+
from src.features.extraction.high_level_features_extractor import HighLevelFeatureExtractor
|
| 10 |
+
from src.models.predict import predict
|
| 11 |
+
|
| 12 |
+
# Set page layout
|
| 13 |
+
st.set_page_config(page_title="Audio Deepfake Detection", layout="wide")
|
| 14 |
+
|
| 15 |
+
# Add a custom style for background and font
|
| 16 |
+
st.markdown("""
|
| 17 |
+
<style>
|
| 18 |
+
.main {
|
| 19 |
+
background-color: #f7f9fc;
|
| 20 |
+
}
|
| 21 |
+
.title {
|
| 22 |
+
font-family: 'Courier New', Courier, monospace;
|
| 23 |
+
color: #493628;
|
| 24 |
+
}
|
| 25 |
+
.section-header {
|
| 26 |
+
font-size: 24px;
|
| 27 |
+
font-weight: bold;
|
| 28 |
+
margin-top: 10px; /* Reduced margin to minimize vertical gap */
|
| 29 |
+
}
|
| 30 |
+
.confidence-score {
|
| 31 |
+
font-size: 20px;
|
| 32 |
+
font-weight: bold;
|
| 33 |
+
color: #ff6f61;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# App title
|
| 39 |
+
st.markdown('<h1 class="title">Audio Deepfake Detection</h1>', unsafe_allow_html=True)
|
| 40 |
+
st.write("This application helps you detect whether an audio file is a deepfake or genuine.")
|
| 41 |
+
|
| 42 |
+
# File uploader
|
| 43 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "ogg"])
|
| 44 |
+
|
| 45 |
+
# Extract features from audio
|
| 46 |
+
def extract_features(audio_data, sample_rate):
|
| 47 |
+
df = pd.DataFrame({
|
| 48 |
+
'audio_id': [0],
|
| 49 |
+
'audio_arr': [audio_data],
|
| 50 |
+
'srate': [sample_rate],
|
| 51 |
+
'real_or_fake': [0]
|
| 52 |
+
})
|
| 53 |
+
audio_processor = LowLevelFeatureExtractor(target_sr=16000, include_only=['spectral', 'prosodic', 'voice_quality'])
|
| 54 |
+
feature_computer = HighLevelFeatureExtractor()
|
| 55 |
+
low_level_gen = audio_processor.low_level_feature_generator(df)
|
| 56 |
+
high_level_features = list(feature_computer.high_level_feature_generator(low_level_gen))
|
| 57 |
+
features_df = pd.DataFrame(high_level_features)
|
| 58 |
+
return features_df
|
| 59 |
+
|
| 60 |
+
# Plot waveform
|
| 61 |
+
def plot_waveform(audio_data, sample_rate):
|
| 62 |
+
fig, ax = plt.subplots(figsize=(10, 2)) # Wide and short waveform plot
|
| 63 |
+
ax.plot(np.linspace(0, len(audio_data) / sample_rate, len(audio_data)), audio_data)
|
| 64 |
+
ax.set_xlabel("Time (s)")
|
| 65 |
+
ax.set_ylabel("Amplitude")
|
| 66 |
+
st.pyplot(fig)
|
| 67 |
+
|
| 68 |
+
# Process the uploaded file
|
| 69 |
+
if uploaded_file is not None:
|
| 70 |
+
# Use columns to display the audio player, waveform, prediction, and confidence side by side
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
|
| 73 |
+
with col1:
|
| 74 |
+
st.subheader("Uploaded Audio")
|
| 75 |
+
st.audio(uploaded_file)
|
| 76 |
+
|
| 77 |
+
# Show waveform
|
| 78 |
+
st.subheader("Audio Waveform")
|
| 79 |
+
audio_bytes = uploaded_file.read()
|
| 80 |
+
audio_data, sample_rate = librosa.load(BytesIO(audio_bytes), sr=None)
|
| 81 |
+
plot_waveform(audio_data, sample_rate)
|
| 82 |
+
|
| 83 |
+
with col2:
|
| 84 |
+
# Extract features
|
| 85 |
+
features_df = extract_features(audio_data, sample_rate)
|
| 86 |
+
|
| 87 |
+
predictions, prediction_probabilities = predict(features_df)
|
| 88 |
+
|
| 89 |
+
# Display prediction and confidence score
|
| 90 |
+
st.subheader("Prediction Results")
|
| 91 |
+
|
| 92 |
+
prediction = predictions[0]
|
| 93 |
+
confidence_score = prediction_probabilities[0][1] * 100
|
| 94 |
+
|
| 95 |
+
if prediction == 1:
|
| 96 |
+
st.error("This audio is classified as a Deepfake!")
|
| 97 |
+
else:
|
| 98 |
+
st.success("This audio is classified as Genuine!")
|
| 99 |
+
|
| 100 |
+
# Show confidence score using a progress bar
|
| 101 |
+
st.markdown('<h3 class="confidence-score">Confidence Score</h3>', unsafe_allow_html=True)
|
| 102 |
+
st.progress(confidence_score / 100)
|
| 103 |
+
|
| 104 |
+
st.write(f"The model is {confidence_score:.2f}% confident in its prediction.")
|
| 105 |
+
|
| 106 |
+
# Footer or additional information
|
| 107 |
+
st.markdown('<h2 class="section-header">How It Works</h2>', unsafe_allow_html=True)
|
| 108 |
+
st.write("""
|
| 109 |
+
This app uses machine learning models trained on various audio features, such as spectral, prosodic, and voice quality metrics.
|
| 110 |
+
It analyzes the audio to classify whether it is a genuine recording or a deepfake, providing a confidence score for its prediction.
|
| 111 |
+
""")
|