File size: 58,698 Bytes
3dea807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
import gradio as gr
import tempfile
import os
import requests
import json
import re
from bs4 import BeautifulSoup
from datetime import datetime
import urllib.parse


# Configuration
SPACE_NAME = 'STEM Adventure Games'
SPACE_DESCRIPTION = 'Choose your own STEM adventure games'

# Default configuration values (used only if config.json is missing)
DEFAULT_CONFIG = {
    'name': SPACE_NAME,
    'description': SPACE_DESCRIPTION,
    'system_prompt': "Transform into an interactive co-learning guide who creates Choose Your Own STEM Adventure games featuring historically significant scientific experiments, opening each session with an eye-catching unicode-styled arcade menu presenting 5-6 adventures drawn from Wikipedia's List of Experiments with brief enticing descriptions, then immersing players in vivid historical moments written in second person (e.g. 'You are Galileo Galilei') that establish the year, location, prevailing beliefs, and tensions between established wisdom and emerging observations, presenting 3-4 numbered decision points per stage that reflect authentic scientific choices ranging from experimental design and measurement approaches to strategic decisions about convincing skeptics or timing publications, with each choice meaningfully different and leading to distinct paths forward, then narrating results with sensory details, colleague reactions, and unexpected observations that teach concepts naturally through unfolding drama rather than lectures, always ending scenes with new branching choices that maintain narrative momentum while reinforcing science as an iterative process of hypothesis, testing, and refinement, offering backtracking options to emphasize how so-called failed experiments provide crucial insights, balancing historical accuracy with engaging gameplay that shows how systematic thinking and creative problem-solving combine in scientific breakthroughs.",
    'temperature': 0.6,
    'max_tokens': 1000,
    'model': 'qwen/qwen3-30b-a3b-instruct-2507',
    'api_key_var': 'API_KEY',
    'theme': 'Glass',
    'grounding_urls': ["https://en.wikipedia.org/wiki/List_of_experiments", "https://en.wikipedia.org/wiki/Scientific_method", "https://en.wikipedia.org/wiki/List_of_experiments#Biology", "https://en.wikipedia.org/wiki/List_of_experiments#Astronomy", "https://en.wikipedia.org/wiki/List_of_experiments#Chemistry", "https://en.wikipedia.org/wiki/List_of_experiments#Physics", "https://en.wikipedia.org/wiki/List_of_experiments#Geology"],
    'enable_dynamic_urls': True,
    'enable_file_upload': True,
    'examples': ['Initiate adventure!', 'How do I play?', "What's the meaning of this?"],
    'locked': False
}

# Load configuration from file - this is the single source of truth
def load_config():
    """Load configuration from config.json with fallback to defaults"""
    try:
        with open('config.json', 'r') as f:
            config = json.load(f)
            print("βœ… Loaded configuration from config.json")
            return config
    except FileNotFoundError:
        print("ℹ️ No config.json found, using default configuration")
        # Save default config for future use
        try:
            with open('config.json', 'w') as f:
                json.dump(DEFAULT_CONFIG, f, indent=2)
            print("βœ… Created config.json with default values")
        except:
            pass
        return DEFAULT_CONFIG
    except Exception as e:
        print(f"⚠️ Error loading config.json: {e}, using defaults")
        return DEFAULT_CONFIG

# Load configuration
config = load_config()

# Initial load of configuration values
SPACE_NAME = config.get('name', DEFAULT_CONFIG['name'])
SPACE_DESCRIPTION = config.get('description', DEFAULT_CONFIG['description'])
SYSTEM_PROMPT = config.get('system_prompt', DEFAULT_CONFIG['system_prompt'])
temperature = config.get('temperature', DEFAULT_CONFIG['temperature'])
max_tokens = config.get('max_tokens', DEFAULT_CONFIG['max_tokens'])
MODEL = config.get('model', DEFAULT_CONFIG['model'])
THEME = config.get('theme', DEFAULT_CONFIG['theme'])
GROUNDING_URLS = config.get('grounding_urls', DEFAULT_CONFIG['grounding_urls'])
ENABLE_DYNAMIC_URLS = config.get('enable_dynamic_urls', DEFAULT_CONFIG['enable_dynamic_urls'])
ENABLE_FILE_UPLOAD = config.get('enable_file_upload', DEFAULT_CONFIG.get('enable_file_upload', True))

# Get access code from environment variable for security
# If ACCESS_CODE is not set, no access control is applied
ACCESS_CODE = os.environ.get("ACCESS_CODE")

# Get API key from environment - customizable variable name with validation
API_KEY_VAR = config.get('api_key_var', DEFAULT_CONFIG['api_key_var'])
API_KEY = os.environ.get(API_KEY_VAR)
if API_KEY:
    API_KEY = API_KEY.strip()  # Remove any whitespace
    if not API_KEY:  # Check if empty after stripping
        API_KEY = None
        
# API Key validation and logging
def validate_api_key():
    """Validate API key configuration with detailed logging"""
    if not API_KEY:
        print(f"⚠️  API KEY CONFIGURATION ERROR:")
        print(f"   Variable name: {API_KEY_VAR}")
        print(f"   Status: Not set or empty")
        print(f"   Action needed: Set '{API_KEY_VAR}' in HuggingFace Space secrets")
        print(f"   Expected format: sk-or-xxxxxxxxxx")
        return False
    elif not API_KEY.startswith('sk-or-'):
        print(f"⚠️  API KEY FORMAT WARNING:")
        print(f"   Variable name: {API_KEY_VAR}")
        print(f"   Current value: {API_KEY[:10]}..." if len(API_KEY) > 10 else "{API_KEY}")
        print(f"   Expected format: sk-or-xxxxxxxxxx")
        print(f"   Note: OpenRouter keys should start with 'sk-or-'")
        return True  # Still try to use it
    else:
        print(f"βœ… API Key configured successfully")
        print(f"   Variable: {API_KEY_VAR}")
        print(f"   Format: Valid OpenRouter key")
        return True

# Validate on startup
try:
    API_KEY_VALID = validate_api_key()
