File size: 1,669 Bytes
8ce97f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f57d05
8ce97f0
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st
import requests

# Your FastAPI endpoint
API_URL = "http://localhost:8000/analytics-chatbot"   # change if deployed

st.set_page_config(page_title="Analytics Chatbot", page_icon="πŸ€–", layout="centered")

st.title("πŸ“Š Analytics Chatbot")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state["messages"] = []

# Display previous messages
for chat in st.session_state["messages"]:
    role = "πŸ§‘ You" if chat["role"] == "user" else "πŸ€– Bot"
    with st.chat_message(chat["role"]):
        st.markdown(f"**{role}:** {chat['content']}")

# User input box
if prompt := st.chat_input("Ask something about analytics..."):
    # Store user message
    st.session_state["messages"].append({"role": "user", "content": prompt})

    # Show user message
    with st.chat_message("user"):
        st.markdown(f"**πŸ§‘ You:** {prompt}")

    try:
        # Call FastAPI backend
        response = requests.post(API_URL, json={"message": prompt})

        if response.status_code == 200:
            data = response.json()
            bot_reply = f"**Endpoint:** {data['endpoint']}\n\n" \
                        f"**Parameters:** `{data['parameters']}`\n\n" \
                        f"**Response:** {data['response']}"
        else:
            bot_reply = f"⚠️ Error {response.status_code}: {response.text}"

    except Exception as e:
        bot_reply = f"Failed to connect to API: {e}"

    # Store bot message
    st.session_state["messages"].append({"role": "assistant", "content": bot_reply})

    # Show bot reply
    with st.chat_message("assistant"):
        st.markdown(f"**πŸ€– Bot:**\n\n{bot_reply}")