File size: 35,808 Bytes
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff39df
771baf7
 
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00eec9
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
c8ff505
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8433748
771baf7
 
8433748
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ce55cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771baf7
 
 
 
 
 
 
 
 
 
 
 
c8ff505
771baf7
c00eec9
8433748
3ce55cd
8433748
5798d9f
771baf7
 
 
 
 
 
c8ff505
771baf7
 
 
 
 
 
 
 
8433748
771baf7
 
 
c8ff505
771baf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import re
import time
import logging
import mimetypes
import zipfile
import tempfile
import chardet
from datetime import datetime
from typing import List, Dict, Optional, Union, Tuple
from pathlib import Path
from urllib.parse import urlparse, urljoin
import requests
import validators
import gradio as gr
from diskcache import Cache
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from cleantext import clean
import qrcode
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import tarfile
import gzip
import math

# Setup enhanced logging with more detailed formatting
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('app.log', encoding='utf-8')
    ])
logger = logging.getLogger(__name__)

# Ensure output directories exist with modern structure
OUTPUTS_DIR = Path('output')
QR_CODES_DIR = OUTPUTS_DIR / 'qr_codes'
TEMP_DIR = OUTPUTS_DIR / 'temp'
for directory in [OUTPUTS_DIR, QR_CODES_DIR, TEMP_DIR]:
    directory.mkdir(parents=True, exist_ok=True)

class EnhancedURLProcessor:
    """Advanced URL processing with complete content extraction"""
    def __init__(self):
        self.session = requests.Session()
        self.timeout = 15  # Extended timeout for larger content
        self.max_retries = 3
        self.user_agent = UserAgent()

        # Enhanced headers for better site compatibility
        self.session.headers.update({
            'User-Agent': self.user_agent.random,
            'Accept': '*/*',  # Accept all content types
            'Accept-Language': 'en-US,en;q=0.9',
            'Accept-Encoding': 'gzip, deflate, br',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-User': '?1',
            'DNT': '1'
        })

    def validate_url(self, url: str) -> Dict:
        """Enhanced URL validation with detailed feedback"""
        try:
            if not validators.url(url):
                return {'is_valid': False, 'message': 'Invalid URL format', 'details': 'URL must begin with http:// or https://'}
            parsed = urlparse(url)
            if not all([parsed.scheme, parsed.netloc]):
                return {'is_valid': False, 'message': 'Incomplete URL', 'details': 'Missing scheme or domain'}
            # Try HEAD request first to check accessibility
            try:
                head_response = self.session.head(url, timeout=5)
                head_response.raise_for_status()
            except requests.exceptions.RequestException:
                # If HEAD fails, try GET as some servers don't support HEAD
                response = self.session.get(url, timeout=self.timeout)
                response.raise_for_status()

            return {
                'is_valid': True,
                'message': 'URL is valid and accessible',
                'details': {
                    'content_type': head_response.headers.get('Content-Type', 'unknown'),
                    'server': head_response.headers.get('Server', 'unknown'),
                    'size': head_response.headers.get('Content-Length', 'unknown')
                }
            }
        except Exception as e:
            return {'is_valid': False, 'message': f'URL validation failed: {str(e)}', 'details': str(e)}

    def fetch_content(self, url: str, retry_count: int = 0) -> Optional[Dict]:
        """Enhanced content fetcher with retry mechanism and complete character extraction"""
        try:
            logger.info(f"Fetching content from URL: {url} (Attempt {retry_count + 1}/{self.max_retries})")

            # Update User-Agent randomly for each request
            self.session.headers.update({'User-Agent': self.user_agent.random})

            response = self.session.get(url, timeout=self.timeout)
            response.raise_for_status()

            # Detect encoding
            if response.encoding is None:
                encoding = chardet.detect(response.content)['encoding'] or 'utf-8'
            else:
                encoding = response.encoding
            # Decode content with fallback
            try:
                raw_content = response.content.decode(encoding, errors='replace')
            except (UnicodeDecodeError, LookupError):
                raw_content = response.content.decode('utf-8', errors='replace')

            # Extract metadata
            metadata = {
                'url': url,
                'timestamp': datetime.now().isoformat(),
                'encoding': encoding,
                'content_type': response.headers.get('Content-Type', ''),
                'content_length': len(response.content),
                'headers': dict(response.headers),
                'status_code': response.status_code
            }

            # Process based on content type
            content_type = response.headers.get('Content-Type', '').lower()
            if 'text/html' in content_type:
                processed_content = self._process_html_content(raw_content, url)
            else:
                processed_content = raw_content
            return {
                'content': processed_content,
                'raw_content': raw_content,
                'metadata': metadata
            }
        except requests.exceptions.RequestException as e:
            if retry_count < self.max_retries - 1:
                logger.warning(f"Retry {retry_count + 1}/{self.max_retries} for URL: {url}")
                time.sleep(2 ** retry_count)  # Exponential backoff
                return self.fetch_content(url, retry_count + 1)
            logger.error(f"Failed to fetch content after {self.max_retries} attempts: {e}")
            return None
        except Exception as e:
            logger.error(f"Unexpected error while fetching content: {e}")
            return None

    def _process_html_content(self, content: str, base_url: str) -> str:
        """Process HTML content while preserving all characters"""
        try:
            soup = BeautifulSoup(content, 'html.parser')

            # Convert relative URLs to absolute
            for tag in soup.find_all(['a', 'img', 'link', 'script']):
                for attr in ['href', 'src']:
                    if tag.get(attr):
                        try:
                            tag[attr] = urljoin(base_url, tag[attr])
                        except Exception:
                            pass
            # Extract all text content
            text_parts = []
            for element in soup.stripped_strings:
                text_parts.append(str(element))
            return '\n'.join(text_parts)
        except Exception as e:
            logger.error(f"HTML processing error: {e}")
            return content

