File size: 1,267 Bytes
e6198f9
bae933c
64371b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32767d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


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()