gabruarya commited on
Commit
ee8fd2e
β€’
1 Parent(s): 405f3b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -34
app.py CHANGED
@@ -1,6 +1,8 @@
 
 
 
1
  from dataclasses import dataclass
2
  from typing import Literal
3
- import streamlit as st
4
  import os
5
  from llamaapi import LlamaAPI
6
  from langchain_experimental.llms import ChatLlamaAPI
@@ -9,7 +11,6 @@ import pinecone
9
  from langchain.vectorstores import Pinecone
10
  from langchain.prompts import PromptTemplate
11
  from langchain.chains import RetrievalQA
12
- import streamlit.components.v1 as components
13
  from langchain_groq import ChatGroq
14
  from langchain.chains import ConversationalRetrievalChain
15
  from langchain.memory import ChatMessageHistory, ConversationBufferMemory
@@ -63,7 +64,6 @@ def initialize_session_state():
63
 
64
  PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
65
 
66
- #chain_type_kwargs = {"prompt": PROMPT}
67
  message_history = ChatMessageHistory()
68
  memory = ConversationBufferMemory(
69
  memory_key="chat_history",
@@ -85,17 +85,11 @@ def initialize_session_state():
85
 
86
  def on_click_callback():
87
  human_prompt = st.session_state.human_prompt
88
- st.session_state.human_prompt=""
89
- response = st.session_state.conversation(
90
- human_prompt
91
- )
92
  llm_response = response['answer']
93
- st.session_state.history.append(
94
- Message("πŸ‘€ Human", human_prompt)
95
- )
96
- st.session_state.history.append(
97
- Message("πŸ‘¨πŸ»β€βš–οΈ Ai", llm_response)
98
- )
99
 
100
 
101
  initialize_session_state()
@@ -120,28 +114,40 @@ st.markdown(
120
  πŸ€– **Getting Started:**
121
 
122
  Feel free to ask any legal question related to Indian law, using keywords like "property rights," "labor laws," or "family law." I'm here to assist you!
123
-
124
  Let's get started! How can I assist you today?
125
  """
126
  )
127
 
128
- chat_placeholder = st.container()
129
- prompt_placeholder = st.form("chat-form")
130
-
131
- with chat_placeholder:
132
- for chat in st.session_state.history:
133
- st.markdown(f"{chat.origin} : {chat.message}")
134
-
135
- with prompt_placeholder:
136
- st.markdown("**Chat**")
137
- cols = st.columns((6, 1))
138
- cols[0].text_input(
139
- "Chat",
140
- label_visibility="collapsed",
141
- key="human_prompt",
142
- )
143
- cols[1].form_submit_button(
144
- "Submit",
145
- type="primary",
146
- on_click=on_click_callback,
147
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_chat import message
3
+ from streamlit.components.v1 import html
4
  from dataclasses import dataclass
5
  from typing import Literal
 
6
  import os
7
  from llamaapi import LlamaAPI
8
  from langchain_experimental.llms import ChatLlamaAPI
 
11
  from langchain.vectorstores import Pinecone
12
  from langchain.prompts import PromptTemplate
13
  from langchain.chains import RetrievalQA
 
14
  from langchain_groq import ChatGroq
15
  from langchain.chains import ConversationalRetrievalChain
16
  from langchain.memory import ChatMessageHistory, ConversationBufferMemory
 
64
 
65
  PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
66
 
 
67
  message_history = ChatMessageHistory()
68
  memory = ConversationBufferMemory(
69
  memory_key="chat_history",
 
85
 
86
  def on_click_callback():
87
  human_prompt = st.session_state.human_prompt
88
+ st.session_state.human_prompt = ""
89
+ response = st.session_state.conversation(human_prompt)
 
 
90
  llm_response = response['answer']
91
+ st.session_state.history.append(Message("πŸ‘€ Human", human_prompt))
92
+ st.session_state.history.append(Message("πŸ‘¨πŸ»β€βš–οΈ Ai", llm_response))
 
 
 
 
93
 
94
 
95
  initialize_session_state()
 
114
  πŸ€– **Getting Started:**
115
 
116
  Feel free to ask any legal question related to Indian law, using keywords like "property rights," "labor laws," or "family law." I'm here to assist you!
 
117
  Let's get started! How can I assist you today?
118
  """
119
  )
120
 
121
+ if "history" not in st.session_state:
122
+ st.session_state.history = []
123
+
124
+ if "generated" not in st.session_state:
125
+ st.session_state.generated = []
126
+
127
+ if "past" not in st.session_state:
128
+ st.session_state.past = []
129
+
130
+ chat_placeholder = st.empty()
131
+
132
+ with chat_placeholder.container():
133
+ for i, chat in enumerate(st.session_state.history):
134
+ if chat.origin == "πŸ‘€ Human":
135
+ message(chat.message, is_user=True, key=f"{i}_user")
136
+ else:
137
+ message(chat.message, key=f"{i}")
138
+
139
+ def on_input_change():
140
+ user_input = st.session_state.user_input
141
+ st.session_state.past.append(user_input)
142
+ st.session_state.generated.append({"type": "normal", "data": f"The message from Bot\nWith new line\n{user_input}"})
143
+ st.session_state.history.append(Message("πŸ‘€ Human", user_input))
144
+ st.session_state.history.append(Message("πŸ‘¨πŸ»β€βš–οΈ Ai", f"The message from Bot\nWith new line\n{user_input}"))
145
+
146
+ def on_btn_click():
147
+ del st.session_state.past[:]
148
+ del st.session_state.generated[:]
149
+ del st.session_state.history[:]
150
+
151
+ with st.container():
152
+ st.text_input("User Input:", on_change=on_input_change, key="user_input")
153
+ st.button("Clear message", on_click=on_btn_click)