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 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# reverted to code v29

import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import PyPDF2
import docx
from docx import Document
import io
import tempfile
import os
from typing import Optional, Tuple
import logging
import spaces
import time

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Import IndicProcessor
from IndicTransToolkit.processor import IndicProcessor

# Authentication credentials from environment variables
VALID_USERNAME = os.getenv("USERNAME", "admin")
VALID_PASSWORD = os.getenv("PASSWORD", "password123")

# Session management
authenticated_sessions = set()

def authenticate(username: str, password: str) -> tuple:
    """Authenticate user credentials and return session info"""
    if username == VALID_USERNAME and password == VALID_PASSWORD:
        session_id = f"session_{int(time.time())}_{hash(username)}"
        authenticated_sessions.add(session_id)
        logger.info(f"Successful login for user: {username}")
        return True, session_id
    else:
        logger.warning(f"Failed login attempt for user: {username}")
        return False, None

def is_authenticated(session_id: str) -> bool:
    """Check if session is authenticated"""
    return session_id in authenticated_sessions

def logout_session(session_id: str):
    """Remove session from authenticated sessions"""
    if session_id in authenticated_sessions:
        authenticated_sessions.remove(session_id)
        logger.info(f"Session logged out: {session_id}")

class IndicTrans2Translator:
    def __init__(self):
        self.en_indic_model = None
        self.en_indic_tokenizer = None
        self.indic_en_model = None
        self.indic_en_tokenizer = None
        self.ip = None
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.load_models()
    
    def load_models(self):
        """Load the IndicTrans2 models and tokenizers optimized for HuggingFace Spaces GPU"""
        try:
            logger.info("Loading IndicTrans2 models with HF Spaces GPU optimizations...")
            
            # Verify CUDA is available
            if torch.cuda.is_available():
                logger.info(f"CUDA available: {torch.cuda.is_available()}")
                logger.info(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
                logger.info(f"CUDA device count: {torch.cuda.device_count()}")
            else:
                logger.warning("CUDA not available, using CPU")
            
            # Initialize IndicProcessor
            self.ip = IndicProcessor(inference=True)
            logger.info("IndicProcessor loaded successfully!")
            
            # Check if accelerate is available for device_map
            try:
                import accelerate
                use_device_map = True
                logger.info("Accelerate available, using device_map for optimal GPU utilization")
            except ImportError:
                use_device_map = False
                logger.info("Accelerate not available, using manual device placement")
            
            # Load English to Indic model with HF Spaces optimizations
            logger.info("Loading English to Indic model...")
            self.en_indic_tokenizer = AutoTokenizer.from_pretrained(
                "ai4bharat/indictrans2-en-indic-1B", 
                trust_remote_code=True
            )
            
            # Use bfloat16 for better performance on modern GPUs (A10G, A100, etc.)
            # Fall back to float16 if bfloat16 is not supported
            if torch.cuda.is_available():
                try:
                    # Check if GPU supports bfloat16
                    torch_dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
                    logger.info(f"Using {torch_dtype} precision for optimal GPU performance")
                except:
                    torch_dtype = torch.float16
                    logger.info("Using float16 precision")
            else:
                torch_dtype = torch.float32
                logger.info("Using float32 precision for CPU")
            
            # Load model with or without device_map based on accelerate availability
            if use_device_map and torch.cuda.is_available():
                self.en_indic_model = AutoModelForSeq2SeqLM.from_pretrained(
                    "ai4bharat/indictrans2-en-indic-1B", 
                    trust_remote_code=True,
                    torch_dtype=torch_dtype,
                    low_cpu_mem_usage=True,
                    device_map="auto"  # Automatically distribute model across available GPUs
                )
            else:
                self.en_indic_model = AutoModelForSeq2SeqLM.from_pretrained(
                    "ai4bharat/indictrans2-en-indic-1B", 
                    trust_remote_code=True,
                    torch_dtype=torch_dtype,
                    low_cpu_mem_usage=True
                )
                self.en_indic_model = self.en_indic_model.to(self.device)
            
            self.en_indic_model.eval()
            
            # Load Indic to English model
            logger.info("Loading Indic to English model...")
            self.indic_en_tokenizer = AutoTokenizer.from_pretrained(
                "ai4bharat/indictrans2-indic-en-1B", 
                trust_remote_code=True
            )
            
            if use_device_map and torch.cuda.is_available():
                self.indic_en_model = AutoModelForSeq2SeqLM.from_pretrained(
                    "ai4bharat/indictrans2-indic-en-1B", 
                    trust_remote_code=True,
                    torch_dtype=torch_dtype,
                    low_cpu_mem_usage=True,
                    device_map="auto"
                )
            else:
                self.indic_en_model = AutoModelForSeq2SeqLM.from_pretrained(
                    "ai4bharat/indictrans2-indic-en-1B", 
                    trust_remote_code=True,
                    torch_dtype=torch_dtype,
                    low_cpu_mem_usage=True
                )
                self.indic_en_model = self.indic_en_model.to(self.device)
            
            self.indic_en_model.eval()
            
            # Optimize models for inference
            if torch.cuda.is_available():
                # Enable cuDNN benchmark for consistent input sizes
                torch.backends.cudnn.benchmark = True
                
                # Compile models for faster inference (PyTorch 2.0+)
                try:
                    if not use_device_map:  # Only compile if not using device_map (can conflict)
                        self.en_indic_model = torch.compile(self.en_indic_model, mode="reduce-overhead")
                        self.indic_en_model = torch.compile(self.indic_en_model, mode="reduce-overhead")
                        logger.info("Models compiled with torch.compile for faster inference")
                    else:
                        logger.info("Skipping torch.compile (using device_map)")
                except Exception as e:
                    logger.info(f"torch.compile not available or failed: {e}")
            
            logger.info("Models loaded successfully with HF Spaces optimizations!")
            
            # Log GPU memory usage
            if torch.cuda.is_available():
                memory_allocated = torch.cuda.memory_allocated(0) / 1024**3  # GB
                memory_reserved = torch.cuda.memory_reserved(0) / 1024**3   # GB
                logger.info(f"GPU Memory - Allocated: {memory_allocated:.2f}GB, Reserved: {memory_reserved:.2f}GB")
                
        except Exception as e:
            logger.error(f"Error loading models: {str(e)}")
            raise e
    
    def split_into_sentences(self, text: str) -> list:
        """Split text into sentences while preserving paragraph structure"""
        import re
        
        # Split by paragraphs first (double newlines or more)
        paragraphs = re.split(r'\n\s*\n', text)
        
        sentence_list = []
        paragraph_markers = []
        
        for para_idx, paragraph in enumerate(paragraphs):
            if not paragraph.strip():
                continue
                
            # Split paragraph into sentences using basic sentence endings
            sentences = re.split(r'(?<=[.!?])\s+', paragraph.strip())
            
            for sent_idx, sentence in enumerate(sentences):
                if sentence.strip():
                    sentence_list.append(sentence.strip())
                    # Mark if this is the last sentence in a paragraph
                    is_para_end = (sent_idx == len(sentences) - 1)
                    is_last_para = (para_idx == len(paragraphs) - 1)
                    paragraph_markers.append({
                        'is_paragraph_end': is_para_end and not is_last_para,
                        'original_sentence': sentence.strip()
                    })
        
        return sentence_list, paragraph_markers

    def reconstruct_formatting(self, translated_sentences: list, paragraph_markers: list) -> str:
        """Reconstruct text with original paragraph formatting"""
        if len(translated_sentences) != len(paragraph_markers):
            # Fallback: join with single spaces if lengths don't match
            return ' '.join(translated_sentences)
        
        result = []
        for i, (translation, marker) in enumerate(zip(translated_sentences, paragraph_markers)):
            result.append(translation)
            
            # Add paragraph break if this sentence ended a paragraph
            if marker['is_paragraph_end']:
                result.append('\n\n')
            # Add space between sentences within same paragraph
            elif i < len(translated_sentences) - 1:
                result.append(' ')
        
        return ''.join(result)

    @spaces.GPU
    def translate_text(self, text: str, source_lang: str, target_lang: str) -> str:
        """Translate text from source language to target language while preserving formatting"""
        try:
            # Get proper language-script codes
            source_lang_code = LANGUAGE_SCRIPT_MAPPING.get(source_lang)
            target_lang_code = LANGUAGE_SCRIPT_MAPPING.get(target_lang)
            
            if not source_lang_code or not target_lang_code:
                return f"Unsupported language: {source_lang} or {target_lang}"
            
            # Check if source and target are the same
            if source_lang == target_lang:
                return text  # Return original text if same language
            
            # Debug logging
            logger.info(f"Translating from {source_lang} ({source_lang_code}) to {target_lang} ({target_lang_code})")
            
            # Check if input is single sentence or multiple paragraphs
            if '\n' not in text and len(text.split('.')) <= 2:
                # Simple single sentence - translate directly
                input_sentences = [text.strip()]
                paragraph_markers = None
            else:
                # Complex text - preserve formatting
                input_sentences, paragraph_markers = self.split_into_sentences(text)
                if not input_sentences:
                    return "No valid text found to translate."
            
            # Determine which models to use based on source and target languages
            if source_lang == "en" and target_lang != "en":
                # English to Indic translation
                tokenizer = self.en_indic_tokenizer
                model = self.en_indic_model
                
            elif source_lang != "en" and target_lang == "en":
                # Indic to English translation
                tokenizer = self.indic_en_tokenizer
                model = self.indic_en_model
                
            elif source_lang != "en" and target_lang != "en":
                # Indic to Indic translation (via English as intermediate)
                logger.info(f"Performing Indic-to-Indic translation via English: {source_lang} -> English -> {target_lang}")
                
                # Step 1: Translate from source Indic language to English
                intermediate_text = self.translate_via_english(input_sentences, source_lang, "en", paragraph_markers)
                
                # Step 2: Translate from English to target Indic language
                if paragraph_markers:
                    # Re-split the intermediate text to maintain structure
                    intermediate_sentences, intermediate_markers = self.split_into_sentences(intermediate_text)
                    final_text = self.translate_via_english(intermediate_sentences, "en", target_lang, intermediate_markers)
                else:
                    final_text = self.translate_via_english([intermediate_text], "en", target_lang, None)
                
                return final_text
                
            else:
                # This shouldn't happen, but just in case
                return "Translation configuration error."
            
            # Direct translation (English <-> Indic)
            return self.perform_direct_translation(input_sentences, source_lang_code, target_lang_code, 
                                                 tokenizer, model, paragraph_markers)
            
        except Exception as e:
            logger.error(f"Translation error: {str(e)}")
            import traceback
            traceback.print_exc()
            return f"Error during translation: {str(e)}"
    
    def translate_via_english(self, input_sentences: list, source_lang: str, target_lang: str, paragraph_markers: list) -> str:
        """Helper method to translate via English intermediate step"""
        source_lang_code = LANGUAGE_SCRIPT_MAPPING.get(source_lang)
        target_lang_code = LANGUAGE_SCRIPT_MAPPING.get(target_lang)
        
        if source_lang == "en":
            # English to Indic
            tokenizer = self.en_indic_tokenizer
            model = self.en_indic_model
        else:
            # Indic to English
            tokenizer = self.indic_en_tokenizer
            model = self.indic_en_model
            
        return self.perform_direct_translation(input_sentences, source_lang_code, target_lang_code,
                                             tokenizer, model, paragraph_markers)
    
    def perform_direct_translation(self, input_sentences: list, source_lang_code: str, target_lang_code: str,
                                 tokenizer, model, paragraph_markers: list) -> str:
        """Perform the actual translation using the specified model optimized for HF Spaces GPU"""
        # Balanced batch size for optimal GPU utilization
        batch_size = 4  # Optimal for most HF Spaces GPU configurations
        
        # For very long sentences, reduce batch size
        avg_sentence_length = sum(len(s.split()) for s in input_sentences) / len(input_sentences) if input_sentences else 0
        if avg_sentence_length > 100:
            batch_size = 2
        elif avg_sentence_length > 200:
            batch_size = 1
        
        logger.info(f"Using batch size {batch_size} for average sentence length {avg_sentence_length:.1f} words")
        
        all_translations = []
        
        for i in range(0, len(input_sentences), batch_size):
            batch_sentences = input_sentences[i:i + batch_size]
            
            try:
                # Preprocess the batch using IndicProcessor
                batch = self.ip.preprocess_batch(
                    batch_sentences, 
                    src_lang=source_lang_code, 
                    tgt_lang=target_lang_code
                )
                
                # Tokenize with optimal settings for GPU
                inputs = tokenizer(
                    batch,
                    truncation=True,
                    padding="longest",
                    max_length=256,  # Keep reasonable max length
                    return_tensors="pt"
                ).to(self.device)
                
                # Generate translation with optimized parameters
                with torch.no_grad():
                    # Use torch.inference_mode() for better performance
                    with torch.inference_mode():
                        outputs = model.generate(
                            **inputs,
                            do_sample=False,  # Greedy decoding is faster
                            max_length=256,
                            num_beams=1,  # Greedy search for speed
                            use_cache=True,  # Enable cache for better speed
                            pad_token_id=tokenizer.pad_token_id,
                            eos_token_id=tokenizer.eos_token_id
                        )
                
                # Decode the generated tokens
                generated_tokens = tokenizer.batch_decode(
                    outputs, 
                    skip_special_tokens=True, 
                    clean_up_tokenization_spaces=True
                )
                
                # Postprocess the translations using IndicProcessor
                batch_translations = self.ip.postprocess_batch(generated_tokens, lang=target_lang_code)
                all_translations.extend(batch_translations)
                
                # Progress logging for large documents
                if len(input_sentences) > 20:
                    progress = min(100, int(((i + batch_size) / len(input_sentences)) * 100))
                    logger.info(f"Translation progress: {progress}% ({i + len(batch_sentences)}/{len(input_sentences)} sentences)")
                
            except Exception as e:
                logger.error(f"Translation error in batch {i//batch_size + 1}: {str(e)}")
                
                # Fallback: try single sentences with more conservative settings
                for single_sentence in batch_sentences:
                    try:
                        single_batch = self.ip.preprocess_batch(
                            [single_sentence], 
                            src_lang=source_lang_code, 
                            tgt_lang=target_lang_code
                        )
                        
                        inputs = tokenizer(
                            single_batch,
                            truncation=True,
                            padding=False,
                            max_length=256,
                            return_tensors="pt"
                        ).to(self.device)
                        
                        with torch.no_grad():
                            with torch.inference_mode():
                                outputs = model.generate(
                                    **inputs,
                                    do_sample=False,
                                    max_length=256,
                                    num_beams=1,
                                    use_cache=True
                                )
                        
                        generated_tokens = tokenizer.batch_decode(
                            outputs, 
                            skip_special_tokens=True, 
                            clean_up_tokenization_spaces=True
                        )
                        
                        single_translations = self.ip.postprocess_batch(generated_tokens, lang=target_lang_code)
                        all_translations.extend(single_translations)
                        
                    except Exception as single_e:
                        logger.error(f"Failed to translate sentence: {str(single_e)}")
                        all_translations.append(f"[Translation failed: {single_sentence[:50]}...]")
        
        # Reconstruct formatting if we have paragraph structure
        if paragraph_markers and len(all_translations) == len(paragraph_markers):
            final_translation = self.reconstruct_formatting(all_translations, paragraph_markers)
        else:
            # Simple join if no paragraph structure or mismatch
            final_translation = ' '.join(all_translations) if all_translations else "Translation failed"
        
        return final_translation

# Language mappings with proper IndicTrans2 language codes
LANGUAGES = {
    "English": "en",
    "Assamese": "asm",
    "Bengali": "ben", 
    "Bodo": "brx",
    "Dogri": "doi",
    "Gujarati": "guj",
    "Hindi": "hin",
    "Kannada": "kan",
    "Kashmiri": "kas",
    "Konkani": "gom",
    "Maithili": "mai",
    "Malayalam": "mal",
    "Manipuri": "mni",
    "Marathi": "mar",
    "Nepali": "nep",
    "Oriya": "ory",
    "Punjabi": "pan",
    "Sanskrit": "san",
    "Santali": "sat",
    "Sindhi": "snd",
    "Tamil": "tam",
    "Telugu": "tel",
    "Urdu": "urd"
}

# Language-script mapping with proper IndicTrans2 codes
LANGUAGE_SCRIPT_MAPPING = {
    "en": "eng_Latn",
    "asm": "asm_Beng",
    "ben": "ben_Beng",
    "brx": "brx_Deva",
    "doi": "doi_Deva", 
    "guj": "guj_Gujr",
    "hin": "hin_Deva",
    "kan": "kan_Knda",
    "kas": "kas_Arab",
    "gom": "gom_Deva",
    "mai": "mai_Deva",
    "mal": "mal_Mlym",
    "mni": "mni_Beng",
    "mar": "mar_Deva",
    "nep": "nep_Deva",
    "ory": "ory_Orya",
    "pan": "pan_Guru",
    "san": "san_Deva",
    "sat": "sat_Olck",
    "snd": "snd_Arab",
    "tam": "tam_Taml",
    "tel": "tel_Telu",
    "urd": "urd_Arab"
}

def extract_text_from_pdf(file_path: str) -> str:
    """Extract text from PDF file while preserving paragraph structure"""
    try:
        with open(file_path, 'rb') as file:
            pdf_reader = PyPDF2.PdfReader(file)
            paragraphs = []
            
            for page in pdf_reader.pages:
                page_text = page.extract_text()
                if page_text.strip():
                    # Split by double newlines and clean up
                    page_paragraphs = [p.strip() for p in page_text.split('\n\n') if p.strip()]
                    paragraphs.extend(page_paragraphs)
            
            # Join paragraphs with double newlines to preserve structure
            return '\n\n'.join(paragraphs)
    except Exception as e:
        logger.error(f"Error extracting text from PDF: {str(e)}")
        return f"Error reading PDF: {str(e)}"

def extract_text_from_docx(file_path: str) -> Tuple[str, list]:
    """Extract text from DOCX file while preserving paragraph structure and formatting info"""
    try:
        doc = Document(file_path)
        paragraphs = []
        formatting_info = []
        
        for para in doc.paragraphs:
            text = para.text.strip()
            if text:  # Only add non-empty paragraphs
                paragraphs.append(text)
                
                # Store paragraph formatting information
                para_format = {
                    'alignment': para.alignment,
                    'left_indent': para.paragraph_format.left_indent,
                    'right_indent': para.paragraph_format.right_indent,
                    'first_line_indent': para.paragraph_format.first_line_indent,
                    'space_before': para.paragraph_format.space_before,
                    'space_after': para.paragraph_format.space_after,
                    'line_spacing': para.paragraph_format.line_spacing,
                    'runs': []
                }
                
                # Store run-level formatting (font, size, bold, italic, etc.)
                for run in para.runs:
                    if run.text.strip():  # Only store formatting for non-empty runs
                        run_format = {
                            'text': run.text,
                            'bold': run.bold,
                            'italic': run.italic,
                            'underline': run.underline,
                            'font_name': run.font.name,
                            'font_size': run.font.size,
                            'font_color': None,
                            'highlight_color': None
                        }
                        
                        # Try to get font color
                        try:
                            if run.font.color and run.font.color.rgb:
                                run_format['font_color'] = run.font.color.rgb
                        except:
                            pass
                            
                        # Try to get highlight color
                        try:
                            if run.font.highlight_color:
                                run_format['highlight_color'] = run.font.highlight_color
                        except:
                            pass
                            
                        para_format['runs'].append(run_format)
                
                formatting_info.append(para_format)
        
        # Join paragraphs with double newlines to preserve structure
        text = '\n\n'.join(paragraphs)
        return text, formatting_info
        
    except Exception as e:
        logger.error(f"Error extracting text from DOCX: {str(e)}")
        return f"Error reading DOCX: {str(e)}", []

def create_formatted_docx(translated_paragraphs: list, formatting_info: list, filename: str) -> str:
    """Create a DOCX file with translated text while preserving original formatting"""
    try:
        doc = Document()
        
        # Remove the default paragraph that gets created
        if doc.paragraphs:
            p = doc.paragraphs[0]
            p._element.getparent().remove(p._element)
        
        for i, (para_text, para_format) in enumerate(zip(translated_paragraphs, formatting_info)):
            if not para_text.strip():
                continue
                
            # Create new paragraph
            paragraph = doc.add_paragraph()
            
            # Apply paragraph-level formatting
            try:
                if para_format.get('alignment') is not None:
                    paragraph.alignment = para_format['alignment']
                if para_format.get('left_indent') is not None:
                    paragraph.paragraph_format.left_indent = para_format['left_indent']
                if para_format.get('right_indent') is not None:
                    paragraph.paragraph_format.right_indent = para_format['right_indent']
                if para_format.get('first_line_indent') is not None:
                    paragraph.paragraph_format.first_line_indent = para_format['first_line_indent']
                if para_format.get('space_before') is not None:
                    paragraph.paragraph_format.space_before = para_format['space_before']
                if para_format.get('space_after') is not None:
                    paragraph.paragraph_format.space_after = para_format['space_after']
                if para_format.get('line_spacing') is not None:
                    paragraph.paragraph_format.line_spacing = para_format['line_spacing']
            except Exception as e:
                logger.warning(f"Could not apply some paragraph formatting: {e}")
            
            # Handle run-level formatting
            runs_info = para_format.get('runs', [])
            
            if runs_info:
                # Determine dominant formatting
                total_runs = len(runs_info)
                bold_count = sum(1 for r in runs_info if r.get('bold'))
                italic_count = sum(1 for r in runs_info if r.get('italic'))
                underline_count = sum(1 for r in runs_info if r.get('underline'))
                
                # Get the most common font info
                font_names = [r.get('font_name') for r in runs_info if r.get('font_name')]
                font_sizes = [r.get('font_size') for r in runs_info if r.get('font_size')]
                font_colors = [r.get('font_color') for r in runs_info if r.get('font_color')]
                
                # Apply formatting to the translated text
                run = paragraph.add_run(para_text)
                
                # Apply dominant formatting
                try:
                    if bold_count > total_runs / 2:
                        run.bold = True
                    if italic_count > total_runs / 2:
                        run.italic = True
                    if underline_count > total_runs / 2:
                        run.underline = True
                        
                    # Apply most common font settings
                    if font_names:
                        run.font.name = max(set(font_names), key=font_names.count)
                    if font_sizes:
                        run.font.size = max(set(font_sizes), key=font_sizes.count)
                    if font_colors:
                        run.font.color.rgb = max(set(font_colors), key=font_colors.count)
                except Exception as e:
                    logger.warning(f"Could not apply some formatting: {e}")
                    
            else:
                # No run formatting info, just add the text
                paragraph.add_run(para_text)
        
        doc.save(filename)
        return filename
        
    except Exception as e:
        logger.error(f"Error creating formatted DOCX: {str(e)}")
        # Fallback to simple version
        return create_docx_with_text('\n\n'.join(translated_paragraphs), filename)

def create_docx_with_text(text: str, filename: str) -> str:
    """Create a DOCX file with the given text, preserving paragraph formatting (fallback method)"""
    try:
        doc = Document()
        
        # Split text by double newlines to preserve paragraph structure
        paragraphs = text.split('\n\n')
        
        for para_text in paragraphs:
            if para_text.strip():  # Only add non-empty paragraphs
                # Clean up any single newlines within paragraphs and replace with spaces
                cleaned_text = para_text.replace('\n', ' ').strip()
                doc.add_paragraph(cleaned_text)
        
        doc.save(filename)
        return filename
    except Exception as e:
        logger.error(f"Error creating DOCX: {str(e)}")
        return None

@spaces.GPU
def translate_text_input(text: str, source_lang: str, target_lang: str, session_id: str = "") -> str:
    """Handle text input translation"""
    if not is_authenticated(session_id):
        return "❌ Please log in to use this feature."
        
    if not text.strip():
        return "Please enter some text to translate."
    
    source_code = LANGUAGES.get(source_lang)
    target_code = LANGUAGES.get(target_lang)
    
    if not source_code or not target_code:
        return "Invalid language selection."
    
    # Allow same language (will return original text)
    # No need to check if source_code == target_code
    
    return translator.translate_text(text, source_code, target_code)

@spaces.GPU
def translate_document(file, source_lang: str, target_lang: str, session_id: str = "") -> Tuple[Optional[str], str]:
    """Handle document translation while preserving original formatting"""
    if not is_authenticated(session_id):
        return None, "❌ Please log in to use this feature."
        
    if file is None:
        return None, "Please upload a document."
    
    source_code = LANGUAGES.get(source_lang)
    target_code = LANGUAGES.get(target_lang)
    
    if not source_code or not target_code:
        return None, "Invalid language selection."
    
    # Start timing the translation
    start_time = time.time()
    
    try:
        # Get file extension
        file_extension = os.path.splitext(file.name)[1].lower()
        formatting_info = None
        
        logger.info(f"Starting document translation: {source_lang}{target_lang}")
        
        # Extract text based on file type
        if file_extension == '.pdf':
            text = extract_text_from_pdf(file.name)
        elif file_extension == '.docx':
            text, formatting_info = extract_text_from_docx(file.name)
        else:
            return None, "Unsupported file format. Please upload PDF or DOCX files only."
        
        if text.startswith("Error"):
            return None, text
        
        # Log document stats
        word_count = len(text.split())
        char_count = len(text)
        logger.info(f"Document stats: {word_count} words, {char_count} characters")
        
        # Translate the text
        translate_start = time.time()
        translated_text = translator.translate_text(text, source_code, target_code)
        translate_end = time.time()
        
        translate_duration = translate_end - translate_start
        logger.info(f"Core translation took: {translate_duration:.2f} seconds")
        
        # Create output file
        output_filename = f"translated_{os.path.splitext(os.path.basename(file.name))[0]}.docx"
        output_path = os.path.join(tempfile.gettempdir(), output_filename)
        
        # Create formatted output if we have formatting info
        if formatting_info and file_extension == '.docx':
            # Split translated text back into paragraphs
            translated_paragraphs = translated_text.split('\n\n')
            
            # Ensure we have the right number of paragraphs
            if len(translated_paragraphs) == len(formatting_info):
                create_formatted_docx(translated_paragraphs, formatting_info, output_path)
            else:
                logger.warning(f"Paragraph count mismatch: {len(translated_paragraphs)} vs {len(formatting_info)}, using fallback")
                create_docx_with_text(translated_text, output_path)
        else:
            # Fallback to regular formatting
            create_docx_with_text(translated_text, output_path)
        
        # Calculate total time
        end_time = time.time()
        total_duration = end_time - start_time
        
        # Format time display
        minutes = int(total_duration // 60)
        seconds = int(total_duration % 60)
        
        # Create detailed status message
        if minutes > 0:
            time_str = f"{minutes}m {seconds}s"
        else:
            time_str = f"{seconds}s"
        
        # Calculate translation speed (words per minute)
        if word_count > 0 and total_duration > 0:
            words_per_minute = int((word_count / total_duration) * 60)
            speed_info = f" • Speed: {words_per_minute} words/min"
        else:
            speed_info = ""
        
        # Determine translation type for status
        if source_code == target_code:
            translation_type = "Document processed"
        elif source_code == "en" or target_code == "en":
            translation_type = "Direct translation"
        else:
            translation_type = "Indic-to-Indic translation (via English)"
        
        status_message = (
            f"✅ Translation completed successfully!\n"
            f"⏱️ Time taken: {time_str}\n"
            f"📄 Document: {word_count} words, {char_count} characters\n"
            f"🔄 Type: {translation_type}{speed_info}\n"
            f"📁 Original formatting preserved in output file."
        )
        
        logger.info(f"Document translation completed in {total_duration:.2f} seconds ({time_str})")
        
        return output_path, status_message
    
    except Exception as e:
        end_time = time.time()
        total_duration = end_time - start_time
        minutes = int(total_duration // 60)
        seconds = int(total_duration % 60)
        time_str = f"{minutes}m {seconds}s" if minutes > 0 else f"{seconds}s"
        
        logger.error(f"Document translation error after {time_str}: {str(e)}")
        return None, f"❌ Error during document translation (after {time_str}): {str(e)}"

# Initialize translator
print("Initializing IndicTrans2 Translator with IndicTransToolkit...")
translator = IndicTrans2Translator()

# Create the app with proper authentication
with gr.Blocks(title="IndicTrans2 Translator", theme=gr.themes.Soft()) as demo:
    # Session state
    session_state = gr.State("")
    
    # Login interface (visible by default)
    with gr.Column(visible=True) as login_column:
        gr.Markdown("""
        # 🔐 IndicTrans2 Translator - Authentication Required
        
        Please enter your credentials to access the translation tool.
        """)
        
        with gr.Row():
            with gr.Column(scale=1):
                pass  # Empty column for centering
            
            with gr.Column(scale=2):
                with gr.Group():
                    gr.Markdown("### Login")
                    username_input = gr.Textbox(
                        label="Username", 
                        placeholder="Enter username",
                        type="text"
                    )
                    password_input = gr.Textbox(
                        label="Password", 
                        placeholder="Enter password",
                        type="password"
                    )
                    login_btn = gr.Button("Login", variant="primary", size="lg")
                    login_status = gr.Markdown("")
            
            with gr.Column(scale=1):
                pass  # Empty column for centering
        
        gr.Markdown("""
        ---
        
        **For Administrators:**
        - Set environment secrets `USERNAME` and `PASSWORD` to configure credentials
        - Secrets are encrypted and secure in HuggingFace Spaces
        
        **Features:**
        - 🔒 Secure authentication system
        - 🌍 Support for 22+ Indian languages
        - 📄 Document translation with formatting preservation
        - 🔥 High-quality translation using IndicTrans2 models
        """)
    
    # Main translator interface (hidden by default)
    with gr.Column(visible=False) as main_column:
        gr.Markdown("""
        # IndicTrans2 Translation Tool
        
        Translate text between English and Indian languages using the IndicTrans2 1B model with IndicTransToolkit for optimal quality.
        """)
        
        with gr.Tabs():
            # Text Translation Tab
            with gr.TabItem("Text Translation"):
                with gr.Row():
                    with gr.Column():
                        text_input = gr.Textbox(
                            label="Input Text",
                            placeholder="Enter text to translate...",
                            lines=5
                        )
                        with gr.Row():
                            source_lang_text = gr.Dropdown(
                                choices=list(LANGUAGES.keys()),
                                label="Source Language",
                                value="English"
                            )
                            target_lang_text = gr.Dropdown(
                                choices=list(LANGUAGES.keys()),
                                label="Target Language",
                                value="Hindi"
                            )
                        translate_text_btn = gr.Button("Translate Text", variant="primary")
                    
                    with gr.Column():
                        text_output = gr.Textbox(
                            label="Translated Text",
                            lines=5,
                            interactive=False
                        )
            
            # Document Translation Tab
            with gr.TabItem("Document Translation"):
                with gr.Row():
                    with gr.Column():
                        file_input = gr.File(
                            label="Upload Document",
                            file_types=[".pdf", ".docx"],
                            type="filepath"
                        )
                        with gr.Row():
                            source_lang_doc = gr.Dropdown(
                                choices=list(LANGUAGES.keys()),
                                label="Source Language",
                                value="English"
                            )
                            target_lang_doc = gr.Dropdown(
                                choices=list(LANGUAGES.keys()),
                                label="Target Language",
                                value="Hindi"
                            )
                        translate_doc_btn = gr.Button("Translate Document", variant="primary")
                    
                    with gr.Column():
                        doc_status = gr.Textbox(
                            label="Status",
                            interactive=False
                        )
                        doc_output = gr.File(
                            label="Download Translated Document"
                        )
        
        # Examples
        gr.Examples(
            examples=[
                ["Hello, how are you?", "English", "Hindi"],
                ["This is a test sentence for translation.", "English", "Bengali"],
                ["Machine learning is changing the world.", "English", "Tamil"],
                ["नमस्ते, आप कैसे हैं?", "Hindi", "English"],
                ["আমি ভালো আছি।", "Bengali", "Hindi"],
                ["मला खूप आनंद झाला।", "Marathi", "Tamil"],
                ["ನಾನು ಚೆನ್ನಾಗಿದ್ದೇನೆ।", "Kannada", "Telugu"]
            ],
            inputs=[text_input, source_lang_text, target_lang_text],
            outputs=[text_output],
            fn=lambda text, src, tgt: translate_text_input(text, src, tgt, ""),
            cache_examples=False
        )
        
        # Logout functionality
        with gr.Row():
            logout_btn = gr.Button("🔓 Logout", variant="secondary", size="sm")
    
    def handle_login(username, password):
        success, session_id = authenticate(username, password)
        if success:
            return (
                gr.Markdown("✅ **Login successful!** Welcome to the translator."),
                gr.Column(visible=False),
                gr.Column(visible=True),
                session_id
            )
        else:
            return (
                gr.Markdown("❌ **Invalid credentials.** Please try again."),
                gr.Column(visible=True),
                gr.Column(visible=False),
                ""
            )
    
    def handle_logout(session_id):
        if session_id:
            logout_session(session_id)
        return (
            gr.Column(visible=True),
            gr.Column(visible=False),
            "",
            gr.Textbox(value=""),
            gr.Textbox(value=""),
            gr.Markdown("🔓 **Logged out successfully.** Please login again.")
        )
    
    # Event handlers
    login_btn.click(
        fn=handle_login,
        inputs=[username_input, password_input],
        outputs=[login_status, login_column, main_column, session_state]
    )
    
    logout_btn.click(
        fn=handle_logout,
        inputs=[session_state],
        outputs=[login_column, main_column, session_state, username_input, password_input, login_status]
    )
    
    translate_text_btn.click(
        fn=lambda text, src, tgt, session: translate_text_input(text, src, tgt, session),
        inputs=[text_input, source_lang_text, target_lang_text, session_state],
        outputs=[text_output]
    )
    
    translate_doc_btn.click(
        fn=lambda file, src, tgt, session: translate_document(file, src, tgt, session),
        inputs=[file_input, source_lang_doc, target_lang_doc, session_state],
        outputs=[doc_output, doc_status]
    )

print("IndicTrans2 Translator with Authentication initialized successfully!")

# Launch the app
if __name__ == "__main__":
    demo.launch(share=True)