Skip to main content

Python Project: Audio Transcription and Text-to-Speech Conversion Using Wav2Vec2 and Pyttsx3

 Explore an advanced Python project that combines audio transcription and text-to-speech synthesis using state-of-the-art tools like Librosa, PyTorch, and Hugging Face's Transformers library. This script demonstrates how to load and resample audio files, transcribe speech to text using Facebook's Wav2Vec2 model, and convert text back to speech with customizable voice options using pyttsx3. Perfect for anyone interested in speech processing, AI-driven voice technology, or natural language processing projects. Ideal for enhancing your Python skills and diving into real-world applications of AI in audio analysis.



import librosa

from scipy.signal import resample

import torch

from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer

import pyttsx3

from scipy.signal import resample


# Load audio file

audio_file = "directory of audio file"

audio, sr = librosa.load(audio_file, sr=None)



def resample_audio(audio, orig_sr, target_sr):

    duration = audio.shape[0] / orig_sr

    target_length = int(duration * target_sr)

    resampled_audio = resample(audio, target_length)

    return resampled_audio


# Example usage:

# resampled_audio = resample_audio(audio, 48000, 16000)


# Resample if necessary

if sr != 16000:

    audio = resample_audio(audio, sr, 16000)

    sr = 16000


print("Audio loaded and resampled successfully.")


# Load Wav2Vec2 model and tokenizer

tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")

model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")


print("Model loaded successfully.")


# Tokenize input

input_values = tokenizer(audio, return_tensors="pt").input_values


# Perform inference

with torch.no_grad():

    logits = model(input_values).logits


# Get predicted ids

predicted_ids = torch.argmax(logits, dim=-1)


# Decode the ids to text

transcription = tokenizer.batch_decode(predicted_ids)[0]

print("Transcription: ", transcription)


# Text-to-Speech

def text_to_speech(text, voice_gender='female', rate=150):

    engine = pyttsx3.init()

    voices = engine.getProperty('voices')

    

    if voice_gender == 'male':

        engine.setProperty('voice', voices[0].id)

    else:

        engine.setProperty('voice', voices[1].id)

    

    engine.setProperty('rate', rate)

    engine.say(text)

    engine.runAndWait()


# Example usage

long_text = "hi what happened"

text_to_speech(long_text, voice_gender='male', rate=150)  # For male voice

text_to_speech(long_text, voice_gender='female', rate=180)  # For female voice


print("Text-to-Speech conversion completed.")



#PythonProject
#AudioTranscription
#TextToSpeech
#Wav2Vec2
#PyTorch
#Librosa
#NLP
#SpeechRecognition
#VoiceSynthesis
#AIinPython
#NaturalLanguageProcessing
#SpeechToText
#Pyttsx3
#MachineLearning
#DeepLearning
#AudioProcessing
#PythonAI
#TransformersLibrary
#PythonCoding
#PythonTutorial

Comments

Popular posts from this blog

Prepare Data for Exploration: Weekly challenge 4

Prepare Data for Exploration: Weekly challenge 4 1 . Question 1 A data analytics team labels its files to indicate their content, creation date, and version number. The team is using what data organization tool? 1 / 1  point File-naming verifications File-naming references File-naming conventions File-naming attributes Correct 2 . Question 2 Your boss assigns you a new multi-phase project and you create a naming convention for all of your files. With this project lasting years and incorporating multiple analysts it’s crucial that you create data explaining how your naming conventions are structured. What is this data called? 0 / 1  point Descriptive data Named convention Metadata Labeled data Incorrect Please review the video on naming conventions . 3 . Question 3 A grocery store is collecting inventory data from their produce section. What is an appropriate naming convention for this file? 0 / 1  point Todays_Produce Produce_Inventory_2022-09-15_V01 Todays Produce 2022-15-09 Inventory

Weekly challenge 3 data analyst google professional certificate

1 . Question 1 The manage stage of the data life cycle is when a business decides what kind of data it needs, how the data will be handled, and who will be responsible for it. 1 / 1  point True False Correct During planning, a business decides what kind of data it needs, how it will be managed throughout its life cycle, who will be responsible for it, and the optimal outcomes. 2 . Question 2 A data analyst is working at a small tech startup. They’ve just completed an analysis project, which involved private company information about a new product launch. In order to keep the information safe, the analyst uses secure data-erasure software for the digital files and a shredder for the paper files. Which stage of the data life cycle does this describe? 1 / 1  point Archive Plan Manage Destroy Correct This describes the destroy phase, during which data analysts use secure data-erasure software and shred paper files to protect private information. 3 . Question 3 In the analyze phase of the d

Prepare Data for Exploration : weekly challenge 1

Prepare Data for Exploration : weekly challenge 1 #coursera #exploration #weekly #challenge 1 #cybersecurity #coursera #quiz #solution #network Are you prepared to increase your data exploration abilities? The goal of Coursera's Week 1 challenge, "Prepare Data for Exploration," is to provide you the skills and resources you need to turn unprocessed data into insightful information. With the knowledge you'll gain from this course, you can ensure that your data is organised, clean, and ready for analysis. Data preparation is one of the most important processes in any data analysis effort. Inaccurate results and flawed conclusions might emerge from poorly prepared data. You may prepare your data for exploration with Coursera's Weekly Challenge 1. You'll discover industry best practises and insider advice. #answers #questions #flashcard 1 . Question 1 What is the most likely reason that a data analyst would use historical data instead of gathering new data? 1 / 1