dipsmom's picture
Update app.py
e6198f9 verified
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from audio_feature_extraction import extract_features
def plot_spectrum(audio_path):
# Extract features from the audio file
mean_features = extract_features(audio_path)
# Perform FFT to obtain frequency components
fft_spectrum = np.fft.fft(mean_features)
frequencies = np.fft.fftfreq(len(fft_spectrum), d=1/16000)
magnitude_spectrum = np.abs(fft_spectrum)
# Plot the frequency spectrum
plt.figure(figsize=(12, 6))
plt.plot(frequencies[:len(frequencies)//2], magnitude_spectrum[:len(magnitude_spectrum)//2])
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude")
plt.title("Frequency Spectrum of the Audio File")
plt.grid()
plt.tight_layout()
# Save the plot to a file
plot_path = "spectrum_plot.png"
plt.savefig(plot_path)
plt.close()
return plot_path
# Define the Gradio interface
iface = gr.Interface(
fn=plot_spectrum,
inputs=gr.Audio(source="upload", type="filepath"),
outputs=gr.Image(type="filepath"),
title="Audio Feature Extraction with Wav2Vec2",
description="Upload an audio file to extract features and view the frequency spectrum."
)
if __name__ == "__main__":
iface.launch()