import gradio as gr import matplotlib.pyplot as plt import numpy as np import os import soundfile as sf def create_spectrogram_and_get_info(audio_file): # Read the audio data from the file audio_data, sample_rate = sf.read(audio_file) # Flatten the audio data if it's not mono if len(audio_data.shape) > 1: audio_data = audio_data.flatten() # Create the spectrogram plt.specgram(audio_data, Fs=sample_rate) # Save the spectrogram to a PNG file plt.savefig('spectrogram.png') # Get the audio file info audio_info = sf.info(audio_file) bit_depth = {'PCM_16': 16, 'FLOAT': 32}.get(audio_info.subtype, 0) # Create a table with the audio file info info_table = f""" | Informazione | Valore | | --- | --- | | Durata | {audio_info.duration} secondi | | Campioni al secondo | {audio_info.samplerate} Hz | | Canali | {audio_info.channels} | | Bitrate | {audio_info.samplerate * audio_info.channels * bit_depth} bit/s | | Estensione | {os.path.splitext(audio_file)[1]} | """ # Return the PNG file of the spectrogram and the info table return info_table, 'spectrogram.png' # Create the Gradio interface iface = gr.Interface(fn=create_spectrogram_and_get_info, inputs=gr.Audio(type="filepath"), outputs=["markdown", "image"]) iface.launch()