except NameError:
    # During template generation, API_KEY might not be defined yet
    API_KEY_VALID = False

def validate_url_domain(url):
    """Basic URL domain validation"""
    try:
        from urllib.parse import urlparse
        parsed = urlparse(url)
        # Check for valid domain structure
        if parsed.netloc and '.' in parsed.netloc:
            return True
    except:
        pass
    return False

def fetch_url_content(url):
    """Enhanced URL content fetching with improved compatibility and error handling"""
    if not validate_url_domain(url):
        return f"Invalid URL format: {url}"
    
    try:
        # Enhanced headers for better compatibility
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Accept-Encoding': 'gzip, deflate',
            'Connection': 'keep-alive'
        }
        
        response = requests.get(url, timeout=15, headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        
        # Enhanced content cleaning
        for element in soup(["script", "style", "nav", "header", "footer", "aside", "form", "button"]):
            element.decompose()
        
        # Extract main content preferentially
        main_content = soup.find('main') or soup.find('article') or soup.find('div', class_=lambda x: bool(x and 'content' in x.lower())) or soup
        text = main_content.get_text()
        
        # Enhanced text cleaning
        lines = (line.strip() for line in text.splitlines())
        chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
        text = ' '.join(chunk for chunk in chunks if chunk and len(chunk) > 2)
        
        # Smart truncation - try to end at sentence boundaries
        if len(text) > 4000:
            truncated_text = text[:4000]
            # Try to find the last complete sentence
            last_period = truncated_text.rfind('.')
            if last_period > 3500:  # Only if we have a reasonably long truncation
                text = truncated_text[:last_period + 1]
            else:
                text = truncated_text + "..."
        
        return text if text.strip() else "No readable content found at this URL"
        
    except requests.exceptions.Timeout:
        return f"Timeout error fetching {url} (15s limit exceeded)"
    except requests.exceptions.RequestException as e:
        return f"Error fetching {url}: {str(e)}"
    except Exception as e:
        return f"Error processing content from {url}: {str(e)}"

def extract_urls_from_text(text):
    """Extract URLs from text using regex with enhanced validation"""
    url_pattern = r'https?://[^\s<>"{}|\^`\[\]"]+'
    urls = re.findall(url_pattern, text)
    
    # Basic URL validation and cleanup
    validated_urls = []
    for url in urls:
        # Remove trailing punctuation that might be captured
        url = url.rstrip('.,!?;:')
        # Basic domain validation
        if '.' in url and len(url) > 10:
            validated_urls.append(url)
    
    return validated_urls

# Global cache for URL content to avoid re-crawling in generated spaces
_url_content_cache = {}

def extract_file_content(file_path, max_chars=8000):
    """Extract content from uploaded files with intelligent chunking for optimal context usage"""
    try:
        # Get file metadata
        file_size = os.path.getsize(file_path)
        file_name = os.path.basename(file_path)
        _, ext = os.path.splitext(file_path.lower())
        
        # Comprehensive list of text file extensions
        text_extensions = [
            '.txt', '.md', '.markdown', '.rst',  # Documentation
            '.py', '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs',  # JavaScript/Python
            '.java', '.c', '.cpp', '.cc', '.h', '.hpp', '.cs',  # C-family
            '.go', '.rs', '.swift', '.kt', '.scala', '.r', '.m',  # Other languages
            '.html', '.htm', '.xml', '.xhtml', '.css', '.scss', '.sass', '.less',  # Web
            '.json', '.yaml', '.yml', '.toml', '.ini', '.conf', '.config',  # Config
            '.csv', '.tsv', '.log', '.sql',  # Data/Logs
            '.sh', '.bash', '.zsh', '.fish', '.ps1', '.bat', '.cmd',  # Scripts
            '.tex', '.latex', '.bib',  # LaTeX
            '.vue', '.svelte', '.astro',  # Modern frameworks
            '.env', '.gitignore', '.dockerignore', '.editorconfig'  # Dotfiles
        ]
        
        # Handle text files with intelligent extraction
        if ext in text_extensions:
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                content = f.read()
                total_chars = len(content)
                
                # If content fits within limit, return all
                if total_chars <= max_chars:
                    return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes)\n```{ext[1:] if ext else ''}\n{content}\n```"
                
                # For larger files, extract intelligently
                # Calculate line information
                lines = content.split('\n')
                total_lines = len(lines)
                
                # Smart extraction: beginning + end with overlap consideration
                if total_lines > 100:  # For files with many lines
                    # Take more from beginning (usually imports/setup) and substantial end
                    begin_chars = int(max_chars * 0.6)
                    end_chars = int(max_chars * 0.4)
                    
                    # Extract beginning
                    begin_content = []
                    char_count = 0
                    for line in lines:
                        if char_count + len(line) + 1 > begin_chars:
                            break
                        begin_content.append(line)
                        char_count += len(line) + 1
                    
                    # Extract end
                    end_content = []
                    char_count = 0
                    for line in reversed(lines):
                        if char_count + len(line) + 1 > end_chars:
                            break
                        end_content.insert(0, line)
                        char_count += len(line) + 1
                    
                    omitted_lines = total_lines - len(begin_content) - len(end_content)
                    
                    begin_text = '\n'.join(begin_content)
                    end_text = '\n'.join(end_content)
                    return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes, {total_lines:,} lines)\n\n**Beginning of file:**\n```{ext[1:] if ext else ''}\n{begin_text}\n```\n\n*... [{omitted_lines:,} lines omitted] ...*\n\n**End of file:**\n```{ext[1:] if ext else ''}\n{end_text}\n```"
                else:
                    # For smaller files, use character-based extraction
                    chunk_size = max_chars // 2
                    beginning = content[:chunk_size]
                    ending = content[-chunk_size:]
                    
                    # Clean breaks at newlines if possible
                    if '\n' in beginning:
                        beginning = beginning[:beginning.rfind('\n')]
                    if '\n' in ending:
                        ending = ending[ending.find('\n')+1:]
                    
                    omitted_chars = total_chars - len(beginning) - len(ending)
                    
                    return f"πŸ“„ **{file_name}** ({ext}, {file_size:,} bytes)\n```{ext[1:] if ext else ''}\n{beginning}\n\n... [{omitted_chars:,} characters omitted] ...\n\n{ending}\n```"
        
        # Handle other file types with simple messages
        elif ext == '.pdf':
            return f"πŸ“‘ **{file_name}** (PDF, {file_size:,} bytes)\n⚠️ PDF support requires PyPDF2"
        elif ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg', '.ico']:
            return f"πŸ–ΌοΈ **{file_name}** ({ext}, {file_size:,} bytes)\nπŸ’‘ Image file uploaded"
        elif ext in ['.xlsx', '.xls']:
            return f"πŸ“Š **{file_name}** (Excel, {file_size:,} bytes)\nπŸ’‘ Consider exporting as CSV"
        elif ext == '.docx':
            return f"πŸ“ **{file_name}** (Word document, {file_size:,} bytes)"
        elif ext in ['.zip', '.tar', '.gz', '.7z', '.rar']:
            return f"πŸ—œοΈ **{file_name}** (Archive, {file_size:,} bytes)"
        else:
            return f"πŸ“Ž **{file_name}** ({ext if ext else 'no extension'}, {file_size:,} bytes)"
            
    except Exception as e:
        return f"❌ Error reading file {file_name}: {str(e)}"

