Spaces:
Sleeping
Sleeping
parvalijaved
commited on
Commit
•
07ae381
1
Parent(s):
0e6cba1
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,90 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import requests
|
3 |
import json
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
-
|
|
|
20 |
|
21 |
-
# Check the
|
22 |
-
if
|
23 |
-
|
24 |
-
|
25 |
-
chapters = {int(chapter_number): details for chapter_number, details in data.items()}
|
26 |
|
27 |
-
|
28 |
-
chapter_id = st.selectbox("Select Chapter", sorted(chapters.keys()), format_func=lambda x: f"Chapter {x}")
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
st.subheader(f"Chapter {chapter_id}")
|
35 |
-
st.markdown(f"**Summary:**\n\n{chapter_summary}")
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
verse_number = verse['verse_number']
|
44 |
-
verse_text = verse['text']
|
45 |
-
verse_translation = verse['word_meanings']
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
st.markdown(f"**Translation:** {verse_translation}")
|
50 |
-
st.markdown("---")
|
51 |
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
61 |
else:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
+
import streamlit as st
|
3 |
|
4 |
+
# Load the JSON data from the specified file path
|
5 |
+
file_path = '/workspace/verse.json'
|
6 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
7 |
+
data = json.load(f)
|
8 |
|
9 |
+
# Ensure data is structured correctly
|
10 |
+
if isinstance(data, list):
|
11 |
+
chapters = {}
|
12 |
+
for item in data:
|
13 |
+
chapter_id = item['chapter_id']
|
14 |
+
if chapter_id not in chapters:
|
15 |
+
chapters[chapter_id] = []
|
16 |
+
chapters[chapter_id].append(item)
|
17 |
+
else:
|
18 |
+
st.error("The JSON data is not in the expected format.")
|
19 |
+
st.stop()
|
20 |
|
21 |
+
# Get sorted list of chapter IDs
|
22 |
+
chapter_ids = sorted(chapters.keys())
|
23 |
|
24 |
+
# Sidebar for chapter and verse selection
|
25 |
+
st.sidebar.title("Gita Explorer")
|
26 |
+
chapter_id = st.sidebar.selectbox("Select Chapter", chapter_ids)
|
27 |
|
28 |
+
# Check if the chapter selection has changed
|
29 |
+
if 'chapter_id' not in st.session_state or st.session_state.chapter_id != chapter_id:
|
30 |
+
st.session_state.chapter_id = chapter_id
|
31 |
+
st.session_state.verse_id = 1
|
|
|
32 |
|
33 |
+
selected_verses = {verse['verse_number']: verse for verse in chapters[chapter_id]}
|
|
|
34 |
|
35 |
+
# Check if verses are available in the expected format
|
36 |
+
if not selected_verses:
|
37 |
+
st.error("No verses found for this chapter or verses are not in expected format.")
|
38 |
+
st.stop()
|
|
|
|
|
39 |
|
40 |
+
# Verse selection dropdown
|
41 |
+
verse_id = st.sidebar.selectbox(
|
42 |
+
"Select Verse",
|
43 |
+
sorted(selected_verses.keys()),
|
44 |
+
index=sorted(selected_verses.keys()).index(st.session_state.verse_id)
|
45 |
+
)
|
|
|
|
|
|
|
46 |
|
47 |
+
# Update session state if verse is selected from dropdown
|
48 |
+
st.session_state.verse_id = verse_id
|
|
|
|
|
49 |
|
50 |
+
# Display the selected verse
|
51 |
+
verse = selected_verses[verse_id]
|
52 |
+
st.header(f"Chapter {verse['chapter_number']} Verse {verse['verse_number']}")
|
53 |
+
st.subheader(verse['text'])
|
54 |
+
st.text("Transliteration:")
|
55 |
+
st.write(verse.get('transliteration', "No transliteration available."))
|
56 |
+
st.text("Translation:")
|
57 |
+
st.write(verse.get('word_meanings', "No translation available."))
|
58 |
|
59 |
+
# Buttons to navigate through verses
|
60 |
+
col1, col2 = st.columns(2)
|
61 |
+
|
62 |
+
with col1:
|
63 |
+
if st.button("Previous"):
|
64 |
+
prev_verse_id = verse_id - 1
|
65 |
+
if prev_verse_id in selected_verses:
|
66 |
+
st.session_state.verse_id = prev_verse_id
|
67 |
else:
|
68 |
+
# Move to the last verse of the previous chapter
|
69 |
+
prev_chapter_index = chapter_ids.index(chapter_id) - 1
|
70 |
+
if prev_chapter_index >= 0:
|
71 |
+
prev_chapter_id = chapter_ids[prev_chapter_index]
|
72 |
+
st.session_state.chapter_id = prev_chapter_id
|
73 |
+
prev_chapter_verses = {verse['verse_number']: verse for verse in chapters[prev_chapter_id]}
|
74 |
+
st.session_state.verse_id = max(prev_chapter_verses.keys())
|
75 |
+
st.experimental_rerun()
|
76 |
+
|
77 |
+
with col2:
|
78 |
+
if st.button("Next"):
|
79 |
+
next_verse_id = verse_id + 1
|
80 |
+
if next_verse_id in selected_verses:
|
81 |
+
st.session_state.verse_id = next_verse_id
|
82 |
+
else:
|
83 |
+
# Move to the first verse of the next chapter
|
84 |
+
next_chapter_index = chapter_ids.index(chapter_id) + 1
|
85 |
+
if next_chapter_index < len(chapter_ids):
|
86 |
+
next_chapter_id = chapter_ids[next_chapter_index]
|
87 |
+
st.session_state.chapter_id = next_chapter_id
|
88 |
+
next_chapter_verses = {verse['verse_number']: verse for verse in chapters[next_chapter_id]}
|
89 |
+
st.session_state.verse_id = min(next_chapter_verses.keys())
|
90 |
+
st.experimental_rerun()
|