import streamlit as st from streamlit_chat import message from langchain.chains import ConversationalRetrievalChain from langchain.document_loaders import DirectoryLoader from langchain.document_loaders import PyPDFLoader from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import CTransformers from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from langchain.memory import ConversationBufferMemory from langchain.document_loaders.csv_loader import CSVLoader st.set_page_config( page_title="SuJokEase", page_icon="🩺", layout="wide", initial_sidebar_state="expanded", ) from langchain.document_loaders.csv_loader import CSVLoader loader = CSVLoader(file_path='data.csv') documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,chunk_overlap=50) text_chunks = text_splitter.split_documents(documents) embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device':"cpu"}) vector_store = FAISS.from_documents(text_chunks,embeddings) llm = CTransformers(model="model.bin",model_type="llama", config={'max_new_tokens':128,'temperature':0.01}) memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) chain = ConversationalRetrievalChain.from_llm(llm=llm,chain_type='stuff', retriever=vector_store.as_retriever(search_kwargs={"k":2}), memory=memory) # Sidebar for user input st.sidebar.title("SuJokEase🩺") st.sidebar.info("A Conversational Retrieval Chain Chat-Bot for Sujok Therapy Points Conversational Retrieval Chain Chat-Bot for Sujok Therapy weather you are Students, Working Professional or in situations where you need instant relaxation. We are at your Back! ") github_link = "[GitHub]()" st.sidebar.info("To contribute and Sponser - " + github_link) st.title("A Conversational Retrieval Chain Chat-Bot for Sujok Therapy Points🩺") st.text("Your wellness companion for instant relaxation.") def conversation_chat(query): result = chain({"question": query, "chat_history": st.session_state['history']}) st.session_state['history'].append((query, result["answer"])) return result["answer"] def initialize_session_state(): if 'history' not in st.session_state: st.session_state['history'] = [] if 'generated' not in st.session_state: st.session_state['generated'] = ["Hello! Ask me anything about Pains"] if 'past' not in st.session_state: st.session_state['past'] = ["Hello!"] def display_chat_history(): reply_container = st.container() container = st.container() with container: with st.form(key='my_form', clear_on_submit=True): user_input = st.text_input("Question:", placeholder="Ask anything about Sujok Thearpy", key='input') submit_button = st.form_submit_button(label='Send') if submit_button and user_input: output = conversation_chat(user_input) st.session_state['past'].append(user_input) st.session_state['generated'].append(output) if st.session_state['generated']: with reply_container: for i in range(len(st.session_state['generated'])): message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="robot") message(st.session_state["generated"][i], key=str(i), avatar_style="superhero") # Initialize session state initialize_session_state() # Display chat history display_chat_history()