File size: 34,781 Bytes
6dc8172
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
deb7578
 
6dc8172
 
deb7578
 
 
6dc8172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
 
6dc8172
 
 
 
 
 
 
f3c53e4
 
6dc8172
0602b0f
deb7578
 
 
0602b0f
6dc8172
deb7578
 
0602b0f
 
f3c53e4
deb7578
 
 
 
 
 
 
f3c53e4
 
0602b0f
f3c53e4
deb7578
 
 
 
 
 
 
f3c53e4
 
deb7578
0602b0f
deb7578
 
6dc8172
0602b0f
f3c53e4
deb7578
 
 
 
 
 
 
f3c53e4
 
deb7578
f3c53e4
deb7578
 
0602b0f
 
f3c53e4
deb7578
 
 
 
 
 
f3c53e4
 
deb7578
f3c53e4
deb7578
 
 
0602b0f
 
f3c53e4
deb7578
 
 
 
 
 
 
f3c53e4
 
deb7578
0602b0f
deb7578
 
0602b0f
 
f3c53e4
deb7578
 
 
 
 
 
 
f3c53e4
 
 
 
deb7578
 
6dc8172
 
0602b0f
6dc8172
f3c53e4
6dc8172
 
f3c53e4
 
 
 
 
0602b0f
f3c53e4
 
 
 
 
deb7578
 
 
 
f3c53e4
deb7578
 
f3c53e4
deb7578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
 
deb7578
f3c53e4
deb7578
f3c53e4
deb7578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
deb7578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
 
deb7578
 
 
 
f3c53e4
 
 
 
 
 
 
 
 
 
deb7578
 
 
f3c53e4
 
6dc8172
f3c53e4
6dc8172
f3c53e4
6dc8172
 
 
0602b0f
 
deb7578
0602b0f
 
6dc8172
 
f3c53e4
 
 
 
 
deb7578
 
 
 
6dc8172
 
 
deb7578
6dc8172
 
deb7578
6dc8172
deb7578
 
6dc8172
 
 
 
 
f3c53e4
6dc8172
f3c53e4
6dc8172
 
 
deb7578
6dc8172
deb7578
 
6dc8172
 
 
f3c53e4
6dc8172
 
 
 
 
deb7578
 
 
6dc8172
deb7578
 
6dc8172
 
f3c53e4
6dc8172
 
 
 
 
f3c53e4
0602b0f
 
 
deb7578
 
 
 
f3c53e4
0602b0f
6dc8172
 
0602b0f
6dc8172
 
 
 
 
 
 
 
 
 
 
 
 
0602b0f
f3c53e4
6dc8172
f3c53e4
 
 
 
 
6dc8172
 
0602b0f
6dc8172
 
f3c53e4
 
6dc8172
f3c53e4
 
 
 
 
deb7578
f3c53e4
 
6dc8172
 
f3c53e4
 
6dc8172
f3c53e4
 
 
 
 
 
 
 
6dc8172
 
f3c53e4
6dc8172
deb7578
6dc8172
f3c53e4
 
6dc8172
f3c53e4
 
 
 
6dc8172
 
 
 
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
 
 
f3c53e4
 
6dc8172
 
0602b0f
6dc8172
 
f3c53e4
deb7578
 
f3c53e4
deb7578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c53e4
6dc8172
 
 
f3c53e4
 
 
 
 
 
deb7578
 
 
6dc8172
 
f3c53e4
 
deb7578
f3c53e4
6dc8172
f3c53e4
 
 
 
 
deb7578
 
 
 
f3c53e4
6dc8172
 
 
f3c53e4
6dc8172
f3c53e4
deb7578
6dc8172
f3c53e4
6dc8172
 
f3c53e4
6dc8172
 
f3c53e4
 
 
 
6dc8172
f3c53e4
6dc8172
 
 
f3c53e4
0602b0f
6dc8172
f3c53e4
6dc8172
 
f3c53e4
deb7578
f3c53e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc8172
 
f3c53e4
deb7578
 
 
 
f3c53e4
 
 
6dc8172
f3c53e4
6dc8172
f3c53e4
6dc8172
f3c53e4
 
 
6dc8172
f3c53e4
 
6dc8172
 
f3c53e4
 
 
 
 
 
6dc8172
 
f3c53e4
 
6dc8172
 
