Akshat1000 commited on
Commit
ae5ef39
·
verified ·
1 Parent(s): 55b705b

Upload Chatbot.py

Browse files
Files changed (1) hide show
  1. Chatbot.py +32 -0
Chatbot.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ st.set_page_config(layout="wide")
5
+ st.write("### 🤖 Client Retention Model")
6
+
7
+ if "messages" not in st.session_state:
8
+ st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
9
+
10
+ with st.sidebar:
11
+ if st.button("New Chat"):
12
+ st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
13
+
14
+ # Display existing chat history
15
+ for msg in st.session_state.messages:
16
+ st.chat_message(msg["role"]).write(msg["content"])
17
+
18
+ # Get user input and handle empty input
19
+ if prompt := st.chat_input("Enter your query"):
20
+ st.session_state.messages.append({"role": "user", "content": prompt})
21
+ st.chat_message("user").write(prompt)
22
+
23
+ # Send user input to backend and get response
24
+ try:
25
+ response = requests.post("http://127.0.0.1:5000/chat", json={"prompt": prompt})
26
+ response.raise_for_status()
27
+ msg = response.json().get("response", "Error: No response from backend.")
28
+ except requests.exceptions.RequestException as e:
29
+ msg = f"Error: {e}"
30
+
31
+ st.session_state.messages.append({"role": "assistant", "content": msg})
32
+ st.chat_message("assistant").write(msg)