def get_grounding_context():
    """Fetch context from grounding URLs with caching"""
    # Handle both string and list formats for grounding_urls
    urls = GROUNDING_URLS
    if isinstance(urls, str):
        try:
            urls = json.loads(urls)
        except:
            urls = []
    
    if not urls:
        return ""
    
    # Create cache key from URLs
    cache_key = tuple(sorted([url for url in urls if url and url.strip()]))
    
    # Check cache first
    if cache_key in _url_content_cache:
        return _url_content_cache[cache_key]
    
    context_parts = []
    for i, url in enumerate(urls, 1):
        if url.strip():
            content = fetch_url_content(url.strip())
            # Add priority indicators
            priority_label = "PRIMARY" if i <= 2 else "SECONDARY"
            context_parts.append(f"[{priority_label}] Context from URL {i} ({url}):\n{content}")
    
    if context_parts:
        result = "\n\n" + "\n\n".join(context_parts) + "\n\n"
    else:
        result = ""
    
    # Cache the result
    _url_content_cache[cache_key] = result
    return result

def export_conversation_to_markdown(conversation_history):
    """Export conversation history to markdown format"""
    if not conversation_history:
        return "No conversation to export."
    
    markdown_content = f"""# Conversation Export
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}

---

"""
    
    message_pair_count = 0
    for i, message in enumerate(conversation_history):
        if isinstance(message, dict):
            role = message.get('role', 'unknown')
            content = message.get('content', '')
            
            if role == 'user':
                message_pair_count += 1
                markdown_content += f"## User Message {message_pair_count}\n\n{content}\n\n"
            elif role == 'assistant':
                markdown_content += f"## Assistant Response {message_pair_count}\n\n{content}\n\n---\n\n"
        elif isinstance(message, (list, tuple)) and len(message) >= 2:
            # Handle legacy tuple format: ["user msg", "assistant msg"]
            message_pair_count += 1
            user_msg, assistant_msg = message[0], message[1]
            if user_msg:
                markdown_content += f"## User Message {message_pair_count}\n\n{user_msg}\n\n"
            if assistant_msg:
                markdown_content += f"## Assistant Response {message_pair_count}\n\n{assistant_msg}\n\n---\n\n"
    
    return markdown_content


