import gradio as gr import soundfile as sf import numpy as np import tempfile import os # Define the file path of the audio file you want to play directly direct_audio_file_path = "Audio/translated_audio.wav" # Replace this with the actual file path # Function to handle audio streaming def audio_streaming(audio=None): # If an audio file is provided as input, use it; otherwise, use the direct file path if audio is None: audio = direct_audio_file_path # Load the audio file data, samplerate = sf.read(audio) # Ensure data is in float32 format data = np.array(data, dtype=np.float32) # Save to a temporary file that Gradio can use for audio playback with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file: sf.write(tmp_file.name, data, samplerate) temp_audio_path = tmp_file.name # Return the file path to Gradio return temp_audio_path # Gradio interface with gr.Blocks() as demo: gr.Markdown("### Audio Streaming App") # Button to play audio from the predefined file path play_button = gr.Button("Play Direct Audio") # Define output for streamed audio audio_output = gr.Audio(label="Streamed Audio") # Set up the Gradio interface to handle the button click play_button.click( fn=audio_streaming, inputs=None, # No input needed for direct play outputs=audio_output ) if __name__ == "__main__": demo.launch()