File size: 43,581 Bytes
03dcabd
4372751
d927a6e
b1a8e9d
616e960
 
d927a6e
 
 
b1a8e9d
 
fbf1f3d
616e960
fbf1f3d
e2255ea
ce07a27
616e960
b1a8e9d
fbf1f3d
 
ada9833
 
b1a8e9d
616e960
b1a8e9d
 
 
 
 
 
 
 
 
616e960
 
b1a8e9d
fbf1f3d
ce07a27
616e960
b1a8e9d
 
 
 
616e960
 
b1a8e9d
fbf1f3d
ce07a27
616e960
b1a8e9d
 
 
 
616e960
 
b1a8e9d
 
616e960
b1a8e9d
 
 
 
 
 
 
 
 
 
 
fb08ce6
cf37bae
fbf1f3d
616e960
b1a8e9d
616e960
b1a8e9d
616e960
b1a8e9d
 
 
fbf1f3d
cf37bae
fbf1f3d
b1a8e9d
fbf1f3d
616e960
ada9833
 
 
 
 
 
616e960
ada9833
 
fbf1f3d
616e960
fbf1f3d
 
 
616e960
fbf1f3d
 
616e960
ada9833
 
616e960
fbf1f3d
 
03dcabd
fbf1f3d
 
 
 
616e960
fbf1f3d
b1a8e9d
 
03dcabd
fbf1f3d
 
616e960
b1a8e9d
 
 
 
 
 
 
 
 
 
4372751
616e960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84739ea
fb08ce6
 
 
 
ada9833
fbf1f3d
 
84739ea
b1a8e9d
616e960
fbf1f3d
ada9833
b1a8e9d
4372751
fbf1f3d
fb08ce6
b1a8e9d
 
4372751
fbf1f3d
03dcabd
fbf1f3d
 
 
 
 
 
cfcb6db
fbf1f3d
 
 
 
 
 
03dcabd
fbf1f3d
d927a6e
616e960
 
 
b1a8e9d
 
 
 
616e960
 
b1a8e9d
fb08ce6
 
84739ea
fb08ce6
 
84739ea
fb08ce6
e2255ea
b1a8e9d
fb08ce6
 
 
b1a8e9d
616e960
fb08ce6
b1a8e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb08ce6
 
b1a8e9d
 
 
 
 
 
 
 
 
 
 
fb08ce6
 
 
b1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
 
b1a8e9d
 
e2255ea
 
 
 
 
b1a8e9d
e2255ea
b1a8e9d
e2255ea
b1a8e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ada9833
fb08ce6
 
 
03dcabd
84739ea
 
 
b1a8e9d
 
 
 
 
 
 
 
84739ea
ac61d23
fbf1f3d
b1a8e9d
 
616e960
 
 
b1a8e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf1f3d
b1a8e9d
 
2e2f7a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb08ce6
 
 
73bb808
d927a6e
84739ea
e2255ea
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
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
from flask import Flask, render_template, request, jsonify
import requests
import os
import time
import random
from collections import Counter

app = Flask(__name__)

# Function to fetch trending spaces from Huggingface with pagination
def fetch_trending_spaces(offset=0, limit=72):
    try:
        # Simple data fetching
        url = "https://huggingface.co./api/spaces"
        params = {"limit": 10000}  # Get max 10000 to fetch more spaces
        
        # Increase timeout
        response = requests.get(url, params=params, timeout=30)
        
        if response.status_code == 200:
            spaces = response.json()
            filtered_spaces = [space for space in spaces if space.get('owner') != 'None' and space.get('id', '').split('/', 1)[0] != 'None']
            
            # Slice according to requested offset and limit
            start = min(offset, len(filtered_spaces))
            end = min(offset + limit, len(filtered_spaces))
            
            print(f"Fetched {len(filtered_spaces)} spaces, returning {end-start} items from {start} to {end}")
            
            return {
                'spaces': filtered_spaces[start:end],
                'total': len(filtered_spaces),
                'offset': offset,
                'limit': limit,
                'all_spaces': filtered_spaces  # Return all spaces for stats calculation
            }
        else:
            print(f"Error fetching spaces: {response.status_code}")
            # Return empty spaces with fake 200 limit data
            return {
                'spaces': generate_dummy_spaces(limit),
                'total': 200,
                'offset': offset,
                'limit': limit,
                'all_spaces': generate_dummy_spaces(500)  # Dummy data for stats
            }
    except Exception as e:
        print(f"Exception when fetching spaces: {e}")
        # Generate fake data
        return {
            'spaces': generate_dummy_spaces(limit),
            'total': 200,
            'offset': offset,
            'limit': limit,
            'all_spaces': generate_dummy_spaces(500)  # Dummy data for stats
        }

# Generate dummy spaces in case of error
def generate_dummy_spaces(count):
    spaces = []
    for i in range(count):
        spaces.append({
            'id': f'dummy/space-{i}',
            'owner': 'dummy',
            'title': f'Example Space {i+1}',
            'likes': 100 - i,
            'createdAt': '2023-01-01T00:00:00.000Z'
        })
    return spaces

