import streamlit as st from transformers import AutoProcessor, BarkModel, AutoModelForSpeechSeq2Seq, pipeline from datasets import load_dataset import torch import scipy st.title('Suno bark - text to speech and Whisper audio to translation') text_input = st.text_input( "Enter some text 👇", max_chars=400 ) if text_input: st.write("You entered: ", text_input) if st.button('Generate and play'): processor = AutoProcessor.from_pretrained("suno/bark") model = BarkModel.from_pretrained("suno/bark") voice_preset = "v2/en_speaker_6" print("input", text_input) inputs = processor(text_input, voice_preset=voice_preset) audio_array = model.generate(**inputs) audio_array = audio_array.cpu().numpy().squeeze() sample_rate = model.generation_config.sample_rate some_file_path = 'bark_out.wav' scipy.io.wavfile.write(some_file_path, rate=sample_rate, data=audio_array) st.audio(some_file_path, format="audio/wav", loop=False) uploaded_file = st.file_uploader("Choose a file") bytes_data = None if uploaded_file is not None: # To read file as bytes: bytes_data = uploaded_file.getvalue() if st.button('Generate and translate'): device = "cuda:0" if torch.cuda.is_available() else "cpu" torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 model_id = "openai/whisper-large-v3" model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True ) model.to(device) processor = AutoProcessor.from_pretrained(model_id) pipe = pipeline( "automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=30, batch_size=16, return_timestamps=True, torch_dtype=torch_dtype, device=device ) dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation") sample = bytes_data result = pipe(sample, generate_kwargs={"task": "translate"}) print(result["text"]) st.write(result["text"])