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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Technology Transactions Assistant (RAG model query interface)</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Rounded', 'SF Pro Display', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: #001122;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: #fff;
        }

        .header-section {
            width: 100%;
            max-width: 1200px;
            background: #333;
            border-radius: 20px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
            text-align: center;
        }

        .header-title {
            color: #fff;
            font-size: 2.5rem;
            font-weight: 500;
            text-decoration: underline;
            text-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
            margin: 0;
        }

        .header-subtitle {
            color: #ccc;
            font-size: 0.85rem;
            font-weight: 400;
            font-style: italic;
            margin: 20px;
        }

        .header-subtitle a.upload-link {
            color: #87CEFA;
            text-decoration: none;
        }

        .header-subtitle a.upload-link:hover {
            text-decoration: underline;
        }

        .banner-image {
            display: none;
        }

        .main-container {
            display: flex;
            gap: 18px;
            width: 100%;
            max-width: 1200px;
            height: 60vh; 
            align-items: stretch; 
        }

        .left-container {
            flex: 1; 
            background: #333;
            border-radius: 20px;
            padding: 32px; 
            box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
            display: flex;
            flex-direction: column;
            justify-content: flex-start; 
            gap: 10px;
            min-height: 0; 
        }

        .right-container {
            flex: 1; 
            background: #333;
            border-radius: 20px;
            padding: 32px; 
            box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
            display: flex;
            flex-direction: column;
            min-height: 0;
            position: relative;
        }

        .left-container h1 {
            display: none; 
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #fff;
            font-size: 0.95rem;
            letter-spacing: 0.5px;
        }

        .char-count {
            float: right;
            font-size: 0.85rem;
            color: #ccc;
            font-weight: normal;
            text-transform: none;
            letter-spacing: normal;
        }

        input[type="text"],
        textarea {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #555;
            border-radius: 10px;
            font-size: 1rem;
            font-family: inherit;
            transition: all 0.3s ease;
            background: #222;
            color: #fff;
        }

        input[type="text"]:focus,
        textarea:focus {
            outline: none;
            border-color: #cc0000;
            box-shadow: 0 0 0 3px rgba(204, 0, 0, 0.1);
        }

        textarea {
            resize: vertical;
            min-height: 120px;
        }

        .button-row {
            display: flex;
            gap: 12px;
            margin-top: 10px;
        }

        .button-row .submit-btn,
        .button-row .cancel-btn {
            flex: 1;
            padding: 12px 18px;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s ease;
            letter-spacing: 1px;
        }

        .submit-btn {
            background: #cfdff0; 
            color: #0b2233;
            border: 1px solid rgba(255,255,255,0.06);
        }

        .submit-btn:hover {
            background: #b9cee3;
            transform: translateY(-2px);
            box-shadow: 0 8px 18px rgba(11,34,51,0.12);
        }

        .submit-btn:active {
            transform: translateY(0);
        }

        .cancel-btn {
            background: rgba(255,102,102,0.45); 
            color: #fff;
            border: none;
        }

        .cancel-btn:hover {
            background: rgba(255,102,102,0.60);
            color: #fff;
        }

        .output-section {
            background: #222;
            border: 2px solid #555;
            border-radius: 10px;
            padding: 20px;
            flex: 1; 
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            justify-content: flex-start;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 0.95rem;
            line-height: 1.6;
            color: #fff;
            overflow-y: auto;
            margin-bottom: 0; 
        }

        .output-section.empty {
            align-items: center;
            justify-content: center;
            text-align: center;
        }

        .output-placeholder {
            color: #ccc;
            font-style: italic;
            display: block;
        }

        .loading {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            align-items: center;
            justify-content: center;
            text-align: center;
            color: #ffd700;
            background: #222;
            border-radius: 10px;
            flex-direction: column;
            gap: 12px;
        }

        .loading.active {
            display: flex;
        }

        .spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255, 215, 0, 0.3);
            border-radius: 50%;
            border-top-color: #ffd700;
            animation: spin 1s ease-in-out infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        @media (max-width: 768px) {
            .main-container {
                flex-direction: column;
                height: auto;
            }
        }
    </style>
