Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +202 -185
src/streamlit_app.py
CHANGED
|
@@ -1,233 +1,250 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
| 6 |
st.set_page_config(
|
| 7 |
-
page_title="Voice of the Sign",
|
| 8 |
-
page_icon="π¦
",
|
| 9 |
-
layout="wide",
|
| 10 |
initial_sidebar_state="expanded"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
| 14 |
backend_loaded = False
|
| 15 |
try:
|
| 16 |
from app import get_rag_chain, search_archives
|
| 17 |
backend_loaded = True
|
| 18 |
-
except
|
| 19 |
-
st.error(f"β
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
"""
|
| 27 |
-
if not filename:
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
# 1. Remove file extension
|
| 31 |
-
clean_name = filename.replace(".PDF", "").replace(".pdf", "")
|
| 32 |
-
|
| 33 |
-
# 2. Replace underscores with dashes (ChurchAges format)
|
| 34 |
-
clean_name = clean_name.replace("_", "-")
|
| 35 |
-
|
| 36 |
-
# 3. Lowercase everything
|
| 37 |
-
slug = clean_name.lower()
|
| 38 |
-
|
| 39 |
-
# 4. Build URL
|
| 40 |
-
return f"https://churchages.net/en/sermon/branham/{slug}"
|
| 41 |
-
except:
|
| 42 |
return "#"
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
st.markdown("""
|
| 46 |
<style>
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
.quote-card {
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
border-left: 5px solid #D4AF37;
|
| 69 |
-
transition: transform 0.2s;
|
| 70 |
-
}
|
| 71 |
-
.quote-meta {
|
| 72 |
-
font-size: 0.9rem;
|
| 73 |
-
margin-bottom: 8px;
|
| 74 |
-
font-weight: 600;
|
| 75 |
-
}
|
| 76 |
-
.quote-meta a {
|
| 77 |
-
text-decoration: none;
|
| 78 |
-
color: #D4AF37 !important; /* Gold Link */
|
| 79 |
-
}
|
| 80 |
-
.quote-text {
|
| 81 |
-
font-family: 'Playfair Display', serif;
|
| 82 |
-
font-size: 1.1rem;
|
| 83 |
-
line-height: 1.6;
|
| 84 |
-
font-style: italic;
|
| 85 |
}
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
@media (prefers-color-scheme: dark) {
|
| 89 |
-
.quote-card { background-color: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.1); }
|
| 90 |
-
.quote-text { color: #E0E0E0; }
|
| 91 |
-
}
|
| 92 |
-
@media (prefers-color-scheme: light) {
|
| 93 |
-
.quote-card { background-color: #FFFFFF; border: 1px solid #E0E0E0; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
|
| 94 |
-
.quote-text { color: #2C3E50; }
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
header {visibility: hidden;}
|
| 98 |
-
#MainMenu {visibility: hidden;}
|
| 99 |
</style>
|
| 100 |
""", unsafe_allow_html=True)
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
if "chat_history" not in st.session_state:
|
| 104 |
st.session_state.chat_history = []
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
with st.sidebar:
|
| 108 |
st.title("π¦
Controls")
|
| 109 |
-
mode = st.radio("Mode
|
| 110 |
st.divider()
|
|
|
|
| 111 |
if st.button("ποΈ Clear Screen", use_container_width=True):
|
| 112 |
-
|
| 113 |
-
st.session_state.chat_history = []
|
| 114 |
st.rerun()
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with col2:
|
| 120 |
-
if mode.startswith("π£οΈ")
|
| 121 |
-
|
| 122 |
-
else:
|
| 123 |
-
st.markdown("# The Table")
|
| 124 |
st.divider()
|
| 125 |
|
| 126 |
-
if not backend_loaded:
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
#
|
|
|
|
|
|
|
| 129 |
@st.cache_resource(show_spinner=False)
|
| 130 |
-
def
|
| 131 |
return get_rag_chain()
|
| 132 |
|
| 133 |
-
# --- 9. MAIN LOGIC ---
|
| 134 |
|
| 135 |
-
#
|
|
|
|
|
|
|
| 136 |
if mode.startswith("π£οΈ"):
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
with st.chat_message(role, avatar="π€" if role == "user" else "π¦
"):
|
| 141 |
-
st.markdown(
|
| 142 |
-
|
|
|
|
| 143 |
with st.expander("π References"):
|
| 144 |
-
for doc in
|
| 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 |
-
src = doc.metadata.get('source', 'Unknown')
|
| 182 |
-
if src not in unique_docs:
|
| 183 |
-
unique_docs[src] = doc
|
| 184 |
-
|
| 185 |
-
final_sources = list(unique_docs.values())
|
| 186 |
-
|
| 187 |
-
if final_sources:
|
| 188 |
-
with st.expander("π References"):
|
| 189 |
-
for doc in final_sources:
|
| 190 |
-
fname = doc.metadata.get('source', 'Unknown')
|
| 191 |
-
para = doc.metadata.get('paragraph', '')
|
| 192 |
-
link = generate_sermon_link(fname)
|
| 193 |
-
st.markdown(f"π [{fname} (Para {para})]({link})")
|
| 194 |
-
|
| 195 |
-
st.session_state.chat_history.append({
|
| 196 |
-
"role": "assistant",
|
| 197 |
-
"content": full_response,
|
| 198 |
-
"sources": final_sources
|
| 199 |
-
})
|
| 200 |
-
except Exception as e:
|
| 201 |
-
st.error(f"Error: {e}")
|
| 202 |
-
|
| 203 |
-
# === B. SEARCH MODE ===
|
| 204 |
else:
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
with st.spinner("Scanning archives..."):
|
| 209 |
-
docs, debug_log = search_archives(
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
<div class="quote-
|
| 226 |
-
<
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
</div>
|
| 233 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
|
|
|
| 3 |
|
| 4 |
+
# ==============================
|
| 5 |
+
# PAGE CONFIG
|
| 6 |
+
# ==============================
|
| 7 |
st.set_page_config(
|
| 8 |
+
page_title="Voice of the Sign",
|
| 9 |
+
page_icon="π¦
",
|
| 10 |
+
layout="wide",
|
| 11 |
initial_sidebar_state="expanded"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# ==============================
|
| 15 |
+
# LOAD BACKEND
|
| 16 |
+
# ==============================
|
| 17 |
backend_loaded = False
|
| 18 |
try:
|
| 19 |
from app import get_rag_chain, search_archives
|
| 20 |
backend_loaded = True
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"β Backend failed to load:\n\n{e}")
|
| 23 |
|
| 24 |
+
# ==============================
|
| 25 |
+
# MESSAGEHUB LINK BUILDER
|
| 26 |
+
# ==============================
|
| 27 |
+
def messagehub_link(filename: str):
|
| 28 |
"""
|
| 29 |
+
Example:
|
| 30 |
+
62-0909E In His Presence.pdf
|
| 31 |
+
β https://www.messagehub.info/en/read.do?ref_num=62-0909E
|
| 32 |
"""
|
| 33 |
+
if not filename:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return "#"
|
| 35 |
|
| 36 |
+
name = filename.replace(".pdf", "").replace(".PDF", "").strip()
|
| 37 |
+
code = name.split()[0] # first token is sermon code
|
| 38 |
+
return f"https://www.messagehub.info/en/read.do?ref_num={code}"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ==============================
|
| 42 |
+
# STYLING
|
| 43 |
+
# ==============================
|
| 44 |
st.markdown("""
|
| 45 |
<style>
|
| 46 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Playfair+Display:wght@600&display=swap');
|
| 47 |
+
|
| 48 |
+
html, body, [class*="css"] {
|
| 49 |
+
font-family: 'Inter', sans-serif;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
h1, h2, h3 {
|
| 53 |
+
font-family: 'Playfair Display', serif;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
div[data-testid="stChatMessage"][data-test-role="user"] {
|
| 57 |
+
background-color: rgba(128,128,128,0.08);
|
| 58 |
+
border-radius: 20px 20px 5px 20px;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
div[data-testid="stChatMessage"][data-test-role="assistant"] {
|
| 62 |
+
background-color: rgba(212,175,55,0.05);
|
| 63 |
+
border-left: 4px solid #D4AF37;
|
| 64 |
+
border-radius: 20px 20px 20px 5px;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
/* Improve markdown spacing */
|
| 68 |
+
.markdown-text-container p {
|
| 69 |
+
margin-bottom: 0.9em;
|
| 70 |
+
line-height: 1.7;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.markdown-text-container ul {
|
| 74 |
+
margin-left: 1.2em;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.markdown-text-container h3 {
|
| 78 |
+
margin-top: 1.2em;
|
| 79 |
+
color: #D4AF37;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/* Reference cards */
|
| 83 |
+
.quote-card {
|
| 84 |
+
padding: 18px;
|
| 85 |
+
margin-bottom: 14px;
|
| 86 |
+
border-radius: 12px;
|
| 87 |
+
border-left: 5px solid #D4AF37;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.quote-meta {
|
| 91 |
+
font-weight: 600;
|
| 92 |
+
margin-bottom: 6px;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.quote-meta a {
|
| 96 |
+
color: #D4AF37;
|
| 97 |
+
text-decoration: none;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.quote-text {
|
| 101 |
+
font-family: 'Playfair Display', serif;
|
| 102 |
+
font-style: italic;
|
| 103 |
+
line-height: 1.6;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
@media (prefers-color-scheme: dark) {
|
| 107 |
+
.quote-card {
|
| 108 |
+
background-color: rgba(255,255,255,0.05);
|
| 109 |
+
border: 1px solid rgba(255,255,255,0.08);
|
| 110 |
}
|
| 111 |
+
}
|
| 112 |
|
| 113 |
+
@media (prefers-color-scheme: light) {
|
| 114 |
.quote-card {
|
| 115 |
+
background: #ffffff;
|
| 116 |
+
border: 1px solid #e6e6e6;
|
| 117 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
+
}
|
| 120 |
|
| 121 |
+
header, #MainMenu { visibility: hidden; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
</style>
|
| 123 |
""", unsafe_allow_html=True)
|
| 124 |
|
| 125 |
+
|
| 126 |
+
# ==============================
|
| 127 |
+
# SESSION STATE
|
| 128 |
+
# ==============================
|
| 129 |
if "chat_history" not in st.session_state:
|
| 130 |
st.session_state.chat_history = []
|
| 131 |
|
| 132 |
+
|
| 133 |
+
# ==============================
|
| 134 |
+
# SIDEBAR
|
| 135 |
+
# ==============================
|
| 136 |
with st.sidebar:
|
| 137 |
st.title("π¦
Controls")
|
| 138 |
+
mode = st.radio("Mode", ["π£οΈ Chat", "π Search"], index=0, label_visibility="collapsed")
|
| 139 |
st.divider()
|
| 140 |
+
|
| 141 |
if st.button("ποΈ Clear Screen", use_container_width=True):
|
| 142 |
+
st.session_state.chat_history = []
|
|
|
|
| 143 |
st.rerun()
|
| 144 |
|
| 145 |
+
|
| 146 |
+
# ==============================
|
| 147 |
+
# HEADER
|
| 148 |
+
# ==============================
|
| 149 |
+
col1, col2 = st.columns([1, 14])
|
| 150 |
+
with col1:
|
| 151 |
+
st.markdown("# π¦
")
|
| 152 |
with col2:
|
| 153 |
+
st.markdown("# The 7th Handle" if mode.startswith("π£οΈ") else "# The Table")
|
| 154 |
+
|
|
|
|
|
|
|
| 155 |
st.divider()
|
| 156 |
|
| 157 |
+
if not backend_loaded:
|
| 158 |
+
st.stop()
|
| 159 |
+
|
| 160 |
|
| 161 |
+
# ==============================
|
| 162 |
+
# LOAD RAG SYSTEM
|
| 163 |
+
# ==============================
|
| 164 |
@st.cache_resource(show_spinner=False)
|
| 165 |
+
def load_chain():
|
| 166 |
return get_rag_chain()
|
| 167 |
|
|
|
|
| 168 |
|
| 169 |
+
# =========================================================
|
| 170 |
+
# CHAT MODE
|
| 171 |
+
# =========================================================
|
| 172 |
if mode.startswith("π£οΈ"):
|
| 173 |
+
|
| 174 |
+
# --- Render chat history ---
|
| 175 |
+
for msg in st.session_state.chat_history:
|
| 176 |
+
with st.chat_message(msg["role"], avatar="π€" if msg["role"] == "user" else "π¦
"):
|
| 177 |
+
st.markdown(msg["content"], unsafe_allow_html=False)
|
| 178 |
+
|
| 179 |
+
if msg.get("sources"):
|
| 180 |
with st.expander("π References"):
|
| 181 |
+
for doc in msg["sources"]:
|
| 182 |
+
src = doc.metadata.get("source", "")
|
| 183 |
+
para = doc.metadata.get("paragraph", "")
|
| 184 |
+
link = messagehub_link(src)
|
| 185 |
+
st.markdown(f"π [{src} (Para {para})]({link})")
|
| 186 |
+
|
| 187 |
+
# --- Input ---
|
| 188 |
+
prompt = st.chat_input("Ask a question...")
|
| 189 |
+
|
| 190 |
+
if prompt:
|
| 191 |
+
# Save user message
|
| 192 |
+
st.session_state.chat_history.append({
|
| 193 |
+
"role": "user",
|
| 194 |
+
"content": prompt
|
| 195 |
+
})
|
| 196 |
+
|
| 197 |
+
with st.spinner("Searching the tapes..."):
|
| 198 |
+
chain = load_chain()
|
| 199 |
+
response = chain.invoke({"question": prompt})
|
| 200 |
+
|
| 201 |
+
answer_text = response.get("result", "")
|
| 202 |
+
sources = response.get("source_documents", [])
|
| 203 |
+
|
| 204 |
+
# Save assistant message (FULLY formed)
|
| 205 |
+
st.session_state.chat_history.append({
|
| 206 |
+
"role": "assistant",
|
| 207 |
+
"content": answer_text,
|
| 208 |
+
"sources": sources
|
| 209 |
+
})
|
| 210 |
+
|
| 211 |
+
# Force rerender so references appear immediately
|
| 212 |
+
st.rerun()
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# =========================================================
|
| 216 |
+
# SEARCH MODE
|
| 217 |
+
# =========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
else:
|
| 219 |
+
query = st.chat_input("Search for a keyword...")
|
| 220 |
+
|
| 221 |
+
if query:
|
| 222 |
+
st.subheader(f"Results for: β{query}β")
|
| 223 |
+
|
| 224 |
with st.spinner("Scanning archives..."):
|
| 225 |
+
docs, debug_log = search_archives(query)
|
| 226 |
+
|
| 227 |
+
with st.expander("π Debug Info"):
|
| 228 |
+
for line in debug_log:
|
| 229 |
+
st.write(line)
|
| 230 |
+
|
| 231 |
+
if not docs:
|
| 232 |
+
st.info("No exact records found.")
|
| 233 |
+
else:
|
| 234 |
+
for doc in docs:
|
| 235 |
+
src = doc.metadata.get("source", "")
|
| 236 |
+
para = doc.metadata.get("paragraph", "")
|
| 237 |
+
link = messagehub_link(src)
|
| 238 |
+
|
| 239 |
+
st.markdown(f"""
|
| 240 |
+
<div class="quote-card">
|
| 241 |
+
<div class="quote-meta">
|
| 242 |
+
<a href="{link}" target="_blank">
|
| 243 |
+
πΌ {src} (Para {para})
|
| 244 |
+
</a>
|
| 245 |
+
</div>
|
| 246 |
+
<div class="quote-text">
|
| 247 |
+
"{doc.page_content}"
|
| 248 |
</div>
|
| 249 |
+
</div>
|
| 250 |
+
""", unsafe_allow_html=True)
|