gabruarya commited on
Commit
7efa083
β€’
1 Parent(s): de7a34f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -42
app.py CHANGED
@@ -1,8 +1,6 @@
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,6 +9,7 @@ import pinecone
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,6 +63,7 @@ def initialize_session_state():
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,11 +85,17 @@ 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(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()
@@ -118,38 +124,23 @@ st.markdown(
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
- response = st.session_state.conversation(user_input)
143
- llm_response = response['answer']
144
- st.session_state.generated.append({"type": "normal", "data": f"The message from Bot\nWith new line\n{user_input}"})
145
- st.session_state.history.append(Message("πŸ‘€ Human", user_input))
146
- st.session_state.history.append(Message("πŸ‘¨πŸ»β€βš–οΈ Ai", llm_response))
147
-
148
- def on_btn_click():
149
- del st.session_state.past[:]
150
- del st.session_state.generated[:]
151
- del st.session_state.history[:]
152
-
153
- with st.container():
154
- st.text_input("User Input:", on_change=on_input_change, key="user_input")
155
- st.button("Clear message", on_click=on_btn_click)
 
 
 
 
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
  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
 
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
 
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()
 
124
  """
125
  )
126
 
127
+ chat_placeholder = st.container()
128
+ prompt_placeholder = st.form("chat-form")
129
+
130
+ with chat_placeholder:
131
+ for chat in st.session_state.history:
132
+ st.markdown(f"{chat.origin} : {chat.message}")
133
+
134
+ with prompt_placeholder:
135
+ st.markdown("**Chat**")
136
+ cols = st.columns((6, 1))
137
+ cols[0].text_input(
138
+ "Chat",
139
+ label_visibility="collapsed",
140
+ key="human_prompt",
141
+ )
142
+ cols[1].form_submit_button(
143
+ "Submit",
144
+ type="primary",
145
+ on_click=on_click_callback,
146
+ )