abanm commited on
Commit
9460bb3
·
verified ·
1 Parent(s): c519b66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -33
app.py CHANGED
@@ -1,30 +1,19 @@
1
  import streamlit as st
2
- from api_helper import call_api # Import the updated API helper
 
 
 
 
 
 
3
 
4
  # Streamlit Configurations
5
- st.set_page_config(page_title="Chatbot", layout="centered")
6
 
7
  # Chat History Initialization
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
- # Display Bot Response Placeholder
19
- with st.spinner("Fetching response..."):
20
- bot_response = call_api(user_message)
21
-
22
- # Save Bot Response
23
- st.session_state["messages"].append({"role": "assistant", "content": bot_response})
24
-
25
- # Clear Input Field
26
- st.session_state.user_input = "" # Reset user input field
27
-
28
  # Main Chat UI
29
  st.title("DUBS v.0.0.3")
30
  st.markdown("Empowering you with a Sustainable AI")
@@ -32,20 +21,20 @@ st.markdown("Empowering you with a Sustainable AI")
32
  # Chat Display
33
  for message in st.session_state["messages"]:
34
  if message["role"] == "user":
35
- st.markdown(
36
- f"<div style='text-align: right; background-color: #f3e5f5; color: #6a1b9a; margin: 10px 0; padding: 10px; border-radius: 10px;'><b>You:</b> {message['content']}</div>",
37
- unsafe_allow_html=True,
38
- )
39
  elif message["role"] == "assistant":
40
- st.markdown(
41
- f"<div style='text-align: left; background-color: #fffde7; color: #fbc02d; margin: 10px 0; padding: 10px; border-radius: 10px;'><b>Bot:</b> {message['content']}</div>",
42
- unsafe_allow_html=True,
43
- )
44
 
45
  # User Input
46
- st.text_input(
47
- "Your message",
48
- placeholder="Type your message here...",
49
- key="user_input",
50
- on_change=handle_input,
51
- )
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from api_helper import call_api # Keep the original API logic
3
+
4
+ # Sidebar Configuration
5
+ with st.sidebar:
6
+ st.markdown("### DUBS Settings")
7
+ st.markdown("""[View the source code](https://github.com/streamlit/llm-examples/blob/main/Chatbot.py)""")
8
+ st.markdown("""[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/streamlit/llm-examples?quickstart=1)""")
9
 
10
  # Streamlit Configurations
11
+ st.set_page_config(page_title="💬 DUBS Chatbot", layout="centered")
12
 
13
  # Chat History Initialization
14
  if "messages" not in st.session_state:
15
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Main Chat UI
18
  st.title("DUBS v.0.0.3")
19
  st.markdown("Empowering you with a Sustainable AI")
 
21
  # Chat Display
22
  for message in st.session_state["messages"]:
23
  if message["role"] == "user":
24
+ st.chat_message("user").write(message["content"])
 
 
 
25
  elif message["role"] == "assistant":
26
+ st.chat_message("assistant").write(message["content"])
 
 
 
27
 
28
  # User Input
29
+ if prompt := st.chat_input():
30
+ # Append the user's message to the chat history
31
+ st.session_state["messages"].append({"role": "user", "content": prompt})
32
+ st.chat_message("user").write(prompt)
33
+
34
+ with st.spinner("Fetching response..."):
35
+ # Call the original API logic to get the response
36
+ response = call_api(prompt)
37
+
38
+ # Append the assistant's response to the chat history
39
+ st.session_state["messages"].append({"role": "assistant", "content": response})
40
+ st.chat_message("assistant").write(response)