Adoption commited on
Commit
86d9e59
Β·
verified Β·
1 Parent(s): 96eaaa5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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}")