Spaces:
Sleeping
Sleeping
File size: 6,245 Bytes
db406df |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import streamlit as st
import requests
import json
import uuid
from typing import Dict, Any
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configuration
API_BASE_URL = os.getenv("API_BASE_URL", "https://pixerse-pixerse.hf.space") # Default to the env value
CHAT_ENDPOINT = f"{API_BASE_URL}/api/mcp/chat"
def call_chat_api(query: str, user_id: str) -> Dict[str, Any]:
"""Call the FastAPI chat endpoint"""
payload = {
"query": query,
"user_id": user_id
}
try:
response = requests.post(
CHAT_ENDPOINT,
json=payload,
headers={"Content-Type": "application/json"},
timeout=30
)
if response.status_code == 200:
return {
"success": True,
"data": response.json()
}
else:
return {
"success": False,
"error": f"API Error: {response.status_code} - {response.text}"
}
except requests.exceptions.RequestException as e:
return {
"success": False,
"error": f"Connection Error: {str(e)}"
}
def main():
st.set_page_config(
page_title="PiXerse Chatbot",
page_icon="🤖",
layout="wide"
)
st.title("🤖 PiXerse AI Chatbot")
st.markdown("---")
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "user_id" not in st.session_state:
st.session_state.user_id = str(uuid.uuid4())
# Sidebar for configuration
with st.sidebar:
st.header("⚙️ Configuration")
# Display user ID
st.text_input(
"User ID",
value=st.session_state.user_id,
disabled=True,
help="Your unique session identifier"
)
# API Status check
st.subheader("🔍 API Status")
if st.button("Check API Status"):
try:
health_response = requests.get(f"{API_BASE_URL}/docs", timeout=5)
if health_response.status_code == 200:
st.success("✅ API is running")
else:
st.error("❌ API is not responding properly")
except:
st.error("❌ Cannot connect to API")
# Clear chat button
if st.button("🗑️ Clear Chat History"):
st.session_state.messages = []
st.rerun()
# Main chat interface
st.subheader("💬 Chat Interface")
# Display chat messages
chat_container = st.container()
with chat_container:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Show additional info for assistant messages
if message["role"] == "assistant" and "metadata" in message:
with st.expander("📊 Response Details"):
metadata = message["metadata"]
col_meta1, col_meta2 = st.columns(2)
with col_meta1:
st.metric("Token Usage", metadata.get("token_usage", "N/A"))
with col_meta2:
st.metric("Tools Used", len(metadata.get("tools_used", [])))
if metadata.get("tools_used"):
st.write("**Tools Used:**")
for tool in metadata["tools_used"]:
st.code(tool, language="text")
if metadata.get("tools_response"):
st.write("**Tool Responses:**")
for i, response in enumerate(metadata["tools_response"]):
st.json(response)
# Chat input (outside columns to avoid Streamlit API error)
if prompt := st.chat_input("Type your message here..."):
# Add user message to chat
st.session_state.messages.append({
"role": "user",
"content": prompt
})
# Rerun to display new message immediately
st.rerun()
# Process last message if it's from user and no assistant response yet
if (st.session_state.messages and
st.session_state.messages[-1]["role"] == "user" and
(len(st.session_state.messages) == 1 or
st.session_state.messages[-2]["role"] == "assistant")):
user_message = st.session_state.messages[-1]["content"]
# Show processing message
with st.chat_message("assistant"):
with st.spinner("🤔 Thinking..."):
response = call_chat_api(user_message, st.session_state.user_id)
if response["success"]:
data = response["data"]
assistant_message = data["response"]
# Display response
st.markdown(assistant_message)
# Add assistant message to chat with metadata
st.session_state.messages.append({
"role": "assistant",
"content": assistant_message,
"metadata": {
"token_usage": data.get("token_usage", 0),
"tools_used": data.get("tools_used", []),
"tools_response": data.get("tools_response", [])
}
})
else:
error_message = f"❌ Error: {response['error']}"
st.error(error_message)
# Add error message to chat
st.session_state.messages.append({
"role": "assistant",
"content": error_message
})
# Rerun to update the display
st.rerun()
if __name__ == "__main__":
# Run Streamlit UI
main() |