Bihar-Now-Then / app.py
divyanshusingh's picture
Update app.py
8705444
import sys
sys.path.append("/home/ece/hdd/divyanshu/Bihar")
from model import get_llm, CustomLLM
from langchain import PromptTemplate
from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE, DEFAULT_TEMPLATE
from langchain.chains.conversation.memory import ConversationEntityMemory
from langchain.chains import ConversationChain
import streamlit as st
st.set_page_config(page_title='Bihar Now & Then', layout='wide')
if "generated" not in st.session_state:
st.session_state["generated"] = []
if "past" not in st.session_state:
st.session_state["past"] = []
if "input" not in st.session_state:
st.session_state["input"] = ""
if "stored_session" not in st.session_state:
st.session_state["stored_session"] = []
def get_text():
input_text = st.text_input("You: ", st.session_state["input"], key="input",
placeholder="Ask me anything related to Bihar ...",
label_visibility='hidden')
return input_text
def new_chat():
"""
Clears session state and starts a new chat.
"""
save = []
for i in range(len(st.session_state['generated'])-1, -1, -1):
save.append("User:" + st.session_state["past"][i])
save.append("Bot:" + st.session_state["generated"][i])
st.session_state["stored_session"].append(save)
st.session_state["generated"] = []
st.session_state["past"] = []
st.session_state["input"] = ""
st.session_state.entity_memory.entity_store = {}
st.session_state.entity_memory.buffer.clear()
# Set up sidebar with various options
with st.sidebar.expander("๐Ÿ› ๏ธ ", expanded=False):
if st.checkbox("Preview memory store"):
with st.expander("Memory-Store", expanded=False):
st.session_state.entity_memory.store
if st.checkbox("Preview memory buffer"):
with st.expander("Bufffer-Store", expanded=False):
st.session_state.entity_memory.buffer
MODEL = st.selectbox(label='Model', options=["databricks/dolly-v2-3b"])
K = st.number_input(' (#)Summary of prompts to consider',
min_value=3, max_value=1000)
st.subheader("Bihar Now Then")
model_name = "t5-large"
pinecone_index = "bert-large-uncased"
llm = "databricks/dolly-v2-3b"
llm_chain, docsearch= get_llm(model_name, pinecone_index, llm)
llm = CustomLLM()
# Create a ConversationEntityMemory object if not already created
if 'entity_memory' not in st.session_state:
st.session_state.entity_memory = ConversationEntityMemory(llm=llm, k=K)
Conversation = ConversationChain(
llm=llm,
prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
memory=st.session_state.entity_memory
)
st.sidebar.button("New Chat", on_click=new_chat, type='primary')
user_input = get_text()
if user_input:
context = docsearch.similarity_search(user_input, k=3, metadata=False)
content = ""
for i in context:
content = content + f"{i.__dict__['page_content']}"
output = Conversation.run(entities=content, input=user_input)
st.session_state.past.append(user_input)
st.session_state.generated.append(output)
# Allow to download as well
download_str = []
with st.expander("Conversation", expanded=True):
for i in range(len(st.session_state['generated'])-1, -1, -1):
st.info(st.session_state["past"][i])
st.success(st.session_state["generated"][i])
download_str.append(st.session_state["past"][i])
download_str.append(st.session_state["generated"][i])
# Can throw error - requires fix
download_str = '\n'.join(download_str)
if download_str:
st.download_button('Download', download_str)
# Display stored conversation sessions in the sidebar
for i, sublist in enumerate(st.session_state.stored_session):
with st.sidebar.expander(label=f"Conversation-Session:{i}"):
st.write(sublist)
# Allow the user to clear all stored conversation sessions
if st.session_state.stored_session:
if st.sidebar.checkbox("Clear-all"):
del st.session_state.stored_session