divyanshusingh commited on
Commit
8705444
β€’
1 Parent(s): 3ca2e10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -35
app.py CHANGED
@@ -1,8 +1,14 @@
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
 
@@ -15,14 +21,14 @@ if "input" not in st.session_state:
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.
@@ -30,7 +36,7 @@ def new_chat():
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"] = []
@@ -38,47 +44,49 @@ def new_chat():
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 = []
@@ -89,18 +97,18 @@ with st.expander("Conversation", expanded=True):
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
 
1
+ import sys
2
+ sys.path.append("/home/ece/hdd/divyanshu/Bihar")
3
+
4
+ from model import get_llm, CustomLLM
5
+ from langchain import PromptTemplate
6
+ from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE, DEFAULT_TEMPLATE
7
  from langchain.chains.conversation.memory import ConversationEntityMemory
8
+ from langchain.chains import ConversationChain
9
+ import streamlit as st
10
+
11
+
12
 
13
  st.set_page_config(page_title='Bihar Now & Then', layout='wide')
14
 
 
21
  if "stored_session" not in st.session_state:
22
  st.session_state["stored_session"] = []
23
 
24
+
25
  def get_text():
26
 
27
  input_text = st.text_input("You: ", st.session_state["input"], key="input",
28
+ placeholder="Ask me anything related to Bihar ...",
29
+ label_visibility='hidden')
30
  return input_text
31
 
 
32
  def new_chat():
33
  """
34
  Clears session state and starts a new chat.
 
36
  save = []
37
  for i in range(len(st.session_state['generated'])-1, -1, -1):
38
  save.append("User:" + st.session_state["past"][i])
39
+ save.append("Bot:" + st.session_state["generated"][i])
40
  st.session_state["stored_session"].append(save)
41
  st.session_state["generated"] = []
42
  st.session_state["past"] = []
 
44
  st.session_state.entity_memory.entity_store = {}
45
  st.session_state.entity_memory.buffer.clear()
46
 
47
+
48
  # Set up sidebar with various options
49
  with st.sidebar.expander("πŸ› οΈ ", expanded=False):
 
50
  if st.checkbox("Preview memory store"):
51
  with st.expander("Memory-Store", expanded=False):
52
  st.session_state.entity_memory.store
 
53
  if st.checkbox("Preview memory buffer"):
54
  with st.expander("Bufffer-Store", expanded=False):
55
  st.session_state.entity_memory.buffer
56
+ MODEL = st.selectbox(label='Model', options=["databricks/dolly-v2-3b"])
57
+ K = st.number_input(' (#)Summary of prompts to consider',
58
+ min_value=3, max_value=1000)
59
 
60
+ st.subheader("Bihar Now Then")
61
 
62
+ model_name = "t5-large"
 
 
63
  pinecone_index = "bert-large-uncased"
64
  llm = "databricks/dolly-v2-3b"
65
+ llm_chain, docsearch= get_llm(model_name, pinecone_index, llm)
66
+
67
+ llm = CustomLLM()
68
  # Create a ConversationEntityMemory object if not already created
69
  if 'entity_memory' not in st.session_state:
70
+ st.session_state.entity_memory = ConversationEntityMemory(llm=llm, k=K)
 
 
 
 
 
 
 
71
 
72
+ Conversation = ConversationChain(
73
+ llm=llm,
74
+ prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
75
+ memory=st.session_state.entity_memory
76
+ )
77
 
78
+ st.sidebar.button("New Chat", on_click=new_chat, type='primary')
79
 
80
  user_input = get_text()
81
 
82
  if user_input:
83
+ context = docsearch.similarity_search(user_input, k=3, metadata=False)
84
+ content = ""
85
+ for i in context:
86
+ content = content + f"{i.__dict__['page_content']}"
87
+ output = Conversation.run(entities=content, input=user_input)
88
+ st.session_state.past.append(user_input)
89
+ st.session_state.generated.append(output)
90
 
91
  # Allow to download as well
92
  download_str = []
 
97
  st.success(st.session_state["generated"][i])
98
  download_str.append(st.session_state["past"][i])
99
  download_str.append(st.session_state["generated"][i])
100
+
101
  # Can throw error - requires fix
102
  download_str = '\n'.join(download_str)
103
  if download_str:
104
+ st.download_button('Download', download_str)
105
 
106
  # Display stored conversation sessions in the sidebar
107
  for i, sublist in enumerate(st.session_state.stored_session):
108
+ with st.sidebar.expander(label=f"Conversation-Session:{i}"):
109
+ st.write(sublist)
110
 
111
  # Allow the user to clear all stored conversation sessions
112
+ if st.session_state.stored_session:
113
  if st.sidebar.checkbox("Clear-all"):
114
+ del st.session_state.stored_session