Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- .gitattributes +2 -0
- app.py +133 -0
- audio_files/p_18661452_26.mp3 +0 -0
- audio_files/recorded_audio.wav +3 -0
- model/model.keras +3 -0
- requirements.txt +11 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
audio_files/recorded_audio.wav filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
model/model.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
import librosa
|
| 8 |
+
import os
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
import tensorflow as tf
|
| 12 |
+
from st_audiorec import st_audiorec
|
| 13 |
+
import altair
|
| 14 |
+
import keras
|
| 15 |
+
import librosa.display
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
+
from keras_preprocessing.image import load_img, img_to_array
|
| 18 |
+
|
| 19 |
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 20 |
+
|
| 21 |
+
st.set_page_config(page_title="Deepfake Audio")
|
| 22 |
+
class_names = ['real', 'fake']
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def file_save(file_sound):
|
| 26 |
+
with open(os.path.join('audio_files/', file_sound.name), 'wb') as f:
|
| 27 |
+
f.write(file_sound.getbuffer())
|
| 28 |
+
|
| 29 |
+
return file_sound.name
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def create_spec(sound):
|
| 33 |
+
audio_file = os.path.join('audio_files/', sound)
|
| 34 |
+
|
| 35 |
+
fig = plt.figure()
|
| 36 |
+
ax = fig.add_subplot(1, 1, 1)
|
| 37 |
+
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
|
| 38 |
+
y, sr = librosa.load(audio_file)
|
| 39 |
+
mel = librosa.feature.melspectrogram(y=y, sr=sr)
|
| 40 |
+
log_ms = librosa.power_to_db(mel, ref=np.max)
|
| 41 |
+
librosa.display.specshow(log_ms, sr=sr)
|
| 42 |
+
plt.savefig('mel_spectrogram.png')
|
| 43 |
+
image_data = load_img('mel_spectrogram.png', target_size=(224, 224))
|
| 44 |
+
st.image(image_data)
|
| 45 |
+
|
| 46 |
+
return image_data
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def pred(image_data, model):
|
| 50 |
+
img_array = np.array(image_data)
|
| 51 |
+
img_array1 = img_array / 255
|
| 52 |
+
img_batch = np.expand_dims(img_array1, axis=0)
|
| 53 |
+
|
| 54 |
+
prediction = model.predict(img_batch)
|
| 55 |
+
class_label = np.argmax(prediction)
|
| 56 |
+
|
| 57 |
+
return class_label, prediction
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def file_upload_page():
|
| 61 |
+
st.write("## File Upload Page")
|
| 62 |
+
uploaded_file = st.file_uploader('Upload a .wav or .mp3 file', type=['wav', 'mp3'])
|
| 63 |
+
if uploaded_file is not None:
|
| 64 |
+
st.write('### Play audio')
|
| 65 |
+
audio_bytes = uploaded_file.read()
|
| 66 |
+
st.audio(audio_bytes, format='audio/wav')
|
| 67 |
+
|
| 68 |
+
st.write('### Spectrogram Image:')
|
| 69 |
+
file_save(uploaded_file)
|
| 70 |
+
sound = uploaded_file.name
|
| 71 |
+
with st.spinner('Fetching Results...'):
|
| 72 |
+
spec = create_spec(sound)
|
| 73 |
+
model = tf.keras.models.load_model('model/model.keras')
|
| 74 |
+
st.write('### Classification results:')
|
| 75 |
+
class_label, prediction = pred(spec, model)
|
| 76 |
+
st.write("#### The uploaded audio file is " + class_names[class_label])
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def record_audio_page():
|
| 80 |
+
|
| 81 |
+
st.write("### Record Your Voice")
|
| 82 |
+
st.write("- ** After that it will automatically process and gives results that audio file is real or fake(AI generated)")
|
| 83 |
+
wav_audio_data = st_audiorec()
|
| 84 |
+
|
| 85 |
+
if wav_audio_data is not None:
|
| 86 |
+
st.audio(wav_audio_data, format='audio/wav')
|
| 87 |
+
st.write("### Spectrogram Image:")
|
| 88 |
+
# Save the recorded audio as a file
|
| 89 |
+
with open('audio_files/recorded_audio.wav', 'wb') as f:
|
| 90 |
+
f.write(wav_audio_data)
|
| 91 |
+
sound = 'recorded_audio.wav'
|
| 92 |
+
with st.spinner('Fetching Results...'):
|
| 93 |
+
spec = create_spec(sound)
|
| 94 |
+
model = tf.keras.models.load_model('model/model.keras')
|
| 95 |
+
st.write('### Classification results:')
|
| 96 |
+
class_label, prediction = pred(spec, model)
|
| 97 |
+
st.write("#### The recorded audio is " + class_names[class_label])
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def main():
|
| 101 |
+
# Default page
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# Sidebar to switch between pages
|
| 105 |
+
page_options = ['Information', 'Upload Audio File', 'Record Audio']
|
| 106 |
+
selected_page = st.sidebar.selectbox('Select Page', page_options)
|
| 107 |
+
|
| 108 |
+
# Show corresponding page based on selection
|
| 109 |
+
if selected_page == 'Information':
|
| 110 |
+
show_information_page()
|
| 111 |
+
elif selected_page == 'Upload Audio File':
|
| 112 |
+
file_upload_page()
|
| 113 |
+
elif selected_page == 'Record Audio':
|
| 114 |
+
record_audio_page()
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def show_information_page():
|
| 118 |
+
st.write("## Deepfake Audio Classification")
|
| 119 |
+
st.write("This web app allows you to classify audio files as real or fake.")
|
| 120 |
+
st.write("Please select an option from the dropdown menu to proceed.")
|
| 121 |
+
|
| 122 |
+
st.write("## Information Page")
|
| 123 |
+
st.write("This page provides information about the Deepfake Audio Classification web app.")
|
| 124 |
+
|
| 125 |
+
st.write("## Audio Features")
|
| 126 |
+
st.write("- **Spectrogram:** A visual representation of the audio frequency content.")
|
| 127 |
+
st.write("- **Classification results:** The prediction of whether the audio is real or fake.")
|
| 128 |
+
st.write("- **Model:** Deep learning model trained to classify audio files.")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
main()
|
audio_files/p_18661452_26.mp3
ADDED
|
Binary file (30 kB). View file
|
|
|
audio_files/recorded_audio.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39716a7a42fb1f0a709098969ae66fcd53072375efe0e2d5ca7032d0c0e8ece6
|
| 3 |
+
size 2392108
|
model/model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b8f62fa0d7f9f1285a58597bb48677088cd2097cc9214a8e470650dddbb49ce6
|
| 3 |
+
size 13220801
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.10.0
|
| 2 |
+
opencv-python
|
| 3 |
+
|
| 4 |
+
librosa
|
| 5 |
+
tensorflow
|
| 6 |
+
matplotlib
|
| 7 |
+
keras
|
| 8 |
+
Keras-Preprocessing
|
| 9 |
+
lime
|
| 10 |
+
scikit-image
|
| 11 |
+
numpy
|