0602b0f
f3c53e4
 
6dc8172
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
f3c53e4
 
 
6dc8172
 
0602b0f
 
6dc8172
 
 
 
 
 
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
f3c53e4
 
 
6dc8172
f3c53e4
6dc8172
 
 
f3c53e4
6dc8172
0602b0f
6dc8172
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
 
f3c53e4
 
6dc8172
f3c53e4
 
 
 
 
 
 
 
 
 
 
deb7578
 
 
f3c53e4
 
 
 
 
deb7578
6dc8172
f3c53e4
deb7578
 
 
 
 
 
 
 
f3c53e4
6dc8172
 
f3c53e4
6dc8172
f3c53e4
 
6dc8172
f3c53e4
6dc8172
f3c53e4
 
6dc8172
 
f3c53e4
6dc8172
f3c53e4
6dc8172
f3c53e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc8172
 
f3c53e4
6dc8172
0602b0f
f3c53e4
 
 
 
6dc8172
f3c53e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deb7578
 
f3c53e4
 
 
 
 
 
 
 
 
 
 
0602b0f
deb7578
 
f3c53e4
deb7578
 
0602b0f
deb7578
0602b0f
deb7578
f3c53e4
deb7578
 
 
 
 
 
0602b0f
f3c53e4
 
 
 
 
0602b0f
f3c53e4
6dc8172
0602b0f
 
 
 
 
 
 
6dc8172
 
 
 
 
a0e97e3
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
"""
Tatar2Vec Demo - Interactive Word Embeddings Explorer
Run: streamlit run app.py
"""

import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import os
from typing import List, Dict, Tuple, Optional
import gc
import tempfile
import shutil

# Import for model loading from Hugging Face Hub
from huggingface_hub import snapshot_download, hf_hub_download
from gensim.models import FastText, Word2Vec, KeyedVectors
import gensim

