No description provided.

import os
import json
import sqlite3
from datetime import datetime
import streamlit as st
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from deep_translator import GoogleTranslator
import speech_recognition as sr

Directory paths and configurations

working_dir = os.path.dirname(os.path.abspath(file))
config_data = json.load(open(f"{working_dir}/config.json"))
GROQ_API_KEY = config_data["GROQ_API_KEY"]
os.environ["GROQ_API_KEY"] = GROQ_API_KEY

Set up the database with check_same_thread=False

def setup_db():
conn = sqlite3.connect("chat_history.db", check_same_thread=False) # Ensure thread-safe connection
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS chat_histories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
timestamp TEXT,
day TEXT,
user_message TEXT,
assistant_response TEXT
)
""")
conn.commit()
return conn # Return the connection

Function to save chat history to SQLite

def save_chat_history(conn, username, timestamp, day, user_message, assistant_response):
cursor = conn.cursor()
cursor.execute("""
INSERT INTO chat_histories (username, timestamp, day, user_message, assistant_response)
VALUES (?, ?, ?, ?, ?)
""", (username, timestamp, day, user_message, assistant_response))
conn.commit()

Function to set up vectorstore for embeddings

def setup_vectorstore():
embeddings = HuggingFaceEmbeddings()
vectorstore = Chroma(persist_directory="vector_db_dir", embedding_function=embeddings)
return vectorstore

Function to set up the chatbot chain

def chat_chain(vectorstore):
llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0)
retriever = vectorstore.as_retriever()
memory = ConversationBufferMemory(
llm=llm,
output_key="answer",
memory_key="chat_history",
return_messages=True
)
chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=retriever,
chain_type="stuff",
memory=memory,
verbose=True,
return_source_documents=True
)
return chain

Function to get audio input from the user

def get_audio_input():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("🎀 Listening... Please ask your question.")
try:
audio = recognizer.listen(source, timeout=5)
query = recognizer.recognize_google(audio)
print(f"You said: {query}")
return query
except sr.WaitTimeoutError:
print("⏳ You didn't speak in time. Please try again.")
except sr.UnknownValueError:
print("❌ Sorry, could not understand the audio. Please try again.")
except sr.RequestError as e:
print(f"⚠️ Error with speech recognition service: {e}")
return ""

Streamlit UI setup

st.set_page_config(page_title="Bhagavad Gita Query Assistant", page_icon="πŸ“š", layout="centered")
st.title("πŸ“š Bhagavad Gita & Yoga Sutras Query Assistant")
st.subheader("Ask questions and explore timeless wisdom!")

Initialize session state

if "conn" not in st.session_state:
st.session_state.conn = setup_db()

if "username" not in st.session_state:
username = st.text_input("Enter your name to proceed:")
if username:
with st.spinner("Loading chatbot interface... Please wait."):
st.session_state.username = username
st.session_state.chat_history = [] # Initialize empty chat history in memory
st.session_state.vectorstore = setup_vectorstore()
st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
st.success(f"Welcome, {username}! The chatbot interface is ready.")
else:
username = st.session_state.username

Initialize components if not already set

if "conversational_chain" not in st.session_state:
st.session_state.vectorstore = setup_vectorstore()
st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)

Language options (30 Indian languages)

languages = [
"English", "Hindi", "Bengali", "Telugu", "Marathi", "Tamil", "Urdu", "Gujarati", "Malayalam", "Kannada",
"Punjabi", "Odia", "Maithili", "Sanskrit", "Santali", "Kashmiri", "Nepali", "Dogri", "Manipuri", "Bodo",
"Sindhi", "Assamese", "Konkani", "Awadhi", "Rajasthani", "Haryanvi", "Bihari", "Chhattisgarhi", "Magahi"
]

Main interface

if "username" in st.session_state:
st.subheader(f"Hello {username}, start your query below!")

# Language selection for translation
selected_language = st.selectbox("Select the output language", languages, index=languages.index("English"))

# Input options for the user to type or use voice input
input_option = st.radio("Choose Input Method", ("Type your question", "Ask via Voice"))

# Container to hold the chat interface (for scrolling)
chat_container = st.container()

with chat_container:
    if "chat_history" in st.session_state:
        for message in st.session_state.chat_history:
            if message['role'] == 'user':
                with st.chat_message("user"):
                    st.markdown(message["content"])
            elif message['role'] == 'assistant':
                with st.chat_message("assistant"):
                    st.markdown(message["content"])

# User input section for typing
user_query = None  # Initialize user_query as None

if input_option == "Type your question":
    user_query = st.chat_input("Ask AI about Bhagavad Gita or Yoga Sutras:")  # Chat input for typing

# User input section for voice
elif input_option == "Ask via Voice":
    if st.button("🎀 Ask via Voice"):
        with st.spinner("Listening for your question..."):
            user_query = get_audio_input()

# If user input is provided, process the query
if user_query:
    with st.spinner("Processing your query... Please wait."):

        # Save user input to chat history in memory
        st.session_state.chat_history.append({"role": "user", "content": user_query})

        # Display user's message in chatbot (for UI display)
        with st.chat_message("user"):
            st.markdown(user_query)

        # Get assistant's response from the chain
        with st.chat_message("assistant"):
            response = st.session_state.conversational_chain({"question": user_query})
            assistant_response = response["answer"]

            # Parse and format RAG pipeline output
            detailed_response = response.get("detailed_answer", {})
            book = detailed_response.get("book", "Unknown")
            chapter = detailed_response.get("chapter_number", "N/A")
            verse = detailed_response.get("verse_number", "N/A")
            shloka = detailed_response.get("shloka", "Not available")
            translation = detailed_response.get("translation", "Not available")
            commentary = detailed_response.get("commentary", "Not available")
            summary = detailed_response.get("summary", "Not available")

            # Save assistant's response to chat history in memory
            st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})

            # Save the chat history to the database (SQLite)
            timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            day = datetime.now().strftime("%A")  # Get the day of the week (e.g., Monday)
            save_chat_history(st.session_state.conn, username, timestamp, day, user_query, assistant_response)

            # Translate the assistant's response based on selected language
            translator = GoogleTranslator(source="en", target=selected_language.lower())
            translated_response = translator.translate(assistant_response)

            # Display detailed RAG response
            st.markdown(f"**Book:** {book}")
            st.markdown(f"**Chapter:** {chapter}")
            st.markdown(f"**Verse:** {verse}")
            st.markdown(f"**Shloka:** {shloka}")
            st.markdown(f"**Translation:** {translation}")
            st.markdown(f"**Commentary:** {commentary}")
            st.markdown(f"**Summary:** {summary}")

            # Display translated response
            st.markdown(f"**Translated Answer ({selected_language}):** {translated_response}")

    # Clear the input field after the query is processed
    st.session_state.user_input = ""
Ready to merge
This branch is ready to get merged automatically.
Your need to confirm your account before you can post a new comment.

Sign up or log in to comment