mustapha commited on
Commit
e38228f
1 Parent(s): 6a59529

Streamlit chat

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -125,32 +125,52 @@ def load_chain():
125
 
126
  chain = load_chain()
127
 
128
- # From here down is all the StreamLit UI.
129
- st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
130
- st.header("LangChain Demo")
131
 
132
- if "generated" not in st.session_state:
133
- st.session_state["generated"] = []
134
 
135
- if "past" not in st.session_state:
136
- st.session_state["past"] = []
137
 
138
 
139
- def get_text():
140
- input_text = st.text_input("Human: ", "Hello, how are you?", key="input")
141
- return input_text
142
 
143
 
144
- user_input = get_text()
145
 
146
- if user_input:
147
- output = chain.predict(human_input=user_input)
148
 
149
- st.session_state.past.append(user_input)
150
- st.session_state.generated.append(output)
151
 
152
- if st.session_state["generated"]:
153
 
154
- for i in range(len(st.session_state["generated"]) - 1, -1, -1):
155
- message(st.session_state["generated"][i], key=str(i))
156
- message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  chain = load_chain()
127
 
128
+ # # From here down is all the StreamLit UI.
129
+ # st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
130
+ # st.header("LangChain Demo")
131
 
132
+ # if "generated" not in st.session_state:
133
+ # st.session_state["generated"] = []
134
 
135
+ # if "past" not in st.session_state:
136
+ # st.session_state["past"] = []
137
 
138
 
139
+ # def get_text():
140
+ # input_text = st.text_input("Human: ", "Hello, how are you?", key="input")
141
+ # return input_text
142
 
143
 
144
+ # user_input = get_text()
145
 
146
+ # if user_input:
147
+ # output = chain.predict(human_input=user_input)
148
 
149
+ # st.session_state.past.append(user_input)
150
+ # st.session_state.generated.append(output)
151
 
152
+ # if st.session_state["generated"]:
153
 
154
+ # for i in range(len(st.session_state["generated"]) - 1, -1, -1):
155
+ # message(st.session_state["generated"][i], key=str(i))
156
+ # message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")
157
+
158
+ st.title("ChatAlpaca")
159
+
160
+ if "history" not in st.session_state:
161
+ st.session_state.history = []
162
+ st.session_state.history.append({"message": "Hey, I'm a Alpaca chatBot. Ask whatever you want!", "is_user": False})
163
+
164
+
165
+ def generate_answer():
166
+ user_message = st.session_state.input_text
167
+ inputs = tokenizer(st.session_state.input_text, return_tensors="pt")
168
+ result = model.generate(**inputs)
169
+ message_bot = tokenizer.decode(result[0], skip_special_tokens=True) # .replace("<s>", "").replace("</s>", "")
170
+ st.session_state.history.append({"message": user_message, "is_user": True})
171
+ st.session_state.history.append({"message": message_bot, "is_user": False})
172
+
173
+ st.text_input("Response", key="input_text", on_change=generate_answer)
174
+
175
+ for chat in st.session_state.history:
176
+ st_message(**chat)