</head>
<body>
    <div class="header-section">
        <h1 class="header-title">Technology Transactions Assistant</h1>
        <p class="header-subtitle">A RAG model that restricts GPT-5 to technology transactions use cases in the legal domain. To add to the corpus, visit <a href="/upload" class="upload-link">/upload</a>.</p>
    </div>
    
    <div class="main-container">
        <div class="left-container">
            <h1>TechTrans Query</h1>
            
            <form id="queryForm">
                <div class="form-group">
                    <label for="query">
                        Query
                        <span class="char-count"><span id="queryCount">0</span>/1000</span>
                    </label>
                    <textarea 
                        id="query" 
                        name="query" 
                        maxlength="1000" 
                        placeholder="Enter query here (e.g., 'Show an example of a confidentiality clause,' 'Define the term Affiliate,' etc.)"
                        required
                    ></textarea>
                </div>

                <div class="form-group">
                    <label for="docType">Document Type (optional)
                        <span class="char-count"><span id="docTypeCount">0</span>/200</span>
                    </label>
                    <input type="text" id="docType" name="docType" maxlength="200" placeholder="e.g., License Agreement, Terms of Use..." />
                </div>

                <div class="form-group">
                    <label for="jurisdiction">Jurisdiction (optional)
                        <span class="char-count"><span id="jurisdictionCount">0</span>/200</span>
                    </label>
                    <input type="text" id="jurisdiction" name="jurisdiction" maxlength="200" placeholder="e.g., US-CA, US-DE, FRG..." />
                </div>

                <div class="form-group">
                    <label for="topK">Top K Chunks <span class="char-count" style="float:none;">(max 20)</span></label>
                    <input type="number" id="topK" name="topK" value="5" min="1" max="20" />
                </div>

                <div class="button-row">
                    <button type="button" class="cancel-btn" id="cancelBtn">Clear</button>
                    <button type="submit" class="submit-btn">Search</button>
                </div>
            </form>
        </div>

        <div class="right-container">
            <label for="response">Query Results</label>
            <div id="response" class="output-section empty">
                <span class="output-placeholder">Response will appear here...</span>
            </div>
            <div class="loading" id="loading">
                <div class="spinner"></div>
                <p>Running vector search...</p>
            </div>
        </div>
    </div>

    <div id="dangerZone" style="margin-top:28px; font-size:0.7rem; text-align:center; max-width:1200px; width:100%; opacity:0.65;">
        <details>
            <summary style="cursor:pointer;">Danger Zone</summary>
            <div style="margin-top:10px;">
                <button id="resetBtn" style="background:#660000; color:#fff; border:1px solid #aa2222; padding:8px 14px; border-radius:6px; cursor:pointer; font-size:0.75rem; letter-spacing:0.5px;">Reset / Wipe Corpus</button>
                <p style="margin-top:8px; line-height:1.3;">This sends a POST to <code>/api/reset</code> and irreversibly deletes all files under <code>/data</code> and clears Chroma collections. Use only in development.</p>
            </div>
        </details>
    </div>

    <script>
        // Character count for inputs
        function updateCharCount(inputId, counterId) {
            const input = document.getElementById(inputId);
            const counter = document.getElementById(counterId);
            input.addEventListener('input', () => counter.textContent = input.value.length);
            counter.textContent = input.value.length;
        }
        updateCharCount('query', 'queryCount');
        updateCharCount('docType', 'docTypeCount');
        updateCharCount('jurisdiction', 'jurisdictionCount');

        document.getElementById('cancelBtn').addEventListener('click', () => {
            document.getElementById('query').value = '';
            document.getElementById('docType').value = '';
            document.getElementById('jurisdiction').value = '';
            document.getElementById('topK').value = '5';
            document.getElementById('queryCount').textContent = '0';
            document.getElementById('docTypeCount').textContent = '0';
            document.getElementById('jurisdictionCount').textContent = '0';
            const responseDiv = document.getElementById('response');
            responseDiv.innerHTML = '<span class="output-placeholder">Response will appear here...</span>';
            responseDiv.classList.add('empty');
        });

        document.getElementById('queryForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            const q = document.getElementById('query').value.trim();
            const topK = parseInt(document.getElementById('topK').value, 10) || 5;
            if (!q) return;

            const filters = {};
            const dt = document.getElementById('docType').value.trim();
            const jur = document.getElementById('jurisdiction').value.trim();
            if (dt) filters.doc_type = dt;
            if (jur) filters.jurisdiction = jur;
            const body = { query: q, top_k: topK, filters };
            const responseDiv = document.getElementById('response');
            const loadingDiv = document.getElementById('loading');
            responseDiv.innerHTML = '';
            responseDiv.classList.remove('empty');
            loadingDiv.classList.add('active');

            try {
                const resp = await fetch('/api/query', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(body)
                });
                const data = await resp.json().catch(() => ({}));
                loadingDiv.classList.remove('active');

                const escape = (str) => String(str ?? '')
                    .replace(/&/g,'&amp;')
                    .replace(/</g,'&lt;')
                    .replace(/>/g,'&gt;');

                if (!resp.ok) {
                    const msg = data.error || resp.statusText || 'Request failed';
                    responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${escape(msg)}</span>`;
                    return;
                }

                if (data.error) {
                    responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${escape(data.error)}</span>`;
                    return;
                }

                const parts = [];
                parts.push(`<u>Query:</u> ${escape(data.query)}`);
                if (data.filters && (data.filters.doc_type || data.filters.jurisdiction)) {
                    parts.push(`<u>Filters:</u> doc_type=${escape(data.filters.doc_type || '—')} jurisdiction=${escape(data.filters.jurisdiction || '—')}`);
                }
                const results = Array.isArray(data.results) ? data.results : [];
                if (!results.length) {
                    parts.push('<u>Results:</u> <em>No chunks returned (vector index empty or not configured).</em>');
                } else {
                    const list = results.map((r, idx) => {
                        const dist = (typeof r.distance === 'number') ? r.distance.toFixed(4) : escape(r.distance);
                        const txt = escape((r.text || '').slice(0, 500));
                        return `<li style="margin-bottom:14px; line-height:1.35;">
                            <div><strong>#${idx+1}</strong> chunk_id=${escape(r.chunk_id)} distance=${dist}</div>
                            <div style="color:#ccc; font-size:0.7rem; margin-top:2px; font-style:italic;">${escape(r.title || '')} ${r.doc_type ? '('+escape(r.doc_type)+')' : ''} ${r.jurisdiction ? '['+escape(r.jurisdiction)+']' : ''}</div>
                            <div style="color:#ccc; font-size:0.85rem; margin-top:4px;">${txt}${r.text && r.text.length > 500 ? '…' : ''}</div>
                        </li>`;
                    }).join('');
                    parts.push(`<details open><summary style="cursor:pointer; margin-top:10px;"><u>Results (${results.length})</u></summary><ol style="margin-top:8px; padding-left:20px;">${list}</ol></details>`);
                }
                responseDiv.innerHTML = parts.join('\n\n');
            } catch (err) {
                loadingDiv.classList.remove('active');
                responseDiv.innerHTML = `<span style="color:#e53e3e;">Error: ${err.message}</span>`;
            }
        });

        // Reset handler
        const resetBtn = document.getElementById('resetBtn');
        if (resetBtn) {
            resetBtn.addEventListener('click', async () => {
                if (!confirm('This will DELETE all data under /data and clear embeddings. Continue?')) return;
                resetBtn.disabled = true;
                resetBtn.textContent = 'Resetting...';
                try {
                    const resp = await fetch('/api/reset', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ confirm: true })
                    });
                    const data = await resp.json().catch(() => ({}));
                    if (!resp.ok) {
                        alert('Reset failed: ' + (data.error || resp.statusText));
                    } else {
                        alert('Reset complete. Files deleted: ' + (data.files_deleted ?? 'n/a'));
                        location.reload();
                    }
                } catch (e) {
                    alert('Reset error: ' + e.message);
                } finally {
                    resetBtn.disabled = false;
                    resetBtn.textContent = 'Reset / Wipe Corpus';
                }
            });
        }
    </script>
</body>
</html>