Spaces:
Sleeping
Sleeping
Vineetiitg commited on
Commit ·
ead06f2
1
Parent(s): 5521d9e
feat: build Streamlit chat interface for support copilot
Browse files
ui/app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="Support Docs Copilot", page_icon="🤖")
|
| 6 |
+
st.title("🤖 Support Docs Copilot")
|
| 7 |
+
|
| 8 |
+
BACKEND_URL = os.getenv("BACKEND_URL", "http://127.0.0.1:8000/chat/stream")
|
| 9 |
+
|
| 10 |
+
if "messages" not in st.session_state:
|
| 11 |
+
st.session_state.messages = []
|
| 12 |
+
|
| 13 |
+
for message in st.session_state.messages:
|
| 14 |
+
with st.chat_message(message["role"]):
|
| 15 |
+
st.markdown(message["content"])
|
| 16 |
+
|
| 17 |
+
if user_query := st.chat_input("Ask a support question..."):
|
| 18 |
+
with st.chat_message("user"):
|
| 19 |
+
st.markdown(user_query)
|
| 20 |
+
st.session_state.messages.append({"role": "user", "content": user_query})
|
| 21 |
+
|
| 22 |
+
with st.chat_message("assistant"):
|
| 23 |
+
response_placeholder = st.empty()
|
| 24 |
+
full_response = ""
|
| 25 |
+
try:
|
| 26 |
+
with requests.post(BACKEND_URL, json={"query": user_query}, stream=True) as response:
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
for chunk in response.iter_content(chunk_size=None, decode_unicode=True):
|
| 29 |
+
if chunk:
|
| 30 |
+
full_response += chunk
|
| 31 |
+
response_placeholder.markdown(full_response + "▌")
|
| 32 |
+
response_placeholder.markdown(full_response)
|
| 33 |
+
elif response.status_code == 400:
|
| 34 |
+
full_response = f"⚠️ {response.json().get('detail', 'Violation.')}"
|
| 35 |
+
response_placeholder.error(full_response)
|
| 36 |
+
else:
|
| 37 |
+
full_response = "⚠️ Server communication error."
|
| 38 |
+
response_placeholder.error(full_response)
|
| 39 |
+
except:
|
| 40 |
+
full_response = "❌ Backend connection error."
|
| 41 |
+
response_placeholder.error(full_response)
|
| 42 |
+
|
| 43 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|