File size: 28,801 Bytes
c70f013
c3b1d58
c70f013
 
7d538d5
c70f013
8cbfc35
7d538d5
ac0ad33
c70f013
ac0ad33
c70f013
ac0ad33
da7f558
c70f013
 
 
f09ec18
c70f013
 
f09ec18
c70f013
ac0ad33
 
c70f013
ac0ad33
c70f013
 
 
 
 
 
da7f558
 
c70f013
8cbfc35
ac0ad33
 
 
 
 
 
d139998
ac0ad33
f09ec18
da7f558
c70f013
 
ac0ad33
 
f09ec18
da7f558
ac0ad33
f09ec18
87efc94
f09ec18
ac0ad33
c70f013
 
ac0ad33
 
 
 
87efc94
f09ec18
 
c70f013
 
f09ec18
d7b800d
c70f013
ac0ad33
da7f558
ac0ad33
 
 
da7f558
f09ec18
ac0ad33
f09ec18
ac0ad33
f09ec18
 
 
 
 
 
 
 
87efc94
 
 
 
 
4aedfb3
c70f013
ac0ad33
9814d50
ac0ad33
f09ec18
c70f013
ac0ad33
da7f558
f09ec18
87efc94
da7f558
f09ec18
c70f013
da7f558
f09ec18
 
 
 
 
da7f558
f09ec18
ac0ad33
f09ec18
87efc94
f09ec18
ac0ad33
f09ec18
ac0ad33
 
 
f09ec18
c70f013
f09ec18
ac0ad33
f09ec18
c70f013
d7b800d
f09ec18
 
ac0ad33
f09ec18
ac0ad33
87efc94
da7f558
c70f013
ac0ad33
87efc94
ac0ad33
c70f013
da7f558
ac0ad33
f09ec18
 
 
 
 
 
c70f013
f09ec18
c70f013
d7b800d
ac0ad33
f09ec18
c70f013
f09ec18
da7f558
f09ec18
 
 
 
 
87efc94
 
 
da7f558
87efc94
 
 
 
da7f558
87efc94
acff31c
f09ec18
 
d7b800d
ac0ad33
f09ec18
87efc94
1538970
c70f013
f09ec18
1538970
87efc94
 
 
a523c40
c70f013
 
f09ec18
87efc94
 
5909e94
c70f013
 
87efc94
c70f013
5909e94
f09ec18
acff31c
5909e94
 
 
 
 
 
 
 
d1233db
 
5909e94
d1233db
c70f013
5909e94
d1233db
ac0ad33
87efc94
 
 
 
a523c40
87efc94
da7f558
505b1a3
87efc94
ac0ad33
da7f558
f09ec18
87efc94
da7f558
87efc94
f09ec18
87efc94
 
 
 
 
da7f558
87efc94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac0ad33
87efc94
505b1a3
6553acb
87efc94
505b1a3
87efc94
505b1a3
1538970
f09ec18
505b1a3
f09ec18
 
505b1a3
 
87efc94
ac0ad33
87efc94
f09ec18
87efc94
 
 
 
 
958af8e
 
 
 
 
 
 
 
 
acff31c
958af8e
505b1a3
 
87efc94
ac0ad33
da7f558
87efc94
da7f558
 
 
 
87efc94
 
 
 
 
 
 
da7f558
 
 
 
 
 
 
 
 
 
 
 
 
87efc94
da7f558
 
87efc94
da7f558
 
ac0ad33
da7f558
 
 
 
 
 
 
 
 
1538970
da7f558
 
87efc94
 
 
da7f558
ac0ad33
 
87efc94
 
 
da7f558
87efc94
ac0ad33
f09ec18
87efc94
 
 
ac0ad33
87efc94
da7f558
ac0ad33
da7f558
ac0ad33
1538970
ac0ad33
c70f013
da7f558
 
 
 
 
 
ac0ad33
c70f013
ac0ad33
87efc94
5fad07a
ac0ad33
 
 
da7f558
87efc94
 
 
 
 
da7f558
ac0ad33
da7f558
ac0ad33
 
da7f558
87efc94
ac0ad33
da7f558
87efc94
 
 
 
 
 
da7f558
87efc94
 
da7f558
ac0ad33
 
87efc94
da7f558
ac0ad33
da7f558
c70f013
87efc94
ac0ad33
c70f013
87efc94
 
c70f013
87efc94
 
da7f558
87efc94
 
 
 
 
ac0ad33
87efc94
ac0ad33
87efc94
 
ac0ad33
 
87efc94
c70f013
87efc94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac0ad33
87efc94
ac0ad33
87efc94
 
ac0ad33
 
87efc94
da7f558
 
c70f013
87efc94
c70f013
 
ac0ad33
 
da7f558
87efc94
c70f013
ac0ad33
 
 
 
 
 
 
 
 
 
 
f09ec18
 
 
 
ac0ad33
 
 
 
 
f09ec18
ac0ad33
 
 
f09ec18
ac0ad33
 
 
f09ec18
 
 
373903f
f09ec18
373903f
f09ec18
 
