gnumanth commited on
Commit
31da208
1 Parent(s): 3fbc338

init0: chat skeleton

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import time
4
+
5
+ st.title("Simple chat")
6
+
7
+ # Initialize chat history
8
+ if "messages" not in st.session_state:
9
+ st.session_state.messages = []
10
+
11
+ # Display chat messages from history on app rerun
12
+ for message in st.session_state.messages:
13
+ with st.chat_message(message["role"]):
14
+ st.markdown(message["content"])
15
+
16
+ # Accept user input
17
+ if prompt := st.chat_input("What is up?"):
18
+ # Add user message to chat history
19
+ st.session_state.messages.append({"role": "user", "content": prompt})
20
+ # Display user message in chat message container
21
+ with st.chat_message("user"):
22
+ st.markdown(prompt)
23
+
24
+ # Display assistant response in chat message container
25
+ with st.chat_message("assistant"):
26
+ message_placeholder = st.empty()
27
+ full_response = ""
28
+ assistant_response = random.choice(
29
+ [
30
+ "Hello there! How can I assist you today?",
31
+ "Hi, human! Is there anything I can help you with?",
32
+ "Do you need help?",
33
+ ]
34
+ )
35
+ # Simulate stream of response with milliseconds delay
36
+ for chunk in assistant_response.split():
37
+ full_response += chunk + " "
38
+ time.sleep(0.05)
39
+ # Add a blinking cursor to simulate typing
40
+ message_placeholder.markdown(full_response + "▌")
41
+ message_placeholder.markdown(full_response)
42
+ # Add assistant response to chat history
43
+ st.session_state.messages.append({"role": "assistant", "content": full_response})