class EnhancedFileProcessor:
    """Advanced file processing with complete content extraction"""
    def __init__(self, max_file_size: int = 5 * 1024 * 1024 * 1024):  # 5GB default
        self.max_file_size = max_file_size
        self.supported_extensions = {
            '.txt', '.md', '.csv', '.json', '.xml', '.html', '.htm',
            '.log', '.yml', '.yaml', '.ini', '.conf', '.cfg',
            '.zip', '.tar', '.gz', '.bz2', '.7z', '.rar',
            '.pdf', '.doc', '.docx', '.rtf', '.odt'
        }

    def process_file(self, file) -> List[Dict]:
        """Process uploaded file with enhanced error handling and complete extraction"""
        if not file:
            return []

        dataset = []
        try:
            file_size = os.path.getsize(file.name)
            if file_size > self.max_file_size:
                logger.warning(f"File size ({file_size} bytes) exceeds maximum allowed size")
                return []

            with tempfile.TemporaryDirectory() as temp_dir:
                temp_dir_path = Path(temp_dir)

                # Handle different archive types
                if self._is_archive(file.name):
                    dataset.extend(self._process_archive(file.name, temp_dir_path))
                elif Path(file.name).suffix.lower() in self.supported_extensions:
                    dataset.extend(self._process_single_file(file))
                else:
                    logger.warning(f"Unsupported file type: {file.name}")

        except Exception as e:
            logger.error(f"Error processing file: {str(e)}")
            return []
        return dataset

    def _is_archive(self, filepath: str) -> bool:
        """Check if file is an archive"""
        return any(filepath.lower().endswith(ext) for ext in [
            '.zip', '.tar', '.gz', '.bz2', '.7z', '.rar'
        ])

    def _process_single_file(self, file) -> List[Dict]:
        """Process a single file with enhanced character extraction and JSON handling"""
        try:
            file_stat = os.stat(file.name)
            file_size = file_stat.st_size

            # Initialize content storage
            content_parts = []

            # Process file in chunks for large files
            chunk_size = 10 * 1024 * 1024  # 10MB chunks
            with open(file.name, 'rb') as f:
                while True:
                    chunk = f.read(chunk_size)
                    if not chunk:
                        break

                    # Detect encoding for each chunk
                    encoding = chardet.detect(chunk)['encoding'] or 'utf-8'
                    try:
                        decoded_chunk = chunk.decode(encoding, errors='replace')
                        content_parts.append(decoded_chunk)
                    except (UnicodeDecodeError, LookupError):
                        decoded_chunk = chunk.decode('utf-8', errors='replace')
                        content_parts.append(decoded_chunk)

            # Combine all chunks
            complete_content = ''.join(content_parts)

            # Check if the content is valid JSON regardless of file extension
            try:
                if mimetypes.guess_type(file.name)[0] == 'application/json' or file.name.lower().endswith('.json'):
                    # It's a JSON file by type or extension
                    json_data = json.loads(complete_content)
                    return [{
                        'source': 'json_file',
                        'filename': os.path.basename(file.name),
                        'file_size': file_size,
                        'mime_type': 'application/json',
                        'created': datetime.fromtimestamp(file_stat.st_ctime).isoformat(),
                        'modified': datetime.fromtimestamp(file_stat.st_mtime).isoformat(),
                        'content': json_data,  # Store the parsed JSON object
                        'raw_content': complete_content,  # Store the original JSON string
                        'timestamp': datetime.now().isoformat()
                    }]
                else:
                    # Try to parse as JSON anyway
                    try:
                        json_data = json.loads(complete_content)
                        # If we get here, it's valid JSON despite the extension
                        return [{
                            'source': 'json_content',
                            'filename': os.path.basename(file.name),
                            'file_size': file_size,
                            'mime_type': 'application/json',
                            'created': datetime.fromtimestamp(file_stat.st_ctime).isoformat(),
                            'modified': datetime.fromtimestamp(file_stat.st_mtime).isoformat(),
                            'content': json_data,  # Store the parsed JSON object
                            'raw_content': complete_content,  # Store the original JSON string
                            'timestamp': datetime.now().isoformat()
                        }]
                    except json.JSONDecodeError:
                        logger.warning(f"File {file.name} is not valid JSON.")
            except Exception as e:
                logger.error(f"Error during JSON processing: {e}")

            return [{
                'source': 'file',
                'filename': os.path.basename(file.name),
                'file_size': file_size,
                'mime_type': mimetypes.guess_type(file.name)[0],
                'created': datetime.fromtimestamp(file_stat.st_ctime).isoformat(),
                'modified': datetime.fromtimestamp(file_stat.st_mtime).isoformat(),
                'content': complete_content,
                'timestamp': datetime.now().isoformat()
            }]
        except Exception as e:
            logger.error(f"File processing error: {e}")
            return []

    def _process_archive(self, archive_path: str, extract_to: Path) -> List[Dict]:
        """Process an archive file with enhanced extraction"""
        dataset = []
        try:
            # Handle ZIP archives
            if zipfile.is_zipfile(archive_path):
                with zipfile.ZipFile(archive_path, 'r') as zip_ref:
                    zip_ref.extractall(extract_to)
                    for file_info in zip_ref.infolist():
                        if file_info.file_size > 0 and not file_info.filename.endswith('/'):
                            extracted_path = extract_to / file_info.filename
                            if extracted_path.suffix.lower() in self.supported_extensions:
                                with open(extracted_path, 'rb') as f:
                                    dataset.extend(self._process_single_file(f))
            # Handle TAR archives
            elif archive_path.lower().endswith(('.tar', '.tar.gz', '.tgz')):
                try:
                    with tarfile.open(archive_path, 'r:*') as tar_ref:
                        for member in tar_ref.getmembers():
                            if member.isfile():
                                extracted_path = extract_to / member.name
                                tar_ref.extract(member, path=extract_to)
                                if extracted_path.suffix.lower() in self.supported_extensions:
                                    with open(extracted_path, 'rb') as f:
                                        dataset.extend(self._process_single_file(f))
                except tarfile.TarError as e:
                    logger.error(f"Error processing TAR archive: {e}")
            # Handle GZIP archives (single file)
            elif archive_path.lower().endswith('.gz'):
                extracted_path = extract_to / Path(archive_path).stem
                try:
                    with gzip.open(archive_path, 'rb') as gz_file, open(extracted_path, 'wb') as outfile:
                        outfile.write(gz_file.read())
                    if extracted_path.suffix.lower() in self.supported_extensions:
                        with open(extracted_path, 'rb') as f:
                            dataset.extend(self._process_single_file(f))
                except gzip.GzipFile as e:
                    logger.error(f"Error processing GZIP archive: {e}")
            # TODO: Add support for other archive types (.bz2, .7z, .rar) - may require external libraries
            elif archive_path.lower().endswith(('.bz2', '.7z', '.rar')):
                logger.warning(f"Support for {Path(archive_path).suffix} archives is not yet fully implemented.")

        except Exception as e:
            logger.error(f"Archive processing error: {e}")
        return dataset

    def chunk_data(self, data: Union[Dict, List], max_size: int = 2953) -> List[Dict]:
        """Enhanced data chunking with sequence metadata"""
        try:
            # Convert data to JSON string
            json_str = json.dumps(data, ensure_ascii=False)
            total_length = len(json_str)

            # Calculate overhead for metadata
            metadata_template = {
                "chunk_index": 0,
                "total_chunks": 1,
                "total_length": total_length,
                "chunk_hash": "",
                "data": ""
            }
            overhead = len(json.dumps(metadata_template)) + 20  # Extra padding for safety

            # Calculate effective chunk size
            effective_chunk_size = max_size - overhead

            if total_length <= effective_chunk_size:
                # Data fits in one chunk
                chunk = {
                    "chunk_index": 0,
                    "total_chunks": 1,
                    "total_length": total_length,
                    "chunk_hash": hash(json_str) & 0xFFFFFFFF,  # 32-bit hash
                    "data": json_str
                }
                return [chunk]

            # Calculate number of chunks needed
            num_chunks = -(-total_length // effective_chunk_size)  # Ceiling division
            chunk_size = -(-total_length // num_chunks)  # Even distribution

            chunks = []
            for i in range(num_chunks):
                start_idx = i * chunk_size
                end_idx = min(start_idx + chunk_size, total_length)
                chunk_data = json_str[start_idx:end_idx]

                chunk = {
                    "chunk_index": i,
                    "total_chunks": num_chunks,
                    "total_length": total_length,
                    "chunk_hash": hash(chunk_data) & 0xFFFFFFFF,
                    "data": chunk_data
                }
                chunks.append(chunk)

            return chunks
        except Exception as e:
            logger.error(f"Error chunking data: {e}")
            return []

def generate_stylish_qr(data: Union[str, Dict],
                        filename: str,
                        size: int = 10,
                        border: int = 4,
                        fill_color: str = "#000000",
                        back_color: str = "#FFFFFF") -> str:
    """Generate a stylish QR code with enhanced visual appeal"""
    try:
        qr = qrcode.QRCode(
            version=None,
            error_correction=qrcode.constants.ERROR_CORRECT_M,
            box_size=size,
            border=border
        )

        # Add data to QR code
        if isinstance(data, dict):
            qr.add_data(json.dumps(data, ensure_ascii=False))
        else:
            qr.add_data(data)

        qr.make(fit=True)

        # Create QR code image with custom colors
        qr_image = qr.make_image(fill_color=fill_color, back_color=back_color)

        # Convert to RGBA for transparency support
        qr_image = qr_image.convert('RGBA')

        # Add subtle gradient overlay
        gradient = Image.new('RGBA', qr_image.size, (0, 0, 0, 0))
        draw = ImageDraw.Draw(gradient)
        for i in range(qr_image.width):
            alpha = int(255 * (1 - i/qr_image.width) * 0.1)  # 10% maximum opacity
            draw.line([(i, 0), (i, qr_image.height)], fill=(255, 255, 255, alpha))

        # Combine images
        final_image = Image.alpha_composite(qr_image, gradient)

        # Save the image
        output_path = QR_CODES_DIR / filename
        final_image.save(output_path, quality=95)

        return str(output_path)
    except Exception as e:
        logger.error(f"QR generation error: {e}")
        return ""

def generate_qr_codes(data: Union[str, Dict, List], combined: bool = True) -> List[str]:
    """Generate QR codes with enhanced visual appeal and metadata"""
    try:
        file_processor = EnhancedFileProcessor()
        paths = []

        if combined:
            # Process combined data
            chunks = file_processor.chunk_data(data)
            for i, chunk in enumerate(chunks):
                filename = f'combined_qr_{int(time.time())}_{i+1}_of_{len(chunks)}.png'
                qr_path = generate_stylish_qr(
                    data=chunk,
                    filename=filename,
                    fill_color="#1a365d",  # Deep blue
                    back_color="#ffffff"
                )
                if qr_path:
                    paths.append(qr_path)
        else:
            # Process individual items
            if isinstance(data, list):
                for idx, item in enumerate(data):
                    chunks = file_processor.chunk_data(item)
                    for chunk_idx, chunk in enumerate(chunks):
                        filename = f'item_{idx+1}_chunk_{chunk_idx+1}_of_{len(chunks)}_{int(time.time())}.png'
                        qr_path = generate_stylish_qr(
                            data=chunk,
                            filename=filename,
                            fill_color="#1a365d",  # Deep blue
                            back_color="#ffffff"
                        )
                        if qr_path:
                            paths.append(qr_path)
            else:
                chunks = file_processor.chunk_data(data)
                for i, chunk in enumerate(chunks):
                    filename = f'single_qr_{i+1}_of_{len(chunks)}_{int(time.time())}.png'
                    qr_path = generate_stylish_qr(
                        data=chunk,
                        filename=filename,
                        fill_color="#1a365d",  # Deep blue
                        back_color="#ffffff"
                    )
                    if qr_path:
                        paths.append(qr_path)
                return paths
    except Exception as e:
        logger.error(f"QR code generation error: {e}")
        return []

def create_modern_interface():
    """Create a modern and visually appealing Gradio interface"""

    # Modern CSS styling
    css = """
    /* Modern color scheme */
    :root {
        --primary-color: #1a365d;
        --secondary-color: #2d3748;
        --accent-color: #4299e1;
        --background-color: #f7fafc;
        --success-color: #48bb78;
        --error-color: #f56565;
        --warning-color: #ed8936;
    }
    /* Container styling */
    .container {
        max-width: 1200px;
        margin: auto;
        padding: 2rem;
        background-color: var(--background-color);
        border-radius: 1rem;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }
    /* Component styling */
    .input-container {
        background-color: white;
        padding: 1.5rem;
        border-radius: 0.5rem;
        border: 1px solid #e2e8f0;
        margin-bottom: 1rem;
    }
    /* Button styling */
    .primary-button {
        background-color: var(--primary-color);
        color: white;
        padding: 0.75rem 1.5rem;
        border-radius: 0.375rem;
        border: none;
        cursor: pointer;
        transition: all 0.2s;
    }
    .primary-button:hover {
        background-color: var(--accent-color);
        transform: translateY(-1px);
    }
    /* Status messages */
    .status {
        padding: 1rem;
        border-radius: 0.375rem;
        margin: 1rem 0;
    }
    .status.success { background-color: #f0fff4; color: var(--success-color); }
    .status.error { background-color: #fff5f5; color: var(--error-color); }
    .status.warning { background-color: #fffaf0; color: var(--warning-color); }
    /* Gallery styling */
    .gallery {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
        padding: 1rem;
        background-color: white;
        border-radius: 0.5rem;
        border: 1px solid #e2e8f0;
    }
    .gallery img {
        width: 100%;
        height: auto;
        border-radius: 0.375rem;
        transition: transform 0.2s;
    }
    .gallery img:hover {
        transform: scale(1.05);
    }
    /* QR Code Viewport Styling */
    .viewport-container {
        display: grid;
        gap: 0.5rem;
        padding: 1rem;
        background-color: white;
        border-radius: 0.5rem;
        border: 1px solid #e2e8f0;
        margin-top: 1rem;
    }
    .viewport-item {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .viewport-item img {
        width: 100%;
        height: auto;
        border-radius: 0.375rem;
        transition: transform 0.2s;
        max-width: 150px; /* Adjust as needed */
        max-height: 150px; /* Adjust as needed */
    }
    """
    # Create interface with modern design
    with gr.Blocks(css=css, title="Advanced Data Processor & QR Generator") as interface:
        interface.head += """
        <script>
        let enabledStates = [];

        function updateEnabledStates(checkbox) {
            const index = parseInt(checkbox.dataset.index);
            if (checkbox.checked) {
                if (!enabledStates.includes(index)) {
                    enabledStates.push(index);
                }
            } else {
                enabledStates = enabledStates.filter(item => item !== index);
            }
            // Send the updated state back to Gradio (you might need to adjust the target component)
            const enabled_qr_codes_component = document.querySelector('[data-component-type="state"][data-state-name="enabled_qr_codes"]');
            if (enabled_qr_codes_component) {
                enabled_qr_codes_component.value = JSON.stringify(enabledStates);
                enabled_qr_codes_component.dispatchEvent(new Event('input')); // Trigger update
            }
            console.log("Enabled QR Code Indices:", enabledStates);
            // You might want to trigger an update of the viewport here if needed
        }
        </script>
        """
        qr_code_paths = gr.State([])
        gr.Markdown("""
        # 🌐 Advanced Data Processing & QR Code Generator

        Transform your data into beautifully designed, sequenced QR codes with our cutting-edge processor.
        """)
        with gr.Tab("πŸ“ URL Processing"):
            url_input = gr.Textbox(
                label="Enter URLs (comma or newline separated)",
                lines=5,
                placeholder="https://example1.com\nhttps://example2.com",
                value=""
            )
        with gr.Tab("πŸ“ File Input"):
            file_input = gr.File(
                label="Upload Files",
                file_types=None,  # Accept all file types
                file_count="multiple"
            )
        with gr.Tab("πŸ“‹ JSON Input"):
            text_input = gr.TextArea(
                label="Direct JSON Input",
                lines=15,
                placeholder="Paste your JSON data here...",
                value=""
            )
            with gr.Row():
                example_btn = gr.Button("πŸ“ Load Example", variant="secondary")
                clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
        with gr.Row():
            combine_data = gr.Checkbox(
                label="Combine all data into sequence",
                value=True,
                info="Generate sequential QR codes for combined data"
            )
            process_btn = gr.Button(
                "πŸ”„ Process & Generate QR",
                variant="primary"
            )
        # Output components
        output_json = gr.JSON(label="Processed Data")
        output_gallery = gr.Gallery(
            label="Generated QR Codes",
            columns=3,
            height=400,
            show_label=True
        )
        output_text = gr.Textbox(
            label="Processing Status",
            interactive=False
        )

        with gr.Tab("πŸ–ΌοΈ QR Code Viewport") as viewport_tab:
            viewport_output = gr.HTML(label="QR Code Sequence Viewport")
            enabled_qr_codes = gr.State([]) # To store the enabled/disabled state

        # Load example data
        def load_example():
            example = {
                "type": "product_catalog",
                "items": [
                    {
                        "id": "123",
                        "name": "Premium Widget",
                        "description": "High-quality widget with advanced features",
                        "price": 299.99,
                        "category": "electronics",
                        "tags": ["premium", "featured", "new"]
                    },
                    {
                        "id": "456",
                        "name": "Basic Widget",
                        "description": "Reliable widget for everyday use",
                        "price": 149.99,
                        "category": "electronics",
                        "tags": ["basic", "popular"]
                    }
                ],
                "metadata": {
                    "timestamp": datetime.now().isoformat(),
                    "version": "2.0",
                    "source": "example"
                }
            }
            return json.dumps(example, indent=2)

        def clear_input():
            return ""

        def update_viewport(paths, enabled_states):
            if not paths:
                return "<p>No QR codes generated yet.</p>"

            num_qr_codes = len(paths)
            cols = math.ceil(math.sqrt(num_qr_codes))
            rows = math.ceil(num_qr_codes / cols)

            viewport_html = '<div class="viewport-container" style="grid-template-columns: repeat({}, 1fr);">'.format(cols)

            for i, path in enumerate(paths):
                is_enabled = i in enabled_states
                border = "border: 2px solid green;" if is_enabled else "border: 2px solid lightgray;"
                viewport_html += f'<div class="viewport-item" id="qr_item_{i}">'
                viewport_html += f'<img src="{path}" style="{border}" alt="QR Code {i+1}">'
                viewport_html += f'<input type="checkbox" id="enable_qr_{i}" data-index="{i}" {"checked" if is_enabled else ""} onchange="updateEnabledStates(this)"> Enable'
                viewport_html += '</div>'
            viewport_html += '</div>'

            return viewport_html

        def process_inputs(urls, files, text, combine):
            """Process all inputs and generate QR codes"""
            try:
                results = []
                url_processor = EnhancedURLProcessor()
                file_processor = EnhancedFileProcessor()

                # Process JSON input
                if text and text.strip():
                    try:
                        json_data = json.loads(text)
                        if isinstance(json_data, list):
                            results.extend(json_data)
                        else:
                            results.append(json_data)
                    except json.JSONDecodeError as e:
                        return None, [], f"❌ Invalid JSON format: {str(e)}"

                # Process URLs
                if urls and urls.strip():
                    url_list = re.split(r'[,\n]', urls)
                    url_list = [url.strip() for url in url_list if url.strip()]
                    for url in url_list:
                        validation = url_processor.validate_url(url)
                        if validation['is_valid']:
                            content = url_processor.fetch_content(url)
                            if content:
                                results.append({
                                    'source': 'url',
                                    'url': url,
                                    'content': content,
                                    'timestamp': datetime.now().isoformat()
                                })

                # Process files
                if files:
                    for file in files:
                        file_results = file_processor.process_file(file)
                        if file_results:
                            results.extend(file_results)

                # Generate QR codes
                if results:
                    qr_paths = generate_qr_codes(results, combine)
                    if qr_paths:
                        return (
                            results,
                            [str(path) for path in qr_paths],
                            f"βœ… Successfully processed {len(results)} items and generated {len(qr_paths)} QR codes!"
                        )
                    else:
                        return None, [], "❌ Failed to generate QR codes"
                else:
                    return None, [], "⚠️ No valid content to process"
            except Exception as e:
                logger.error(f"Processing error: {e}")
                return None, [], f"❌ Error: {str(e)}"

        def on_qr_generation(results, qr_paths):
            return qr_paths, qr_paths  # Update state with generated paths

        process_btn.click(
            process_inputs,
            inputs=[url_input, file_input, text_input, combine_data],
            outputs=[output_json, output_gallery, output_text]
        ).then(on_qr_generation, inputs=[output_json, output_gallery], outputs=[qr_code_paths, viewport_output])

        viewport_tab.select(update_viewport, inputs=[qr_code_paths, enabled_qr_codes], outputs=[viewport_output])

        # Add helpful documentation
        gr.Markdown("""
        ### πŸš€ Features
                - **Complete URL Scraping**: Extracts every character from web pages
                - **Advanced File Processing**: Full content extraction from various text-based files and common archives. Supports flexible JSON handling.
                - **Smart JSON Handling**: Processes any size JSON with automatic chunking, either via direct input or file upload.
                - **Sequential QR Codes**: Maintains data integrity across multiple codes
                - **QR Code Viewport**: Visualize generated QR codes in a sequenced square, with options to enable/disable individual codes.
                - **Modern Design**: Clean, responsive interface with visual feedback

                ### πŸ’‘ Tips
                1. **URLs**: Enter multiple URLs separated by commas or newlines
                2. **Files**: Upload any type of file. The processor will attempt to handle supported text-based files, archives (.zip, .tar, .gz), and JSON files.
                3. **JSON**: Use the example button to see the expected format or upload a .json file. The system will also try to detect JSON content in other file types.
                4. **QR Codes**: Choose whether to combine data into sequential codes
                5. **Processing**: Monitor the status for real-time feedback

                ### 🎨 Output
                - Generated QR codes are saved in the `output/qr_codes` directory
                - Each QR code contains metadata for proper sequencing
                - Hover over QR codes in the gallery to see details
                - The **QR Code Viewport** tab displays the generated QR codes in a grid.

                ### βš™οΈ QR Code Viewport Instructions
                1. Navigate to the **QR Code Viewport** tab after generating QR codes.
                2. The generated QR codes will be displayed in a square arrangement.
                3. Use the checkboxes below each QR code to enable or disable it.
                4. The visualization will update to reflect the enabled/disabled state (currently by a green border).
                """)
    return interface

def main():
    """Initialize and launch the application"""
    try:
        # Configure system settings
        mimetypes.init()

        # Create and launch interface
        interface = create_modern_interface()

        # Launch with configuration
        interface.launch(
            share=False,
            debug=False,
            show_error=True,
            show_api=False
        )
    except Exception as e:
        logger.error(f"Application startup error: {e}")
        raise

if __name__ == "__main__":
    main()