File size: 2,155 Bytes
6a91298 5fc13d5 6a91298 5fc13d5 6a91298 5fc13d5 6a91298 bac1636 5fc13d5 bac1636 5fc13d5 bac1636 6a91298 5fc13d5 6a91298 5fc13d5 6a91298 5fc13d5 6a91298 | 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 | import streamlit as st
import requests
import os
import json
import sys
from dotenv import load_dotenv
# Add parent directory to path to import utils
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from utils import load_css
load_dotenv()
API_URL = os.getenv("API_URL", "http://localhost:8000/")
st.set_page_config(page_title="Chat App", page_icon="💬", layout="wide")
load_css()
col_title, col_btn = st.columns([0.8, 0.2])
with col_title:
st.markdown('<h1 class="gradient-text">💬 Chat Interface</h1>', unsafe_allow_html=True)
with col_btn:
if st.button("Clear History", type="primary"):
st.session_state.messages = []
st.rerun()
if "messages" not in st.session_state:
st.session_state.messages = []
# Chat container
chat_container = st.container()
with chat_container:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Ask a question about your documents..."):
st.session_state.messages.append(
{"role": "user", "content": prompt}
)
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
res = None
try:
res = requests.post(
API_URL+"chat",
json={"question": prompt, "history": json.dumps(st.session_state.messages)}
)
except requests.exceptions.RequestException:
st.error("⚠️ Could not connect to the backend. Please try again later.")
st.stop()
if res is None:
st.stop()
if res.status_code == 200:
reply = res.json()["response"]
else:
reply = "Sorry, something went wrong. Please try again later."
st.session_state.messages.append(
{"role": "assistant", "content": reply}
)
st.markdown(reply)
|