def generate_response(message, history):
    """Generate response using OpenRouter API with multimodal support"""
    
    # Enhanced API key validation with helpful messages
    if not API_KEY:
        error_msg = f"πŸ”‘ **API Key Required**\n\n"
        error_msg += f"Please configure your OpenRouter API key:\n"
        error_msg += f"1. Go to Settings (βš™οΈ) in your HuggingFace Space\n"
        error_msg += f"2. Click 'Variables and secrets'\n"
        error_msg += f"3. Add secret: **{API_KEY_VAR}**\n"
        error_msg += f"4. Value: Your OpenRouter API key (starts with `sk-or-`)\n\n"
        error_msg += f"Get your API key at: https://openrouter.ai/keys"
        print(f"❌ API request failed: No API key configured for {API_KEY_VAR}")
        return error_msg
    
    # Get grounding context
    grounding_context = get_grounding_context()
    
    # Process multimodal message
    file_context = ""
    file_notification = ""
    text_message = message
    
    # Check if message is multimodal (dict with 'text' and 'files')
    if isinstance(message, dict):
        text_message = message.get('text', '')
        files = message.get('files', [])
        
        if files:
            file_contents = []
            file_names = []
            for file_path in files:
                if file_path:
                    try:
                        file_content = extract_file_content(file_path)
                        file_contents.append(file_content)
                        file_names.append(os.path.basename(file_path))
                        print(f"πŸ“„ Processing file: {os.path.basename(file_path)}")
                    except Exception as e:
                        error_msg = f"Error processing file: {str(e)}"
                        file_contents.append(error_msg)
                        file_names.append("error")
                        print(f"❌ {error_msg}")
            
            if file_contents:
                file_context = "\n\n[UPLOADED FILES]\n" + "\n\n".join(file_contents) + "\n"
                # Add file notification to user message
                file_list = ", ".join(file_names)
                file_notification = f"\n\n[Note: The user has uploaded the following file(s): {file_list}. The content has been extracted and is available in context.]"
                print(f"βœ… Successfully processed {len(file_names)} file(s): {file_list}")
    
    # If dynamic URLs are enabled, check message for URLs to fetch
    if ENABLE_DYNAMIC_URLS:
        urls_in_message = extract_urls_from_text(text_message)
        if urls_in_message:
            dynamic_context = ""
            for url in urls_in_message[:3]:  # Limit to 3 URLs per message
                content = fetch_url_content(url)
                dynamic_context += f"\n\n[DYNAMIC] Context from {url}:\n{content}"
            grounding_context += dynamic_context
    
    # Build enhanced system prompt with grounding context and file content
    enhanced_system_prompt = SYSTEM_PROMPT + grounding_context + file_context
    
    # Build messages array for the API
    messages = [{"role": "system", "content": enhanced_system_prompt}]
    
    # Add conversation history - handle both modern messages format and legacy tuples
    for chat in history:
        if isinstance(chat, dict):
            messages.append(chat)
        elif isinstance(chat, (list, tuple)) and len(chat) >= 2:
            messages.append({"role": "user", "content": chat[0]})
            messages.append({"role": "assistant", "content": chat[1]})
    
    # Add current message with file notification if applicable
    user_message = text_message
    if file_notification:
        user_message = text_message + file_notification
    messages.append({"role": "user", "content": user_message})
    
    # Make API request with enhanced error handling
    try:
        print(f"πŸ”„ Making API request to OpenRouter...")
        print(f"   Model: {MODEL}")
        print(f"   Messages: {len(messages)} in conversation")
        if files:
            print(f"   Files uploaded: {len([f for f in files if f is not None])}")
        
        response = requests.post(
            url="https://openrouter.ai/api/v1/chat/completions",
            headers={
                "Authorization": f"Bearer {API_KEY}",
                "Content-Type": "application/json",
                "HTTP-Referer": "https://huggingface.co",
                "X-Title": "HuggingFace Space"
            },
            json={
                "model": MODEL,
                "messages": messages,
                "temperature": temperature,
                "max_tokens": max_tokens
            },
            timeout=30
        )
        
        print(f"πŸ“‘ API Response: {response.status_code}")
        
        if response.status_code == 200:
            try:
                result = response.json()
                return result['choices'][0]['message']['content']
            except (KeyError, IndexError, json.JSONDecodeError) as e:
                error_msg = f"❌ **Response Parsing Error**\n\n"
                error_msg += f"Received response from API but couldn't parse it properly.\n"
                error_msg += f"Error: {str(e)}\n\n"
                error_msg += f"**Troubleshooting:**\n"
                error_msg += f"1. Check OpenRouter service status\n"
                error_msg += f"2. Try again in a few moments\n"
                error_msg += f"3. Try a different model if available"
                print(f"❌ Response parsing error: {str(e)}")
                return error_msg
        elif response.status_code == 401:
            error_msg = f"πŸ”‘ **Authentication Error**\n\n"
            error_msg += f"Your API key appears to be invalid or expired.\n\n"
            error_msg += f"**Troubleshooting:**\n"
            error_msg += f"1. Check that your **{API_KEY_VAR}** secret is set correctly\n"
            error_msg += f"2. Verify your OpenRouter API key at https://openrouter.ai/keys\n"
            error_msg += f"3. Make sure the key starts with `sk-or-`\n"
            error_msg += f"4. Check if you have sufficient credits"
            print(f"❌ Authentication failed: Invalid API key")
            return error_msg
        elif response.status_code == 429:
            error_msg = f"⏱️ **Rate Limit Exceeded**\n\n"
            error_msg += f"Too many requests. Please wait a moment and try again.\n\n"
            error_msg += f"**Troubleshooting:**\n"
            error_msg += f"1. Wait 30-60 seconds before trying again\n"
            error_msg += f"2. Check your OpenRouter usage limits\n"
            print(f"❌ Rate limit exceeded")
            return error_msg
        elif response.status_code == 400:
            error_msg = f"πŸ“ **Request Error**\n\n"
            error_msg += f"There was a problem with the request format.\n"
            error_msg += f"Response: {response.text[:500]}\n\n"
            error_msg += f"**Troubleshooting:**\n"
            error_msg += f"1. Try a shorter message\n"
            error_msg += f"2. Check for special characters in your message\n"
            error_msg += f"3. Try a different model"
            print(f"❌ Bad request: {response.status_code} - {response.text[:200]}")
            return error_msg
        else:
            error_msg = f"🌐 **API Error {response.status_code}**\n\n"
            error_msg += f"An unexpected error occurred.\n"
            error_msg += f"Response: {response.text[:500]}\n\n"
            error_msg += f"**Troubleshooting:**\n"
            error_msg += f"1. Try again in a few moments\n"
            error_msg += f"2. Check OpenRouter service status\n"
            error_msg += f"3. Contact support if this persists"
            print(f"❌ API error: {response.status_code} - {response.text[:200]}")
            return error_msg
            
    except requests.exceptions.Timeout:
        error_msg = f"⏰ **Request Timeout**\n\n"
        error_msg += f"The API request took too long (30s limit).\n\n"
        error_msg += f"**Troubleshooting:**\n"
        error_msg += f"1. Try again with a shorter message\n"
        error_msg += f"2. Check your internet connection\n"
        error_msg += f"3. Try a different model"
        print(f"❌ Request timeout after 30 seconds")
        return error_msg
    except requests.exceptions.ConnectionError:
        error_msg = f"🌐 **Connection Error**\n\n"
        error_msg += f"Could not connect to OpenRouter API.\n\n"
        error_msg += f"**Troubleshooting:**\n"
        error_msg += f"1. Check your internet connection\n"
        error_msg += f"2. Check OpenRouter service status\n"
        error_msg += f"3. Try again in a few moments"
        print(f"❌ Connection error to OpenRouter API")
        return error_msg
    except Exception as e:
        error_msg = "❌ **Unexpected Error**\n\n"
        error_msg += "An unexpected error occurred:\n"
        error_msg += f"`{str(e)}`\n\n"
        error_msg += "Please try again or contact support if this persists."
        print(f"❌ Unexpected error: {str(e)}")
        return error_msg

# Access code verification
access_granted = gr.State(False)
_access_granted_global = False  # Global fallback

