can you modifie the code so that the macedonian wake words are picked up and make espeak…
- User
can you modifie the code so that the macedonian wake words are picked up and make espeak ng use the macedonian version import subprocess import speech_recognition as sr import numpy as np import os import whisper import ollama import torch
Get device
device = "cuda" if torch.cuda.is_available() else "cpu"
Load whisper model
model = whisper.load_model("small")
Initialize speech recognizer
recognizer = sr.Recognizer()
Initialize ollama
ollama_api = "http://localhost:11434" ollama_model = "macedon"
Define a function to normalize audio data
def normalize_audio(audio_data): # Convert audio data to floating point and normalize to the range [-1, 1] audio_data_normalized = audio_data.astype(np.float32) / 32767.0 return audio_data_normalized
Define a function to transcribe audio and generate text
def transcribe_audio(audio): # Convert AudioData to raw audio data (bytes) raw_audio = audio.get_raw_data(convert_rate=16000, convert_width=2)
# Convert raw audio data to NumPy array audio_np = np.frombuffer(raw_audio, dtype=np.int16) # Normalize audio data audio_np_normalized = normalize_audio(audio_np) # Transcribe audio using whisper result = model.transcribe(audio_np_normalized) text = result["text"] print("Transcribed text:", text) # Check if transcribed text is empty if text.strip() == "": print("Transcribed text is empty. Skipping Ollama API call.") return # Send the user message to Ollama API stream = ollama.chat( model='macedon', messages=[{'role': 'user', 'content': text}], stream=True, ) # Concatenate all response chunks into a single response response_text = "" for chunk in stream: response_text += chunk['message']['content'] # Print the concatenated response print("Ollama response:", response_text) # Speak response using espeak-ng speak_response(response_text)Define a function to speak the response using espeak-ng
def speak_response(response_text): # Use espeak-ng command to generate speech from text espeak_command = ['espeak-ng', response_text] subprocess.run(espeak_command)
Define a function to listen for audio until a wake word is detected
def listen_for_audio(wake_words=["здраво","астро","ти","hello"]): with sr.Microphone() as source: recognizer.adjust_for_ambient_noise(source) print("Listening for wake word...") while True: audio = recognizer.listen(source) try: # Convert the audio to text spoken_text = recognizer.recognize_google(audio).lower() print("Spoken:", spoken_text) # Check if any wake word is in the spoken text if any(word in spoken_text for word in wake_words): print("Wake word detected. Listening...") break # Break the loop if wake word detected else: print("No wake word detected. Listening again...") except sr.UnknownValueError: print("Could not understand audio. Listening again...") return audio
Main loop
while True: # Listen for audio until wake word is detected audio = listen_for_audio()
# Transcribe audio and generate text transcribe_audio(audio) - Assistant
import subprocess import speech_recognition as sr import numpy as np import os import whisper import ollama import torch
Get device
device = "cuda" if torch.cuda.is_available() else "cpu"
Load whisper model
model = whisper.load_model("small")
Initialize speech recognizer
recognizer = sr.Recognizer()
Initialize ollama
ollama_api = "http://localhost:11434" ollama_model = "macedon"
Define a function to normalize audio data
def normalize_audio(audio_data): # Convert audio data to floating point and normalize to the range [-1, 1] audio_data_normalized = audio_data.astype(np.float32) / 32767.0 return audio_data_normalized
Define a function to transcribe audio and generate text
def transcribe_audio(audio): # Convert AudioData to raw audio data (bytes) raw_audio = audio.get_raw_data(convert_rate=16000, convert_width=2)
# Convert raw audio data to NumPy array audio_np = np.frombuffer(raw_audio, dtype=np.int16) # Normalize audio data audio_np_normalized = normalize_audio(audio_np) # Transcribe audio using whisper result = model.transcribe(audio_np_normalized) text = result["text"] print("Transcribed text:", text) # Check if transcribed text is empty if text.strip() == "": print("Transcribed text is empty. Skipping Ollama API call.") return # Send the user message to Ollama API stream = ollama.chat( model='macedon', messages=[{'role': 'user', 'content': text}], stream=True, ) # Concatenate all response chunks into a single response response_text = "" for chunk in stream: response_text += chunk['message']['content'] # Print the concatenated response print("Ollama response:", response_text) # Speak response using espeak-ng speak_response(response_text)Define a function to speak the response using espeak-ng in Macedonian language
def speak_response(response_text): # Use espeak-ng command to generate speech from text in Macedonian language espeak_command = ['espeak-ng', '-v', 'mk', response_text] subprocess.run(espeak_command)
Define a function to listen for audio until a wake word is detected
def listen_for_audio(wake_words=["здраво","астро","ти","hello"]): with sr.Microphone() as source: recognizer.adjust_for_ambient_noise(source) print("Listening for wake word...") while True: audio = recognizer.listen(source) try: # Convert the audio to text spoken_text = recognizer.recognize_google(audio).lower() print("Spoken:", spoken_text) # Check if any wake word is in the spoken text if any(word in spoken_text for word in wake_words): print("Wake word detected. Listening...") break # Break the loop if wake word detected else: print("No wake word detected. Listening again...") except sr.UnknownValueError: print("Could not understand audio. Listening again...") return audio
Main loop
while True: # Listen for audio until wake word is detected audio = listen_for_audio()
# Transcribe audio and generate text transcribe_audio(audio)