Akshat1000's picture
Rename ChatBot_UI/Chatbot.py to ChatBot_UI/app.py
326fb0e verified
import streamlit as st
import requests
st.set_page_config(layout="wide")
st.write("### 🤖 Client Retention Model")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
with st.sidebar:
if st.button("New Chat"):
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
# Display existing chat history
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
# Get user input and handle empty input
if prompt := st.chat_input("Enter your query"):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
# Send user input to backend and get response
try:
response = requests.post("http://127.0.0.1:5000/chat", json={"prompt": prompt})
response.raise_for_status()
msg = response.json().get("response", "Error: No response from backend.")
except requests.exceptions.RequestException as e:
msg = f"Error: {e}"
st.session_state.messages.append({"role": "assistant", "content": msg})
st.chat_message("assistant").write(msg)