File size: 1,228 Bytes
ae5ef39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)