def verify_access_code(code):
    """Verify the access code"""
    global _access_granted_global
    if ACCESS_CODE is None:
        _access_granted_global = True
        return gr.update(value="No access code required.", style={"color": "green"}), gr.update(visible=True), True
    
    if code == ACCESS_CODE:
        _access_granted_global = True
        return gr.update(value="βœ… Access granted!", style={"color": "green"}), gr.update(visible=True), True
    else:
        _access_granted_global = False
        return gr.update(value="❌ Invalid access code. Please try again.", style={"color": "red"}), gr.update(visible=False), False

def protected_generate_response(message, history):
    """Protected response function that checks access"""
    # Check if access is granted via the global variable
    if ACCESS_CODE is not None and not _access_granted_global:
        return "Please enter the access code to continue."
    return generate_response(message, history)

# Global variable to store chat history for export
chat_history_store = []

def store_and_generate_response(message, history):
    """Wrapper function that stores history and generates response"""
    global chat_history_store
    
    # Generate response using the protected function
    response = protected_generate_response(message, history)
    
    # Convert current history to the format we need for export
    # history comes in as [["user1", "bot1"], ["user2", "bot2"], ...]
    chat_history_store = []
    if history:
        for exchange in history:
            if isinstance(exchange, dict):
                chat_history_store.append(exchange)
            elif isinstance(exchange, (list, tuple)) and len(exchange) >= 2:
                chat_history_store.append({"role": "user", "content": exchange[0]})
                chat_history_store.append({"role": "assistant", "content": exchange[1]})
    
    # Add the current exchange
    chat_history_store.append({"role": "user", "content": message})
    chat_history_store.append({"role": "assistant", "content": response})
    
    return response

def export_current_conversation():
    """Export the current conversation"""
    if not chat_history_store:
        return gr.update(visible=False)
    
    markdown_content = export_conversation_to_markdown(chat_history_store)
    
    # Create logical filename with space name and timestamp
    # Handle SPACE_NAME being a repr() string with quotes
    # Remove quotes and whitespace from SPACE_NAME
    if isinstance(SPACE_NAME, str):
        space_name_clean = SPACE_NAME.strip()
        for char in ["'", '"']:
            space_name_clean = space_name_clean.strip(char)
    else:
        space_name_clean = "assistant"
    space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', space_name_clean).strip('_').lower()
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    filename = f"{space_name_safe}_conversation_{timestamp}.md"
    
    # Save to temporary file with meaningful name
    temp_dir = tempfile.gettempdir()
    temp_file = os.path.join(temp_dir, filename)
    
    with open(temp_file, 'w', encoding='utf-8') as f:
        f.write(markdown_content)
    
    return gr.update(value=temp_file, visible=True)

def export_conversation(history):
    """Export conversation to markdown file"""
    if not history:
        return gr.update(visible=False)
    
    markdown_content = export_conversation_to_markdown(history)
    
    # Create logical filename with space name and timestamp
    # Handle SPACE_NAME being a repr() string with quotes
    # Remove quotes and whitespace from SPACE_NAME
    if isinstance(SPACE_NAME, str):
        space_name_clean = SPACE_NAME.strip()
        for char in ["'", '"']:
            space_name_clean = space_name_clean.strip(char)
    else:
        space_name_clean = "assistant"
    space_name_safe = re.sub(r'[^a-zA-Z0-9]+', '_', space_name_clean).strip('_').lower()
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    filename = f"{space_name_safe}_conversation_{timestamp}.md"
    
    # Save to temporary file with meaningful name
    temp_dir = tempfile.gettempdir()
    temp_file = os.path.join(temp_dir, filename)
    
    with open(temp_file, 'w', encoding='utf-8') as f:
        f.write(markdown_content)
    
    return gr.update(value=temp_file, visible=True)

# Configuration status display
def get_configuration_status():
    """Generate a clean configuration status message for display"""
    status_parts = []
    
    # Basic configuration info (without redundant "Configuration:" header)
    status_parts.append(f"**Name:** {SPACE_NAME}")
    status_parts.append(f"**Model:** {MODEL}")
    status_parts.append(f"**Theme:** {THEME}")
    status_parts.append(f"**Temperature:** {temperature}")
    status_parts.append(f"**Max Response Tokens:** {max_tokens}")
    status_parts.append(f"**File Upload:** {'Enabled' if ENABLE_FILE_UPLOAD else 'Disabled'}")
    status_parts.append("")
    
    # Example prompts
    status_parts.append("")
    examples_list = config.get('examples', [])
    if isinstance(examples_list, str):
        try:
            import ast
            examples_list = ast.literal_eval(examples_list)
        except:
            examples_list = []
    
    if examples_list and len(examples_list) > 0:
        status_parts.append("**Example Prompts:**")
        for example in examples_list[:5]:  # Show first 5 examples
            status_parts.append(f"β€’ {example}")
        if len(examples_list) > 5:
            status_parts.append(f"β€’ ... and {len(examples_list) - 5} more")
    else:
        status_parts.append("**Example Prompts:** No example prompts configured")
    
    # URL Context if configured
    urls = GROUNDING_URLS
    if isinstance(urls, str):
        try:
            import ast
            urls = ast.literal_eval(urls)
        except:
            urls = []
    
    if urls and len(urls) > 0:
        status_parts.append("")
        status_parts.append("**Grounding URLs:**")
        for i, url in enumerate(urls[:5], 1):  # Show first 5 URLs
            status_parts.append(f"{i}. {url}")
        if len(urls) > 5:
            status_parts.append(f"... and {len(urls) - 5} more URLs")
    
    # System prompt at the end
    status_parts.append("")
    status_parts.append(f"**System Prompt:** {SYSTEM_PROMPT}")
    
    # API Key status (minimal, at the end)
    status_parts.append("")
    if not API_KEY_VALID:
        status_parts.append(f"**Note:** API key ({API_KEY_VAR}) not configured in Space secrets")
    
    return "\n".join(status_parts)

