File size: 2,512 Bytes
43a4e2c 80ee071 43a4e2c 80ee071 43a4e2c 80ee071 43a4e2c 80ee071 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import speech_recognition as sr
from langchain.llms import HuggingFacePipeline
from langchain.chains import RetrievalQA
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.document_loaders import UnstructuredFileLoader
import os
import torch
import soundfile as sf
from playsound import playsound
from TTS.api import TTS
from langchain.llms import Ollama
# Loading RAG data
loader = UnstructuredFileLoader("Foduu_KnowledgeBase.pdf")
documents = loader.load()
# Open-source embedding model
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(documents, embeddings)
ollama = Ollama(base_url='http://localhost:11434',model="llama3")
qa = RetrievalQA.from_chain_type(llm=ollama, chain_type="stuff", retriever=vectorstore.as_retriever())
# Speech recognition setup
r = sr.Recognizer()
tts = TTS(model_name="tts_models/en/ljspeech/glow-tts")
def speak(text):
"""
Converts text to speech using Mozilla TTS, plays the audio, and then deletes the file.
"""
try:
# Generate speech
output_file = "output.wav"
tts.tts_to_file(text=text, file_path=output_file)
# Play the speech
playsound(output_file)
os.remove(output_file)
print(f"Speech played and file {output_file} removed.")
except Exception as e:
print(f"Error: {e}")
def listen():
"""
Records audio and converts it to text using speech recognition.
"""
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(f"You said: {text}")
return text
except sr.UnknownValueError:
print("Could not understand audio")
speak('could not understand audio')
return None
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
return None
def process_audio(text):
if text is not None:
try:
response = qa.run(text)
print(response)
speak(response)
except Exception as e:
print(f"An error occurred: {e}")
speak("Sorry, I'm having trouble processing that right now.")
def main():
"""
Main loop for the voice assistant.
"""
while True:
text = listen()
process_audio(text)
if __name__ == "__main__":
main()
|