import gradio as gr import os import whisper from transformers import AutoTokenizer, AutoModelForSeq2SeqLM from gtts import gTTS import IPython.display as ipd # Load Whisper STT model whisper_model = whisper.load_model("base") # Load translation models tokenizer = AutoTokenizer.from_pretrained("alirezamsh/small100") model = AutoModelForSeq2SeqLM.from_pretrained("alirezamsh/small100") def translate_speech(audio_file, target_lang): # Load audio audio = whisper.load_audio(audio_file) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(whisper_model.device) # Detect language _, probs = whisper_model.detect_language(mel) lang = max(probs, key=probs.get) # Decode audio into text options = whisper.DecodingOptions() result = whisper.decode(whisper_model, mel, options) text = result.text # Translate text tokenizer.src_lang = lang encoded_text = tokenizer(text, return_tensors="pt") generated_tokens = model.generate(**encoded_text) translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0] # Text-to-speech (TTS) tts = gTTS(text=translated_text, lang=target_lang) audio_path = "translated_audio.mp3" tts.save(audio_path) return audio_path def translate_speech_interface(audio, target_lang): audio_path = "recorded_audio.wav" with open(audio_path, "wb") as f: f.write(audio.read()) translated_audio = translate_speech(audio_path, target_lang) translated_audio = open(translated_audio, "rb") return translated_audio # Define the Gradio interface audio_recording = gr.inputs.Audio(source="microphone", type="wav", label="Record your speech") target_language = gr.inputs.Dropdown(["en", "ru", "fr"], label="Target Language") output_audio = gr.outputs.Audio(type="audio/mpeg", label="Translated Audio") gr.Interface(fn=translate_speech_interface, inputs=[audio_recording, target_language], outputs=output_audio, title="Speech Translator").launch()