# HuggingFace Authentication Utility
def verify_hf_token_access():
    """Verify HF_TOKEN has write access to the space"""
    hf_token = os.environ.get("HF_TOKEN")
    space_id = os.environ.get("SPACE_ID")
    
    if not hf_token or not space_id:
        return False, "Missing HF_TOKEN or SPACE_ID environment variables"
    
    try:
        from huggingface_hub import HfApi
        api = HfApi(token=hf_token)
        # Test access by getting space info
        api.space_info(space_id)
        return True, "Authenticated successfully"
    except Exception as e:
        return False, f"Authentication failed: {str(e)}"

# Create interface with access code protection
# Dynamically set theme based on configuration
theme_class = getattr(gr.themes, THEME, gr.themes.Default)
with gr.Blocks(title=SPACE_NAME, theme=theme_class()) as demo:
    # Check if HF_TOKEN is configured to determine configuration panel availability
    HF_TOKEN = os.environ.get("HF_TOKEN", "").strip()
    SPACE_ID = os.environ.get("SPACE_ID", "").strip()
    
    # Verify actual HuggingFace API access
    HF_ACCESS_VALID, HF_ACCESS_MESSAGE = verify_hf_token_access()
    
    # Always use tabs structure, Configuration tab visible only with HF_TOKEN
    with gr.Tabs() as main_tabs:
        with gr.Tab("Chat U/I"):
                gr.Markdown(f"# {SPACE_NAME}")
                gr.Markdown(SPACE_DESCRIPTION)
                
                # Access code section (shown only if ACCESS_CODE is set)
                with gr.Column(visible=(ACCESS_CODE is not None)) as access_section:
                    gr.Markdown("### πŸ” Access Required")
                    gr.Markdown("Please enter the access code provided by your instructor:")
                    
                    access_input = gr.Textbox(
                        label="Access Code",
                        placeholder="Enter access code...",
                        type="password"
                    )
                    access_btn = gr.Button("Submit", variant="primary")
                    access_error = gr.Markdown(visible=False)
                
                # Main chat interface (hidden until access granted)
                with gr.Column(visible=(ACCESS_CODE is None)) as chat_section:
                    # Get examples from config
                    examples = config.get('examples', [])
                    if isinstance(examples, str):
                        try:
                            import ast
                            examples = ast.literal_eval(examples)
                        except:
                            examples = []
                    
                    # Format examples for multimodal ChatInterface
                    formatted_examples = None
                    if examples:
                        # Check if examples are already formatted correctly
                        if examples and isinstance(examples[0], dict) and 'text' in examples[0]:
                            # Already formatted as multimodal, use as-is
                            formatted_examples = examples
                        else:
                            # Format as multimodal messages
                            formatted_examples = [{"text": example, "files": []} for example in examples]
                    
                    # Configure multimodal textbox if file upload is enabled
                    if ENABLE_FILE_UPLOAD:
                        textbox = gr.MultimodalTextbox(
                            file_count="multiple",
                            file_types=None,  # Accept all file types
                            show_label=False,
                            placeholder="Type a message...",
                            container=False
                        )
                        multimodal = True
                    else:
                        textbox = None
                        multimodal = False
                    
                    chat_interface = gr.ChatInterface(
                        fn=store_and_generate_response,  # Use wrapper function to store history
                        title="",  # Title already shown above
                        description="",  # Description already shown above
                        examples=formatted_examples,
                        type="messages",  # Use modern message format for better compatibility
                        multimodal=multimodal,
                        textbox=textbox
                    )
                    
                    # Export functionality
                    with gr.Row():
                        export_btn = gr.Button("πŸ“₯ Export Conversation", variant="secondary", size="sm")
                    export_file = gr.File(label="Download", visible=False)
                    
                    # Connect export functionality
                    export_btn.click(
                        export_current_conversation,
                        outputs=[export_file]
                    )
                    
                    # Configuration status
                    with gr.Accordion("Configuration", open=False):
                        gr.Markdown(get_configuration_status())
                
                # Connect access verification within tab context
                if ACCESS_CODE is not None:
                    access_btn.click(
                        verify_access_code,
                        inputs=[access_input],
                        outputs=[access_error, chat_section, access_granted]
                    )
                    access_input.submit(
                        verify_access_code,
                        inputs=[access_input],
                        outputs=[access_error, chat_section, access_granted]
                    )
            
        # Add Configuration tab (only visible with valid HF_TOKEN)
        with gr.Tab("Configuration", visible=HF_ACCESS_VALID) as config_tab:
            gr.Markdown("## Configuration Management")
            
            # Show authentication status
            if HF_ACCESS_VALID:
                gr.Markdown(f"βœ… **Authenticated** - {HF_ACCESS_MESSAGE}")
                gr.Markdown("Configuration changes will be saved to the HuggingFace repository and the Space will restart automatically.")
                faculty_auth_state = gr.State(True)
            else:
                gr.Markdown(f"❌ **Not Available** - {HF_ACCESS_MESSAGE}")
                gr.Markdown("Set HF_TOKEN and SPACE_ID in Space secrets to enable configuration management.")
                faculty_auth_state = gr.State(False)
            
            # Configuration editor (visible if HF authentication is valid)
            with gr.Column(visible=HF_ACCESS_VALID) as faculty_config_section:
                gr.Markdown("### Edit Assistant Configuration")
                gr.Markdown("⚠️ **Warning:** Changes will affect all users immediately.")
                
                # Load current configuration
                try:
                    with open('config.json', 'r') as f:
                        current_config = json.load(f)
                except:
                    # Use DEFAULT_CONFIG as fallback
                    current_config = DEFAULT_CONFIG.copy()
                
                # Editable fields
                # System Prompt
                edit_system_prompt = gr.Textbox(
                    label="System Prompt",
                    value=current_config.get('system_prompt', SYSTEM_PROMPT),
                    lines=5
                )
                
                # 3. Model Selection
                edit_model = gr.Dropdown(
                    label="Model",
                    choices=[
                        "google/gemini-2.0-flash-001",
                        "google/gemma-3-27b-it",
                        "anthropic/claude-3.5-sonnet",
                        "anthropic/claude-3.5-haiku",
                        "openai/gpt-4o-mini-search-preview",
                        "openai/gpt-4.1-nano",
                        "nvidia/llama-3.1-nemotron-70b-instruct",
                        "qwen/qwen3-30b-a3b-instruct-2507"
                    ],
                    value=current_config.get('model', MODEL)
                )
            
                # 4. Example prompts field
                examples_value = current_config.get('examples', [])
                if isinstance(examples_value, list):
                    examples_text_value = "\n".join(examples_value)
                else:
                    examples_text_value = ""
                
                edit_examples = gr.Textbox(
                    label="Example Prompts (one per line)",
                    value=examples_text_value,
                    lines=3,
                    placeholder="What can you help me with?\nExplain this concept\nHelp me understand..."
                )
                
                # 5. Model Parameters
                with gr.Row():
                    edit_temperature = gr.Slider(
                        label="Temperature",
                        minimum=0,
                        maximum=2,
                        value=current_config.get('temperature', 0.7),
                        step=0.1
                    )
                    edit_max_tokens = gr.Slider(
                        label="Max Tokens",
                        minimum=50,
                        maximum=4096,
                        value=current_config.get('max_tokens', 750),
                        step=50
                    )
                
                # URL Grounding fields
                gr.Markdown("### URL Grounding")
                grounding_urls_value = current_config.get('grounding_urls', [])
                if isinstance(grounding_urls_value, str):
                    try:
                        import ast
                        grounding_urls_value = ast.literal_eval(grounding_urls_value)
                    except:
                        grounding_urls_value = []
                
                # Create 10 URL input fields
                url_fields = []
                for i in range(10):
                    url_value = grounding_urls_value[i] if i < len(grounding_urls_value) else ""
                    url_field = gr.Textbox(
                        label=f"URL {i+1}" + (" (Primary)" if i < 2 else " (Secondary)"),
                        value=url_value,
                        placeholder="https://..."
                    )
                    url_fields.append(url_field)
                
                # Configuration file upload
                gr.Markdown("### Upload Configuration")
                with gr.Accordion("πŸ“€ Upload Existing Configuration", open=False):
                    gr.Markdown("Have a config.json from another deployment? Upload it here to restore all settings.")
                    config_upload = gr.File(
                        label="Upload Configuration (JSON)",
                        file_types=[".json"],
                        file_count="single",
                        interactive=True
                    )
                    upload_status = gr.Markdown("", visible=False)
                
                config_locked = gr.Checkbox(
                    label="Lock Configuration (Prevent further edits)",
                    value=current_config.get('locked', False)
                )
                
                with gr.Row():
                    save_config_btn = gr.Button("Save Configuration", variant="primary")
                    reset_config_btn = gr.Button("Reset to Defaults", variant="secondary")
                
                config_status = gr.Markdown("")
            
            # Apply uploaded configuration function
            def apply_uploaded_config(config_file):
                """Apply uploaded configuration file to all form fields"""
                if config_file is None:
                    return [
                        gr.update(),  # system_prompt
                        gr.update(),  # model
                        gr.update(),  # examples
                        gr.update(),  # temperature
                        gr.update(),  # max_tokens
                        *[gr.update() for _ in range(10)],  # url1-url10
                        gr.update(value="", visible=False),  # upload_status
                    ]
                
                try:
                    # Read and parse the JSON file
                    with open(config_file.name, 'r') as f:
                        config = json.load(f)
                    
                    # Extract values with defaults
                    system_prompt = config.get('system_prompt', SYSTEM_PROMPT)
                    model = config.get('model', MODEL)
                    temperature = config.get('temperature', 0.7)
                    max_tokens = config.get('max_tokens', 750)
                    
                    # Process examples
                    examples = config.get('examples', [])
                    if isinstance(examples, list):
                        examples_text = '\n'.join(examples)
                    else:
                        examples_text = ""
                    
                    # Process grounding URLs
                    grounding_urls = config.get('grounding_urls', [])
                    url_updates = []
                    for i in range(10):
                        if i < len(grounding_urls):
                            url_updates.append(gr.update(value=grounding_urls[i]))
                        else:
                            url_updates.append(gr.update(value=""))
                    
                    # Show success message
                    status_msg = f"βœ… Configuration loaded successfully from: {config_file.name.split('/')[-1]}"
                    
                    return [
                        gr.update(value=system_prompt),  # system_prompt
                        gr.update(value=model),  # model
                        gr.update(value=examples_text),  # examples
                        gr.update(value=temperature),  # temperature
                        gr.update(value=max_tokens),  # max_tokens
                        *url_updates,  # url1-url10
                        gr.update(value=status_msg, visible=True),  # upload_status
                    ]
                    
                except json.JSONDecodeError as e:
                    error_msg = f"❌ Invalid JSON format: {str(e)}"
                    return [
                        gr.update(),  # system_prompt
                        gr.update(),  # model
                        gr.update(),  # examples
                        gr.update(),  # temperature
                        gr.update(),  # max_tokens
                        *[gr.update() for _ in range(10)],  # url1-url10
                        gr.update(value=error_msg, visible=True),  # upload_status
                    ]
                except Exception as e:
                    error_msg = f"❌ Error loading configuration: {str(e)}"
                    return [
                        gr.update(),  # system_prompt
                        gr.update(),  # model
                        gr.update(),  # examples
                        gr.update(),  # temperature
                        gr.update(),  # max_tokens
                        *[gr.update() for _ in range(10)],  # url1-url10
                        gr.update(value=error_msg, visible=True),  # upload_status
                    ]
            
            # Save configuration function
            def save_configuration(is_authenticated, new_prompt, new_model, new_examples, new_temp, new_tokens, *url_values):
                if not is_authenticated:
                    return "Not authenticated"
                
                # Check if configuration is already locked
                try:
                    with open('config.json', 'r') as f:
                        existing_config = json.load(f)
                        if existing_config.get('locked', False):
                            return "Configuration is locked and cannot be modified"
                except:
                    pass
                
                # Load current config to preserve all values
                try:
                    with open('config.json', 'r') as f:
                        current_full_config = json.load(f)
                except:
                    # If config.json doesn't exist, use default configuration
                    current_full_config = DEFAULT_CONFIG.copy()
                
                # Process example prompts
                examples_list = [ex.strip() for ex in new_examples.split('\n') if ex.strip()]
                
                # Process URL values - lock_config is the last parameter
                urls = list(url_values[:-1])  # All but last are URLs
                lock_config_from_args = url_values[-1]  # Last is lock_config
                # Filter out empty URLs
                grounding_urls = [url.strip() for url in urls if url.strip()]
                
                # Create backup before making changes
                try:
                    # Create backups directory if it doesn't exist
                    os.makedirs('config_backups', exist_ok=True)
                    
                    # Create timestamped backup
                    backup_filename = f"config_backups/config_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
                    with open(backup_filename, 'w') as backup_file:
                        json.dump(current_full_config, backup_file, indent=2)
                    
                    # Keep only last 10 backups
                    backups = sorted([f for f in os.listdir('config_backups') if f.endswith('.json')])
                    if len(backups) > 10:
                        for old_backup in backups[:-10]:
                            os.remove(os.path.join('config_backups', old_backup))
                except Exception as backup_error:
                    print(f"Warning: Could not create backup: {backup_error}")
                    # Continue with save even if backup fails
                
                # Update all editable fields while preserving everything else
                current_full_config.update({
                    'system_prompt': new_prompt,
                    'model': new_model,
                    'examples': examples_list,
                    'temperature': new_temp,
                    'max_tokens': int(new_tokens),
                    'grounding_urls': grounding_urls,
                    'locked': lock_config_from_args,
                    'last_modified': datetime.now().isoformat(),
                    'last_modified_by': 'faculty'
                })
                
                try:
                    with open('config.json', 'w') as f:
                        json.dump(current_full_config, f, indent=2)
                    
                    # Optional: Auto-commit to HuggingFace if token is available
                    hf_token = os.environ.get("HF_TOKEN")
                    space_id = os.environ.get("SPACE_ID") 
                    
                    if hf_token and space_id:
                        try:
                            from huggingface_hub import HfApi, CommitOperationAdd, restart_space
                            api = HfApi(token=hf_token)
                            
                            # Create commit operation to upload config.json
                            operations = [
                                CommitOperationAdd(
                                    path_or_fileobj="config.json",
                                    path_in_repo="config.json"
                                )
                            ]
                            
                            # Create commit with updated configuration
                            api.create_commit(
                                repo_id=space_id,
                                operations=operations,
                                commit_message=f"Update configuration by faculty at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
                                commit_description="Faculty configuration update through web interface",
                                repo_type="space",
                                token=hf_token
                            )
                            
                            # Automatic restart
                            try:
                                restart_space(space_id, token=hf_token)
                                return f"βœ… Configuration saved and committed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Space is restarting automatically!**\n\nThe page will refresh in about 30 seconds. Your changes will be applied."
                            except Exception as restart_error:
                                print(f"Could not auto-restart: {restart_error}")
                                return f"βœ… Configuration saved and committed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Please restart manually** (auto-restart failed)\n\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds"
                        except Exception as commit_error:
                            print(f"Note: Could not auto-commit to repository: {commit_error}")
                            return f"βœ… Configuration saved locally at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Manual Restart Required**\nFor changes to take effect:\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds for restart"
                    else:
                        return f"βœ… Configuration saved at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nπŸ”„ **Manual Restart Required**\nFor changes to take effect:\n1. Go to Settings (βš™οΈ)\n2. Click 'Factory reboot'\n3. Wait ~30 seconds for restart"
                except Exception as e:
                    return f"❌ Error saving configuration: {str(e)}"
            
            # Reset configuration function
            def reset_configuration(is_authenticated):
                if not is_authenticated:
                    updates = ["Not authenticated"] + [gr.update() for _ in range(14)]  # 1 status + 14 fields (prompt, model, examples, temp, tokens + 10 urls)
                    return tuple(updates)
                
                # Check if locked
                try:
                    with open('config.json', 'r') as f:
                        existing_config = json.load(f)
                        if existing_config.get('locked', False):
                            updates = ["Configuration is locked"] + [gr.update() for _ in range(14)]
                            return tuple(updates)
                except:
                    pass
                
                # Get default examples as text
                default_examples = DEFAULT_CONFIG.get('examples', [])
                if isinstance(default_examples, list):
                    examples_text = "\n".join(default_examples)
                else:
                    examples_text = ""
                
                # Get default URLs - parse from JSON string if needed
                default_urls = DEFAULT_CONFIG.get('grounding_urls', [])
                if isinstance(default_urls, str):
                    try:
                        import json
                        default_urls = json.loads(default_urls)
                    except:
                        default_urls = []
                elif not isinstance(default_urls, list):
                    default_urls = []
                
                # Reset to original default values
                updates = [
                    "Reset to default values",
                    gr.update(value=DEFAULT_CONFIG.get('system_prompt', SYSTEM_PROMPT)),
                    gr.update(value=DEFAULT_CONFIG.get('model', MODEL)),
                    gr.update(value=examples_text),
                    gr.update(value=DEFAULT_CONFIG.get('temperature', temperature)),
                    gr.update(value=DEFAULT_CONFIG.get('max_tokens', max_tokens))
                ]
                
                # Add URL updates
                for i in range(10):
                    url_value = default_urls[i] if i < len(default_urls) else ""
                    updates.append(gr.update(value=url_value))
                
                return tuple(updates)
            
            
            # Connect configuration buttons
            save_config_btn.click(
                save_configuration,
                inputs=[faculty_auth_state, edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields + [config_locked],
                outputs=[config_status]
            )
            
            reset_config_btn.click(
                reset_configuration,
                inputs=[faculty_auth_state],
                outputs=[config_status, edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields
            )
            
            # Connect upload handler
            config_upload.change(
                apply_uploaded_config,
                inputs=[config_upload],
                outputs=[edit_system_prompt, edit_model, edit_examples, edit_temperature, edit_max_tokens] + url_fields + [upload_status]
            )

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