# Page configuration
st.set_page_config(
    page_title="Tatar2Vec Demo",
    page_icon="🏆",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for improved styling
st.markdown("""
<style>
    .main-header {
        font-size: 2.5rem;
        color: #1f77b4;
        text-align: center;
        margin-bottom: 2rem;
    }
    .model-card {
        background-color: #f0f2f6;
        padding: 1.5rem;
        border-radius: 10px;
        border-left: 4px solid #1f77b4;
        margin-bottom: 1rem;
    }
    .metric-card {
        background-color: white;
        padding: 1rem;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        text-align: center;
    }
    .stProgress > div > div > div > div {
        background-color: #1f77b4;
    }
</style>
""", unsafe_allow_html=True)

class Tatar2VecExplorer:
    def __init__(self):
        self.loaded_models = {}
        
        # Model names and their paths in Hugging Face repo
        self.available_models = {
            "Word2Vec": {
                "cbow_100": "w2v_cbow_100",      # CBOW 100-dim
                "sg_100": "w2v_sg_100",          # Skip-gram 100-dim
                "cbow_200": "w2v_cbow_200"       # CBOW 200-dim
            },
            "FastText": {
                "cbow_100": "ft_cbow_100",        # FastText CBOW 100-dim
                "cbow_200": "ft_cbow_200"         # FastText CBOW 200-dim
            }
        }
        
        # Human-readable names for variants
        self.variant_names = {
            "cbow_100": "🥇 CBOW (100-dim) - Best for analogies",
            "sg_100": "🎯 Skip-gram (100-dim) - Better for rare words",
            "cbow_200": "📈 CBOW (200-dim) - Higher dimensionality"
        }
        
        # Model paths in the Hugging Face repository
        self.model_configs = {
            "w2v_cbow_100": {
                "subdir": "word2vec/cbow100",
                "has_main_file": True,
                "main_file": "w2v_cbow_100.model",
                "files": [
                    "w2v_cbow_100.model",
                    "w2v_cbow_100.model.syn1neg.npy",
                    "w2v_cbow_100.model.wv.vectors.npy"
                ],
                "type": "word2vec",
                "dim": 100,
                "description": "Word2Vec CBOW 100-dim - Best for analogies (60% accuracy)",
                "analogy_accuracy": 0.60,
                "semantic_similarity": 0.568,
                "variant": "cbow_100"
            },
            "w2v_cbow_200": {
                "subdir": "word2vec/cbow200",
                "has_main_file": True,
                "main_file": "w2v_cbow_200.model",
                "files": [
                    "w2v_cbow_200.model",
                    "w2v_cbow_200.model.syn1neg.npy",
                    "w2v_cbow_200.model.wv.vectors.npy"
                ],
                "type": "word2vec",
                "dim": 200,
                "description": "Word2Vec CBOW 200-dim - Higher dimensionality, more expressive",
                "analogy_accuracy": None,
                "semantic_similarity": None,
                "variant": "cbow_200"
            },
            "w2v_sg_100": {
                "subdir": "word2vec/sg100",
                "has_main_file": False,  # No main .model file
                "main_file": None,
                "files": [
                    "w2v_sg_100.model.syn1neg.npy",
                    "w2v_sg_100.model.wv.vectors.npy"
                ],
                "type": "word2vec",
                "dim": 100,
                "description": "Word2Vec Skip-gram 100-dim - Better for rare words (only vectors available)",
                "analogy_accuracy": None,
                "semantic_similarity": None,
                "variant": "sg_100",
                "note": "Only word vectors available, full model with training weights not included"
            },
            "ft_cbow_100": {
                "subdir": "fasttext/cbow100",
                "has_main_file": True,
                "main_file": "ft_cbow_100.model",
                "files": [
                    "ft_cbow_100.model",
                    "ft_cbow_100.model.syn1neg.npy",
                    "ft_cbow_100.model.wv.vectors.npy"
                ],
                "type": "fasttext",
                "dim": 100,
                "description": "FastText CBOW 100-dim - Handles morphology, good for OOV words",
                "analogy_accuracy": 0.0,
                "semantic_similarity": 0.582,
                "variant": "cbow_100"
            },
            "ft_cbow_200": {
                "subdir": "fasttext/cbow200",
                "has_main_file": True,
                "main_file": "ft_cbow_200.model",
                "files": [
                    "ft_cbow_200.model",
                    "ft_cbow_200.model.syn1neg.npy",
                    "ft_cbow_200.model.wv.vectors.npy"
                ],
                "type": "fasttext",
                "dim": 200,
                "description": "FastText CBOW 200-dim - Larger FastText model",
                "analogy_accuracy": 0.0,
                "semantic_similarity": None,
                "variant": "cbow_200"
            }
        }

    @st.cache_resource(show_spinner="Loading Tatar2Vec model...")
    def load_model(_self, model_key: str):
        """Load model with caching for better performance"""
        try:
            if model_key not in _self.model_configs:
                st.error(f"Unknown model: {model_key}")
                return None
            
            config = _self.model_configs[model_key]
            repo_id = "TatarNLPWorld/Tatar2Vec"
            
            # Show progress
            progress_bar = st.progress(0)
            status_text = st.empty()
            
            # Create a temporary directory for this model
            temp_dir = tempfile.mkdtemp()
            model_dir = os.path.join(temp_dir, config['subdir'])
            os.makedirs(model_dir, exist_ok=True)
            
            status_text.text(f"Downloading {_self.get_model_display_name(model_key)} from Hugging Face...")
            progress_bar.progress(10)
            
            # Download all required files for the model
            total_files = len(config['files'])
            for i, filename in enumerate(config['files']):
                file_path = os.path.join(config['subdir'], filename)
                status_text.text(f"Downloading {filename}... ({i+1}/{total_files})")
                
                try:
                    # Download the file
                    downloaded_path = hf_hub_download(
                        repo_id=repo_id,
                        filename=file_path,
                        repo_type="model",
                        local_dir=temp_dir,
                        local_dir_use_symlinks=False
                    )
                    
                    # Update progress
                    progress = 10 + (i + 1) * 60 // total_files
                    progress_bar.progress(progress)
                    
                except Exception as e:
                    st.warning(f"Note: {filename} may be downloaded differently: {e}")
                    continue
            
            progress_bar.progress(80)
            status_text.text("Files downloaded, loading model...")
            
            # Load the model based on available files
            try:
                if config['has_main_file'] and config['main_file']:
                    # Full model with main file
                    model_path = os.path.join(temp_dir, config['subdir'], config['main_file'])
                    if os.path.exists(model_path):
                        if config['type'] == "fasttext":
                            model = FastText.load(model_path)
                        else:
                            model = Word2Vec.load(model_path)
                    else:
                        # Try to find any .model file
                        model_files = [f for f in os.listdir(os.path.join(temp_dir, config['subdir'])) 
                                     if f.endswith('.model')]
                        if model_files:
                            model_path = os.path.join(temp_dir, config['subdir'], model_files[0])
                            if config['type'] == "fasttext":
                                model = FastText.load(model_path)
                            else:
                                model = Word2Vec.load(model_path)
                        else:
                            # If no model file, try to load just the vectors
                            status_text.text("Loading word vectors only...")
                            vectors_file = None
                            for file in config['files']:
                                if 'vectors' in file:
                                    vectors_file = os.path.join(temp_dir, config['subdir'], file)
                                    break
                            
                            if vectors_file and os.path.exists(vectors_file):
                                # Create a KeyedVectors instance
                                model = KeyedVectors.load(vectors_file)
                                # Add a dummy train method to maintain compatibility
                                model.train = lambda *args, **kwargs: None
                            else:
                                raise Exception("No model or vectors file found")
                else:
                    # Model with only vectors (like sg100)
                    status_text.text("Loading word vectors only (Skip-gram model)...")
                    vectors_file = None
                    for file in config['files']:
                        if 'vectors' in file:
                            vectors_file = os.path.join(temp_dir, config['subdir'], file)
                            break
                    
                    if vectors_file and os.path.exists(vectors_file):
                        # Create a KeyedVectors instance
                        model = KeyedVectors.load(vectors_file)
                        # Add a dummy train method to maintain compatibility
                        model.train = lambda *args, **kwargs: None
                        # Add warning about limited functionality
                        st.info("⚠️ Skip-gram model loaded in vectors-only mode. Some training features are not available.")
                    else:
                        raise Exception("No vectors file found for Skip-gram model")
                
                progress_bar.progress(100)
                status_text.text(f"✅ Successfully loaded {_self.get_model_display_name(model_key)}!")
                
                # Store temp dir to clean up later if needed
                model._temp_dir = temp_dir
                
                # Clean up progress indicators after 2 seconds
                import time
                time.sleep(2)
                progress_bar.empty()
                status_text.empty()
                
                return model
                
            except Exception as e:
                st.error(f"Error loading model: {str(e)}")
                # Clean up temp dir
                shutil.rmtree(temp_dir, ignore_errors=True)
                return None
                
        except Exception as e:
            st.error(f"Error downloading/loading model: {str(e)}")
            return None
    
    def get_model_display_name(self, model_key: str) -> str:
        """Get human-readable model name"""
        names = {
            "w2v_cbow_100": "🥇 Word2Vec CBOW (100-dim)",
            "w2v_cbow_200": "📈 Word2Vec CBOW (200-dim)",
            "w2v_sg_100": "🎯 Word2Vec Skip-gram (100-dim) [Vectors Only]",
            "ft_cbow_100": "⚡ FastText CBOW (100-dim)",
            "ft_cbow_200": "🚀 FastText CBOW (200-dim)"
        }
        return names.get(model_key, model_key)
    
    def get_model_info(self, model_key: str) -> dict:
        """Get model information"""
        return self.model_configs.get(model_key, {})
    
    def get_variant_name(self, variant_key: str) -> str:
        """Get human-readable variant name"""
        return self.variant_names.get(variant_key, variant_key)
    
    def find_similar_words(self, model, word: str, topn: int = 10):
        """Find semantically similar words"""
        try:
            # Handle both Word2Vec/FastText models and KeyedVectors
            if hasattr(model, 'wv'):
                return model.wv.most_similar(word, topn=topn)
            elif hasattr(model, 'most_similar'):
                return model.most_similar(word, topn=topn)
            else:
                return []
        except KeyError:
            return []
        except Exception as e:
            st.error(f"Error finding similar words: {e}")
            return []
    
    def word_analogy(self, model, positive: List[str], negative: List[str], topn: int = 5):
        """Perform word analogy operation"""
        try:
            if hasattr(model, 'wv'):
                return model.wv.most_similar(positive=positive, negative=negative, topn=topn)
            elif hasattr(model, 'most_similar'):
                return model.most_similar(positive=positive, negative=negative, topn=topn)
            else:
                return []
        except Exception as e:
            st.error(f"Error performing analogy: {e}")
            return []
    
    def get_word_vector(self, model, word: str):
        """Get word vector representation"""
        try:
            if hasattr(model, 'wv'):
                return model.wv[word]
            elif hasattr(model, 'get_vector'):
                return model.get_vector(word)
            elif hasattr(model, '__getitem__'):
                return model[word]
            else:
                return None
        except KeyError:
            return None
    
    def handle_oov_words(self, model, words: List[str]):
        """Handle Out-of-Vocabulary words (FastText only)"""
        results = []
        for word in words:
            try:
                # Check if word is in vocabulary
                in_vocab = False
                if hasattr(model, 'wv'):
                    in_vocab = word in model.wv.key_to_index
                elif hasattr(model, 'key_to_index'):
                    in_vocab = word in model.key_to_index
                elif hasattr(model, 'vocab'):
                    in_vocab = word in model.vocab
                
                similar = self.find_similar_words(model, word, 3) if in_vocab else []
                results.append({
                    'word': word,
                    'in_vocab': in_vocab,
                    'similar_words': similar
                })
            except Exception:
                results.append({
                    'word': word,
                    'in_vocab': False,
                    'similar_words': []
                })
        return results

def create_performance_comparison():
    """Create model performance comparison charts"""
    models = [
        "w2v_cbow_100",
        "ft_cbow_100"
    ]
    
    analogy_scores = [0.60, 0.0]
    semantic_scores = [0.568, 0.582]
    
    # Create subplots
    fig = make_subplots(
        rows=1, cols=2,
        subplot_titles=('Analogy Accuracy', 'Semantic Similarity'),
        specs=[[{"type": "bar"}, {"type": "bar"}]]
    )
    
    # Analogy accuracy
    fig.add_trace(
        go.Bar(
            name='Analogy Accuracy',
            x=['Word2Vec CBOW 100', 'FastText CBOW 100'],
            y=analogy_scores,
            marker_color=['#1f77b4', '#d62728'],
            text=[f"{score*100:.1f}%" if score and score > 0 else "0%" for score in analogy_scores],
            textposition='auto',
        ),
        row=1, col=1
    )
    
    # Semantic similarity
    fig.add_trace(
        go.Bar(
            name='Semantic Similarity',
            x=['Word2Vec CBOW 100', 'FastText CBOW 100'],
            y=semantic_scores,
            marker_color=['#1f77b4', '#d62728'],
            text=[f"{score:.3f}" for score in semantic_scores],
            textposition='auto',
        ),
        row=1, col=2
    )
    
    fig.update_layout(
        title_text="Model Performance Comparison (Best Models)",
        showlegend=False,
        height=400,
        width=800
    )
    
    fig.update_yaxes(range=[0, 0.7], row=1, col=1)
    fig.update_yaxes(range=[0, 0.7], row=1, col=2)
    
    return fig

def main():
    # Application header
    st.markdown('<h1 class="main-header">🏆 Tatar2Vec Demo - Tatar Word Embeddings</h1>', unsafe_allow_html=True)
    
    # Initialize explorer
    explorer = Tatar2VecExplorer()
    
    # Sidebar configuration
    with st.sidebar:
        st.header("⚙️ Model Settings")
        
        # Model type selection
        model_type = st.selectbox(
            "Model Type:",
            ["Word2Vec", "FastText"],
            index=0
        )
        
        st.markdown("---")
        st.subheader("Model Variant:")
        
        # Model variant selection based on type
        if model_type == "Word2Vec":
            # Three variants for Word2Vec
            variant_options = ["cbow_100", "sg_100", "cbow_200"]
            
            selected_variant = st.radio(
                "Select Word2Vec variant:",
                options=variant_options,
                format_func=lambda x: explorer.get_variant_name(x),
                index=0  # Default to CBOW 100
            )
            
            # Show note for Skip-gram
            if selected_variant == "sg_100":
                st.info("ℹ️ Skip-gram model is available in vectors-only mode")
            
        else:  # FastText
            # Two variants for FastText
            variant_options = ["cbow_100", "cbow_200"]
            
            selected_variant = st.radio(
                "Select FastText variant:",
                options=variant_options,
                format_func=lambda x: "⚡ CBOW (100-dim)" if x == "cbow_100" else "🚀 CBOW (200-dim)",
                index=0
            )
        
        # Get model key based on type and variant
        model_key = explorer.available_models[model_type][selected_variant]
        
        # Model information section
        st.markdown("---")
        st.subheader("📊 Model Information")
        model_info = explorer.get_model_info(model_key)
        
        if model_info:
            st.markdown(f"**{explorer.get_model_display_name(model_key)}**")
            st.caption(model_info.get('description', ''))
            
            if 'note' in model_info:
                st.caption(f"*Note: {model_info['note']}*")
            
            col1, col2 = st.columns(2)
            with col1:
                if model_info.get('analogy_accuracy') is not None:
                    acc = model_info['analogy_accuracy']
                    st.metric("Analogy Accuracy", f"{acc*100:.1f}%" if acc and acc > 0 else "N/A")
            
            with col2:
                if model_info.get('semantic_similarity') is not None:
                    sim = model_info['semantic_similarity']
                    st.metric("Semantic Similarity", f"{sim:.3f}" if sim else "N/A")
            
            st.metric("Vector Dimension", model_info.get('dim', 'N/A'))
            
            # Show file info
            file_count = len(model_info.get('files', []))
            st.caption(f"📁 {file_count} file(s) in model")
        
        # Quick search examples
        st.markdown("---")
        st.subheader("🔍 Quick Search")
        quick_words = ["татар", "Казан", "тел", "мәктәп", "китап", "уку", "язу", "бәйрәм"]
        selected_quick = st.selectbox("Example words:", quick_words)
        
        if st.button("Quick Similarity Search", use_container_width=True):
            st.session_state.quick_search = selected_quick
    
    # Main content area with tabs
    tab1, tab2, tab3, tab4 = st.tabs(["🔍 Word Search", "🧠 Analogies", "📊 Analysis", "ℹ️ About"])
    
    with tab1:
        st.header("Similar Word Search")
        
        # Check if we need to activate from quick search
        default_word = getattr(st.session_state, 'quick_search', 'татар')
        
        col1, col2 = st.columns([2, 1])
        
        with col1:
            search_word = st.text_input(
                "Enter Tatar word:",
                value=default_word,
                placeholder="e.g., татар, Казан, тел..."
            )
        
        with col2:
            top_n = st.slider("Number of similar words:", 5, 20, 10)
        
        if st.button("Find Similar Words", type="primary", use_container_width=True):
            if search_word.strip():
                with st.spinner(f"Finding words similar to '{search_word}'..."):
                    model = explorer.load_model(model_key)
                    
                    if model:
                        similar_words = explorer.find_similar_words(model, search_word.strip(), top_n)
                        
                        if similar_words:
                            # Display results in two columns
                            col1, col2 = st.columns([1, 1])
                            
                            with col1:
                                st.subheader("📋 Similar Words")
                                df = pd.DataFrame(similar_words, columns=["Word", "Similarity"])
                                df['Similarity'] = df['Similarity'].round(4)
                                st.dataframe(df, use_container_width=True)
                                
                                # Download button
                                csv = df.to_csv(index=False)
                                st.download_button(
                                    label="📥 Download as CSV",
                                    data=csv,
                                    file_name=f"similar_words_{search_word}.csv",
                                    mime="text/csv"
                                )
                            
                            with col2:
                                st.subheader("📊 Visualization")
                                # Create bar chart
                                fig = px.bar(
                                    df.head(10),
                                    x='Similarity',
                                    y='Word',
                                    orientation='h',
                                    title=f"Top 10 words similar to '{search_word}'",
                                    color='Similarity',
                                    color_continuous_scale='viridis'
                                )
                                fig.update_layout(yaxis={'categoryorder':'total ascending'})
                                st.plotly_chart(fig, use_container_width=True)
                            
                            # Additional information
                            st.subheader("📋 Details")
                            col1, col2, col3 = st.columns(3)
                            
                            with col1:
                                try:
                                    vector = explorer.get_word_vector(model, search_word.strip())
                                    if vector is not None:
                                        st.metric("Vector Dimension", len(vector))
                                except:
                                    pass
                            
                            with col2:
                                st.metric("Similar Words Found", len(similar_words))
                            
                            with col3:
                                if similar_words:
                                    st.metric("Max Similarity", f"{similar_words[0][1]:.4f}")
                        else:
                            st.warning(f"Word '{search_word}' not found in model vocabulary.")
    
    with tab2:
        st.header("Word Analogies")
        
        # Check if model supports analogies (Skip-gram in vectors mode might have limitations)
        if model_key == "w2v_sg_100":
            st.warning("⚠️ Skip-gram model is in vectors-only mode. Analogies might not work perfectly.")
        
        st.info("""
        **Example:** Париж - Франция + Татарстан = Казан?  
        (Paris - France + Tatarstan = Kazan?)
        """)
        
        col1, col2, col3 = st.columns(3)
        
        with col1:
            positive1 = st.text_input("Positive word 1:", "Париж")
            positive2 = st.text_input("Positive word 2:", "Татарстан")
        
        with col2:
            negative = st.text_input("Negative word:", "Франция")
        
        with col3:
            analogy_topn = st.slider("Number of results:", 3, 10, 5)
        
        col1, col2, col3 = st.columns([1, 1, 1])
        with col2:
            analogy_button = st.button("🎯 Perform Analogy", type="primary", use_container_width=True)
        
        if analogy_button:
            if positive1 and positive2 and negative:
                with st.spinner("Performing analogy..."):
                    model = explorer.load_model(model_key)
                    
                    if model:
                        analogy_results = explorer.word_analogy(
                            model,
                            positive=[positive1.strip(), positive2.strip()],
                            negative=[negative.strip()],
                            topn=analogy_topn
                        )
                        
                        if analogy_results:
                            st.subheader("🎯 Analogy Results")
                            
                            df = pd.DataFrame(analogy_results, columns=["Word", "Similarity"])
                            df['Similarity'] = df['Similarity'].round(4)
                            st.dataframe(df, use_container_width=True)
                            
                            # Visualization
                            fig = px.bar(
                                df,
                                x='Similarity',
                                y='Word',
                                orientation='h',
                                title=f"Analogy: {positive1} - {negative} + {positive2}",
                                color='Similarity',
                                color_continuous_scale='viridis'
                            )
                            fig.update_layout(yaxis={'categoryorder':'total ascending'})
                            st.plotly_chart(fig, use_container_width=True)
                        else:
                            st.error("Could not perform analogy. Please check the input words.")
        
        # Predefined analogy examples
        st.subheader("🎪 Example Analogies")
        
        presets = {
            "Capital": ("Мәскәү", "Казан", "Россия", "Moscow - Russia + Tatarstan = ?"),
            "Language": ("татар", "рус", "Татарстан", "Tatar - Tatarstan + Russia = ?"),
            "Profession": ("укытучы", "мәктәп", "университет", "teacher - school + university = ?")
        }
        
        cols = st.columns(len(presets))
        for idx, (name, (p1, p2, n, desc)) in enumerate(presets.items()):
            with cols[idx]:
                if st.button(f"🧩 {name}", key=f"preset_{idx}", use_container_width=True):
                    st.session_state.analogy_p1 = p1
                    st.session_state.analogy_p2 = p2
                    st.session_state.analogy_n = n
                    st.rerun()
    
    with tab3:
        st.header("Model Analysis")
        
        # Performance comparison
        st.subheader("📊 Model Performance Comparison")
        st.markdown("Based on the official Tatar2Vec model card:")
        
        perf_fig = create_performance_comparison()
        st.plotly_chart(perf_fig, use_container_width=True)
        
        # Model comparison table
        st.subheader("📋 Model Specifications")
        
        specs_data = []
        for key, config in explorer.model_configs.items():
            specs_data.append({
                "Model": explorer.get_model_display_name(key),
                "Type": "Word2Vec" if "w2v" in key else "FastText",
                "Dimensions": config['dim'],
                "Files": len(config['files']),
                "Analogy Accuracy": f"{config['analogy_accuracy']*100:.1f}%" if config.get('analogy_accuracy') else "N/A",
                "Semantic Similarity": f"{config['semantic_similarity']:.3f}" if config.get('semantic_similarity') else "N/A"
            })
        
        df_specs = pd.DataFrame(specs_data)
        st.dataframe(df_specs, use_container_width=True)
        
        # OOV words testing (only for FastText)
        st.subheader("🔤 OOV (Out-of-Vocabulary) Testing")
        
        if model_type == "FastText":
            st.info("""
            **FastText models** can handle words not seen during training thanks to subword information.
            """)
        else:
            st.info("""
            **Word2Vec models** cannot generate vectors for OOV words. Only words in vocabulary will show results.
            """)
        
        oov_words = st.text_area(
            "Enter words for OOV testing (one per line):",
            "технологияләштерү\nцифрлаштыру\nвиртуальлаштыру\nмәктәпчә\nбәйрәмнәр"
        )
        
        if st.button("Test OOV Words", type="primary"):
            test_words = [word.strip() for word in oov_words.split('\n') if word.strip()]
            
            with st.spinner("Testing OOV words..."):
                model = explorer.load_model(model_key)
                
                if model:
                    results = explorer.handle_oov_words(model, test_words)
                    
                    st.subheader("OOV Testing Results")
                    
                    for result in results:
                        with st.container():
                            col1, col2 = st.columns([1, 3])
                            
                            with col1:
                                if result['in_vocab']:
                                    st.markdown(f"**{result['word']}** - ✅ In Vocabulary")
                                else:
                                    st.markdown(f"**{result['word']}** - 🆕 OOV Word")
                            
                            with col2:
                                if result['similar_words']:
                                    similar_str = ", ".join([f"{word}({score:.3f})" for word, score in result['similar_words']])
                                    st.write(f"Similar: {similar_str}")
                                else:
                                    st.write("No similar words found")
                            
                            st.divider()
    
    with tab4:
        st.header("ℹ️ About Tatar2Vec")
        
        st.markdown("""
        ## 🏆 Tatar2Vec - Word Embeddings for the Tatar Language
        
        This repository contains a collection of pre-trained word embedding models for the Tatar language, 
        trained on a large Tatar corpus using Word2Vec and FastText.
        
        ### 🎯 Key Features:
        
        - **Large Vocabulary**: 1.29M unique tokens, achieving 100% coverage on the training corpus
        - **Multiple Architectures**: Word2Vec (CBOW, Skip-gram) and FastText (CBOW) with 100 and 200 dimensions
        - **OOV Support**: FastText models handle out-of-vocabulary words using subword information
        - **High Performance**: Word2Vec CBOW 100-dim excels at analogy tasks (60% accuracy)
        
        ### 📊 Model Performance:
        
        | Model | Analogy Accuracy | Semantic Similarity | Best For |
        |-------|-----------------|---------------------|----------|
        | Word2Vec CBOW 100 | 60% | 0.568 | Semantic analogies |
        | FastText CBOW 100 | 0% | 0.582 | Morphological tasks |
        
        ### 📁 Model Files Structure:
        
        - **CBOW models**: 3 files (`.model`, `.syn1neg.npy`, `.wv.vectors.npy`)
        - **Skip-gram model**: 2 files (`.syn1neg.npy`, `.wv.vectors.npy`) - vectors only
        
        ### 📜 Certificate:
        
        This software is registered with Rospatent:
        - **Certificate number**: 2026610619
        - **Registration date**: January 14, 2026
        - **Author**: Mullosharaf K. Arabov
        - **Applicant**: Kazan Federal University
        
        ### 🚀 Usage Example:
        
        ```python
        from huggingface_hub import hf_hub_download
        from gensim.models import Word2Vec, KeyedVectors
        
        # For CBOW models with full model
        model_path = hf_hub_download(
            repo_id="TatarNLPWorld/Tatar2Vec",
            filename="word2vec/cbow100/w2v_cbow_100.model"
        )
        model = Word2Vec.load(model_path)
        
        # For Skip-gram with vectors only
        vectors_path = hf_hub_download(
            repo_id="TatarNLPWorld/Tatar2Vec",
            filename="word2vec/sg100/w2v_sg_100.model.wv.vectors.npy"
        )
        vectors = KeyedVectors.load(vectors_path)
        ```
        
        ### 📝 License:
        
        MIT License
        
        ### 🤝 Citation:
        
        ```bibtex
        @software{tatar2vec_2026,
            title = {Tatar2Vec},
            author = {Arabov, Mullosharaf Kurbonvoich},
            year = {2026},
            publisher = {Kazan Federal University},
            note = {Registered software, Certificate No. 2026610619},
            url = {https://huggingface.co/TatarNLPWorld/Tatar2Vec}
        }
        ```
        """)

if __name__ == "__main__":
    main()