import os import json import streamlit as st from langchain_huggingface import HuggingFaceEmbeddings from langchain_chroma import Chroma from langchain.memory import ConversationBufferMemory from langchain.chains import ConversationalRetrievalChain from vectorize_documents import embeddings # Import embeddings from the vectorization script import speech_recognition as sr # For voice recognition # Set up working directory and API configuration working_dir = os.path.dirname(os.path.abspath(__file__)) config_data = json.load(open(f"{working_dir}/config.json")) os.environ["GROQ_API_KEY"] = config_data["GROQ_API_KEY"] def setup_vectorstore(): persist_directory = f"{working_dir}/vector_db_dir" vectorstore = Chroma( persist_directory=persist_directory, embedding_function=embeddings ) return vectorstore def chat_chain(vectorstore): from langchain_groq import ChatGroq # Import the LLM class llm = ChatGroq( model="llama-3.1-70b-versatile", # Replace with your LLM of choice temperature=0 # Set low temperature to reduce hallucinations ) retriever = vectorstore.as_retriever() # Retrieve relevant chunks memory = ConversationBufferMemory( llm=llm, output_key="answer", memory_key="chat_history", return_messages=True ) # Build the conversational retrieval chain chain = ConversationalRetrievalChain.from_llm( llm=llm, retriever=retriever, chain_type="stuff", # Define how documents are combined memory=memory, verbose=True, return_source_documents=True ) return chain def transcribe_audio(selected_language): """Function to capture and transcribe audio in the selected language.""" try: recognizer = sr.Recognizer() with sr.Microphone() as source: st.write("🎤 Listening... Please ask your question.") try: audio = recognizer.listen(source, timeout=5) # 5 seconds to start speaking query = recognizer.recognize_google(audio, language=selected_language) # Transcribe audio in selected language st.write(f"**🗣️ You said:** {query}") return query except sr.WaitTimeoutError: st.error("⏳ You didn't speak in time. Please try again.") except sr.UnknownValueError: st.error("❌ Sorry, could not understand the audio. Please try again.") except sr.RequestError as e: st.error(f"⚠️ Error with speech recognition service: {e}") except AttributeError: st.error("❌ Microphone or PyAudio not available. Please check installation.") except OSError as e: st.error(f"⚠️ Audio input error: {e}") return None # Streamlit UI st.markdown( """ """, unsafe_allow_html=True ) st.markdown('