File size: 7,790 Bytes
682ea6c
5ef463a
8b13354
 
b861f00
04eac3c
061fe70
 
8b13354
35f8e95
 
061fe70
 
8b13354
061fe70
b861f00
35f8e95
 
5ef463a
 
fc56677
b861f00
682ea6c
 
061fe70
b861f00
35f8e95
8b13354
 
 
682ea6c
b861f00
 
 
 
 
682ea6c
b861f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390fc75
5ef463a
390fc75
 
 
8b13354
35f8e95
fc56677
35f8e95
8b13354
04eac3c
 
fc56677
35f8e95
b861f00
8b13354
 
b861f00
8b13354
 
b861f00
8b13354
 
 
061fe70
b861f00
061fe70
 
682ea6c
 
061fe70
682ea6c
 
8b13354
 
 
fc56677
04eac3c
 
8b13354
fc56677
04eac3c
 
 
 
fc56677
 
04eac3c
 
 
061fe70
 
8b13354
 
682ea6c
8b13354
b861f00
8b13354
 
061fe70
 
682ea6c
 
061fe70
390fc75
061fe70
390fc75
5ef463a
 
b861f00
061fe70
 
04eac3c
35f8e95
 
b861f00
061fe70
35f8e95
 
b861f00
 
 
 
061fe70
 
682ea6c
061fe70
b861f00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682ea6c
061fe70
 
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
# app.py (Updated with a toggle for the admin panel)

import sys
import subprocess
import json
from flask import Flask, render_template, request, flash, redirect, url_for, jsonify
import torch
from transformers import AutoTokenizer, AutoModel
import os
import chromadb
from huggingface_hub import snapshot_download

app = Flask(__name__)
app.secret_key = os.urandom(24)

# --- App Configuration ---
CHROMA_PATH = "chroma_db"
COLLECTION_NAME = "bible_verses"
MODEL_NAME = "sentence-transformers/multi-qa-mpnet-base-dot-v1"
DATASET_REPO = "broadfield-dev/bible-chromadb-multi-qa-mpnet"
STATUS_FILE = "build_status.log"
JSON_DIRECTORY = 'bible_json'
SHOW_ADMIN_PANEL = os.environ.get('SHOW_ADMIN_PANEL', 'False').lower() in ('true', '1', 't', 'yes')


# --- Globals and Helpers ---
chroma_collection = None
tokenizer = None
embedding_model = None

# (BOOK_ID_TO_NAME and BOOK_NAME_TO_ID dictionaries remain the same)
BOOK_ID_TO_NAME = {
    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"
}
BOOK_NAME_TO_ID = {v: k for k, v in BOOK_ID_TO_NAME.items()}


def get_verses_from_json(version, book_name, chapter=None):
    book_id = BOOK_NAME_TO_ID.get(book_name)
    if not book_id: return None
    
    file_path = os.path.join(JSON_DIRECTORY, f"t_{version.lower()}.json")
    if not os.path.exists(file_path): return None

    with open(file_path, 'r') as f: data = json.load(f)
    
    rows = data.get("resultset", {}).get("row", [])
    verses = []
    for row in rows:
        field = row.get("field", [])
        if len(field) == 5:
            row_book_id, row_chapter, row_verse, text = field[1], field[2], field[3], field[4]
            if row_book_id == book_id and (chapter is None or row_chapter == chapter):
                verses.append({'chapter': row_chapter, 'verse': row_verse, 'text': text.strip()})
    
    verses.sort(key=lambda v: (v['chapter'], v['verse']))
    return verses

