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ca96eb9
59f0b85
 
ca96eb9
59f0b85
 
e93499e
ca96eb9
59f0b85
 
 
 
 
ca96eb9
59f0b85
 
 
 
e93499e
59f0b85
ca96eb9
59f0b85
 
 
 
e93499e
 
 
59f0b85
 
e93499e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f0b85
 
 
 
 
ca96eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b910cc
 
 
 
e93499e
 
2b910cc
 
 
 
 
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
"""
Dataset service for centralized dataset operations and caching.

This module provides a centralized service for dataset operations including
metadata caching, dataset loading, and sampling functionality.
"""

import logging
import os
import json
import time
from typing import Optional, Dict, Any
from pathlib import Path
from datasets import load_dataset
from datasets.utils.logging import disable_progress_bar

from hf_eda_mcp.config import get_config
from hf_eda_mcp.integrations.hf_client import (
    HfClient,
    DatasetNotFoundError,
    AuthenticationError,
    NetworkError
)
from hf_eda_mcp.integrations.dataset_viewer_adapter import DatasetViewerAdapter
from hf_eda_mcp.error_handling import (
    retry_with_backoff,
    RetryConfig,
    log_error_with_context,
)

logger = logging.getLogger(__name__)

# Disable datasets progress bars for cleaner logging
disable_progress_bar()


class DatasetServiceError(Exception):
    """Base exception for dataset service errors."""
    pass


class CacheError(DatasetServiceError):
    """Raised when cache operations fail."""
    pass


class DatasetNotParquetError(DatasetServiceError):
    """Raised when a dataset is not in parquet format but parquet is required."""
    pass


class NoTextColumnsError(DatasetServiceError):
    """Raised when a dataset has no text columns for search."""
    pass


