abanm commited on
Commit
2d4e21d
·
verified ·
1 Parent(s): ca1674f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -8,6 +8,20 @@ st.set_page_config(page_title="Chatbot", layout="centered")
8
  if "messages" not in st.session_state:
9
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Main Chat UI
12
  st.title("ChatGPT-like Frontend")
13
  st.markdown("A simple ChatGPT-style frontend powered by your custom backend.")
@@ -26,16 +40,9 @@ for message in st.session_state["messages"]:
26
  )
27
 
28
  # User Input
29
- with st.container():
30
- user_input = st.text_input("Your message", placeholder="Type your message here...", key="user_input")
31
-
32
- if user_input:
33
- # Save User Message
34
- st.session_state["messages"].append({"role": "user", "content": user_input})
35
-
36
- # Get Bot Response
37
- bot_response = call_api(user_input)
38
- st.session_state["messages"].append({"role": "assistant", "content": bot_response})
39
-
40
- # Clear Input
41
- st.session_state["user_input"] = "" # Reset user input field
 
8
  if "messages" not in st.session_state:
9
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
10
 
11
+ # Function to Handle User Input
12
+ def handle_input():
13
+ user_message = st.session_state.user_input
14
+ if user_message:
15
+ # Save User Message
16
+ st.session_state["messages"].append({"role": "user", "content": user_message})
17
+
18
+ # Get Bot Response
19
+ bot_response = call_api(user_message)
20
+ st.session_state["messages"].append({"role": "assistant", "content": bot_response})
21
+
22
+ # Clear Input Field
23
+ st.session_state.user_input = "" # Reset user input field
24
+
25
  # Main Chat UI
26
  st.title("ChatGPT-like Frontend")
27
  st.markdown("A simple ChatGPT-style frontend powered by your custom backend.")
 
40
  )
41
 
42
  # User Input
43
+ st.text_input(
44
+ "Your message",
45
+ placeholder="Type your message here...",
46
+ key="user_input",
47
+ on_change=handle_input,
48
+ )