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}")