File size: 7,664 Bytes
34257f6
 
 
 
 
df69d5c
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
df69d5c
 
 
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df69d5c
 
 
 
 
 
 
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df69d5c
 
 
 
 
34257f6
 
 
 
 
 
df69d5c
34257f6
 
 
 
df69d5c
 
34257f6
 
 
 
df69d5c
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df69d5c
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174cad1
 
 
34257f6
174cad1
34257f6
 
 
 
 
 
 
 
 
 
 
174cad1
 
34257f6
174cad1
 
 
34257f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Nuremberg Trials AI - RAG-powered Q&A system
Deployed on HuggingFace Spaces
"""

import os
import json
import gradio as gr
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
from huggingface_hub import hf_hub_download, InferenceClient
from datasets import load_dataset

# Configuration
DATASET_ID = "Adherence/nuremberg-trials-rag"
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
TOP_K = 5

# Try to get HF token from environment (set in Space secrets)
HF_TOKEN = os.environ.get("HF_TOKEN")


class NurembergRAG:
    def __init__(self):
        self.index = None
        self.chunks = None
        self.model = None
        self.llm_client = None

    def load(self):
        """Load RAG components from HuggingFace."""
        print("Loading Nuremberg Trials RAG system...")

        # Load embedding model
        print("  Loading embedding model...")
        self.model = SentenceTransformer(EMBEDDING_MODEL)

        # Load chunks from dataset
        print("  Loading document chunks...")
        dataset = load_dataset(DATASET_ID, split="train")
        self.chunks = [
            {"text": row["text"], "source": row["source"]}
            for row in dataset
        ]

        # Load FAISS index
        print("  Loading FAISS index...")
        index_path = hf_hub_download(
            repo_id=DATASET_ID,
            filename="faiss_index.bin",
            repo_type="dataset"
        )
        self.index = faiss.read_index(index_path)

        # Initialize LLM client if token available
        if HF_TOKEN:
            print("  Initializing LLM client...")
            self.llm_client = InferenceClient(token=HF_TOKEN)
        else:
            print("  No HF_TOKEN - running in retrieval-only mode")
            self.llm_client = None

        print(f"  Loaded {len(self.chunks)} document chunks")
        print("Ready!")

    def search(self, query: str, top_k: int = TOP_K):
        """Search for relevant chunks."""
        query_embedding = self.model.encode([query], convert_to_numpy=True)
        distances, indices = self.index.search(
            query_embedding.astype(np.float32), top_k
        )

        results = []
        for idx, distance in zip(indices[0], distances[0]):
            if idx < len(self.chunks):
                chunk = self.chunks[idx]
                similarity = 1 / (1 + distance)
                results.append((chunk, similarity))

        return results

    def generate_answer(self, question: str, context: str) -> str:
        """Generate answer using LLM with retrieved context."""
        if not self.llm_client:
            # No LLM available - provide retrieval-only summary
            return "**Retrieved passages below contain the answer.** (LLM generation requires HF_TOKEN)"

        prompt = f"""You are an expert on the Nuremberg Trials. Answer the question based ONLY on the provided context from historical documents. If the context doesn't contain enough information, say so. Be concise.

Context from Nuremberg Trial documents:
{context}

Question: {question}

Answer:"""

        try:
            response = self.llm_client.text_generation(
                prompt,
                model="HuggingFaceH4/zephyr-7b-beta",
                max_new_tokens=400,
                temperature=0.3,
            )
            return response
        except Exception as e:
            return f"**Retrieved passages below contain the answer.** (LLM error: {str(e)[:100]})"

    def query(self, question: str) -> tuple:
        """Full RAG pipeline: retrieve + generate."""
        if not question.strip():
            return "Please enter a question.", ""

        # Retrieve relevant passages
        results = self.search(question, TOP_K)

        if not results:
            return "No relevant information found.", ""

        # Format context for LLM
        context_parts = []
        sources_md = []

        for i, (chunk, score) in enumerate(results, 1):
            context_parts.append(f"[{i}] {chunk['text'][:1000]}")
            sources_md.append(
                f"**[{i}] {chunk['source']}** (relevance: {score:.0%})\n\n"
                f"{chunk['text'][:600]}..."
            )

        context = "\n\n".join(context_parts)

        # Generate answer
        answer = self.generate_answer(question, context)

        # Format sources
        sources = "\n\n---\n\n".join(sources_md)

        return answer, sources


# Initialize RAG system
print("Initializing Nuremberg Trials AI...")
rag = NurembergRAG()
rag.load()


def answer_question(question: str) -> tuple:
    """Gradio interface function."""
    return rag.query(question)


# Example questions
examples = [
    "How many defendants were sentenced to death at Nuremberg?",
    "What were the four counts in the Nuremberg indictment?",
    "Who was the chief prosecutor for the United States?",
    "What happened to Hermann Goering?",
    "What was the legal basis for the Nuremberg trials?",
    "What were the Nazi medical experiments?",
    "What was the Einsatzgruppen trial about?",
    "Who was prosecuted in the IG Farben trial?",
    "What was the verdict for Albert Speer?",
    "What were crimes against humanity as defined at Nuremberg?",
]

# Build Gradio interface
with gr.Blocks(
    title="Nuremberg Trials AI",
    theme=gr.themes.Soft(),
) as demo:
    gr.Markdown(
        """
        # Nuremberg Trials AI

        Ask questions about the Nuremberg Trials (1945-1949). This system uses
        **Retrieval-Augmented Generation (RAG)** to search through **120,000+ passages** from:

        - **Harvard Law School** - All 13 trials, 153,010 pages (IMT + 12 NMT trials)
        - **Yale Avalon Project** - 857 documents, 11.3M words (judgments, charter, 22 volumes of proceedings)
        - **Wikipedia** - 49 pages (defendants, prosecutors, organizations)

        All answers are grounded in actual historical documents with source citations.
        """
    )

    with gr.Row():
        with gr.Column(scale=2):
            question_input = gr.Textbox(
                label="Your Question",
                placeholder="e.g., How many defendants were sentenced to death?",
                lines=2,
            )
            submit_btn = gr.Button("Ask", variant="primary")

        with gr.Column(scale=1):
            gr.Examples(
                examples=examples,
                inputs=question_input,
                label="Example Questions",
            )

    with gr.Row():
        with gr.Column():
            answer_output = gr.Textbox(
                label="Answer",
                lines=8,
                show_copy_button=True,
            )

    with gr.Accordion("Source Documents", open=False):
        sources_output = gr.Markdown(label="Retrieved Passages")

    submit_btn.click(
        fn=answer_question,
        inputs=question_input,
        outputs=[answer_output, sources_output],
    )

    question_input.submit(
        fn=answer_question,
        inputs=question_input,
        outputs=[answer_output, sources_output],
    )

    gr.Markdown(
        """
        ---
        **About**: This project aims to make the historical record of the Nuremberg Trials
        accessible through AI. Built with sentence-transformers, FAISS, and Mistral-7B.

        **Data Sources**: [Harvard Nuremberg Project](https://nuremberg.law.harvard.edu/) |
        [Yale Avalon Project](https://avalon.law.yale.edu/subject_menus/imt.asp)

        **Code**: [GitHub](https://github.com/your-repo) |
        **Dataset**: [HuggingFace](https://huggingface.co/datasets/Adherence/nuremberg-trials-rag)
        """
    )

if __name__ == "__main__":
    demo.launch()