Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +83 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,85 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
from app import get_rag_chain
|
| 4 |
|
| 5 |
+
# --- CONFIG ---
|
| 6 |
+
st.set_page_config(page_title="The Seventh Handle", page_icon="π", layout="centered")
|
| 7 |
+
|
| 8 |
+
# --- CUSTOM CSS ---
|
| 9 |
+
st.markdown("""
|
| 10 |
+
<style>
|
| 11 |
+
.stChatMessage { border-radius: 10px; border: 1px solid #E0E0E0; }
|
| 12 |
+
div[data-testid="stChatMessage"]:nth-child(even) { background-color: #FFF; border-left: 4px solid #8B5E3C; }
|
| 13 |
+
div[data-testid="stChatMessage"]:nth-child(odd) { background-color: #F9F9F9; }
|
| 14 |
+
</style>
|
| 15 |
+
""", unsafe_allow_html=True)
|
| 16 |
+
|
| 17 |
+
# --- SIDEBAR ---
|
| 18 |
+
with st.sidebar:
|
| 19 |
+
st.title("About")
|
| 20 |
+
st.info("This AI simulates the persona of William Branham using a local database of sermon transcripts.")
|
| 21 |
+
if st.button("Clear Chat"):
|
| 22 |
+
st.session_state.messages = []
|
| 23 |
+
st.rerun()
|
| 24 |
+
|
| 25 |
+
# --- INIT ---
|
| 26 |
+
if "messages" not in st.session_state:
|
| 27 |
+
st.session_state.messages = [{"role": "assistant", "content": "God bless you. What is on your heart?"}]
|
| 28 |
+
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def load_chain():
|
| 31 |
+
return get_rag_chain()
|
| 32 |
+
|
| 33 |
+
chain = load_chain()
|
| 34 |
+
|
| 35 |
+
# --- CHAT UI ---
|
| 36 |
+
st.title("The Message AI")
|
| 37 |
+
st.caption("Interactive Archive β’ Powered by Gemini Flash")
|
| 38 |
+
st.divider()
|
| 39 |
+
|
| 40 |
+
for msg in st.session_state.messages:
|
| 41 |
+
with st.chat_message(msg["role"], avatar="π" if msg["role"] == "assistant" else "π€"):
|
| 42 |
+
st.markdown(msg["content"])
|
| 43 |
+
if "sources" in msg:
|
| 44 |
+
with st.expander("Sermon References"):
|
| 45 |
+
for src in msg["sources"]:
|
| 46 |
+
st.markdown(f"- *{src}*")
|
| 47 |
+
|
| 48 |
+
if prompt := st.chat_input("Ask a question..."):
|
| 49 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 50 |
+
with st.chat_message("user", avatar="π€"):
|
| 51 |
+
st.markdown(prompt)
|
| 52 |
+
|
| 53 |
+
with st.chat_message("assistant", avatar="π"):
|
| 54 |
+
with st.spinner("Searching the archives..."):
|
| 55 |
+
try:
|
| 56 |
+
response = chain.invoke({"query": prompt})
|
| 57 |
+
result_text = response['result']
|
| 58 |
+
|
| 59 |
+
# Extract Sources
|
| 60 |
+
source_docs = response.get('source_documents', [])
|
| 61 |
+
sources = list(set([doc.metadata.get('source', 'Unknown') for doc in source_docs]))
|
| 62 |
+
|
| 63 |
+
# Typing Effect
|
| 64 |
+
placeholder = st.empty()
|
| 65 |
+
full_response = ""
|
| 66 |
+
for chunk in result_text.split():
|
| 67 |
+
full_response += chunk + " "
|
| 68 |
+
time.sleep(0.04)
|
| 69 |
+
placeholder.markdown(full_response + "β")
|
| 70 |
+
placeholder.markdown(full_response)
|
| 71 |
+
|
| 72 |
+
# Save History
|
| 73 |
+
st.session_state.messages.append({
|
| 74 |
+
"role": "assistant",
|
| 75 |
+
"content": full_response,
|
| 76 |
+
"sources": sources
|
| 77 |
+
})
|
| 78 |
+
|
| 79 |
+
if sources:
|
| 80 |
+
with st.expander("Sermon References"):
|
| 81 |
+
for src in sources:
|
| 82 |
+
st.markdown(f"- *{src}*")
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.error(f"Error: {e}")
|