import gradio as gr import librosa import numpy as np import soundfile as sf import tempfile def change_pitch(audio_file, pitch_shift): # Load the audio file y, sr = librosa.load(audio_file, sr=None) # Apply pitch shifting y_shifted = librosa.effects.pitch_shift(y=y, sr=sr, n_steps=pitch_shift) # Save original and shifted audio for playback original_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name shifted_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name sf.write(original_path, y, sr) sf.write(shifted_path, y_shifted, sr) return original_path, shifted_path with gr.Blocks() as demo: gr.Markdown("## 🎧 Audio Pitch Shifter") gr.Markdown("Upload an audio file, choose how many semitones to shift the pitch, and listen to the result.") with gr.Row(): audio_input = gr.Audio(type="filepath", label="Upload Audio") pitch_slider = gr.Slider(-12, 12, value=0, step=0.1, label="Pitch Shift (semitones)") submit_btn = gr.Button("Apply Pitch Shift") with gr.Row(): original_audio = gr.Audio(label="Original Audio", interactive=False) shifted_audio = gr.Audio(label="Pitch-Shifted Audio", interactive=False) submit_btn.click( fn=change_pitch, inputs=[audio_input, pitch_slider], outputs=[original_audio, shifted_audio] ) if __name__ == "__main__": # Fix: Add share=True to create a shareable link when localhost isn't accessible demo.launch(share=True)