# Transform Huggingface URL to direct space URL
def transform_url(owner, name):
    # 1. Replace '.' with '-'
    name = name.replace('.', '-')
    # 2. Replace '_' with '-'
    name = name.replace('_', '-')
    # 3. Convert to lowercase
    owner = owner.lower()
    name = name.lower()
    
    return f"https://{owner}-{name}.hf.space"

# Get space details
def get_space_details(space_data, index, offset):
    try:
        # Extract common info
        if '/' in space_data.get('id', ''):
            owner, name = space_data.get('id', '').split('/', 1)
        else:
            owner = space_data.get('owner', '')
            name = space_data.get('id', '')
        
        # Ignore if contains None
        if owner == 'None' or name == 'None':
            return None
        
        # Construct URLs
        original_url = f"https://huggingface.co./spaces/{owner}/{name}"
        embed_url = transform_url(owner, name)
        
        # Likes count
        likes_count = space_data.get('likes', 0)
        
        # Extract title
        title = space_data.get('title', name)
        
        # Tags
        tags = space_data.get('tags', [])
        
        return {
            'url': original_url,
            'embedUrl': embed_url,
            'title': title,
            'owner': owner,
            'name': name,  # Store Space name
            'likes_count': likes_count,
            'tags': tags,
            'rank': offset + index + 1
        }
    except Exception as e:
        print(f"Error processing space data: {e}")
        # Return basic object even if error occurs
        return {
            'url': 'https://huggingface.co./spaces',
            'embedUrl': 'https://huggingface.co./spaces',
            'title': 'Error Loading Space',
            'owner': 'huggingface',
            'name': 'error',
            'likes_count': 0,
            'tags': [],
            'rank': offset + index + 1
        }

# Get owner statistics from all spaces
def get_owner_stats(all_spaces):
    owners = []
    for space in all_spaces:
        if '/' in space.get('id', ''):
            owner, _ = space.get('id', '').split('/', 1)
        else:
            owner = space.get('owner', '')
        
        if owner != 'None':
            owners.append(owner)
    
    # Count occurrences of each owner
    owner_counts = Counter(owners)
    
    # Get top 30 owners by count
    top_owners = owner_counts.most_common(30)
    
    return top_owners

# Homepage route
@app.route('/')
def home():
    return render_template('index.html')

# Trending spaces API
@app.route('/api/trending-spaces', methods=['GET'])
def trending_spaces():
    search_query = request.args.get('search', '').lower()
    offset = int(request.args.get('offset', 0))
    limit = int(request.args.get('limit', 72))  # Default 72
    
    # Fetch trending spaces
    spaces_data = fetch_trending_spaces(offset, limit)
    
    # Process and filter spaces
    results = []
    for index, space_data in enumerate(spaces_data['spaces']):
        space_info = get_space_details(space_data, index, offset)
        
        if not space_info:
            continue
            
        # Apply search filter if needed
        if search_query:
            title = space_info['title'].lower()
            owner = space_info['owner'].lower()
            url = space_info['url'].lower()
            tags = ' '.join([str(tag) for tag in space_info.get('tags', [])]).lower()
            
            if (search_query not in title and 
                search_query not in owner and 
                search_query not in url and 
                search_query not in tags):
                continue
        
        results.append(space_info)
    
    # Get owner statistics for all spaces
    top_owners = get_owner_stats(spaces_data.get('all_spaces', []))
    
    return jsonify({
        'spaces': results,
        'total': spaces_data['total'],
        'offset': offset,
        'limit': limit,
        'top_owners': top_owners  # Add top owners data
    })

