File size: 30,294 Bytes
cc0151e
2714b3b
cc0151e
 
 
a292e96
cc0151e
 
d8e65c6
a292e96
2714b3b
cc0151e
 
e8a03a6
2714b3b
cc0151e
 
 
d8e65c6
 
 
cc0151e
 
d8e65c6
 
cc0151e
e8a03a6
cc0151e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f032c
e8a03a6
 
 
 
 
cc0151e
66f032c
cc0151e
 
66f032c
cc0151e
e8a03a6
cc0151e
 
 
d8e65c6
1b2be00
2714b3b
 
 
 
 
 
 
cc0151e
 
fb45bed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc0151e
e8a03a6
cc0151e
e8a03a6
cc0151e
2714b3b
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea9da1
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
 
 
 
 
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
 
 
 
 
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
e672643
 
 
 
2714b3b
 
 
 
 
 
e672643
 
 
 
 
 
 
 
2714b3b
 
e672643
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
 
 
 
 
 
 
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e672643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2714b3b
 
 
 
 
 
 
 
 
 
1ea9da1
2714b3b
 
 
 
 
 
 
 
 
 
 
e672643
2714b3b
 
 
 
e672643
2714b3b
 
 
 
e672643
 
2714b3b
e672643
 
2714b3b
 
 
 
 
 
 
 
 
 
 
 
e672643
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e672643
2714b3b
 
 
 
 
 
 
 
 
e672643
2714b3b
 
 
 
 
 
 
e672643
2714b3b
 
 
 
 
 
 
 
 
 
 
 
e672643
2714b3b
 
e672643
2714b3b
 
 
 
 
 
 
 
8ded10f
2714b3b
 
 
 
 
 
 
 
8ded10f
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e672643
2714b3b
 
 
 
 
 
 
 
 
 
 
 
8ded10f
2714b3b
 
 
 
8ded10f
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ded10f
2714b3b
e672643
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ded10f
2714b3b
8ded10f
2714b3b
 
 
 
e672643
 
2714b3b
 
 
 
e672643
2714b3b
 
 
 
 
 
8ded10f
2714b3b
 
 
 
8ded10f
2714b3b
 
 
8ded10f
e672643
2714b3b
e672643
 
 
 
a292e96
cc0151e
 
e8a03a6
2714b3b
e8a03a6
 
 
 
 
 
 
 
 
 
cc0151e
e8a03a6
2714b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc0151e
 
2714b3b
 
cc0151e
2714b3b
 
 
a292e96
cc0151e
 
2714b3b
 
 
a292e96
cc0151e
2714b3b
e8a03a6
 
 
 
2714b3b
 
 
a292e96
cc0151e
 
 
2714b3b
cc0151e
 
e8a03a6
cc0151e
 
 
e8a03a6
cc0151e
e8a03a6
 
cc0151e
 
2714b3b
cc0151e
e8a03a6
cc0151e
 
 
 
 
 
 
 
2714b3b
 
 
a292e96
cc0151e
 
2714b3b
d8e65c6
cc0151e
 
2714b3b
 
 
8ded10f
2714b3b
 
cc0151e
 
2714b3b
 
cc0151e
 
1ea9da1
cc0151e
 
 
e8a03a6
b98029f
 
 
 
 
 
 
 
 
 
 
 
 
0bd6316
b98029f
 
0bd6316
b98029f
 
 
 
 
 
 
 
 
 
97c15e2
b98029f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97c15e2
b98029f
 
97c15e2
b98029f
 
 
 
 
 
 
 
 
 
 
 
c76387b
b98029f
 
 
cc0151e
 
e8a03a6
cc0151e
fddf765
cc0151e
2714b3b
 
 
1b2be00
2714b3b
 
 
 
cc0151e
 
 
fddf765
cc0151e
2714b3b
 
 
cc0151e
 
 
 
e8a03a6
cc0151e
e8a03a6
 
6e1b09a
cc0151e
 
 
6bb103d
 
 
 
 
 
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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
"""
FastAPI-sync + Gradio with AJAX (all route handlers are now synchronous with JSON responses)
"""

import logging
import os
import tempfile
import traceback
import uuid
from pathlib import Path
from typing import List, Dict, Any

import gradio as gr
from fastapi import FastAPI, File, HTTPException, Request, Response, UploadFile, status
from fastapi.responses import HTMLResponse, RedirectResponse, FileResponse, JSONResponse
from starlette.middleware.sessions import SessionMiddleware
from starlette.responses import PlainTextResponse
from werkzeug.utils import secure_filename  # type: ignore

