LoveJesus commited on
Commit
0e45b7f
Β·
verified Β·
1 Parent(s): 0842b96

Deploy Intertextual Reference Network Space - chirho

Browse files
Files changed (2) hide show
  1. app.py +322 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # For God so loved the world that he gave his only begotten Son,
2
+ # that whoever believes in him should not perish but have eternal life. - John 3:16
3
+
4
+ """
5
+ app.py - HuggingFace Space for the Intertextual Reference Network.
6
+ Loads embedder + classifier from HuggingFace Hub for cross-reference discovery.
7
+ """
8
+
9
+ import json
10
+
11
+ import gradio as gr
12
+ import numpy as np
13
+ import torch
14
+ from sentence_transformers import SentenceTransformer
15
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
16
+
17
+ # HuggingFace model IDs
18
+ EMBEDDER_ID_CHIRHO = "LoveJesus/intertextual-embedder-chirho"
19
+ CLASSIFIER_ID_CHIRHO = "LoveJesus/intertextual-classifier-chirho"
20
+ DATASET_ID_CHIRHO = "LoveJesus/intertextual-dataset-chirho"
21
+
22
+ LABELS_CHIRHO = [
23
+ "direct_quote", "allusion", "thematic_parallel", "typological",
24
+ "prophecy_fulfillment", "parallel_narrative", "contrast",
25
+ ]
26
+
27
+ LABEL_DISPLAY_CHIRHO = {
28
+ "direct_quote": "Direct Quote",
29
+ "allusion": "Allusion",
30
+ "thematic_parallel": "Thematic Parallel",
31
+ "typological": "Typological",
32
+ "prophecy_fulfillment": "Prophecy Fulfillment",
33
+ "parallel_narrative": "Parallel Narrative",
34
+ "contrast": "Contrast",
35
+ }
36
+
37
+ # Global model holders
38
+ embedder_chirho = None
39
+ classifier_chirho = None
40
+ classifier_tokenizer_chirho = None
41
+ verse_ids_chirho = []
42
+ verse_texts_chirho = []
43
+ verse_embeddings_chirho = None
44
+ device_chirho = None
45
+
46
+
47
+ def load_models_chirho():
48
+ """Load both models from HuggingFace Hub."""
49
+ global embedder_chirho, classifier_chirho, classifier_tokenizer_chirho
50
+ global verse_ids_chirho, verse_texts_chirho, verse_embeddings_chirho, device_chirho
51
+
52
+ # Device
53
+ if torch.cuda.is_available():
54
+ device_chirho = torch.device("cuda")
55
+ elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
56
+ device_chirho = torch.device("mps")
57
+ else:
58
+ device_chirho = torch.device("cpu")
59
+ print(f"Using device: {device_chirho}")
60
+
61
+ # Embedder
62
+ print("Loading embedder...")
63
+ embedder_chirho = SentenceTransformer(EMBEDDER_ID_CHIRHO, device=str(device_chirho))
64
+
65
+ # Classifier
66
+ print("Loading classifier...")
67
+ classifier_tokenizer_chirho = AutoTokenizer.from_pretrained(CLASSIFIER_ID_CHIRHO)
68
+ classifier_chirho = AutoModelForSequenceClassification.from_pretrained(CLASSIFIER_ID_CHIRHO)
69
+ classifier_chirho.to(device_chirho)
70
+ classifier_chirho.eval()
71
+
72
+ # Load verse map from dataset repo
73
+ print("Loading verse map...")
74
+ try:
75
+ from huggingface_hub import hf_hub_download
76
+ verse_map_path_chirho = hf_hub_download(
77
+ repo_id=DATASET_ID_CHIRHO,
78
+ filename="verse-map-chirho.json",
79
+ repo_type="dataset",
80
+ )
81
+ with open(verse_map_path_chirho, "r") as f_chirho:
82
+ verse_map_chirho = json.load(f_chirho)
83
+ verse_ids_chirho = list(verse_map_chirho.keys())
84
+ verse_texts_chirho = list(verse_map_chirho.values())
85
+ print(f" Loaded {len(verse_ids_chirho)} verses")
86
+
87
+ # Pre-encode all verses
88
+ print("Encoding all verses (this takes a moment)...")
89
+ verse_embeddings_chirho = embedder_chirho.encode(
90
+ verse_texts_chirho,
91
+ batch_size=256,
92
+ show_progress_bar=True,
93
+ convert_to_numpy=True,
94
+ normalize_embeddings=True,
95
+ )
96
+ print(" Index built!")
97
+ except Exception as e_chirho:
98
+ print(f" Warning: Could not load verse map: {e_chirho}")
99
+ print(" Find References tab will be unavailable.")
100
+
101
+ print("All models loaded!")
102
+
103
+
104
+ def classify_pair_chirho(text_a_chirho: str, text_b_chirho: str) -> dict:
105
+ """Classify connection type between two texts."""
106
+ inputs_chirho = classifier_tokenizer_chirho(
107
+ text_a_chirho, text_b_chirho,
108
+ return_tensors="pt", truncation=True, max_length=256, padding=True,
109
+ )
110
+ inputs_chirho = {k_chirho: v_chirho.to(device_chirho) for k_chirho, v_chirho in inputs_chirho.items()}
111
+
112
+ with torch.no_grad():
113
+ outputs_chirho = classifier_chirho(**inputs_chirho)
114
+ probs_chirho = torch.softmax(outputs_chirho.logits, dim=-1).cpu().numpy()[0]
115
+
116
+ pred_idx_chirho = int(np.argmax(probs_chirho))
117
+ return {
118
+ "type_chirho": LABELS_CHIRHO[pred_idx_chirho],
119
+ "confidence_chirho": float(probs_chirho[pred_idx_chirho]),
120
+ "scores_chirho": {LABELS_CHIRHO[i_chirho]: float(probs_chirho[i_chirho]) for i_chirho in range(len(LABELS_CHIRHO))},
121
+ }
122
+
123
+
124
+ # ─── Tab Functions ───
125
+
126
+ def find_references_tab_chirho(query_text_chirho: str, k_chirho: int = 10) -> str:
127
+ """Tab 1: Find cross-references for a verse."""
128
+ if not query_text_chirho.strip():
129
+ return "Please enter a verse text."
130
+ if verse_embeddings_chirho is None:
131
+ return "Verse index not available. Please try again later."
132
+
133
+ query_emb_chirho = embedder_chirho.encode(
134
+ [query_text_chirho], normalize_embeddings=True, convert_to_numpy=True
135
+ )[0]
136
+
137
+ similarities_chirho = np.dot(verse_embeddings_chirho, query_emb_chirho)
138
+ top_indices_chirho = np.argsort(similarities_chirho)[::-1][:int(k_chirho)]
139
+
140
+ lines_chirho = ["| # | Verse | Similarity | Connection Type | Confidence |", "| --- | --- | --- | --- | --- |"]
141
+
142
+ for rank_chirho, idx_chirho in enumerate(top_indices_chirho, 1):
143
+ verse_id_chirho = verse_ids_chirho[idx_chirho]
144
+ verse_text_chirho = verse_texts_chirho[idx_chirho][:80]
145
+ sim_chirho = float(similarities_chirho[idx_chirho])
146
+
147
+ cls_chirho = classify_pair_chirho(query_text_chirho, verse_texts_chirho[idx_chirho])
148
+ type_display_chirho = LABEL_DISPLAY_CHIRHO.get(cls_chirho["type_chirho"], cls_chirho["type_chirho"])
149
+
150
+ lines_chirho.append(
151
+ f"| {rank_chirho} | **{verse_id_chirho}** {verse_text_chirho}... | {sim_chirho:.3f} | {type_display_chirho} | {cls_chirho['confidence_chirho']:.1%} |"
152
+ )
153
+
154
+ return "\n".join(lines_chirho)
155
+
156
+
157
+ def classify_pair_tab_chirho(text_a_chirho: str, text_b_chirho: str) -> tuple:
158
+ """Tab 2: Classify connection between two verses."""
159
+ if not text_a_chirho.strip() or not text_b_chirho.strip():
160
+ return "Please enter both verses.", ""
161
+
162
+ result_chirho = classify_pair_chirho(text_a_chirho, text_b_chirho)
163
+ type_display_chirho = LABEL_DISPLAY_CHIRHO.get(result_chirho["type_chirho"], result_chirho["type_chirho"])
164
+ main_result_chirho = f"**{type_display_chirho}** (confidence: {result_chirho['confidence_chirho']:.1%})"
165
+
166
+ scores_lines_chirho = ["| Connection Type | Score |", "| --- | --- |"]
167
+ for type_chirho, score_chirho in sorted(result_chirho["scores_chirho"].items(), key=lambda x: -x[1]):
168
+ display_chirho = LABEL_DISPLAY_CHIRHO.get(type_chirho, type_chirho)
169
+ bar_chirho = "=" * int(score_chirho * 20)
170
+ scores_lines_chirho.append(f"| {display_chirho} | {score_chirho:.3f} {bar_chirho} |")
171
+
172
+ return main_result_chirho, "\n".join(scores_lines_chirho)
173
+
174
+
175
+ def explore_tab_chirho(verse_id_chirho: str, k_chirho: int = 10) -> str:
176
+ """Tab 3: Explore references for a specific verse ID."""
177
+ if not verse_id_chirho.strip():
178
+ return "Please enter a verse ID (e.g., Gen.1.1, John.3.16)."
179
+ if verse_embeddings_chirho is None:
180
+ return "Verse index not available."
181
+
182
+ verse_id_chirho = verse_id_chirho.strip()
183
+ if verse_id_chirho in verse_ids_chirho:
184
+ idx_chirho = verse_ids_chirho.index(verse_id_chirho)
185
+ query_text_chirho = verse_texts_chirho[idx_chirho]
186
+ return f"**{verse_id_chirho}**: _{query_text_chirho}_\n\n" + find_references_tab_chirho(query_text_chirho, k_chirho)
187
+ else:
188
+ return f"Verse ID '{verse_id_chirho}' not found. Use OSIS format: Gen.1.1, Matt.5.3, Rev.21.1"
189
+
190
+
191
+ # ─── Build Gradio Interface ───
192
+
193
+ def build_demo_chirho() -> gr.Blocks:
194
+ """Build the Gradio demo."""
195
+ with gr.Blocks(
196
+ title="Intertextual Reference Network - loveJesus/models-chirho",
197
+ theme=gr.themes.Soft(),
198
+ ) as demo_chirho:
199
+ gr.Markdown("# Intertextual Reference Network")
200
+ gr.Markdown(
201
+ "*For God so loved the world that he gave his only begotten Son, "
202
+ "that whoever believes in him should not perish but have eternal life. - John 3:16*"
203
+ )
204
+ gr.Markdown(
205
+ "Discover **biblical cross-references** and classify their **connection types** "
206
+ "using a two-model ML pipeline: MiniLM-L12 embedder + RoBERTa-base classifier. "
207
+ "Trained on 344,799 cross-reference pairs from the Treasury of Scripture Knowledge."
208
+ )
209
+
210
+ with gr.Tab("Find References"):
211
+ query_input_chirho = gr.Textbox(
212
+ label="Enter verse text",
213
+ placeholder="In the beginning God created the heaven and the earth.",
214
+ lines=2,
215
+ )
216
+ k_input_chirho = gr.Slider(5, 25, value=10, step=1, label="Number of results")
217
+ find_btn_chirho = gr.Button("Find Cross-References", variant="primary")
218
+ find_output_chirho = gr.Markdown()
219
+
220
+ find_btn_chirho.click(
221
+ find_references_tab_chirho,
222
+ inputs=[query_input_chirho, k_input_chirho],
223
+ outputs=[find_output_chirho],
224
+ )
225
+
226
+ gr.Examples(
227
+ examples=[
228
+ ["In the beginning God created the heaven and the earth."],
229
+ ["For God so loved the world, that he gave his only begotten Son, that whosoever believeth in him should not perish, but have everlasting life."],
230
+ ["The LORD is my shepherd; I shall not want."],
231
+ ["But thou, Bethlehem Ephratah, though thou be little among the thousands of Judah, yet out of thee shall he come forth unto me that is to be ruler in Israel."],
232
+ ],
233
+ inputs=[query_input_chirho],
234
+ )
235
+
236
+ with gr.Tab("Classify Pair"):
237
+ pair_a_chirho = gr.Textbox(label="Verse A", lines=2, placeholder="Enter first verse...")
238
+ pair_b_chirho = gr.Textbox(label="Verse B", lines=2, placeholder="Enter second verse...")
239
+ classify_btn_chirho = gr.Button("Classify Connection")
240
+
241
+ cls_result_chirho = gr.Markdown(label="Result")
242
+ cls_scores_chirho = gr.Markdown(label="All Scores")
243
+
244
+ classify_btn_chirho.click(
245
+ classify_pair_tab_chirho,
246
+ inputs=[pair_a_chirho, pair_b_chirho],
247
+ outputs=[cls_result_chirho, cls_scores_chirho],
248
+ )
249
+
250
+ gr.Examples(
251
+ examples=[
252
+ [
253
+ "Therefore the Lord himself shall give you a sign; Behold, a virgin shall conceive, and bear a son, and shall call his name Immanuel.",
254
+ "Now all this was done, that it might be fulfilled which was spoken of the Lord by the prophet, saying, Behold, a virgin shall be with child.",
255
+ ],
256
+ [
257
+ "The LORD is my shepherd; I shall not want.",
258
+ "I am the good shepherd: the good shepherd giveth his life for the sheep.",
259
+ ],
260
+ [
261
+ "For as in Adam all die, even so in Christ shall all be made alive.",
262
+ "Wherefore, as by one man sin entered into the world, and death by sin; and so death passed upon all men, for that all have sinned.",
263
+ ],
264
+ ],
265
+ inputs=[pair_a_chirho, pair_b_chirho],
266
+ )
267
+
268
+ with gr.Tab("Explore"):
269
+ explore_input_chirho = gr.Textbox(
270
+ label="Verse ID (OSIS format)",
271
+ placeholder="Gen.1.1, John.3.16, Ps.23.1, Rev.21.1",
272
+ )
273
+ explore_k_chirho = gr.Slider(5, 25, value=10, step=1, label="Results")
274
+ explore_btn_chirho = gr.Button("Explore")
275
+ explore_output_chirho = gr.Markdown()
276
+ explore_btn_chirho.click(
277
+ explore_tab_chirho,
278
+ inputs=[explore_input_chirho, explore_k_chirho],
279
+ outputs=[explore_output_chirho],
280
+ )
281
+
282
+ with gr.Tab("About"):
283
+ gr.Markdown("""# Intertextual Reference Network
284
+
285
+ ## What This Does
286
+ This AI system discovers **cross-references** between Bible verses and classifies the **type of connection**:
287
+
288
+ | Type | Description | Example |
289
+ | --- | --- | --- |
290
+ | **Direct Quote** | NT directly quotes OT | Mt 1:23 quotes Is 7:14 |
291
+ | **Allusion** | Clear reference without direct quotation | Rev 5:5 alludes to Gen 49:9 |
292
+ | **Thematic Parallel** | Shared theme or motif | Ps 23 parallels Jn 10 |
293
+ | **Typological** | OT type foreshadows NT antitype | Isaac sacrifice prefigures Christ |
294
+ | **Prophecy Fulfillment** | OT prophecy fulfilled in NT | Is 53 fulfilled in Passion |
295
+ | **Parallel Narrative** | Same event in parallel accounts | Synoptic gospels |
296
+ | **Contrast** | Deliberate theological contrast | Adam vs Christ (Rom 5) |
297
+
298
+ ## Two-Model Pipeline
299
+ 1. **Embedder** (MiniLM-L12) β€” Encodes verses into semantic space for similarity search
300
+ 2. **Classifier** (RoBERTa-base) β€” Classifies the connection type between verse pairs
301
+
302
+ ## Training Data
303
+ - **344,799** cross-reference pairs from the Treasury of Scripture Knowledge (OpenBible.info)
304
+ - **31,102** KJV verses indexed for retrieval
305
+ - **28,612** Grok-labeled pairs for connection type classification
306
+
307
+ ## Important Note
308
+ This is a **research tool** for exploring biblical intertextuality. Always consult scholarly commentaries and original languages for serious study.
309
+
310
+ ---
311
+ Built with love for Jesus. Published by [loveJesus](https://huggingface.co/LoveJesus).
312
+ """)
313
+
314
+ return demo_chirho
315
+
316
+
317
+ # Load models at startup
318
+ load_models_chirho()
319
+
320
+ # Launch
321
+ demo_chirho = build_demo_chirho()
322
+ demo_chirho.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # For God so loved the world that he gave his only begotten Son,
2
+ # that whoever believes in him should not perish but have eternal life. - John 3:16
3
+
4
+ transformers>=4.40
5
+ sentence-transformers>=3.0
6
+ torch>=2.3
7
+ gradio>=4.0
8
+ numpy
9
+ huggingface-hub>=0.23