373903f
f09ec18
 
ac0ad33
 
 
f09ec18
ac0ad33
 
 
 
f09ec18
 
 
ac0ad33
f09ec18
 
87efc94
ac0ad33
 
 
 
f09ec18
ac0ad33
f09ec18
 
 
ac0ad33
 
 
f09ec18
ac0ad33
373903f
c70f013
da7f558
ac0ad33
f09ec18
ac0ad33
 
da7f558
f09ec18
ac0ad33
da7f558
87efc94
 
 
 
 
 
 
da7f558
87efc94
 
 
5909e94
87efc94
 
da7f558
87efc94
 
 
 
 
 
 
da7f558
87efc94
 
 
da7f558
f09ec18
87efc94
 
 
 
 
 
 
 
 
da7f558
87efc94
 
 
 
 
 
 
 
 
 
 
 
373903f
f09ec18
5909e94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87efc94
f09ec18
5909e94
373903f
5909e94
 
87efc94
5909e94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21309b3
5909e94
 
 
 
 
 
 
 
 
 
 
 
 
87efc94
5909e94
 
 
 
 
 
 
 
 
 
 
 
 
 
3a36f7c
5909e94
 
 
da7f558
5909e94
 
 
 
 
da7f558
5909e94
da7f558
5909e94
 
 
 
 
da7f558
5909e94
da7f558
5909e94
 
 
 
da7f558
5909e94
da4162d
c70f013
3a36f7c
 
87efc94
 
3a36f7c
 
 
da7f558
3a36f7c
 
87efc94
 
 
 
3a36f7c
 
87efc94
3a36f7c
 
 
da7f558
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
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

# 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 * 1024 *1024):
        self.max_file_size = max_file_size
        self.supported_extensions = {
            '.txt', '.md', '.csv', '.json', '.xml', '.html', '.html', 
            '.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))
                else:
                    dataset.extend(self._process_single_file(file))

        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"""
        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)

            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:
            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))
            elif tarfile.is_tarfile(archive_path):
                with tarfile.open(archive_path, 'r') as tar_ref:
                    tar_ref.extractall(extract_to)
                    for member in tar_ref.getmembers():
                        if member.isfile():
                            extracted_path = extract_to / member.name
                            if extracted_path.suffix.lower() in self.supported_extensions:
                                with open(extracted_path, 'rb') as f:
                                    dataset.extend(self._process_single_file(f))
        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]:
        try:
            # Convert data to JSON bytes
            json_str = json.dumps(data, ensure_ascii=False)
            json_bytes = json_str.encode('utf-8')
            total_length = len(json_bytes)
    
            # Calculate metadata overhead in bytes
            metadata_template = {
                "chunk_index": 0,
                "total_chunks": 1,
                "total_length": total_length,
                "chunk_hash": "",
                "data": ""
            }
            overhead_bytes = len(json.dumps(metadata_template).encode('utf-8')) + 20  # Add padding
    
            effective_chunk_size = max_size - overhead_bytes
    
            if effective_chunk_size <= 0:
                raise ValueError("Max size is too small after accounting for metadata overhead")
    
            chunks = []
            start = 0
            while start < total_length:
                end = start + effective_chunk_size
                # Ensure valid Unicode by decoding
                chunk_str = json_bytes[start:end].decode('utf-8', errors='replace')
                chunk = {
                    "chunk_index": len(chunks),
                    "total_chunks": -1,  # To be set later
                    "total_length": total_length,
                    "chunk_hash": hash(chunk_str) & 0xFFFFFFFF,
                    "data": chunk_str
                }
                chunks.append(chunk)
                start = end
    
            # Update total_chunks in each chunk
            for i, chunk in enumerate(chunks):
                chunk["total_chunks"] = len(chunks)
    
            return chunks
        except Exception as e:
            logger.error(f"Error chunking data: {{e}}")
            return []
            
            # 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_L,
            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);
    }
    """

    # Create interface with modern design
    with gr.Blocks(css=css, title="Advanced Data Processor & QR Generator") as interface:
        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=["*"],  # Allow 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
        )

        # 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 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)}"

        # Set up event handlers
        example_btn.click(load_example, outputs=[text_input])
        clear_btn.click(clear_input, outputs=[text_input])
        process_btn.click(
            process_inputs,
            inputs=[url_input, file_input, text_input, combine_data],
            outputs=[output_json, output_gallery, output_text]
        )

        # Add helpful documentation
        gr.Markdown("""
        ### πŸš€ Features
        
        - **Complete URL Scraping**: Extracts every character from web pages
        - **Advanced File Processing**: Full content extraction from text files and archives
        - **Smart JSON Handling**: Processes any size JSON with automatic chunking
        - **Sequential QR Codes**: Maintains data integrity across multiple codes
        - **Modern Design**: Clean, responsive interface with visual feedback
        
        ### πŸ’‘ Tips
        
        1. **URLs**: Enter multiple URLs separated by commas or newlines
        2. **Files**: Upload text files or ZIP archives containing text files
        3. **JSON**: Use the example button to see the expected format
        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
        """)

    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()