Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +215 -103
src/streamlit_app.py
CHANGED
|
@@ -2,120 +2,232 @@ import streamlit as st
|
|
| 2 |
import time
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
-
from app import get_rag_chain
|
|
|
|
| 8 |
except ImportError as e:
|
| 9 |
-
st.error(f"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
div[data-testid="stChatMessage"] {
|
| 20 |
-
background-color: #ffffff !important;
|
| 21 |
-
border: 1px solid #e0e0e0;
|
| 22 |
-
color: #000000 !important;
|
| 23 |
}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
color: #000000 !important;
|
| 28 |
}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
""", unsafe_allow_html=True)
|
| 33 |
|
| 34 |
-
# ---
|
| 35 |
-
if "
|
| 36 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# --- LOAD
|
| 39 |
@st.cache_resource(show_spinner=False)
|
| 40 |
-
def
|
| 41 |
return get_rag_chain()
|
| 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 |
-
if prompt := st.chat_input("Ask a question..."):
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
sources = []
|
| 81 |
-
for doc in response.get('source_documents', []):
|
| 82 |
-
meta = doc.metadata
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Logic: Avoid repeating if Title IS the Date
|
| 91 |
-
if title == date:
|
| 92 |
-
display_str = f"**{source_file}** | Para {para}"
|
| 93 |
-
elif title != "Unknown Title":
|
| 94 |
-
display_str = f"**{title}** ({date}) | Para {para}"
|
| 95 |
-
else:
|
| 96 |
-
display_str = f"**{source_file}** | Para {para}"
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- 1. PAGE CONFIGURATION ---
|
| 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 |
+
# --- 2. BACKEND LOADING ---
|
| 14 |
+
backend_loaded = False
|
| 15 |
try:
|
| 16 |
+
from app import get_rag_chain, search_archives
|
| 17 |
+
backend_loaded = True
|
| 18 |
except ImportError as e:
|
| 19 |
+
st.error(f"β System Error: Could not load the Brain.\n\nDetails: {e}")
|
| 20 |
+
|
| 21 |
+
# --- 3. HELPER: SMART LINK GENERATOR ---
|
| 22 |
+
def generate_sermon_link(filename):
|
| 23 |
+
"""
|
| 24 |
+
Converts '65_1128M_Gods_Only_Provided_Place.PDF'
|
| 25 |
+
into 'https://churchages.net/en/sermon/branham/65-1128m-gods-only-provided-place'
|
| 26 |
+
"""
|
| 27 |
+
if not filename: return "#"
|
| 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 |
+
# --- 4. ADAPTIVE CSS ---
|
| 45 |
+
st.markdown("""
|
| 46 |
+
<style>
|
| 47 |
+
/* FONTS & BASICS */
|
| 48 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Playfair+Display:ital,wght@0,600;1,600&display=swap');
|
| 49 |
+
html, body, [class*="css"] { font-family: 'Inter', sans-serif; }
|
| 50 |
+
h1, h2, h3 { font-family: 'Playfair Display', serif !important; }
|
| 51 |
+
|
| 52 |
+
/* CHAT BUBBLES */
|
| 53 |
+
div[data-testid="stChatMessage"][data-test-role="user"] {
|
| 54 |
+
background-color: rgba(128, 128, 128, 0.1);
|
| 55 |
+
border-radius: 20px 20px 5px 20px;
|
| 56 |
+
}
|
| 57 |
+
div[data-testid="stChatMessage"][data-test-role="assistant"] {
|
| 58 |
+
background-color: rgba(212, 175, 55, 0.05);
|
| 59 |
+
border-radius: 20px 20px 20px 5px;
|
| 60 |
+
border-left: 4px solid #D4AF37;
|
| 61 |
+
}
|
| 62 |
|
| 63 |
+
/* QUOTE CARDS (SEARCH) */
|
| 64 |
+
.quote-card {
|
| 65 |
+
padding: 20px;
|
| 66 |
+
margin-bottom: 15px;
|
| 67 |
+
border-radius: 12px;
|
| 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 |
+
/* DARK/LIGHT ADAPTATION */
|
| 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 |
+
# --- 5. SESSION STATE ---
|
| 103 |
+
if "chat_history" not in st.session_state:
|
| 104 |
+
st.session_state.chat_history = []
|
| 105 |
+
|
| 106 |
+
# --- 6. SIDEBAR ---
|
| 107 |
+
with st.sidebar:
|
| 108 |
+
st.title("π¦
Controls")
|
| 109 |
+
mode = st.radio("Mode:", ["π£οΈ Chat", "π Search"], index=0, label_visibility="collapsed")
|
| 110 |
+
st.divider()
|
| 111 |
+
if st.button("ποΈ Clear Screen", use_container_width=True):
|
| 112 |
+
if mode.startswith("π£οΈ"):
|
| 113 |
+
st.session_state.chat_history = []
|
| 114 |
+
st.rerun()
|
| 115 |
+
|
| 116 |
+
# --- 7. HEADER ---
|
| 117 |
+
col1, col2 = st.columns([1, 15])
|
| 118 |
+
with col1: st.markdown("# π¦
")
|
| 119 |
+
with col2:
|
| 120 |
+
if mode.startswith("π£οΈ"):
|
| 121 |
+
st.markdown("# The 7th Handle")
|
| 122 |
+
else:
|
| 123 |
+
st.markdown("# The Table")
|
| 124 |
+
st.divider()
|
| 125 |
+
|
| 126 |
+
if not backend_loaded: st.stop()
|
| 127 |
|
| 128 |
+
# --- 8. LOAD SYSTEM ---
|
| 129 |
@st.cache_resource(show_spinner=False)
|
| 130 |
+
def load_system():
|
| 131 |
return get_rag_chain()
|
| 132 |
|
| 133 |
+
# --- 9. MAIN LOGIC ---
|
| 134 |
+
|
| 135 |
+
# === A. CHAT MODE ===
|
| 136 |
+
if mode.startswith("π£οΈ"):
|
| 137 |
+
# Display History
|
| 138 |
+
for message in st.session_state.chat_history:
|
| 139 |
+
role = message["role"]
|
| 140 |
+
with st.chat_message(role, avatar="π€" if role == "user" else "π¦
"):
|
| 141 |
+
st.markdown(message["content"])
|
| 142 |
+
if "sources" in message and message["sources"]:
|
| 143 |
+
with st.expander("π References"):
|
| 144 |
+
for doc in message["sources"]:
|
| 145 |
+
# Handle both string filenames and document objects
|
| 146 |
+
if hasattr(doc, 'metadata'):
|
| 147 |
+
fname = doc.metadata.get('source', 'Unknown')
|
| 148 |
+
para = doc.metadata.get('paragraph', '')
|
| 149 |
+
else:
|
| 150 |
+
fname = str(doc)
|
| 151 |
+
para = ""
|
| 152 |
+
|
| 153 |
+
link = generate_sermon_link(fname)
|
| 154 |
+
st.markdown(f"π [{fname} (Para {para})]({link})")
|
| 155 |
+
|
| 156 |
+
# Handle Input
|
| 157 |
+
if prompt := st.chat_input("Ask a question..."):
|
| 158 |
+
st.session_state.chat_history.append({"role": "user", "content": prompt})
|
| 159 |
+
with st.chat_message("user", avatar="π€"):
|
| 160 |
+
st.markdown(prompt)
|
| 161 |
+
|
| 162 |
+
with st.chat_message("assistant", avatar="π¦
"):
|
| 163 |
+
placeholder = st.empty()
|
| 164 |
+
full_response = ""
|
| 165 |
+
with st.spinner("Thinking..."):
|
| 166 |
+
try:
|
| 167 |
+
chain = load_system()
|
| 168 |
+
response = chain.invoke({"question": prompt})
|
| 169 |
+
result_text = response['result']
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
for chunk in result_text.split():
|
| 172 |
+
full_response += chunk + " "
|
| 173 |
+
time.sleep(0.02)
|
| 174 |
+
placeholder.markdown(full_response + "β")
|
| 175 |
+
placeholder.markdown(full_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
# Process Sources
|
| 178 |
+
source_docs = response.get('source_documents', [])
|
| 179 |
+
unique_docs = {}
|
| 180 |
+
for doc in source_docs:
|
| 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 |
+
if prompt := st.chat_input("Search for a keyword..."):
|
| 206 |
+
st.subheader(f"Results for: '{prompt}'")
|
| 207 |
+
|
| 208 |
+
with st.spinner("Scanning archives..."):
|
| 209 |
+
docs, debug_log = search_archives(prompt)
|
| 210 |
+
|
| 211 |
+
with st.expander("π οΈ Debug Info", expanded=False):
|
| 212 |
+
for line in debug_log: st.write(line)
|
| 213 |
+
|
| 214 |
+
if not docs:
|
| 215 |
+
st.info("No exact records found.")
|
| 216 |
+
else:
|
| 217 |
+
for doc in docs:
|
| 218 |
+
src = doc.metadata.get('source', 'Tape')
|
| 219 |
+
para = doc.metadata.get('paragraph', '')
|
| 220 |
+
txt = doc.page_content.replace(prompt, f"**{prompt}**")
|
| 221 |
+
link = generate_sermon_link(src)
|
| 222 |
+
|
| 223 |
+
# Render Card with Link
|
| 224 |
+
st.markdown(f"""
|
| 225 |
+
<div class="quote-card">
|
| 226 |
+
<div class="quote-meta">
|
| 227 |
+
<a href="{link}" target="_blank">
|
| 228 |
+
πΌ {src} (Para {para}) β
|
| 229 |
+
</a>
|
| 230 |
+
</div>
|
| 231 |
+
<div class="quote-text">"{txt}"</div>
|
| 232 |
+
</div>
|
| 233 |
+
""", unsafe_allow_html=True)
|