class DatasetService:
    """
    Centralized service for dataset operations with caching support.
    
    Provides metadata caching, dataset loading, and sampling functionality
    while managing authentication and error handling.
    """

    def __init__(
        self, 
        cache_dir: Optional[str] = None, 
        token: Optional[str] = None,
        cache_ttl: int = 3600  # 1 hour default TTL
    ):
        """
        Initialize dataset service with optional caching and authentication.
        
        Args:
            cache_dir: Directory for caching metadata and samples
            token: HuggingFace authentication token
            cache_ttl: Cache time-to-live in seconds (default: 1 hour)
        """
        self.hf_client = HfClient(token=token)
        self.dataset_viewer = DatasetViewerAdapter(token=token)
        self.cache_ttl = cache_ttl
        
        # Set up cache directory
        if cache_dir is None:
            cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "hf_eda_mcp")
        
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        
        # Cache subdirectories
        self.metadata_cache_dir = self.cache_dir / "metadata"
        self.sample_cache_dir = self.cache_dir / "samples"
        self.statistics_cache_dir = self.cache_dir / "statistics"
        
        self.metadata_cache_dir.mkdir(exist_ok=True)
        self.sample_cache_dir.mkdir(exist_ok=True)
        self.statistics_cache_dir.mkdir(exist_ok=True)
        
        logger.info(f"DatasetService initialized with cache dir: {self.cache_dir}")

    def _get_cache_key(self, dataset_id: str, config_name: Optional[str] = None) -> str:
        """Generate cache key for dataset metadata."""
        if config_name:
            return f"{dataset_id}_{config_name}".replace("/", "_")
        return dataset_id.replace("/", "_")

    def _get_sample_cache_key(
        self, 
        dataset_id: str, 
        split: str, 
        num_samples: int, 
        config_name: Optional[str] = None
    ) -> str:
        """Generate cache key for dataset samples."""
        base_key = self._get_cache_key(dataset_id, config_name)
        return f"{base_key}_{split}_{num_samples}"
    
    def _get_statistics_cache_key(
        self,
        dataset_id: str,
        split: str,
        config_name: Optional[str] = None
    ) -> str:
        """Generate cache key for dataset statistics."""
        base_key = self._get_cache_key(dataset_id, config_name)
        return f"{base_key}_{split}_stats"

    def _is_cache_valid(self, cache_file: Path) -> bool:
        """Check if cache file exists and is within TTL."""
        if not cache_file.exists():
            return False
        
        # Check if cache is within TTL
        cache_age = time.time() - cache_file.stat().st_mtime
        return cache_age < self.cache_ttl

    def _save_to_cache(self, cache_file: Path, data: Dict[str, Any]) -> None:
        """Save data to cache file."""
        try:
            cache_file.parent.mkdir(parents=True, exist_ok=True)
            with open(cache_file, 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
            logger.debug(f"Saved data to cache: {cache_file}")
        except Exception as e:
            logger.warning(f"Failed to save cache file {cache_file}: {e}")
            raise CacheError(f"Failed to save cache: {e}")

    def _load_from_cache(self, cache_file: Path) -> Optional[Dict[str, Any]]:
        """Load data from cache file."""
        try:
            if not self._is_cache_valid(cache_file):
                return None
            
            with open(cache_file, 'r', encoding='utf-8') as f:
                data = json.load(f)
            logger.debug(f"Loaded data from cache: {cache_file}")
            return data
        except Exception as e:
            logger.warning(f"Failed to load cache file {cache_file}: {e}")
            return None

    def _merge_viewer_data(
        self, 
        hub_metadata: Dict[str, Any], 
        viewer_data: Dict[str, Any],
        config_name: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Merge Dataset Viewer API data into Hub metadata.
        
        Enriches the basic Hub metadata with detailed information from the
        Dataset Viewer API including split sizes, features schema, and byte sizes.
        
        When no config is specified, returns detailed information for all configs.
        
        Args:
            hub_metadata: Basic metadata from Hub API
            viewer_data: Detailed data from Dataset Viewer API
            config_name: Optional configuration name to extract
            
        Returns:
            Merged metadata dictionary
        """
        merged = hub_metadata.copy()
        
        # Extract dataset_info from viewer response
        dataset_info = viewer_data.get('dataset_info', {})
        
        if not dataset_info:
            logger.warning("No dataset_info in viewer data")
            return merged
        
        # Handle two response formats:
        # 1. When config is specified in API call: dataset_info is the config data directly
        # 2. When no config specified: dataset_info is a dict with config names as keys
        
        if isinstance(dataset_info, dict) and 'config_name' in dataset_info:
            # Format 1: Single config data (config was specified in API call)
            config_data = dataset_info
            self._enrich_with_single_config(merged, config_data)
        elif config_name:
            # Format 2: Specific config requested
            if config_name in dataset_info:
                config_data = dataset_info[config_name]
                self._enrich_with_single_config(merged, config_data)
            else:
                logger.warning(f"Config '{config_name}' not found in viewer data")
                return merged
        else:
            # No config specified
            if len(dataset_info) == 1:
                # Only one config - use single config format for consistency
                config_data = next(iter(dataset_info.values()))
                self._enrich_with_single_config(merged, config_data)
            else:
                # Multiple configs - return all configs with detailed information
                self._enrich_with_all_configs(merged, dataset_info)
        
        return merged

    def _enrich_with_single_config(self, merged: Dict[str, Any], config_data: Dict[str, Any]) -> None:
        """
        Enrich metadata with a single config's data.
        
        Args:
            merged: Metadata dictionary to enrich (modified in place)
            config_data: Configuration data from Dataset Viewer API
        """
        # Enrich features with detailed schema from viewer
        if 'features' in config_data:
            merged['features'] = config_data['features']
        
        # Enrich splits with actual sizes
        if 'splits' in config_data:
            viewer_splits = config_data['splits']
            enriched_splits = {}
            
            for split_name, split_info in viewer_splits.items():
                enriched_splits[split_name] = {
                    'num_examples': split_info.get('num_examples', 0),
                    'num_bytes': split_info.get('num_bytes', 0)
                }
            
            merged['splits'] = enriched_splits
            merged['total_splits'] = len(enriched_splits)
        
        # Add dataset size information
        if 'dataset_size' in config_data:
            merged['dataset_size'] = config_data['dataset_size']
            merged['size_bytes'] = config_data['dataset_size']
            
            # Update human-readable size
            size_bytes = config_data['dataset_size']
            if size_bytes > 0:
                merged['size_human'] = self._format_bytes(size_bytes)
        
        if 'download_size' in config_data:
            merged['download_size'] = config_data['download_size']
            merged['download_size_human'] = self._format_bytes(config_data['download_size'])
        
        # Add builder and version info
        if 'builder_name' in config_data:
            merged['builder_name'] = config_data['builder_name']
        
        if 'version' in config_data:
            merged['version'] = config_data['version']
        
        # Update summary with enriched information
        if 'splits' in merged and merged['splits']:
            total_examples = sum(s.get('num_examples', 0) for s in merged['splits'].values())
            merged['total_examples'] = total_examples
            
            # Update summary string
            split_names = ', '.join(merged['splits'].keys())
            size_str = merged.get('size_human', 'Unknown')
            merged['summary'] = (
                f"Dataset: {merged['id']} | "
                f"Author: {merged.get('author', 'Unknown')} | "
                f"Size: {size_str} | "
                f"Examples: {total_examples:,} | "
                f"Downloads: {merged.get('downloads', 0):,} | "
                f"Likes: {merged.get('likes', 0)} | "
                f"Splits: {split_names}"
            )

    def _enrich_with_all_configs(self, merged: Dict[str, Any], dataset_info: Dict[str, Any]) -> None:
        """
        Enrich metadata with all configs' data.
        
        Creates a detailed 'config_details' list with information for each config.
        
        Args:
            merged: Metadata dictionary to enrich (modified in place)
            dataset_info: Dict mapping config names to their data
        """
        config_details = []
        total_dataset_size = 0
        total_download_size = 0
        total_examples_all_configs = 0
        
        for cfg_name, cfg_data in dataset_info.items():
            config_detail = {
                'config_name': cfg_name,
                'features': cfg_data.get('features', {}),
                'splits': {},
                'dataset_size': cfg_data.get('dataset_size', 0),
                'download_size': cfg_data.get('download_size', 0),
                'builder_name': cfg_data.get('builder_name', ''),
                'version': cfg_data.get('version', {}),
            }
            
            # Process splits for this config
            if 'splits' in cfg_data:
                for split_name, split_info in cfg_data['splits'].items():
                    config_detail['splits'][split_name] = {
                        'num_examples': split_info.get('num_examples', 0),
                        'num_bytes': split_info.get('num_bytes', 0)
                    }
            
            # Calculate totals for this config
            config_total_examples = sum(
                s.get('num_examples', 0) for s in config_detail['splits'].values()
            )
            config_detail['total_examples'] = config_total_examples
            config_detail['dataset_size_human'] = self._format_bytes(config_detail['dataset_size'])
            config_detail['download_size_human'] = self._format_bytes(config_detail['download_size'])
            
            config_details.append(config_detail)
            
            # Accumulate totals across all configs
            total_dataset_size += config_detail['dataset_size']
            total_download_size += config_detail['download_size']
            total_examples_all_configs += config_total_examples
        
        # Add detailed config information
        merged['config_details'] = config_details
        
        # Remove redundant top-level fields since they're in config_details
        merged.pop('splits', None)
        merged.pop('features', None)
        
        # Add aggregate information
        merged['total_dataset_size'] = total_dataset_size
        merged['total_dataset_size_human'] = self._format_bytes(total_dataset_size)
        merged['total_download_size'] = total_download_size
        merged['total_download_size_human'] = self._format_bytes(total_download_size)
        merged['total_examples'] = total_examples_all_configs
        
        # Update summary for multi-config datasets
        merged['summary'] = (
            f"Dataset: {merged['id']} | "
            f"Author: {merged.get('author', 'Unknown')} | "
            f"Configs: {len(config_details)} | "
            f"Total Size: {merged['total_dataset_size_human']} | "
            f"Total Examples: {total_examples_all_configs:,} | "
            f"Downloads: {merged.get('downloads', 0):,} | "
            f"Likes: {merged.get('likes', 0)}"
        )

    def _format_bytes(self, size_bytes: int) -> str:
        """Format bytes into human-readable string."""
        for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
            if size_bytes < 1024.0:
                return f"{size_bytes:.2f} {unit}"
            size_bytes /= 1024.0
        return f"{size_bytes:.2f} PB"

    def load_dataset_info(self, dataset_id: str, config_name: Optional[str] = None) -> Dict[str, Any]:
        """
        Load dataset information from HuggingFace Hub with caching.
        
        Combines data from both the Hub API and Dataset Viewer API to provide
        comprehensive metadata including split sizes, features schema, and more.
        
        Includes automatic retry logic for transient failures and comprehensive
        error handling with helpful suggestions.
        
        Args:
            dataset_id: HuggingFace dataset identifier
            config_name: Optional configuration name
            
        Returns:
            Dictionary containing dataset metadata
            
        Raises:
            DatasetNotFoundError: If dataset doesn't exist
            AuthenticationError: If dataset is private and authentication fails
            NetworkError: If network operations fail after retries
        """
        context = {
            "dataset_id": dataset_id,
            "config_name": config_name,
            "operation": "load_dataset_info"
        }
        
        cache_key = self._get_cache_key(dataset_id, config_name)
        cache_file = self.metadata_cache_dir / f"{cache_key}.json"
        
        # Try to load from cache first
        cached_data = self._load_from_cache(cache_file)
        if cached_data is not None:
            logger.debug(f"Using cached metadata for {dataset_id}")
            return cached_data
        
        # Fetch from HuggingFace Hub with retry logic
        try:
            logger.info(f"Fetching metadata for dataset: {dataset_id}")
            
            # Get basic metadata from Hub API
            metadata = self.hf_client.get_dataset_info(dataset_id, config_name)
            
            # Try to enrich with Dataset Viewer API data
            # Use the full dataset ID from the metadata response
            try:
                full_dataset_id = metadata.get('id', dataset_id)
                viewer_data = self.dataset_viewer.get_dataset_information(full_dataset_id, config_name)
                metadata = self._merge_viewer_data(metadata, viewer_data, config_name)
                logger.debug("Successfully enriched metadata with Dataset Viewer API")
            except Exception as e:
                # Log but don't fail if viewer API fails - we still have basic metadata
                logger.warning(f"Failed to fetch Dataset Viewer data, using basic metadata only: {e}")
            
            # Add cache timestamp
            metadata['_cached_at'] = time.time()
            
            # Save to cache (don't fail if caching fails)
            try:
                self._save_to_cache(cache_file, metadata)
            except CacheError as e:
                logger.warning(f"Failed to cache metadata, continuing anyway: {e}")
            
            return metadata
            
        except (DatasetNotFoundError, AuthenticationError, NetworkError):
            # Re-raise these specific errors with context
            log_error_with_context(
                Exception(f"Failed to load dataset info for {dataset_id}"),
                context,
                level=logging.WARNING
            )
            raise
            
        except Exception as e:
            # Unexpected error
            log_error_with_context(e, context)
            raise DatasetServiceError(f"Unexpected error loading dataset info: {str(e)}") from e

    @retry_with_backoff(config=RetryConfig(max_attempts=3, initial_delay=1.0))
    def load_dataset_sample(
        self, 
        dataset_id: str, 
        split: str = "train", 
        num_samples: int = 10,
        config_name: Optional[str] = None,
        streaming: bool = True
    ) -> Dict[str, Any]:
        """
        Load samples from the specified dataset with caching.
        
        Includes automatic retry logic for transient failures and comprehensive
        error handling.
        
        Args:
            dataset_id: HuggingFace dataset identifier
            split: Dataset split to sample from
            num_samples: Number of samples to retrieve
            config_name: Optional configuration name
            streaming: Whether to use streaming mode for large datasets
            
        Returns:
            Dictionary containing sampled data and metadata
            
        Raises:
            DatasetNotFoundError: If dataset or split doesn't exist
            AuthenticationError: If dataset is private and authentication fails
            NetworkError: If network operations fail after retries
            DatasetServiceError: If sampling fails for other reasons
        """
        context = {
            "dataset_id": dataset_id,
            "split": split,
            "num_samples": num_samples,
            "config_name": config_name,
            "operation": "load_dataset_sample"
        }
        
        # For small samples, check cache first
        if num_samples <= 100:  # Only cache small samples
            cache_key = self._get_sample_cache_key(dataset_id, split, num_samples, config_name)
            cache_file = self.sample_cache_dir / f"{cache_key}.json"
            
            cached_data = self._load_from_cache(cache_file)
            if cached_data is not None:
                logger.debug(f"Using cached sample for {dataset_id}")
                return cached_data
        
        try:
            logger.info(f"Loading sample from dataset: {dataset_id}, split: {split}")
            
            # Load dataset with streaming for efficiency
            dataset = load_dataset(
                dataset_id,
                name=config_name,
                split=split,
                streaming=streaming,
                token=self.hf_client.token
            )
            
            # Take the requested number of samples
            if streaming:
                # For streaming datasets, take samples from iterator
                samples = []
                for i, sample in enumerate(dataset):
                    if i >= num_samples:
                        break
                    samples.append(sample)
            else:
                # For non-streaming datasets, use select
                max_samples = min(num_samples, len(dataset))
                samples = dataset.select(range(max_samples))
                samples = [samples[i] for i in range(len(samples))]
            
            # Get dataset info for schema
            dataset_info = self.load_dataset_info(dataset_id, config_name)
            
            # Prepare response
            sample_data = {
                'dataset_id': dataset_id,
                'config_name': config_name,
                'split': split,
                'num_samples': len(samples),
                'requested_samples': num_samples,
                'data': samples,
                'schema': dataset_info.get('features', {}),
                '_sampled_at': time.time()
            }
            
            # Cache small samples (don't fail if caching fails)
            if num_samples <= 100:
                try:
                    self._save_to_cache(cache_file, sample_data)
                except CacheError as e:
                    logger.warning(f"Failed to cache sample, continuing anyway: {e}")
            
            return sample_data
            
        except DatasetNotFoundError:
            # Re-raise as-is
            log_error_with_context(
                Exception(f"Dataset or split not found: {dataset_id}/{split}"),
                context,
                level=logging.WARNING
            )
            raise
            
        except AuthenticationError:
            # Re-raise as-is
            log_error_with_context(
                Exception(f"Authentication failed for dataset: {dataset_id}"),
                context,
                level=logging.WARNING
            )
            raise
            
        except Exception as e:
            log_error_with_context(e, context)
            
            # Try to provide more specific error messages
            error_str = str(e).lower()
            
            if "not found" in error_str or "doesn't exist" in error_str:
                if "split" in error_str or split in error_str:
                    raise DatasetNotFoundError(
                        f"Split '{split}' not found in dataset '{dataset_id}'. "
                        f"Available splits may be different."
                    ) from e
                else:
                    raise DatasetNotFoundError(
                        f"Dataset '{dataset_id}' not found on HuggingFace Hub."
                    ) from e
            
            elif "gated" in error_str or "private" in error_str or "authentication" in error_str:
                raise AuthenticationError(
                    f"Authentication required for dataset '{dataset_id}'. "
                    "Please provide a valid HuggingFace token."
                ) from e
            
            elif "timeout" in error_str or "connection" in error_str:
                raise NetworkError(
                    f"Network error while loading dataset sample: {str(e)}"
                ) from e
            
            else:
                raise DatasetServiceError(
                    f"Failed to load dataset sample: {str(e)}"
                ) from e

    def get_dataset_statistics(
        self,
        dataset_id: str,
        split: str = "train",
        config_name: Optional[str] = None,
        use_cache: bool = True
    ) -> Optional[Dict[str, Any]]:
        """
        Get detailed statistics from Dataset Viewer API with caching.
        
        This method provides comprehensive statistics directly from HuggingFace's
        Dataset Viewer API, which is more efficient and complete than sampling.
        
        Statistics are only available for datasets with builder_name="parquet".
        If statistics are not available, returns None and the caller should fall
        back to sample-based analysis.
        
        Args:
            dataset_id: HuggingFace dataset identifier
            split: Dataset split to get statistics for
            config_name: Optional configuration name
            use_cache: Whether to use cached statistics (default: True)
            
        Returns:
            Dictionary containing statistics or None if unavailable:
            - num_examples: Total number of examples
            - statistics: List of column statistics
            - partial: Whether response is partial
            - _cached_at: Cache timestamp
            
        Raises:
            DatasetServiceError: If the API request fails unexpectedly
        """
        context = {
            "dataset_id": dataset_id,
            "split": split,
            "config_name": config_name,
            "operation": "get_dataset_statistics"
        }
        
        # Check cache first if enabled
        if use_cache:
            cache_key = self._get_statistics_cache_key(dataset_id, split, config_name)
            cache_file = self.statistics_cache_dir / f"{cache_key}.json"
            
            cached_data = self._load_from_cache(cache_file)
            if cached_data is not None:
                logger.debug(f"Using cached statistics for {dataset_id}/{split}")
                return cached_data
        
        try:
            # First, check if statistics are available for this dataset
            logger.info(f"Checking statistics availability for {dataset_id}")
            availability = self._check_statistics_availability(dataset_id, config_name)
            
            if not availability['available']:
                logger.info(
                    f"Statistics not available for {dataset_id}: {availability['reason']}"
                )
                return None
            
            # Determine which config to use
            if config_name is None:
                # Use first available config
                available_configs = availability['configs']
                if not available_configs:
                    logger.warning(f"No configs with statistics found for {dataset_id}")
                    return None
                config_name = available_configs[0]
                logger.info(f"Using config '{config_name}' for statistics")
            elif config_name not in availability['configs']:
                logger.warning(
                    f"Config '{config_name}' does not support statistics. "
                    f"Available configs: {availability['configs']}"
                )
                return None
            
            # Fetch statistics from API
            logger.info(f"Fetching statistics for {dataset_id}/{config_name}/{split}")
            statistics = self.dataset_viewer.get_dataset_statistics(
                dataset_name=availability.get("full_dataset_id", dataset_id),
                config=config_name,
                split_name=split
            )
            
            # Add metadata
            statistics['_cached_at'] = time.time()
            statistics['_config_used'] = config_name
            statistics['_dataset_id'] = dataset_id
            statistics['_split'] = split
            
            # Cache the results
            if use_cache:
                try:
                    self._save_to_cache(cache_file, statistics)
                except CacheError as e:
                    logger.warning(f"Failed to cache statistics: {e}")
            
            logger.info(
                f"Successfully fetched statistics for {dataset_id}: "
                f"{statistics.get('num_examples', 0)} examples, "
                f"{len(statistics.get('statistics', []))} columns"
            )
            
            return statistics
            
        except Exception as e:
            # Log but don't fail - caller can fall back to sampling
            log_error_with_context(e, context, level=logging.WARNING)
            logger.info(
                f"Could not fetch statistics for {dataset_id}, "
                "caller should use sample-based analysis"
            )
            return None

    def get_cached_metadata(self, dataset_id: str, config_name: Optional[str] = None) -> Optional[Dict[str, Any]]:
        """
        Retrieve cached metadata without making API calls.
        
        Args:
            dataset_id: HuggingFace dataset identifier
            config_name: Optional configuration name
            
        Returns:
            Cached metadata dictionary or None if not cached/expired
        """
        cache_key = self._get_cache_key(dataset_id, config_name)
        cache_file = self.metadata_cache_dir / f"{cache_key}.json"
        
        return self._load_from_cache(cache_file)

    def clear_cache(self, dataset_id: Optional[str] = None) -> None:
        """
        Clear cached data for a specific dataset or all datasets.
        
        Args:
            dataset_id: Optional dataset ID to clear. If None, clears all cache.
        """
        try:
            if dataset_id is None:
                # Clear all cache
                for cache_file in self.metadata_cache_dir.glob("*.json"):
                    cache_file.unlink()
                for cache_file in self.sample_cache_dir.glob("*.json"):
                    cache_file.unlink()
                for cache_file in self.statistics_cache_dir.glob("*.json"):
                    cache_file.unlink()
                logger.info("Cleared all cache")
            else:
                # Clear cache for specific dataset
                cache_key = self._get_cache_key(dataset_id)
                
                # Clear metadata cache
                for cache_file in self.metadata_cache_dir.glob(f"{cache_key}*.json"):
                    cache_file.unlink()
                
                # Clear sample cache
                for cache_file in self.sample_cache_dir.glob(f"{cache_key}*.json"):
                    cache_file.unlink()
                
                # Clear statistics cache
                for cache_file in self.statistics_cache_dir.glob(f"{cache_key}*.json"):
                    cache_file.unlink()
                
                logger.info(f"Cleared cache for dataset: {dataset_id}")
                
        except Exception as e:
            logger.warning(f"Failed to clear cache: {e}")
            raise CacheError(f"Failed to clear cache: {e}")

    def get_cache_stats(self) -> Dict[str, Any]:
        """
        Get statistics about the current cache.
        
        Returns:
            Dictionary with cache statistics
        """
        try:
            metadata_files = list(self.metadata_cache_dir.glob("*.json"))
            sample_files = list(self.sample_cache_dir.glob("*.json"))
            statistics_files = list(self.statistics_cache_dir.glob("*.json"))
            
            # Calculate cache sizes
            metadata_size = sum(f.stat().st_size for f in metadata_files)
            sample_size = sum(f.stat().st_size for f in sample_files)
            statistics_size = sum(f.stat().st_size for f in statistics_files)
            
            return {
                'cache_dir': str(self.cache_dir),
                'metadata_files': len(metadata_files),
                'sample_files': len(sample_files),
                'statistics_files': len(statistics_files),
                'total_files': len(metadata_files) + len(sample_files) + len(statistics_files),
                'metadata_size_bytes': metadata_size,
                'sample_size_bytes': sample_size,
                'statistics_size_bytes': statistics_size,
                'total_size_bytes': metadata_size + sample_size + statistics_size,
                'cache_ttl_seconds': self.cache_ttl
            }
        except Exception as e:
            logger.warning(f"Failed to get cache stats: {e}")
            return {'error': str(e)}

    def validate_dataset_access(
        self, 
        dataset_id: str, 
        config_name: Optional[str] = None
    ) -> bool:
        """
        Validate that a dataset can be accessed with current authentication.
        
        Args:
            dataset_id: HuggingFace dataset identifier
            config_name: Optional configuration name
            
        Returns:
            True if dataset is accessible, False otherwise
        """
        return self.hf_client.validate_dataset_access(dataset_id, config_name)

    def _check_statistics_availability(
        self,
        dataset_name: str,
        config_name: Optional[str] = None
    ) -> dict:
        """
        Check if statistics are available for a dataset.

        Statistics are only available for datasets with builder_name="parquet".
        This method checks the dataset information to determine availability.

        Args:
            dataset_name: HuggingFace dataset identifier
            config_name: Optional configuration name

        Returns:
            Dictionary with availability information:
            - available: Boolean indicating if statistics are available
            - configs: List of configs with statistics support
            - reason: Explanation if statistics are not available

        Raises:
            DatasetViewerError: If the API request fails
        """
        try:
            dataset_info = self.load_dataset_info(dataset_name, config_name)
            full_dataset_id = dataset_info.get('id', dataset_name)

            if len(dataset_info["configs"]) == 1:
                # Single config format
                builder_name = dataset_info.get('builder_name', '')
                is_parquet = builder_name == 'parquet'
                configs = [dataset_info["configs"][0]] if is_parquet else [],
                reason = 'Statistics available' if is_parquet else f'Statistics only available for parquet datasets (found: {builder_name})'

            else:
                # Multiple configs format
                if config_name is None:
                    # Take every configs
                    configs = []
                    for cfg_data in dataset_info["config_details"]:
                        if cfg_data.get('builder_name') == 'parquet':
                            configs.append(cfg_data.get("config_name"))
                    is_parquet = len(configs) > 0
                    reason = f'Statistics available for {len(configs)} config(s)' if configs else 'No parquet configs found'

                else:
                    configs = [config_name]
                    builder_name = dataset_info.get('builder_name', '')
                    is_parquet = builder_name == 'parquet'
                    reason = f'Statistics available for provided config {config_name}' if is_parquet else f'No parquet found for config {config_name}'

            return {
                "available": is_parquet,
                "full_dataset_id": full_dataset_id,
                "configs": configs,
                "reason": reason
            }

        except Exception as e:
            error_msg = f"Unexpected error checking statistics availability: {str(e)}"
            logger.error(error_msg)
            raise DatasetServiceError(error_msg) from e

    def search_text_in_dataset(
        self,
        dataset_id: str,
        config_name: str,
        split_name: str,
        query: str,
        offset: int = 0,
        length: int = 50
    ) -> Dict[str, Any]:
        """
        Search for text in text columns of a dataset using the Dataset Viewer API.

        This method delegates to the DatasetViewerAdapter to perform the search.
        Only text columns are searched and only parquet datasets are supported.

        Args:
            dataset_id: HuggingFace dataset identifier
            config_name: Configuration name (required)
            split_name: Split name (required)
            query: Search query (required)
            offset: Offset for pagination (default: 0)
            length: Number of examples to return (default: 50)

        Returns:
            Dictionary containing search results from the Dataset Viewer API

        Raises:
            DatasetNotParquetError: If the dataset is not in parquet format
            NoTextColumnsError: If the dataset has no text columns
            DatasetServiceError: If the search operation fails
        """
        try:
            # Check if dataset is in parquet format and has text columns
            dataset_info = self.load_dataset_info(dataset_id, config_name)

            # Check builder_name for parquet format
            # Also check tags as a fallback since builder_name might not be available
            builder_name = dataset_info.get('builder_name', '')
            tags = dataset_info.get('tags', [])
            is_parquet = builder_name == 'parquet' or 'format:parquet' in tags

            if not is_parquet:
                error_msg = (
                    f"Search is only supported for parquet datasets. "
                    f"Dataset '{dataset_id}' has builder_name='{builder_name}' "
                    f"and tags={tags}. "
                    f"Please use a dataset in parquet format."
                )
                logger.warning(error_msg)
                raise DatasetNotParquetError(error_msg)

            # Check if dataset has text columns
            features = dataset_info.get('features', {})
            if not features:
                error_msg = f"No features found for dataset '{dataset_id}'"
                logger.warning(error_msg)
                raise DatasetServiceError(error_msg)

            # Check for text/string columns
            has_text_columns = False
            for _, feature_info in features.items():
                # Check for various text types
                if isinstance(feature_info, dict):
                    feature_type = feature_info.get('dtype', '')
                elif isinstance(feature_info, str):
                    feature_type = feature_info
                else:
                    continue

                # Check if it's a text column (string, text, or Value with string dtype)
                if any(text_type in str(feature_type).lower() for text_type in ['string', 'text']):
                    has_text_columns = True
                    break

            if not has_text_columns:
                error_msg = (
                    f"No text columns found in dataset '{dataset_id}'. "
                    f"Search requires at least one text/string column. "
                    f"Available features: {list(features.keys())}"
                )
                logger.warning(error_msg)
                raise NoTextColumnsError(error_msg)

            # Perform the search
            return self.dataset_viewer.search_text_in_dataset(
                dataset_name=dataset_id,
                config_name=config_name,
                split_name=split_name,
                query=query,
                offset=offset,
                length=length
            )
        except (DatasetNotParquetError, NoTextColumnsError):
            # Re-raise our custom exceptions
            raise
        except Exception as e:
            error_msg = f"Failed to search in dataset: {str(e)}"
            logger.error(error_msg)
            raise DatasetServiceError(error_msg) from e


def get_dataset_service(hf_api_token: str) -> DatasetService:
    """Get or create the global dataset service instance using current config."""
    config = get_config()
    if hf_api_token is None or len(hf_api_token) == 0:
        hf_api_token = config.hf_token
    dataset_service = DatasetService(
        cache_dir=config.cache_dir,
        token=hf_api_token
    )
    return dataset_service