divyanshusingh commited on
Commit
909f926
·
1 Parent(s): 26175df

Added: app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.chains import ConversationChain
3
+ from langchain.chains.conversation.memory import ConversationEntityMemory
4
+ from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
5
+ from model import get_llm
6
+
7
+ st.set_page_config(page_title='Bihar Now & Then', layout='wide')
8
+
9
+ if "generated" not in st.session_state:
10
+ st.session_state["generated"] = []
11
+ if "past" not in st.session_state:
12
+ st.session_state["past"] = []
13
+ if "input" not in st.session_state:
14
+ st.session_state["input"] = ""
15
+ if "stored_session" not in st.session_state:
16
+ st.session_state["stored_session"] = []
17
+
18
+ def get_text():
19
+
20
+ input_text = st.text_input("You: ", st.session_state["input"], key="input",
21
+ placeholder="Ask me anything related to Bihar ...",
22
+ label_visibility='hidden')
23
+ return input_text
24
+
25
+ # Define function to start a new chat
26
+ def new_chat():
27
+ """
28
+ Clears session state and starts a new chat.
29
+ """
30
+ save = []
31
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
32
+ save.append("User:" + st.session_state["past"][i])
33
+ save.append("Bot:" + st.session_state["generated"][i])
34
+ st.session_state["stored_session"].append(save)
35
+ st.session_state["generated"] = []
36
+ st.session_state["past"] = []
37
+ st.session_state["input"] = ""
38
+ st.session_state.entity_memory.entity_store = {}
39
+ st.session_state.entity_memory.buffer.clear()
40
+
41
+ # Set up sidebar with various options
42
+ with st.sidebar.expander("🛠️ ", expanded=False):
43
+ # Option to preview memory store
44
+ if st.checkbox("Preview memory store"):
45
+ with st.expander("Memory-Store", expanded=False):
46
+ st.session_state.entity_memory.store
47
+ # Option to preview memory buffer
48
+ if st.checkbox("Preview memory buffer"):
49
+ with st.expander("Bufffer-Store", expanded=False):
50
+ st.session_state.entity_memory.buffer
51
+ MODEL = st.selectbox(label='Model', options=['gpt-3.5-turbo','text-davinci-003','text-davinci-002','code-davinci-002'])
52
+ K = st.number_input(' (#)Summary of prompts to consider',min_value=3,max_value=1000)
53
+
54
+ # Set up the Streamlit app layout
55
+
56
+ st.subheader(" Powered by 🦜 LangChain + 🤗 HuggingFace + Streamlit")
57
+
58
+ model_name = "bert-large-uncased"
59
+ pinecone_index = "bert-large-uncased"
60
+ llm = "databricks/dolly-v2-3b"
61
+ llm_chain, docsearch = get_llm(model_name,pinecone_index,llm)
62
+ # Create a ConversationEntityMemory object if not already created
63
+ if 'entity_memory' not in st.session_state:
64
+ st.session_state.entity_memory = ConversationEntityMemory(llm=llm, k=K )
65
+
66
+ # Create the ConversationChain object with the specified configuration
67
+ Conversation = ConversationChain(
68
+ llm=llm,
69
+ prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
70
+ memory=st.session_state.entity_memory
71
+ )
72
+
73
+
74
+ st.sidebar.button("New Chat", on_click = new_chat, type='primary')
75
+
76
+ user_input = get_text()
77
+
78
+ if user_input:
79
+ output = Conversation.run(input=user_input)
80
+ st.session_state.past.append(user_input)
81
+ st.session_state.generated.append(output)
82
+
83
+ # Allow to download as well
84
+ download_str = []
85
+
86
+ with st.expander("Conversation", expanded=True):
87
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
88
+ st.info(st.session_state["past"][i])
89
+ st.success(st.session_state["generated"][i])
90
+ download_str.append(st.session_state["past"][i])
91
+ download_str.append(st.session_state["generated"][i])
92
+
93
+ # Can throw error - requires fix
94
+ download_str = '\n'.join(download_str)
95
+ if download_str:
96
+ st.download_button('Download',download_str)
97
+
98
+ # Display stored conversation sessions in the sidebar
99
+ for i, sublist in enumerate(st.session_state.stored_session):
100
+ with st.sidebar.expander(label= f"Conversation-Session:{i}"):
101
+ st.write(sublist)
102
+
103
+ # Allow the user to clear all stored conversation sessions
104
+ if st.session_state.stored_session:
105
+ if st.sidebar.checkbox("Clear-all"):
106
+ del st.session_state.stored_session