Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
import sys
|
| 4 |
import subprocess
|
|
@@ -20,19 +20,21 @@ MODEL_NAME = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
|
|
| 20 |
DATASET_REPO = "broadfield-dev/bible-chromadb-multi-qa-mpnet"
|
| 21 |
STATUS_FILE = "build_status.log"
|
| 22 |
JSON_DIRECTORY = 'bible_json'
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# --- Globals and Helpers ---
|
| 25 |
chroma_collection = None
|
| 26 |
tokenizer = None
|
| 27 |
embedding_model = None
|
| 28 |
|
| 29 |
-
#
|
| 30 |
BOOK_ID_TO_NAME = {
|
| 31 |
1: "Genesis", 2: "Exodus", 3: "Leviticus", 4: "Numbers", 5: "Deuteronomy", 6: "Joshua", 7: "Judges", 8: "Ruth", 9: "1 Samuel", 10: "2 Samuel", 11: "1 Kings", 12: "2 Kings", 13: "1 Chronicles", 14: "2 Chronicles", 15: "Ezra", 16: "Nehemiah", 17: "Esther", 18: "Job", 19: "Psalms", 20: "Proverbs", 21: "Ecclesiastes", 22: "Song of Solomon", 23: "Isaiah", 24: "Jeremiah", 25: "Lamentations", 26: "Ezekiel", 27: "Daniel", 28: "Hosea", 29: "Joel", 30: "Amos", 31: "Obadiah", 32: "Jonah", 33: "Micah", 34: "Nahum", 35: "Habakkuk", 36: "Zephaniah", 37: "Haggai", 38: "Zechariah", 39: "Malachi", 40: "Matthew", 41: "Mark", 42: "Luke", 43: "John", 44: "Acts", 45: "Romans", 46: "1 Corinthians", 47: "2 Corinthians", 48: "Galatians", 49: "Ephesians", 50: "Philippians", 51: "Colossians", 52: "1 Thessalonians", 53: "2 Thessalonians", 54: "1 Timothy", 55: "2 Timothy", 56: "Titus", 57: "Philemon", 58: "Hebrews", 59: "James", 60: "1 Peter", 61: "2 Peter", 62: "1 John", 63: "2 John", 64: "3 John", 65: "Jude", 66: "Revelation"
|
| 32 |
}
|
| 33 |
BOOK_NAME_TO_ID = {v: k for k, v in BOOK_ID_TO_NAME.items()}
|
| 34 |
|
| 35 |
-
|
| 36 |
def get_verses_from_json(version, book_name, chapter=None):
|
| 37 |
book_id = BOOK_NAME_TO_ID.get(book_name)
|
| 38 |
if not book_id: return None
|
|
@@ -82,8 +84,11 @@ resources_loaded = load_resources()
|
|
| 82 |
# --- Routes ---
|
| 83 |
@app.route('/')
|
| 84 |
def home():
|
| 85 |
-
|
|
|
|
| 86 |
|
|
|
|
|
|
|
| 87 |
@app.route('/build-rag', methods=['POST'])
|
| 88 |
def build_rag_route():
|
| 89 |
try:
|
|
@@ -105,14 +110,15 @@ def status():
|
|
| 105 |
def search():
|
| 106 |
global resources_loaded
|
| 107 |
if not resources_loaded:
|
| 108 |
-
resources_loaded = load_resources()
|
| 109 |
if not resources_loaded:
|
| 110 |
flash("Database not ready. Please build the database from the Admin Panel.", "error")
|
| 111 |
return redirect(url_for('home'))
|
| 112 |
|
| 113 |
user_query = request.form['query']
|
| 114 |
if not user_query:
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
encoded_input = tokenizer([user_query], padding=True, truncation=True, return_tensors='pt')
|
| 118 |
with torch.no_grad():
|
|
@@ -126,7 +132,6 @@ def search():
|
|
| 126 |
|
| 127 |
for i in range(len(documents)):
|
| 128 |
meta = metadatas[i]
|
| 129 |
-
# *** CHANGE 3: PASS NEW METADATA TO TEMPLATE FOR LINKING ***
|
| 130 |
results_list.append({
|
| 131 |
'score': distances[i],
|
| 132 |
'text': documents[i],
|
|
@@ -136,9 +141,8 @@ def search():
|
|
| 136 |
'chapter': meta.get('chapter', '')
|
| 137 |
})
|
| 138 |
|
| 139 |
-
return render_template('index.html', results=results_list, query=user_query)
|
| 140 |
|
| 141 |
-
# *** ADD 3: NEW ROUTE FOR VIEWING A FULL CHAPTER ***
|
| 142 |
@app.route('/chapter/<version>/<book_name>/<int:chapter_num>')
|
| 143 |
def view_chapter(version, book_name, chapter_num):
|
| 144 |
verses = get_verses_from_json(version, book_name, chapter=chapter_num)
|
|
@@ -146,7 +150,6 @@ def view_chapter(version, book_name, chapter_num):
|
|
| 146 |
return "Chapter not found.", 404
|
| 147 |
return render_template('chapter.html', book_name=book_name, chapter_num=chapter_num, verses=verses, version=version)
|
| 148 |
|
| 149 |
-
# *** ADD 4: NEW ROUTE FOR VIEWING A FULL BOOK ***
|
| 150 |
@app.route('/book/<version>/<book_name>')
|
| 151 |
def view_book(version, book_name):
|
| 152 |
all_verses = get_verses_from_json(version, book_name)
|
|
@@ -161,5 +164,6 @@ def view_book(version, book_name):
|
|
| 161 |
|
| 162 |
return render_template('book.html', book_name=book_name, chapters=chapters)
|
| 163 |
|
|
|
|
| 164 |
if __name__ == '__main__':
|
| 165 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
# app.py (Updated with a toggle for the admin panel)
|
| 2 |
|
| 3 |
import sys
|
| 4 |
import subprocess
|
|
|
|
| 20 |
DATASET_REPO = "broadfield-dev/bible-chromadb-multi-qa-mpnet"
|
| 21 |
STATUS_FILE = "build_status.log"
|
| 22 |
JSON_DIRECTORY = 'bible_json'
|
| 23 |
+
SHOW_ADMIN_PANEL = os.environ.get('SHOW_ADMIN_PANEL', 'False').lower() in ('true', '1', 't', 'yes')
|
| 24 |
+
|
| 25 |
|
| 26 |
# --- Globals and Helpers ---
|
| 27 |
chroma_collection = None
|
| 28 |
tokenizer = None
|
| 29 |
embedding_model = None
|
| 30 |
|
| 31 |
+
# (BOOK_ID_TO_NAME and BOOK_NAME_TO_ID dictionaries remain the same)
|
| 32 |
BOOK_ID_TO_NAME = {
|
| 33 |
1: "Genesis", 2: "Exodus", 3: "Leviticus", 4: "Numbers", 5: "Deuteronomy", 6: "Joshua", 7: "Judges", 8: "Ruth", 9: "1 Samuel", 10: "2 Samuel", 11: "1 Kings", 12: "2 Kings", 13: "1 Chronicles", 14: "2 Chronicles", 15: "Ezra", 16: "Nehemiah", 17: "Esther", 18: "Job", 19: "Psalms", 20: "Proverbs", 21: "Ecclesiastes", 22: "Song of Solomon", 23: "Isaiah", 24: "Jeremiah", 25: "Lamentations", 26: "Ezekiel", 27: "Daniel", 28: "Hosea", 29: "Joel", 30: "Amos", 31: "Obadiah", 32: "Jonah", 33: "Micah", 34: "Nahum", 35: "Habakkuk", 36: "Zephaniah", 37: "Haggai", 38: "Zechariah", 39: "Malachi", 40: "Matthew", 41: "Mark", 42: "Luke", 43: "John", 44: "Acts", 45: "Romans", 46: "1 Corinthians", 47: "2 Corinthians", 48: "Galatians", 49: "Ephesians", 50: "Philippians", 51: "Colossians", 52: "1 Thessalonians", 53: "2 Thessalonians", 54: "1 Timothy", 55: "2 Timothy", 56: "Titus", 57: "Philemon", 58: "Hebrews", 59: "James", 60: "1 Peter", 61: "2 Peter", 62: "1 John", 63: "2 John", 64: "3 John", 65: "Jude", 66: "Revelation"
|
| 34 |
}
|
| 35 |
BOOK_NAME_TO_ID = {v: k for k, v in BOOK_ID_TO_NAME.items()}
|
| 36 |
|
| 37 |
+
|
| 38 |
def get_verses_from_json(version, book_name, chapter=None):
|
| 39 |
book_id = BOOK_NAME_TO_ID.get(book_name)
|
| 40 |
if not book_id: return None
|
|
|
|
| 84 |
# --- Routes ---
|
| 85 |
@app.route('/')
|
| 86 |
def home():
|
| 87 |
+
# *** CHANGE 2: PASS THE TOGGLE VARIABLE TO THE TEMPLATE ***
|
| 88 |
+
return render_template('index.html', show_admin=SHOW_ADMIN_PANEL)
|
| 89 |
|
| 90 |
+
# The /build-rag and /status routes are only used by the admin panel's javascript.
|
| 91 |
+
# They will simply be unused if the panel is hidden, which is fine. No changes needed.
|
| 92 |
@app.route('/build-rag', methods=['POST'])
|
| 93 |
def build_rag_route():
|
| 94 |
try:
|
|
|
|
| 110 |
def search():
|
| 111 |
global resources_loaded
|
| 112 |
if not resources_loaded:
|
| 113 |
+
resources_loaded = load_resources()
|
| 114 |
if not resources_loaded:
|
| 115 |
flash("Database not ready. Please build the database from the Admin Panel.", "error")
|
| 116 |
return redirect(url_for('home'))
|
| 117 |
|
| 118 |
user_query = request.form['query']
|
| 119 |
if not user_query:
|
| 120 |
+
# *** CHANGE 3: PASS THE TOGGLE ON ALL RENDERS OF INDEX.HTML ***
|
| 121 |
+
return render_template('index.html', results=[], show_admin=SHOW_ADMIN_PANEL)
|
| 122 |
|
| 123 |
encoded_input = tokenizer([user_query], padding=True, truncation=True, return_tensors='pt')
|
| 124 |
with torch.no_grad():
|
|
|
|
| 132 |
|
| 133 |
for i in range(len(documents)):
|
| 134 |
meta = metadatas[i]
|
|
|
|
| 135 |
results_list.append({
|
| 136 |
'score': distances[i],
|
| 137 |
'text': documents[i],
|
|
|
|
| 141 |
'chapter': meta.get('chapter', '')
|
| 142 |
})
|
| 143 |
|
| 144 |
+
return render_template('index.html', results=results_list, query=user_query, show_admin=SHOW_ADMIN_PANEL)
|
| 145 |
|
|
|
|
| 146 |
@app.route('/chapter/<version>/<book_name>/<int:chapter_num>')
|
| 147 |
def view_chapter(version, book_name, chapter_num):
|
| 148 |
verses = get_verses_from_json(version, book_name, chapter=chapter_num)
|
|
|
|
| 150 |
return "Chapter not found.", 404
|
| 151 |
return render_template('chapter.html', book_name=book_name, chapter_num=chapter_num, verses=verses, version=version)
|
| 152 |
|
|
|
|
| 153 |
@app.route('/book/<version>/<book_name>')
|
| 154 |
def view_book(version, book_name):
|
| 155 |
all_verses = get_verses_from_json(version, book_name)
|
|
|
|
| 164 |
|
| 165 |
return render_template('book.html', book_name=book_name, chapters=chapters)
|
| 166 |
|
| 167 |
+
|
| 168 |
if __name__ == '__main__':
|
| 169 |
app.run(host='0.0.0.0', port=7860)
|