if __name__ == '__main__':
    # Create templates folder
    os.makedirs('templates', exist_ok=True)
    
    # Create index.html file
    with open('templates/index.html', 'w', encoding='utf-8') as f:
        f.write('''<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Huggingface Spaces Gallery</title>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <style>
    @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@300;400;500;600;700&display=swap');
    
    :root {
      --pastel-pink: #FFD6E0;
      --pastel-blue: #C5E8FF;
      --pastel-purple: #E0C3FC;
      --pastel-yellow: #FFF2CC;
      --pastel-green: #C7F5D9;
      --pastel-orange: #FFE0C3;
      
      --mac-window-bg: rgba(250, 250, 250, 0.85);
      --mac-toolbar: #F5F5F7;
      --mac-border: #E2E2E2;
      --mac-button-red: #FF5F56;
      --mac-button-yellow: #FFBD2E;
      --mac-button-green: #27C93F;
      
      --text-primary: #333;
      --text-secondary: #666;
      --box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    }
    
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
      line-height: 1.6;
      color: var(--text-primary);
      background-color: #f8f9fa;
      background-image: linear-gradient(135deg, var(--pastel-blue) 0%, var(--pastel-purple) 100%);
      min-height: 100vh;
      padding: 2rem;
    }
    
    .container {
      max-width: 1600px;
      margin: 0 auto;
    }
    
    /* Mac OS Window Styling */
    .mac-window {
      background-color: var(--mac-window-bg);
      border-radius: 10px;
      box-shadow: var(--box-shadow);
      backdrop-filter: blur(10px);
      overflow: hidden;
      margin-bottom: 2rem;
      border: 1px solid var(--mac-border);
    }
    
    .mac-toolbar {
      display: flex;
      align-items: center;
      padding: 10px 15px;
      background-color: var(--mac-toolbar);
      border-bottom: 1px solid var(--mac-border);
    }
    
    .mac-buttons {
      display: flex;
      gap: 8px;
      margin-right: 15px;
    }
    
    .mac-button {
      width: 12px;
      height: 12px;
      border-radius: 50%;
      cursor: default;
    }
    
    .mac-close {
      background-color: var(--mac-button-red);
    }
    
    .mac-minimize {
      background-color: var(--mac-button-yellow);
    }
    
    .mac-maximize {
      background-color: var(--mac-button-green);
    }
    
    .mac-title {
      flex-grow: 1;
      text-align: center;
      font-size: 0.9rem;
      color: var(--text-secondary);
    }
    
    .mac-content {
      padding: 20px;
    }
    
    /* Header Styling */
    .header {
      text-align: center;
      margin-bottom: 1.5rem;
      position: relative;
    }
    
    .header h1 {
      font-size: 2.2rem;
      font-weight: 700;
      margin: 0;
      color: #2d3748;
      letter-spacing: -0.5px;
    }
    
    .header p {
      color: var(--text-secondary);
      margin-top: 0.5rem;
      font-size: 1.1rem;
    }
    
    /* Controls Styling */
    .search-bar {
      display: flex;
      align-items: center;
      margin-bottom: 1.5rem;
      background-color: white;
      border-radius: 30px;
      padding: 5px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
      max-width: 600px;
      margin-left: auto;
      margin-right: auto;
    }
    
    .search-bar input {
      flex-grow: 1;
      border: none;
      padding: 12px 20px;
      font-size: 1rem;
      outline: none;
      background: transparent;
      border-radius: 30px;
    }
    
    .search-bar .refresh-btn {
      background-color: var(--pastel-green);
      color: #1a202c;
      border: none;
      border-radius: 30px;
      padding: 10px 20px;
      font-size: 1rem;
      font-weight: 600;
      cursor: pointer;
      transition: all 0.2s;
      display: flex;
      align-items: center;
      gap: 8px;
    }
    
    .search-bar .refresh-btn:hover {
      background-color: #9ee7c0;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
    
    .refresh-icon {
      display: inline-block;
      width: 16px;
      height: 16px;
      border: 2px solid #1a202c;
      border-top-color: transparent;
      border-radius: 50%;
      animation: none;
    }
    
    .refreshing .refresh-icon {
      animation: spin 1s linear infinite;
    }
    
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    
    /* Grid Styling */
    .grid-container {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
      gap: 1.5rem;
      margin-bottom: 2rem;
    }
    
    .grid-item {
      height: 500px;
      position: relative;
      overflow: hidden;
      transition: all 0.3s ease;
      border-radius: 15px;
    }
    
    .grid-item:nth-child(6n+1) { background-color: var(--pastel-pink); }
    .grid-item:nth-child(6n+2) { background-color: var(--pastel-blue); }
    .grid-item:nth-child(6n+3) { background-color: var(--pastel-purple); }
    .grid-item:nth-child(6n+4) { background-color: var(--pastel-yellow); }
    .grid-item:nth-child(6n+5) { background-color: var(--pastel-green); }
    .grid-item:nth-child(6n+6) { background-color: var(--pastel-orange); }
    
    .grid-item:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
    }
    
    .grid-header {
      padding: 15px;
      display: flex;
      flex-direction: column;
      background-color: rgba(255, 255, 255, 0.7);
      backdrop-filter: blur(5px);
      border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    
    .grid-header-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 8px;
    }
    
    .rank-badge {
      background-color: #1a202c;
      color: white;
      font-size: 0.8rem;
      font-weight: 600;
      padding: 4px 8px;
      border-radius: 50px;
    }
    
    .grid-header h3 {
      margin: 0;
      font-size: 1.2rem;
      font-weight: 700;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .grid-meta {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 0.9rem;
    }
    
    .owner-info {
      color: var(--text-secondary);
      font-weight: 500;
    }
    
    .likes-counter {
      display: flex;
      align-items: center;
      color: #e53e3e;
      font-weight: 600;
    }
    
    .likes-counter span {
      margin-left: 4px;
    }
    
    .grid-actions {
      padding: 10px 15px;
      text-align: right;
      background-color: rgba(255, 255, 255, 0.7);
      backdrop-filter: blur(5px);
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      z-index: 10;
      display: flex;
      justify-content: flex-end;
    }
    
    .open-link {
      text-decoration: none;
      color: #2c5282;
      font-weight: 600;
      padding: 5px 10px;
      border-radius: 5px;
      transition: all 0.2s;
      background-color: rgba(237, 242, 247, 0.8);
    }
    
    .open-link:hover {
      background-color: #e2e8f0;
    }
    
    .grid-content {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      padding-top: 85px; /* Header height */
      padding-bottom: 45px; /* Actions height */
    }
    
    .iframe-container {
      width: 100%;
      height: 100%;
      overflow: hidden;
      position: relative;
    }
    
    /* Apply 70% scaling to iframes */
    .grid-content iframe {
      transform: scale(0.7);
      transform-origin: top left;
      width: 142.857%;
      height: 142.857%;
      border: none;
      border-radius: 0;
    }
    
    .error-placeholder {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      padding: 20px;
      background-color: rgba(255, 255, 255, 0.9);
      text-align: center;
    }
    
    .error-emoji {
      font-size: 6rem;
      margin-bottom: 1.5rem;
      animation: bounce 1s infinite alternate;
      text-shadow: 0 10px 20px rgba(0,0,0,0.1);
    }
    
    @keyframes bounce {
      from {
        transform: translateY(0px) scale(1);
      }
      to {
        transform: translateY(-15px) scale(1.1);
      }
    }
    
    /* Pagination Styling */
    .pagination {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 10px;
      margin: 2rem 0;
    }
    
    .pagination-button {
      background-color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 10px;
      font-size: 1rem;
      font-weight: 600;
      cursor: pointer;
      transition: all 0.2s;
      color: var(--text-primary);
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    }
    
    .pagination-button:hover {
      background-color: #f8f9fa;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }
    
    .pagination-button.active {
      background-color: var(--pastel-purple);
      color: #4a5568;
    }
    
    .pagination-button:disabled {
      background-color: #edf2f7;
      color: #a0aec0;
      cursor: default;
      box-shadow: none;
    }
    
    /* Loading Indicator */
    .loading {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(255, 255, 255, 0.8);
      backdrop-filter: blur(5px);
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 1000;
    }
    
    .loading-content {
      text-align: center;
    }
    
    .loading-spinner {
      width: 60px;
      height: 60px;
      border: 5px solid #e2e8f0;
      border-top-color: var(--pastel-purple);
      border-radius: 50%;
      animation: spin 1s linear infinite;
      margin: 0 auto 15px;
    }
    
    .loading-text {
      font-size: 1.2rem;
      font-weight: 600;
      color: #4a5568;
    }
    
    .loading-error {
      display: none;
      margin-top: 10px;
      color: #e53e3e;
      font-size: 0.9rem;
    }
    
    /* Stats window styling */
    .stats-window {
      margin-top: 2rem;
      margin-bottom: 2rem;
    }
    
    .stats-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 1rem;
    }
    
    .stats-title {
      font-size: 1.5rem;
      font-weight: 700;
      color: #2d3748;
    }
    
    .stats-toggle {
      background-color: var(--pastel-blue);
      border: none;
      padding: 8px 16px;
      border-radius: 20px;
      font-weight: 600;
      cursor: pointer;
      transition: all 0.2s;
    }
    
    .stats-toggle:hover {
      background-color: var(--pastel-purple);
    }
    
    .stats-content {
      background-color: white;
      border-radius: 10px;
      padding: 20px;
      box-shadow: var(--box-shadow);
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.5s ease-out;
    }
    
    .stats-content.open {
      max-height: 600px;
    }
    
    .chart-container {
      width: 100%;
      height: 500px;
    }
    
    /* Responsive Design */
    @media (max-width: 768px) {
      body {
        padding: 1rem;
      }
      
      .grid-container {
        grid-template-columns: 1fr;
      }
      
      .search-bar {
        flex-direction: column;
        padding: 10px;
      }
      
      .search-bar input {
        width: 100%;
        margin-bottom: 10px;
      }
      
      .search-bar .refresh-btn {
        width: 100%;
        justify-content: center;
      }
      
      .pagination {
        flex-wrap: wrap;
      }
      
      .chart-container {
        height: 300px;
      }
    }
    
    .error-emoji-detector { 
      position: fixed; 
      top: -9999px; 
      left: -9999px;
      z-index: -1; 
      opacity: 0;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="mac-window">
      <div class="mac-toolbar">
        <div class="mac-buttons">
          <div class="mac-button mac-close"></div>
          <div class="mac-button mac-minimize"></div>
          <div class="mac-button mac-maximize"></div>
        </div>
        <div class="mac-title">Huggingface Explorer</div>
      </div>
      
      <div class="mac-content">
        <div class="header">
          <h1>HF Space Leaderboard</h1>
          <p>Discover the top 500 trending spaces from the Huggingface</p>
        </div>
        
        <!-- Stats Section -->
        <div class="stats-window mac-window">
          <div class="mac-toolbar">
            <div class="mac-buttons">
              <div class="mac-button mac-close"></div>
              <div class="mac-button mac-minimize"></div>
              <div class="mac-button mac-maximize"></div>
            </div>
            <div class="mac-title">Creator Statistics</div>
          </div>
          <div class="mac-content">
            <div class="stats-header">
              <div class="stats-title">Top 30 Creators by Number of Spaces</div>
              <button id="statsToggle" class="stats-toggle">Show Stats</button>
            </div>
            <div id="statsContent" class="stats-content">
              <div class="chart-container">
                <canvas id="creatorStatsChart"></canvas>
              </div>
            </div>
          </div>
        </div>
        
        <div class="search-bar">
          <input type="text" id="searchInput" placeholder="Search by name, owner, or tags..." />
          <button id="refreshButton" class="refresh-btn">
            <span class="refresh-icon"></span>
            Refresh
          </button>
        </div>
        
        <div id="gridContainer" class="grid-container"></div>
        
        <div id="pagination" class="pagination">
          <!-- Pagination buttons will be dynamically created by JavaScript -->
        </div>
      </div>
    </div>
  </div>
  
  <div id="loadingIndicator" class="loading">
    <div class="loading-content">
      <div class="loading-spinner"></div>
      <div class="loading-text">Loading amazing spaces...</div>
      <div id="loadingError" class="loading-error">
        If this takes too long, try refreshing the page.
      </div>
    </div>
  </div>
  
  <script>
    // DOM element references
    const elements = {
      gridContainer: document.getElementById('gridContainer'),
      loadingIndicator: document.getElementById('loadingIndicator'),
      loadingError: document.getElementById('loadingError'),
      searchInput: document.getElementById('searchInput'),
      refreshButton: document.getElementById('refreshButton'),
      pagination: document.getElementById('pagination'),
      statsToggle: document.getElementById('statsToggle'),
      statsContent: document.getElementById('statsContent'),
      creatorStatsChart: document.getElementById('creatorStatsChart')
    };
    
    // Application state
    const state = {
      isLoading: false,
      spaces: [],
      currentPage: 0,
      itemsPerPage: 72,  // 72 items per page
      totalItems: 0,
      loadingTimeout: null,
      staticModeAttempted: {}, // Track which spaces have attempted static mode
      statsVisible: false,
      chartInstance: null,
      topOwners: [],
      iframeStatuses: {} // Track iframe loading status
    };
    
    // Advanced iframe loader for better error detection
    const iframeLoader = {
      checkQueue: {},
      maxAttempts: 5, // Try multiple times
      checkInterval: 5000, // Check every 5 seconds
      
      // Start checking iframe loading status
      startChecking: function(iframe, owner, name, title, spaceKey) {
        // Initialize tracking
        this.checkQueue[spaceKey] = {
          iframe: iframe,
          owner: owner,
          name: name,
          title: title,
          attempts: 0,
          status: 'loading'
        };
        
        // Start recursive checking
        this.checkIframeStatus(spaceKey);
      },
      
      // Check iframe loading status
      checkIframeStatus: function(spaceKey) {
        if (!this.checkQueue[spaceKey]) return;
        
        const item = this.checkQueue[spaceKey];
        const iframe = item.iframe;
        
        // If already processed, stop checking
        if (item.status !== 'loading') {
          delete this.checkQueue[spaceKey];
          return;
        }
        
        // Increment attempt counter
        item.attempts++;
        
        try {
          // 1. Check if iframe was removed from DOM
          if (!iframe || !iframe.parentNode) {
            delete this.checkQueue[spaceKey];
            return;
          }
          
          // 2. Check if content has loaded
          try {
            const hasContent = iframe.contentWindow && 
                             iframe.contentWindow.document && 
                             iframe.contentWindow.document.body;
            
            // 2.1 If content exists and has actual content loaded
            if (hasContent && iframe.contentWindow.document.body.innerHTML.length > 100) {
              // Check if it contains error text
              const bodyText = iframe.contentWindow.document.body.textContent.toLowerCase();
              if (bodyText.includes('forbidden') || 
                  bodyText.includes('404') || 
                  bodyText.includes('not found') ||
                  bodyText.includes('error')) {
                item.status = 'error';
                handleIframeError(iframe, item.owner, item.name, item.title);
              } else {
                item.status = 'success';
              }
              delete this.checkQueue[spaceKey];
              return;
            }
          } catch(e) {
            // Cross-origin access errors are expected - might be normal loading
          }
          
          // 3. Check iframe's visible size
          const rect = iframe.getBoundingClientRect();
          if (rect.width > 50 && rect.height > 50 && item.attempts > 2) {
            // If it has sufficient size, mark as success
            item.status = 'success';
            delete this.checkQueue[spaceKey];
            return;
          }
          
          // 4. If we've reached max attempts
          if (item.attempts >= this.maxAttempts) {
            // Final check: is iframe visible?
            if (iframe.offsetWidth > 0 && iframe.offsetHeight > 0) {
              // If visible, mark as success
              item.status = 'success';
            } else {
              // If still not visible, mark as error
              item.status = 'error';
              handleIframeError(iframe, item.owner, item.name, item.title);
            }
            delete this.checkQueue[spaceKey];
            return;
          }
          
          // Schedule next check with exponential backoff
          const nextDelay = this.checkInterval * Math.pow(1.5, item.attempts - 1);
          setTimeout(() => this.checkIframeStatus(spaceKey), nextDelay);
          
        } catch (e) {
          console.error('Error checking iframe status:', e);
          
          // If error occurs, try a few more times
          if (item.attempts >= this.maxAttempts) {
            item.status = 'error';
            handleIframeError(iframe, item.owner, item.name, item.title);
            delete this.checkQueue[spaceKey];
          } else {
            // Try again
            setTimeout(() => this.checkIframeStatus(spaceKey), this.checkInterval);
          }
        }
      }
    };
    
    // Toggle stats display
    function toggleStats() {
      state.statsVisible = !state.statsVisible;
      elements.statsContent.classList.toggle('open', state.statsVisible);
      elements.statsToggle.textContent = state.statsVisible ? 'Hide Stats' : 'Show Stats';
      
      if (state.statsVisible && state.topOwners.length > 0) {
        renderCreatorStats();
      }
    }
    
    // Render creator stats chart
    function renderCreatorStats() {
      if (state.chartInstance) {
        state.chartInstance.destroy();
      }
      
      const ctx = elements.creatorStatsChart.getContext('2d');
      
      // Prepare data
      const labels = state.topOwners.map(item => item[0]);
      const data = state.topOwners.map(item => item[1]);
      
      // Generate colors for bars
      const colors = [];
      for (let i = 0; i < labels.length; i++) {
        const hue = (i * 360 / labels.length) % 360;
        colors.push(`hsla(${hue}, 70%, 80%, 0.7)`);
      }
      
      // Create chart
      state.chartInstance = new Chart(ctx, {
        type: 'bar',
        data: {
          labels: labels,
          datasets: [{
            label: 'Number of Spaces',
            data: data,
            backgroundColor: colors,
            borderColor: colors.map(color => color.replace('0.7', '1')),
            borderWidth: 1
          }]
        },
        options: {
          indexAxis: 'y',
          responsive: true,
          maintainAspectRatio: false,
          plugins: {
            legend: {
              display: false
            },
            tooltip: {
              callbacks: {
                title: function(tooltipItems) {
                  return tooltipItems[0].label;
                },
                label: function(context) {
                  return `Spaces: ${context.raw}`;
                }
              }
            }
          },
          scales: {
            x: {
              beginAtZero: true,
              title: {
                display: true,
                text: 'Number of Spaces'
              }
            },
            y: {
              title: {
                display: true,
                text: 'Creator ID'
              },
              // Ensure all labels are shown without gaps
              ticks: {
                autoSkip: false,
                font: function(context) {
                  // Adjust font size to fit all labels if needed
                  const defaultSize = 11;
                  return {
                    size: labels.length > 20 ? defaultSize - 1 : defaultSize
                  };
                }
              }
            }
          }
        }
      });
    }
    
    // Load spaces with timeout
    async function loadSpaces(page = 0) {
      setLoading(true);
      
      try {
        const searchText = elements.searchInput.value;
        const offset = page * state.itemsPerPage;
        
        // Set timeout (30 seconds)
        const timeoutPromise = new Promise((_, reject) => 
          setTimeout(() => reject(new Error('Request timeout')), 30000)
        );
        
        const fetchPromise = fetch(`/api/trending-spaces?search=${encodeURIComponent(searchText)}&offset=${offset}&limit=${state.itemsPerPage}`);
        
        // Use the first Promise that completes
        const response = await Promise.race([fetchPromise, timeoutPromise]);
        const data = await response.json();
        
        // Update state on successful load
        state.spaces = data.spaces;
        state.totalItems = data.total;
        state.currentPage = page;
        state.topOwners = data.top_owners || [];
        
        renderGrid(data.spaces);
        renderPagination();
        
        // If stats are visible, update chart
        if (state.statsVisible && state.topOwners.length > 0) {
          renderCreatorStats();
        }
      } catch (error) {
        console.error('Error loading spaces:', error);
        
        // Show empty grid with error message
        elements.gridContainer.innerHTML = `
          <div style="grid-column: 1/-1; text-align: center; padding: 40px;">
            <div style="font-size: 3rem; margin-bottom: 20px;">⚠️</div>
            <h3 style="margin-bottom: 10px;">Unable to load spaces</h3>
            <p style="color: #666;">Please try refreshing the page. If the problem persists, try again later.</p>
            <button id="retryButton" style="margin-top: 20px; padding: 10px 20px; background: var(--pastel-purple); border: none; border-radius: 5px; cursor: pointer;">
              Try Again
            </button>
          </div>
        `;
        
        // Add event listener to retry button
        document.getElementById('retryButton')?.addEventListener('click', () => loadSpaces(0));
        
        // Render simple pagination
        renderPagination();
      } finally {
        setLoading(false);
      }
    }
    
    // Render pagination
    function renderPagination() {
      elements.pagination.innerHTML = '';
      
      const totalPages = Math.ceil(state.totalItems / state.itemsPerPage);
      
      // Previous page button
      const prevButton = document.createElement('button');
      prevButton.className = `pagination-button ${state.currentPage === 0 ? 'disabled' : ''}`;
      prevButton.textContent = 'Previous';
      prevButton.disabled = state.currentPage === 0;
      prevButton.addEventListener('click', () => {
        if (state.currentPage > 0) {
          loadSpaces(state.currentPage - 1);
        }
      });
      elements.pagination.appendChild(prevButton);
      
      // Page buttons (maximum of 7)
      const maxButtons = 7;
      let startPage = Math.max(0, state.currentPage - Math.floor(maxButtons / 2));
      let endPage = Math.min(totalPages - 1, startPage + maxButtons - 1);
      
      // Adjust start page if the end page is less than maximum buttons
      if (endPage - startPage + 1 < maxButtons) {
        startPage = Math.max(0, endPage - maxButtons + 1);
      }
      
      for (let i = startPage; i <= endPage; i++) {
        const pageButton = document.createElement('button');
        pageButton.className = `pagination-button ${i === state.currentPage ? 'active' : ''}`;
        pageButton.textContent = i + 1;
        pageButton.addEventListener('click', () => {
          if (i !== state.currentPage) {
            loadSpaces(i);
          }
        });
        elements.pagination.appendChild(pageButton);
      }
      
      // Next page button
      const nextButton = document.createElement('button');
      nextButton.className = `pagination-button ${state.currentPage >= totalPages - 1 ? 'disabled' : ''}`;
      nextButton.textContent = 'Next';
      nextButton.disabled = state.currentPage >= totalPages - 1;
      nextButton.addEventListener('click', () => {
        if (state.currentPage < totalPages - 1) {
          loadSpaces(state.currentPage + 1);
        }
      });
      elements.pagination.appendChild(nextButton);
    }
    
    // Handle iframe error and provide fallback error message
    function handleIframeError(iframe, owner, name, title) {
      const container = iframe.parentNode;
      
      // Error message container
      const errorPlaceholder = document.createElement('div');
      errorPlaceholder.className = 'error-placeholder';
      
      // Error message
      const errorMessage = document.createElement('p');
      errorMessage.textContent = `"${title}" space couldn't be loaded`;
      errorPlaceholder.appendChild(errorMessage);
      
      // Direct HF link
      const directLink = document.createElement('a');
      directLink.href = `https://huggingface.co./spaces/${owner}/${name}`;
      directLink.target = '_blank';
      directLink.textContent = 'Visit HF Space';
      directLink.style.color = '#3182ce';
      directLink.style.marginTop = '10px';
      directLink.style.display = 'inline-block';
      directLink.style.padding = '8px 16px';
      directLink.style.background = '#ebf8ff';
      directLink.style.borderRadius = '5px';
      directLink.style.fontWeight = '600';
      errorPlaceholder.appendChild(directLink);
      
      // Hide iframe and show error message
      iframe.style.display = 'none';
      container.appendChild(errorPlaceholder);
    }
    
    // Render grid
    function renderGrid(spaces) {
      elements.gridContainer.innerHTML = '';
      
      if (!spaces || spaces.length === 0) {
        const noResultsMsg = document.createElement('p');
        noResultsMsg.textContent = 'No spaces found matching your search.';
        noResultsMsg.style.padding = '2rem';
        noResultsMsg.style.textAlign = 'center';
        noResultsMsg.style.fontStyle = 'italic';
        noResultsMsg.style.color = '#718096';
        elements.gridContainer.appendChild(noResultsMsg);
        return;
      }
      
      spaces.forEach((item) => {
        try {
          const { url, title, likes_count, owner, name, rank } = item;
          
          // Skip if owner is 'None'
          if (owner === 'None') {
            return;
          }
          
          // Create grid item - Apply rotating pastel colors
          const gridItem = document.createElement('div');
          gridItem.className = 'grid-item';
          
          // Header
          const header = document.createElement('div');
          header.className = 'grid-header';
          
          // Header top part with rank
          const headerTop = document.createElement('div');
          headerTop.className = 'grid-header-top';
          
          // Title
          const titleEl = document.createElement('h3');
          titleEl.textContent = title;
          titleEl.title = title; // For tooltip on hover
          headerTop.appendChild(titleEl);
          
          // Rank badge
          const rankBadge = document.createElement('div');
          rankBadge.className = 'rank-badge';
          rankBadge.textContent = `#${rank}`;
          headerTop.appendChild(rankBadge);
          
          header.appendChild(headerTop);
          
          // Grid meta info
          const metaInfo = document.createElement('div');
          metaInfo.className = 'grid-meta';
          
          // Owner info
          const ownerEl = document.createElement('div');
          ownerEl.className = 'owner-info';
          ownerEl.textContent = `by ${owner}`;
          metaInfo.appendChild(ownerEl);
          
          // Likes counter
          const likesCounter = document.createElement('div');
          likesCounter.className = 'likes-counter';
          likesCounter.innerHTML = '♥ <span>' + likes_count + '</span>';
          metaInfo.appendChild(likesCounter);
          
          header.appendChild(metaInfo);
          
          // Add header to grid item
          gridItem.appendChild(header);
          
          // Content area
          const content = document.createElement('div');
          content.className = 'grid-content';
          
          // iframe container
          const iframeContainer = document.createElement('div');
          iframeContainer.className = 'iframe-container';
          
          // Create iframe to display the content
          const iframe = document.createElement('iframe');
          const directUrl = createDirectUrl(owner, name);
          iframe.src = directUrl;
          iframe.title = title;
          // Remove microphone permission
          iframe.allow = 'accelerometer; camera; encrypted-media; geolocation; gyroscope;';
          iframe.setAttribute('allowfullscreen', '');
          iframe.setAttribute('frameborder', '0');
          iframe.loading = 'lazy'; // Lazy load iframes for better performance
          
          // Unique ID for this iframe
          const iframeId = `iframe-${owner}-${name}`;
          iframe.id = iframeId;
          
          // Track this space
          const spaceKey = `${owner}/${name}`;
          state.iframeStatuses[spaceKey] = 'loading';
          
          // Use the advanced loader for better error detection
          iframe.onload = function() {
            iframeLoader.startChecking(iframe, owner, name, title, spaceKey);
          };
          
          // Direct error handling
          iframe.onerror = function() {
            handleIframeError(iframe, owner, name, title);
            state.iframeStatuses[spaceKey] = 'error';
          };
          
          // Final fallback - if nothing has happened after 30 seconds, show error
          setTimeout(() => {
            if (state.iframeStatuses[spaceKey] === 'loading') {
              handleIframeError(iframe, owner, name, title);
              state.iframeStatuses[spaceKey] = 'error';
            }
          }, 30000);
          
          // Add iframe to container
          iframeContainer.appendChild(iframe);
          content.appendChild(iframeContainer);
          
          // Actions section at bottom
          const actions = document.createElement('div');
          actions.className = 'grid-actions';
          
          // Open link
          const linkEl = document.createElement('a');
          linkEl.href = url;
          linkEl.target = '_blank';
          linkEl.className = 'open-link';
          linkEl.textContent = 'Open in new window';
          actions.appendChild(linkEl);
          
          // Add content and actions to grid item
          gridItem.appendChild(content);
          gridItem.appendChild(actions);
          
          // Add grid item to container
          elements.gridContainer.appendChild(gridItem);
        } catch (error) {
          console.error('Item rendering error:', error);
          // Continue rendering other items even if one fails
        }
      });
    }
    
    // Filter event listeners
    elements.searchInput.addEventListener('input', () => {
      // Debounce input to prevent API calls on every keystroke
      clearTimeout(state.searchTimeout);
      state.searchTimeout = setTimeout(() => loadSpaces(0), 300);
    });
    
    // Enter key in search box
    elements.searchInput.addEventListener('keyup', (event) => {
      if (event.key === 'Enter') {
        loadSpaces(0);
      }
    });
    
    // Refresh button event listener
    elements.refreshButton.addEventListener('click', () => loadSpaces(0));
    
    // Stats toggle button event listener
    elements.statsToggle.addEventListener('click', toggleStats);
    
    // Mac buttons functionality (just for show)
    document.querySelectorAll('.mac-button').forEach(button => {
      button.addEventListener('click', function(e) {
        e.preventDefault();
        // Mac buttons don't do anything, just for style
      });
    });
    
    // Page load complete event detection
    window.addEventListener('load', function() {
      // Start loading data when page is fully loaded
      setTimeout(() => loadSpaces(0), 500);
    });
    
    // Safety mechanism to prevent infinite loading
    setTimeout(() => {
      if (state.isLoading) {
        setLoading(false);
        elements.gridContainer.innerHTML = `
          <div style="grid-column: 1/-1; text-align: center; padding: 40px;">
            <div style="font-size: 3rem; margin-bottom: 20px;">⏱️</div>
            <h3 style="margin-bottom: 10px;">Loading is taking longer than expected</h3>
            <p style="color: #666;">Please try refreshing the page.</p>
            <button onClick="window.location.reload()" style="margin-top: 20px; padding: 10px 20px; background: var(--pastel-purple); border: none; border-radius: 5px; cursor: pointer;">
              Reload Page
            </button>
          </div>
        `;
      }
    }, 20000); // Force end loading state after 20 seconds
    
    // Start loading immediately - dual call with window.load for reliability
    loadSpaces(0);
    
    // Display loading indicator control
    function setLoading(isLoading) {
      state.isLoading = isLoading;
      elements.loadingIndicator.style.display = isLoading ? 'flex' : 'none';
      
      if (isLoading) {
        elements.refreshButton.classList.add('refreshing');
        // Show error message if loading takes too long
        clearTimeout(state.loadingTimeout);
        state.loadingTimeout = setTimeout(() => {
          elements.loadingError.style.display = 'block';
        }, 10000); // Show error message after 10 seconds
      } else {
        elements.refreshButton.classList.remove('refreshing');
        clearTimeout(state.loadingTimeout);
        elements.loadingError.style.display = 'none';
      }
    }
    
    // Create direct URL function with fixes for static sites
    function createDirectUrl(owner, name) {
      try {
        // 1. Replace '.' characters with '-'
        name = name.replace(/\./g, '-');
        // 2. Replace '_' characters with '-'
        name = name.replace(/_/g, '-');
        // 3. Convert everything to lowercase
        owner = owner.toLowerCase();
        name = name.toLowerCase();
        
        return `https://${owner}-${name}.hf.space`;
      } catch (error) {
        console.error('URL creation error:', error);
        return 'https://huggingface.co.';
      }
    }
  </script>
</body>
</html>
''')
    
    # Use port 7860 for Huggingface Spaces
    app.run(host='0.0.0.0', port=7860)