try:
    from rag_document_viewer import RAG_DV
except ImportError as err:
    print(f"Warning: Could not import RAG_DV: {err}")
    RAG_DV = None

# --------------------------------------------------------------------------- #
#  Config / constants                                                         #
# --------------------------------------------------------------------------- #
SECRET_KEY = (
    "a&VhLelkAo!dKXm9o5RRHQ@#BxoI3Q5378qwFP&aJKA#PLjO7TU*Aq5Kwg4OTdMI7N3%wFAmnwezlEbPUdEQKhGJD10E8@0gSrS"
)
MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16 MB
ALLOWED_EXTENSIONS = {
    "txt",
    "pdf",
    "doc",
    "docx",
    "ppt",
    "pptx",
    "xls",
    "xlsx",
    "odt",
    "odp",
    "ods",
}

UPLOADS_DIR = (
    Path(tempfile.gettempdir()) / "uploads"
    if os.getenv("SPACE_ID")
    else Path("uploads")
)
UPLOADS_DIR.mkdir(exist_ok=True, parents=True)

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("rag_dv_demo")

# --------------------------------------------------------------------------- #
#  Helpers                                                                    #
# --------------------------------------------------------------------------- #
def _allowed_file(name: str) -> bool:
    return "." in name and name.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS


def _json_response(success: bool = True, message: str = "", data: Dict[Any, Any] = None) -> JSONResponse:
    """Helper to create consistent JSON responses"""
    return JSONResponse({
        "success": success,
        "message": message,
        "data": data or {}
    })


def _rewrite_html_urls(content: str, base_url: str, story_id: str) -> str:
    """Rewrite relative URLs in HTML content to use absolute URLs"""
    import re
    
    logger.info(f"Rewriting URLs with base: {base_url}, story_id: {story_id}")
    
    # Patterns to match various asset references
    patterns = [
        # src="assets/..." -> src="[base_url]/..."
        (r'src="assets/', f'src="{base_url}/'),
        
        # href="assets/..." -> href="[base_url]/..."  
        (r'href="assets/', f'href="{base_url}/'),
        
        # CSS url(assets/...) -> url([base_url]/...)
        (r'url\(assets/', f'url({base_url}/'),
        (r'url\("assets/', f'url("{base_url}/'),
        (r"url\('assets/", f"url('{base_url}/"),
        
        # Background images and other asset references
        (r'background-image:\s*url\(assets/', f'background-image: url({base_url}/'),
        (r'background:\s*url\(assets/', f'background: url({base_url}/'),
        
        # Script src
        (r'<script[^>]*src="assets/', f'<script src="{base_url}/'),
        
        # Link href (stylesheets, etc.)
        (r'<link[^>]*href="assets/', f'<link href="{base_url}/'),
    ]
    
    # Apply all patterns
    for pattern, replacement in patterns:
        old_content = content
        content = re.sub(pattern, replacement, content, flags=re.IGNORECASE)
        if content != old_content:
            logger.info(f"Applied pattern: {pattern}")
    
    # Add base tag if not present
    if '<base' not in content.lower():
        base_tag = f'<base href="{base_url}/">'
        if '<head>' in content.lower():
            content = re.sub(r'<head>', f'<head>\n    {base_tag}', content, flags=re.IGNORECASE)
        else:
            # If no head tag, add it
            content = re.sub(r'<html[^>]*>', f'<html>\n<head>\n    {base_tag}\n</head>', content, flags=re.IGNORECASE)
    
    return content


# --------------------------------------------------------------------------- #
#  Template                                                                   #
# --------------------------------------------------------------------------- #
from jinja2 import Template  # noqa

HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Helvetica Neue', Arial, sans-serif;
        }

        body {
            background: -webkit-linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
            background: linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
            background-attachment: fixed;
            min-height: 100vh;
            position: relative;
            color: #f3f3f3;
            line-height: 1.6;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        /* Upload Area Styles */
        .upload-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }

        .upload-area {
            width: 100%;
            max-width: 500px;
            height: 300px;
            border: 2px dashed #4a5568;
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.05);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.2s ease;
            position: relative;
            overflow: hidden;
            padding: 0 2rem;
            margin: 0 1rem;
        }

        .upload-area:hover {
            border-color: #6b7280;
            background: rgba(255, 255, 255, 0.08);
        }

        .upload-area.dragover {
            border-color: #9ca3af;
            background: rgba(255, 255, 255, 0.1);
            transform: translateY(-2px);
            box-shadow: 0 4px 16px rgba(255, 255, 255, 0.1);
        }

        .upload-area.uploading {
            pointer-events: none;
            opacity: 0.7;
        }

        .upload-icon {
            font-size: 48px;
            color: #9ca3af;
            margin-bottom: 20px;
        }

        .upload-text {
            font-size: 18px;
            color: #f3f3f3;
            margin-bottom: 10px;
            font-weight: 500;
        }

        .upload-subtext {
            font-size: 14px;
            color: #9ca3af;
            text-align: center;
        }

        .file-input {
            display: none;
        }

        /* Content Area Styles */
        .content-container {
            min-height: 90vh;
            padding: 20px;
            display: none;
        }

        .content-container.active {
            display: block;
        }

        .content-area {
            width: 100%;
            height: calc(100vh - 80px);
            background: rgba(255, 255, 255, 0.05);
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
            overflow: hidden;
            border: 1px solid #374151;
        }

        .content-iframe {
            width: 100%;
            height: 100%;
            border: none;
            background: rgba(255, 255, 255, 0.05);
            display: block;
            box-sizing: border-box;
        }

        /* Refresh Button Styles */
        .refresh-btn {
            font-family: helvetica neue;
            font-weight: bold;
            position: fixed;
            top: 0.5rem;
            left: 0.5rem;
            background: #212529;
            color: #fff;
            border: 0px;
            border-radius: 6px;
            padding: 12px 20px;
            font-size: 14px;
            cursor: pointer;
            backdrop-filter: blur(10px);
            transition: all 0.2s ease;
            opacity: 0;
            z-index: 1000;
            white-space: nowrap;
            width: auto;
            max-width: fit-content;
            pointer-events: none;
        }

        .refresh-btn.visible {
            opacity: 1;
            pointer-events: all;
        }

        .refresh-btn:hover {
            background: #424649;
            border-color: #6b7280;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }

        /* Message Styles */
        .message {
            position: fixed;
            top: 25px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 1001;
            background: rgba(0, 57, 150, 0.4);
            color: #fff;
            padding: 12px 20px;
            border-radius: 6px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
            font-weight: 500;
            backdrop-filter: blur(10px);
            text-align: center;
            opacity: 0;
            transition: opacity 0.3s ease;
            pointer-events: none;
        }

        .message.visible {
            opacity: 1;
        }

        /* Loading Animation */
        .loading {
            display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .loading.active {
            display: block;
        }

        .spinner {
            width: 40px;
            height: 40px;
            border: 3px solid #4a5568;
            border-top: 3px solid #f3f3f3;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* Progress Bar */
        .progress-container {
            position: absolute;
            bottom: 20px;
            left: 20px;
            right: 20px;
            height: 4px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 2px;
            overflow: hidden;
            display: none;
        }

        .progress-container.active {
            display: block;
        }

        .progress-bar {
            height: 100%;
            background: linear-gradient(90deg, #4f46e5, #7c3aed);
            width: 0%;
            transition: width 0.3s ease;
            border-radius: 2px;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .upload-area {
                height: 250px;
                margin: 0 1rem;
                padding: 0 1rem;
                max-width: calc(100vw - 2rem);
            }
            
            .upload-text {
                font-size: 16px;
                text-align: center;
            }
            
            .upload-subtext {
                font-size: 13px;
                text-align: center;
            }
            
            .upload-container {
                padding: 0 1rem;
            }
            
            .refresh-btn {
                top: 10px;
                left: 10px;
                padding: 8px 12px;
                font-size: 11px;
                white-space: nowrap;
                width: auto;
                max-width: fit-content;
            }
        }

        @media (max-width: 480px) {
            .upload-area {
                height: 220px;
                margin: 0 0.5rem;
                padding: 0 0.5rem;
                max-width: calc(100vw - 1rem);
            }
            
            .upload-text {
                font-size: 14px;
            }
            
            .upload-subtext {
                font-size: 12px;
            }
            
            .upload-icon {
                font-size: 36px;
                margin-bottom: 15px;
            }
            
            .upload-container {
                padding: 0 0.5rem;
            }
        }
    </style>
</head>
<body>
    <!-- Message Display -->
    <div class="message" id="message"></div>

    <!-- Refresh Button -->
    <button class="refresh-btn" id="refreshBtn" onclick="refreshSession()">
        Start over
    </button>

    <!-- Upload Interface -->
    <div class="upload-container" id="uploadContainer">
        <h2 style="text-align: center; position: absolute; top: 1rem;">Rag Document Viewer Demo</h2>
        <div class="upload-area" id="uploadArea">
            <div class="upload-icon">📂</div>
            <div class="upload-text">Drop your file here or click to browse</div>
            <div class="upload-subtext">Supports: PDFs, Office Documents, OpenOffice Documents <br>(Max: 16MB)</div>
            <input type="file" class="file-input" id="fileInput" accept=".txt,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.odt,.odp,.ods">
            <div class="loading" id="loading">
                <div class="spinner"></div>
            </div>
            <div class="progress-container" id="progressContainer">
                <div class="progress-bar" id="progressBar"></div>
            </div>
        </div>
    </div>

    <!-- Content Interface -->
    <div class="content-container" id="contentContainer">
        <h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2>
        <div class="content-area">
            <div class="content-iframe">
                <iframe id="contentFrame" style="width: 100%; height: 100%; border: none; display: block;">
                </iframe>
            </div>
        </div>
    </div>

    <script>
        class FileUploader {
            constructor() {
                this.uploadArea = document.getElementById('uploadArea');
                this.fileInput = document.getElementById('fileInput');
                this.loading = document.getElementById('loading');
                this.progressContainer = document.getElementById('progressContainer');
                this.progressBar = document.getElementById('progressBar');
                this.uploadContainer = document.getElementById('uploadContainer');
                this.contentContainer = document.getElementById('contentContainer');
                this.contentFrame = document.getElementById('contentFrame');
                this.refreshBtn = document.getElementById('refreshBtn');
                this.messageEl = document.getElementById('message');
                
                this.maxFileSize = 16 * 1024 * 1024; // 16MB
                this.isUploading = false;
                
                this.initEventListeners();
                this.checkSession();
            }
            
            initEventListeners() {
                // Click to browse
                this.uploadArea.addEventListener('click', () => {
                    if (!this.isUploading) {
                        this.fileInput.click();
                    }
                });

                // Drag and drop
                this.uploadArea.addEventListener('dragover', (e) => {
                    e.preventDefault();
                    if (!this.isUploading) {
                        this.uploadArea.classList.add('dragover');
                    }
                });

                this.uploadArea.addEventListener('dragleave', () => {
                    this.uploadArea.classList.remove('dragover');
                });

                this.uploadArea.addEventListener('drop', (e) => {
                    e.preventDefault();
                    this.uploadArea.classList.remove('dragover');
                    
                    if (this.isUploading) return;
                    
                    const files = e.dataTransfer.files;
                    if (files.length > 0) {
                        this.handleFileSelection(files[0]);
                    }
                });

                // File input change
                this.fileInput.addEventListener('change', () => {
                    if (this.fileInput.files.length > 0 && !this.isUploading) {
                        this.handleFileSelection(this.fileInput.files[0]);
                    }
                });
            }
            
            async checkSession() {
                try {
                    const response = await fetch('/api/session');
                    const result = await response.json();
                    
                    if (result.success && result.data.has_uploaded_file) {
                        this.showContentInterface(result.data.filename);
                    }
                } catch (error) {
                    console.error('Session check failed:', error);
                }
            }
            
            validateFile(file) {
                // Check file size
                if (file.size > this.maxFileSize) {
                    this.showMessage('File size too large. Please upload a file smaller than 16MB.');
                    return false;
                }
                
                // Check file type
                const allowedExtensions = ['txt', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'odt', 'odp', 'ods'];
                const fileExtension = file.name.split('.').pop().toLowerCase();
                
                if (!allowedExtensions.includes(fileExtension)) {
                    this.showMessage('Invalid file type. Please upload a supported document.');
                    return false;
                }
                
                return true;
            }
            
            async handleFileSelection(file) {
                if (!this.validateFile(file)) {
                    this.fileInput.value = '';
                    return;
                }
                
                await this.uploadFile(file);
            }
            
            async uploadFile(file) {
                this.isUploading = true;
                this.uploadArea.classList.add('uploading');
                this.loading.classList.add('active');
                this.progressContainer.classList.add('active');
                
                const formData = new FormData();
                formData.append('file', file);
                
                try {
                    const xhr = new XMLHttpRequest();
                    
                    return new Promise((resolve, reject) => {
                        xhr.upload.addEventListener('progress', (e) => {
                            if (e.lengthComputable) {
                                const percentComplete = (e.loaded / e.total) * 100;
                                this.progressBar.style.width = percentComplete + '%';
                            }
                        });
                        
                        xhr.addEventListener('load', () => {
                            if (xhr.status === 200) {
                                const result = JSON.parse(xhr.responseText);
                                if (result.success) {
                                    this.showMessage(result.message);
                                    setTimeout(() => {
                                        this.showContentInterface(result.data.filename);
                                    }, 1000);
                                } else {
                                    this.showMessage(result.message);
                                    this.resetUploadState();
                                }
                                resolve(result);
                            } else {
                                reject(new Error('Upload failed'));
                            }
                        });
                        
                        xhr.addEventListener('error', () => {
                            reject(new Error('Upload failed'));
                        });
                        
                        xhr.open('POST', '/api/upload');
                        xhr.send(formData);
                    });
                } catch (error) {
                    console.error('Upload error:', error);
                    this.showMessage('Upload failed. Please try again.');
                    this.resetUploadState();
                }
            }
            
            resetUploadState() {
                this.isUploading = false;
                this.uploadArea.classList.remove('uploading');
                this.loading.classList.remove('active');
                this.progressContainer.classList.remove('active');
                this.progressBar.style.width = '0%';
                this.fileInput.value = '';
            }
            
            showContentInterface(filename) {
                this.uploadContainer.style.display = 'none';
                this.contentContainer.classList.add('active');
                this.refreshBtn.classList.add('visible');
                this.contentFrame.src = `/load/${filename}`;
            }
            
            showUploadInterface() {
                this.uploadContainer.style.display = 'flex';
                this.contentContainer.classList.remove('active');
                this.refreshBtn.classList.remove('visible');
                this.resetUploadState();
            }
            
            showMessage(message) {
                this.messageEl.textContent = message;
                this.messageEl.className = `message visible`;
                
                setTimeout(() => {
                    this.messageEl.classList.remove('visible');
                }, 5000);
            }
        }
        
        // Initialize uploader
        const uploader = new FileUploader();
        
        // Refresh session function
        async function refreshSession() {
            try {
                const response = await fetch('/api/refresh', { method: 'POST' });
                const result = await response.json();
                
                if (result.success) {
                    uploader.showMessage(result.message);
                    setTimeout(() => {
                        uploader.showUploadInterface();
                    }, 1000);
                } else {
                    uploader.showMessage(result.message);
                }
            } catch (error) {
                console.error('Refresh failed:', error);
                uploader.showMessage('Refresh failed. Please try again.');
            }
        }
    </script>
</body>
</html>
"""

template = Template(HTML_TEMPLATE)

# --------------------------------------------------------------------------- #
#  FastAPI (synchronous with AJAX endpoints)                                  #
# --------------------------------------------------------------------------- #
backend = FastAPI()
backend.add_middleware(
    SessionMiddleware,
    secret_key=SECRET_KEY,
    same_site="none",  # HF iframe needs SameSite=None
    https_only=True,
)


@backend.get("/", name="index")
def index(request: Request):
    return HTMLResponse(template.render())


@backend.get("/api/session")
def get_session_info(request: Request):
    """Get current session information"""
    has_uploaded_file = "uploaded_file" in request.session
    filename = request.session.get("uploaded_file", "")
    original_filename = request.session.get("original_filename", "")
    
    return _json_response(
        success=True,
        data={
            "has_uploaded_file": has_uploaded_file,
            "filename": filename,
            "original_filename": original_filename
        }
    )


@backend.post("/api/upload")
def upload_file_ajax(request: Request, file: UploadFile = File(...)):
    if RAG_DV is None:
        return _json_response(
            success=False, 
            message="RAG Document Viewer is not available. Please check installation."
        )

    if not file.filename or not _allowed_file(file.filename):
        return _json_response(
            success=False, 
            message="Invalid file type. Please upload a supported document."
        )

    # Check file size
    file.file.seek(0, 2)  # go to end
    size = file.file.tell()
    file.file.seek(0)
    if size > MAX_CONTENT_LENGTH:
        return _json_response(
            success=False, 
            message="File is too large. Maximum size is 16MB."
        )

    filename = secure_filename(file.filename)
    if not filename:
        return _json_response(success=False, message="Invalid filename")

    unique_filename = f"{uuid.uuid4()}_{filename}"
    dir_path = UPLOADS_DIR / Path(unique_filename).stem
    dir_path.mkdir(parents=True, exist_ok=True)
    filepath = dir_path / unique_filename

    # Save upload synchronously
    try:
        with open(filepath, "wb") as f_out:
            f_out.write(file.file.read())
    except Exception as err:
        logger.error(f"Failed saving upload: {err}")
        return _json_response(success=False, message="Failed to save uploaded file")

    # RAG_DV processing
    out_path = filepath.parent / "rag_dv"
    try:
        RAG_DV(filepath, out_path)
    except Exception as err:
        logger.error(f"RAG_DV failed: {err}\n{traceback.format_exc()}")
        filepath.unlink(missing_ok=True)
        if not any(dir_path.iterdir()):
            dir_path.rmdir()
        return _json_response(
            success=False, 
            message="Failed to process document. Please try again."
        )

    if not (out_path / "index.html").exists():
        return _json_response(success=False, message="Document processing incomplete.")

    request.session["uploaded_file"] = unique_filename
    request.session["original_filename"] = filename
    
    return _json_response(
        success=True, 
        message="File successfully uploaded.",
        data={"filename": unique_filename, "original_filename": filename}
    )


@backend.post("/api/refresh")
def refresh_session_ajax(request: Request):
    request.session.pop("uploaded_file", None)
    request.session.pop("original_filename", None)
    return _json_response(success=True, message="Started over successfully.")


@backend.get("/load/{filename:path}")
def load(request: Request, filename: str):
    if filename.startswith("assets"):
        asset_path = filename.replace("assets/", "")
        if "uploaded_file" not in request.session:
            logger.error("No active session for asset request")
            raise HTTPException(status_code=404, detail="No active session")

        # Log the request for debugging
        logger.info(f"Asset request: {asset_path}")
        logger.info(f"Uploaded file: {request.session.get('uploaded_file')}")

        uploaded_file = request.session["uploaded_file"]
        story_id = Path(uploaded_file).stem  # This is our story_id
        base_dir = UPLOADS_DIR / story_id / "rag_dv" / "assets"
        
        # Construct the full file path
        file_path = base_dir / asset_path
        
        logger.info(f"Story ID: {story_id}")
        logger.info(f"Looking for asset at: {file_path}")
        logger.info(f"Base directory exists: {base_dir.exists()}")
        
        if base_dir.exists():
            logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}")
        
        if not file_path.exists() or not file_path.is_file():
            logger.error(f"Asset not found: {file_path}")
            raise HTTPException(status_code=404, detail=f"Asset not found: {asset_path}")

        # Set appropriate content type based on file extension
        from mimetypes import guess_type
        content_type, _ = guess_type(str(file_path))
        
        return FileResponse(
            file_path,
            media_type=content_type,
            headers={
                "Cache-Control": "public, max-age=3600",  # Cache for 1 hour
                "X-Story-ID": story_id  # Custom header for debugging
            }
        )
    else:
        filename = secure_filename(filename)
        filepath = UPLOADS_DIR / Path(filename).stem / filename
        idx_file = filepath.parent / "rag_dv" / "index.html"

        if not idx_file.exists():
            raise HTTPException(status_code=404, detail="Document index not found.")

        logger.info(f"Loading document: {filename}")
        
        # Get the story_id (filename without extension and UUID prefix)
        story_id = Path(filename).stem
        
        # Construct base URL for assets
        base_url = str(request.base_url).rstrip('/') + f"/load"
        
        with open(idx_file, "r", encoding="utf-8") as f:
            content = f.read()
            
            # Rewrite URLs to use absolute paths
            # content = _rewrite_html_urls(content, base_url, story_id)
            
            logger.info(f"Document loaded with base URL: {base_url}")
            return HTMLResponse(content)


# ----------------------------- Error handling ------------------------------ #
@backend.exception_handler(HTTPException)
def http_exception_handler(request: Request, exc: HTTPException):
    if exc.status_code == status.HTTP_413_REQUEST_ENTITY_TOO_LARGE:
        return _json_response(
            success=False, 
            message="File is too large. Maximum size is 16MB."
        )
    return JSONResponse(
        {"success": False, "message": str(exc.detail)}, 
        status_code=exc.status_code
    )


@backend.exception_handler(Exception)
def unhandled_exception(request: Request, exc: Exception):
    logger.error(f"Unhandled error: {exc}\n{traceback.format_exc()}")
    return _json_response(
        success=False, 
        message="An unexpected error occurred. Please try again."
    )


# --------------------------------------------------------------------------- #
#  Gradio mounting (Blocks path = /gradio)                                    #
# --------------------------------------------------------------------------- #
blocks = gr.Blocks()  # no visual elements needed
app = gr.mount_gradio_app(backend, blocks, path="/gradio")

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "server:app",
        host="0.0.0.0",
        port=7860,
        reload=True,
    )