Spaces:
Sleeping
Sleeping
| import requests | |
| import streamlit as st | |
| API_URL = "http://localhost:8000" | |
| def main(): | |
| st.set_page_config(page_title="π Cat Assistant", layout="wide") | |
| st.title("π Chat Assistant") | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| collection_name = st.text_input("Name Your Collection", value="my_collection") | |
| with st.expander("π€ Upload Documents or URL"): | |
| upload_type = st.radio("Choose upload type", ["File", "Web URL"], horizontal=True) | |
| if upload_type == "File": | |
| uploaded_file = st.file_uploader("Upload .pdf or .txt", type=["pdf", "txt"]) | |
| if uploaded_file and st.button("Upload File"): | |
| res = requests.post( | |
| f"{API_URL}/upload/file", | |
| files={"file": uploaded_file}, | |
| data={"collection": collection_name} | |
| ) | |
| st.success(res.json().get("message", "Uploaded")) | |
| if upload_type == "Web URL": | |
| url = st.text_input("Enter Web URL") | |
| if st.button("Upload URL"): | |
| res = requests.post(f"{API_URL}/upload/url", json={ | |
| "doc_type": "url", | |
| "content": url, | |
| "file_name": collection_name, | |
| }) | |
| st.success(res.json().get("message", "URL Indexed")) | |
| for msg in st.session_state.messages: | |
| with st.chat_message(msg["role"]): | |
| st.markdown(msg["content"]) | |
| if query := st.chat_input("Ask me anything..."): | |
| st.session_state.messages.append({"role": "user", "content": query}) | |
| with st.chat_message("user"): | |
| st.markdown(query) | |
| with st.chat_message("assistant"): | |
| with st.spinner("Thinking..."): | |
| res = requests.post(f"{API_URL}/chat/", json={ | |
| "query": query, | |
| "collection": collection_name | |
| }) | |
| answer = res.json()["answer"] | |
| st.markdown(answer) | |
| st.session_state.messages.append({"role": "assistant", "content": answer}) |