Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import gradio as grfrom transformers import pipelinefrom vosk import Model, KaldiRecognizerimport osimport wavefrom gtts import gTTS# Load the Hugging Face conversational modelconversational_pipeline = pipeline("text-generation", model="microsoft/DialoGPT-medium")# Initialize the Vosk ASR modelmodel = Model("model") # Download the Vosk model beforehandrecognizer = KaldiRecognizer(model, 16000)def process_audio(audio_file):# Convert the audio file to text using Voskaudio = audio_file.getarray() # Get audio data as arrayif recognizer.AcceptWaveform(audio):text = recognizer.Result() # Convert to textelse:text = "Sorry, I couldn't understand that."# Use Hugging Face's model to get a responseresponse = conversational_pipeline(text, max_length=50) # Limit the response lengthbot_response = response[0]['generated_text']# Convert the bot's response to speech using gTTStts = gTTS(bot_response)tts.save("response.mp3")# Play the audio fileos.system("mpg321 response.mp3") # Make sure mpg321 is installed in the Hugging Face spacereturn bot_response, "response.mp3"# Create Gradio interfaceiface = gr.Interface(fn=process_audio,inputs=gr.inputs.Audio(source="microphone", type="file"),outputs=[gr.outputs.Textbox(), gr.outputs.Audio(type="file")],live=True,title="Voice Bot",description="Talk to the bot, and it will respond!")# Launch the interface12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import gradio as grimport torchfrom transformers import Wav2Vec2ForCTC, Wav2Vec2Processorfrom gtts import gTTSimport os# Load Wav2Vec2 model and processor for speech-to-textprocessor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-960h")model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h")def speech_to_text(audio_file):# Load audio file and process with Wav2Vec 2.0audio_input, _ = librosa.load(audio_file, sr=16000)input_values = processor(audio_input, return_tensors="pt").input_values# Perform speech-to-textwith torch.no_grad():logits = model(input_values).logits# Get the predicted ids and convert them back to textpredicted_ids = torch.argmax(logits, dim=-1)transcription = processor.decode(predicted_ids[0])return transcriptiondef generate_response(text):# Using Hugging Face to generate a text-based response# Use any model like DialoGPT for text response generationconversational_pipeline = pipeline("text-generation", model="microsoft/DialoGPT-medium")response = conversational_pipeline(text, max_length=50)return response[0]['generated_text']def process_audio(audio_file):# Convert speech to text using Wav2Vec 2.0text = speech_to_text(audio_file)print(f"User said: {text}")# Get the bot's responsebot_response = generate_response(text)print(f"Bot response: {bot_response}")# Convert the bot's response to speechtts = gTTS(bot_response)tts.save("response.mp3")# Play the responseos.system("mpg321 response.mp3")return bot_response, "response.mp3"# Create Gradio interface for audio input/outputiface = gr.Interface(fn=process_audio,inputs=gr.inputs.Audio(source="microphone", type="file"),outputs=[gr.outputs.Textbox(), gr.outputs.Audio(type="file")],live=True,title="Voice Bot with Wav2Vec2.0",description="Speak to the bot and get a response!")# Launch the interface
app.py+42-27