def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0]
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def load_resources():
    global chroma_collection, tokenizer, embedding_model
    if chroma_collection and embedding_model: return True
    print("Attempting to load resources...")
    try:
        if not os.path.exists(CHROMA_PATH) or not os.listdir(CHROMA_PATH):
            print(f"Local DB not found. Downloading from '{DATASET_REPO}'...")
            snapshot_download(repo_id=DATASET_REPO, repo_type="dataset", local_dir=CHROMA_PATH, local_dir_use_symlinks=False)
        client = chromadb.PersistentClient(path=CHROMA_PATH)
        chroma_collection = client.get_collection(name=COLLECTION_NAME)
        tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
        embedding_model = AutoModel.from_pretrained(MODEL_NAME)
        print(f"Resources loaded: DB has {chroma_collection.count()} items. Model is '{MODEL_NAME}'.")
        return True
    except Exception as e:
        print(f"Could not load resources. DB may not be built. Error: {e}")
        return False

resources_loaded = load_resources()

# --- Routes ---
@app.route('/')
def home():
    # *** CHANGE 2: PASS THE TOGGLE VARIABLE TO THE TEMPLATE ***
    return render_template('index.html', show_admin=SHOW_ADMIN_PANEL)

# The /build-rag and /status routes are only used by the admin panel's javascript.
# They will simply be unused if the panel is hidden, which is fine. No changes needed.
@app.route('/build-rag', methods=['POST'])
def build_rag_route():
    try:
        with open(STATUS_FILE, "w") as f: f.write("IN_PROGRESS: Starting build process...")
        subprocess.Popen([sys.executable, "build_rag.py"])
        return jsonify({"status": "started"})
    except Exception as e:
        with open(STATUS_FILE, "w") as f: f.write(f"FAILED: Could not start process - {e}")
        return jsonify({"status": "error", "message": str(e)}), 500

@app.route('/status')
def status():
    if not os.path.exists(STATUS_FILE): return jsonify({"status": "NOT_STARTED"})
    with open(STATUS_FILE, "r") as f: status_line = f.read().strip()
    status_code, _, message = status_line.partition(': ')
    return jsonify({"status": status_code, "message": message})
    
@app.route('/search', methods=['POST'])
def search():
    global resources_loaded
    if not resources_loaded:
        resources_loaded = load_resources()
        if not resources_loaded:
            flash("Database not ready. Please build the database from the Admin Panel.", "error")
            return redirect(url_for('home'))

    user_query = request.form['query']
    if not user_query:
        # *** CHANGE 3: PASS THE TOGGLE ON ALL RENDERS OF INDEX.HTML ***
        return render_template('index.html', results=[], show_admin=SHOW_ADMIN_PANEL)

    encoded_input = tokenizer([user_query], padding=True, truncation=True, return_tensors='pt')
    with torch.no_grad():
        model_output = embedding_model(**encoded_input)
    query_embedding = mean_pooling(model_output, encoded_input['attention_mask'])

    search_results = chroma_collection.query(query_embeddings=query_embedding.cpu().tolist(), n_results=10)

    results_list = []
    documents, metadatas, distances = search_results['documents'][0], search_results['metadatas'][0], search_results['distances'][0]

    for i in range(len(documents)):
        meta = metadatas[i]
        results_list.append({
            'score': distances[i],
            'text': documents[i],
            'reference': meta.get('reference', 'N/A'),
            'version': meta.get('version', 'N/A'),
            'book_name': meta.get('book_name', ''),
            'chapter': meta.get('chapter', '')
        })
    
    return render_template('index.html', results=results_list, query=user_query, show_admin=SHOW_ADMIN_PANEL)

@app.route('/chapter/<version>/<book_name>/<int:chapter_num>')
def view_chapter(version, book_name, chapter_num):
    verses = get_verses_from_json(version, book_name, chapter=chapter_num)
    if not verses:
        return "Chapter not found.", 404
    return render_template('chapter.html', book_name=book_name, chapter_num=chapter_num, verses=verses, version=version)

@app.route('/book/<version>/<book_name>')
def view_book(version, book_name):
    all_verses = get_verses_from_json(version, book_name)
    if not all_verses:
        return "Book not found.", 404
    
    chapters = {}
    for verse in all_verses:
        chap_num = verse['chapter']
        if chap_num not in chapters: chapters[chap_num] = []
        chapters[chap_num].append(verse)
        
    return render_template('book.html', book_name=book_name, chapters=chapters)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)