-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.py
211 lines (174 loc) Β· 7.97 KB
/
app.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import streamlit as st
import streamlit_analytics
from config import *
st.set_page_config(
page_title="Bible Semantic Search & Study Tool | Biblos",
layout="wide",
menu_items={
"Get Help": HELP_URL,
"Report a bug": BUG_REPORT_URL,
"About": ABOUT_URL,
},
initial_sidebar_state="auto"
)
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
.stDeployButton {
display: none;
}
.css-18e3th9 {
padding-top: 0rem;
padding-bottom: 0rem;
padding-left: 1rem;
padding-right: 1rem;
}
.css-1d391kg {
padding-top: 0rem;
}
div.st-emotion-cache-16txtl3.eczjsme4 {
padding-top: 2rem !important;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.markdown("""
<style>
.block-container {
padding-top: 1rem;
padding-bottom: 0rem;
}
</style>
""", unsafe_allow_html=True)
streamlit_analytics.start_tracking(load_from_json=ANALYTICS_JSON_PATH)
from modules.search import perform_search
from modules.reader import load_bible_xml, get_full_chapter_text, split_content_into_paragraphs, find_matching_verses
from modules.greek import display_greek_results
from modules.commentary import display_commentary_results
from modules.summaries import generate_summaries, setup_llm
def main():
st.markdown(HEADER_LABEL, unsafe_allow_html=True)
if 'search_count' not in st.session_state:
st.session_state.search_count = 4
if 'current_book' not in st.session_state:
st.session_state.current_book = list(BIBLE_BOOK_NAMES.keys())[0]
if 'current_chapter' not in st.session_state:
st.session_state.current_chapter = 1
with st.sidebar:
st.subheader("Search Options")
ot_checkbox = st.checkbox("Old Testament", value=True)
nt_checkbox = st.checkbox("New Testament", value=True)
st.session_state.enable_commentary = st.checkbox("Church Fathers", value=False)
st.session_state.show_greek = st.checkbox("Greek NT", value=False)
summarize = st.checkbox("Insights", value=True)
st.markdown(SIDEBAR_LABEL, unsafe_allow_html=True)
search_query = st.text_input(
"Search",
value=st.session_state.get('search_query', ''),
key="search_input",
placeholder="What did Jesus say about...?",
on_change=lambda: st.session_state.update({'search_query': st.session_state['search_input'], 'search_count': 4})
)
search_results = None
commentary_results = None
if search_query:
with st.spinner("Searching..."):
search_results, commentary_results = perform_search(search_query, ot_checkbox, nt_checkbox, st.session_state.search_count)
if search_results:
update_book_chapter_from_search(search_results[0][0].metadata)
# Determine which tabs to display with alternative names
tabs_to_display = [f"π **{BIBLE_BOOK_NAMES[st.session_state.current_book]} {st.session_state.current_chapter}** "]
if search_results and len(search_results) > 1:
tabs_to_display.append("π More Results")
if st.session_state.show_greek and search_results:
tabs_to_display.append("β§ Greek NT")
if st.session_state.enable_commentary and commentary_results:
tabs_to_display.append("π Church Fathers")
if summarize and search_results:
tabs_to_display.append("π Insights")
tabs = st.tabs(tabs_to_display)
for i, tab_name in enumerate(tabs_to_display):
with tabs[i]:
if i == 0:
display_chapter_text(search_results)
book_col, chapter_col = st.columns([2, 1])
with book_col:
select_book()
with chapter_col:
select_chapter()
elif tab_name == "π More Results":
display_results(search_results[1:])
if len(search_results) == st.session_state.search_count and st.session_state.search_count < 8:
if st.button("Show more"):
st.session_state.search_count = min(8, st.session_state.search_count + 2)
st.rerun()
elif tab_name == "β§ Greek NT":
display_greek_results(search_results)
elif tab_name == "π Church Fathers":
display_commentary_results(commentary_results)
elif tab_name == "π Insights":
with st.spinner("Generating insights..."):
llm = setup_llm()
summaries = generate_summaries(search_query, search_results, commentary_results)
if 'bible' in summaries:
st.success(summaries['bible'])
if 'commentary' in summaries:
st.success(summaries['commentary'])
# show a footer line break to add white space
st.write("---")
def update_book_chapter_from_search(metadata):
st.session_state.current_book = metadata['book']
st.session_state.current_chapter = int(metadata['chapter'])
def select_book():
books = list(BIBLE_BOOK_NAMES.keys())
book_options = [f"{BIBLE_BOOK_NAMES[book]}" for book in books]
current_book_index = books.index(st.session_state.current_book)
selected_book_option = st.selectbox("Book", book_options, index=current_book_index, key="book_select", on_change=handle_book_change)
def handle_book_change():
selected_book = [book for book, name in BIBLE_BOOK_NAMES.items() if name == st.session_state.book_select][0]
if selected_book != st.session_state.current_book:
st.session_state.current_book = selected_book
st.session_state.current_chapter = 1
def select_chapter():
bible_xml = load_bible_xml(BIBLE_XML_FILE)
max_chapters = max(int(verse.get('c')) for verse in bible_xml.findall(f".//v[@b='{st.session_state.current_book}']"))
selected_chapter = st.number_input("Chapter", min_value=1, max_value=max_chapters, value=st.session_state.current_chapter, key="chapter_select", on_change=handle_chapter_change)
def handle_chapter_change():
if st.session_state.chapter_select != st.session_state.current_chapter:
st.session_state.current_chapter = st.session_state.chapter_select
def display_chapter_text(search_results):
if 'current_book' in st.session_state and 'current_chapter' in st.session_state:
book = st.session_state.current_book
chapter = st.session_state.current_chapter
verses = get_full_chapter_text(book, chapter)
matching_verses = []
if search_results:
first_result_content = search_results[0][0].page_content
matching_verses = find_matching_verses(verses, first_result_content)
highlighted_text = ""
for verse_num, verse_text in verses:
if verse_num in matching_verses:
highlighted_text += f'<span style="background-color: #FFD700; color: #000000;"><sup>{verse_num}</sup> {verse_text}</span> '
else:
highlighted_text += f'<sup>{verse_num}</sup> {verse_text} '
if search_results:
score = search_results[0][1]
highlighted_text = highlighted_text + f"\n\n**Similarity Score:** {round(score, 4)}\n"
paragraphs = split_content_into_paragraphs(highlighted_text)
for paragraph in paragraphs:
st.markdown(paragraph, unsafe_allow_html=True)
def display_results(bible_results):
for result in bible_results:
display_search_result(result)
def display_search_result(result):
content, metadata, score = result[0].page_content, result[0].metadata, result[1]
book = BIBLE_BOOK_NAMES.get(metadata['book'], metadata['book'])
chapter = metadata['chapter']
with st.expander(f"**{book} {chapter}**", expanded=True):
st.markdown(content)
st.write(SCORE_RESULT.format(value=round(score, 4)))
if __name__ == "__main__":
main()
streamlit_analytics.stop_tracking(
save_to_json=ANALYTICS_JSON_PATH, unsafe_password=UNSAFE_PASSWORD
)