File size: 39,523 Bytes
85a12ab
 
 
 
 
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
85a12ab
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
 
 
 
85a12ab
 
 
 
 
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
 
 
 
25a8112
85a12ab
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
85a12ab
 
 
 
9a54213
85a12ab
 
 
 
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
25a8112
 
 
85a12ab
25a8112
 
 
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
 
85a12ab
 
 
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
 
 
 
 
 
 
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
85a12ab
 
 
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
25a8112
 
 
 
 
 
 
 
 
 
 
 
9a54213
25a8112
85a12ab
 
 
 
 
 
 
 
25a8112
85a12ab
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a54213
 
 
 
85a12ab
 
9a54213
 
 
85a12ab
 
 
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
85a12ab
 
 
25a8112
85a12ab
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
85a12ab
 
 
 
 
 
 
 
 
 
9a54213
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a54213
85a12ab
9a54213
 
 
 
 
85a12ab
9a54213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a12ab
 
9a54213
 
85a12ab
 
 
 
 
9a54213
85a12ab
 
 
 
9a54213
85a12ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25a8112
 
 
 
 
 
85a12ab
 
 
 
 
 
 
 
 
 
 
9a54213
25a8112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a54213
 
 
 
 
 
 
 
 
 
25a8112
 
 
 
 
9a54213
 
 
 
 
25a8112
9a54213
 
 
 
25a8112
 
 
 
 
 
 
85a12ab
 
 
9a54213
 
 
 
85a12ab
 
 
 
 
 
 
 
9a54213
 
 
 
85a12ab
 
 
 
 
 
 
9a54213
85a12ab
 
 
 
 
 
 
 
 
 
9a54213
85a12ab
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
import gradio as gr
import numpy as np
import pandas as pd
from datetime import datetime
import random
from collections import defaultdict
import math
import traceback

# ==================== কনফিগারেশন ====================
CONFIG = {
    "HISTORY_LIMIT": 1000,
    "SEQUENCE_LENGTH": 12,
    "PATTERN_DEPTH": 6,
    "PINK_THRESHOLD": 3.0,
    "BIG_PINK_THRESHOLD": 5.0,
    "CONFIDENCE_THRESHOLD": 0.65,
    "ENSEMBLE_WEIGHTS": {
        "neuralNetwork": 0.35,
        "sequencePattern": 0.30,
        "markovChain": 0.20,
        "statisticalModel": 0.15
    }
}

# টাইম উইন্ডো কনফিগারেশন (পিঙ্ক জোন)
TIME_WINDOWS = [
    {"start": 55, "end": 57, "name": "প্রাইম পিঙ্ক", "multiplier": 1.9, "probability": 0.90},
    {"start": 59, "end": 1, "name": "প্রাইম পিঙ্ক", "multiplier": 1.9, "probability": 0.90},
    {"start": 18, "end": 20, "name": "পিঙ্ক জোন", "multiplier": 1.4, "probability": 0.70},
    {"start": 27, "end": 29, "name": "পিঙ্ক জোন", "multiplier": 1.4, "probability": 0.70},
    {"start": 38, "end": 40, "name": "পিঙ্ক জোন", "multiplier": 1.4, "probability": 0.70},
    {"start": 43, "end": 45, "name": "পিঙ্ক জোন", "multiplier": 1.4, "probability": 0.70},
    {"start": 48, "end": 50, "name": "পিঙ্ক জোন", "multiplier": 1.4, "probability": 0.70},
]

# ==================== এমএল মডেল ক্লাস ====================

class NeuralNetwork:
    """নিউরাল নেটওয়ার্ক - প্যাটার্ন রিকগনিশনের জন্য"""
    
    def __init__(self):
        self.weights = {
            'input': np.random.randn(15) * 0.1,
            'hidden': np.random.randn(10) * 0.1,
            'output': np.random.randn(5) * 0.1
        }
        self.performance_history = []
    
    def extract_features(self, history):
        """ডাটা থেকে ফিচার এক্সট্র্যাক্ট করে"""
        recent = history[:12]
        features = []
        
        # 1. সাম্প্রতিক ভ্যালু (লগ স্কেলে)
        for val in recent:
            features.append(math.log(val + 0.1) / math.log(10))
        
        # 2. পরিসংখ্যানিক ফিচার
        mean_val = np.mean(recent) if recent else 1.5
        std_val = np.std(recent) if recent else 0.2
        features.append(mean_val)
        features.append(std_val / (mean_val + 0.1))  # কোএফিশিয়েন্ট অফ ভেরিয়েশন
        
        # 3. ট্রেন্ড
        if len(recent) >= 3:
            trend = (recent[0] - recent[-1]) / len(recent)
            features.append(trend)
        else:
            features.append(0)
        
        # 4. পিঙ্ক রেশিও
        pink_count = sum(1 for v in recent if v >= CONFIG["PINK_THRESHOLD"])
        features.append(pink_count / len(recent) if recent else 0)
        
        # প্যাডিং যদি কম হয়
        while len(features) < 15:
            features.append(0)
        
        return np.array(features[:15])  # ম্যাক্স ১৫টি ফিচার
    
    def predict(self, history):
        """প্রেডিকশন করে"""
        try:
            if len(history) < 5:
                return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'পর্যাপ্ত ডাটা নেই', 'raw_value': 0}
            
            features = self.extract_features(history)
            
            # সিম্পল নিউরাল নেটওয়ার্ক ফরওয়ার্ড পাস
            hidden = np.tanh(np.dot(features, self.weights['input'][:len(features)]))
            output = np.tanh(hidden * np.mean(self.weights['hidden']))
            
            # প্রেডিকশন ক্যালকুলেশন
            prediction = 1.5 + (output * 3.0)
            prediction = max(1.05, min(15.0, prediction))
            
            # কনফিডেন্স ক্যালকুলেশন
            confidence = min(0.9, 0.5 + (len(history) / 200) + abs(output) * 0.2)
            
            analysis = self.generate_analysis(features, output)
            
            return {
                'prediction': float(prediction),
                'confidence': float(confidence),
                'analysis': analysis,
                'raw_value': float(output)
            }
        except Exception as e:
            print(f"NeuralNetwork error: {e}")
            return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'এরর হয়েছে', 'raw_value': 0}
    
    def generate_analysis(self, features, output):
        """অ্যানালাইসিস জেনারেট করে"""
        if output > 0.6:
            return "🧠 শক্তিশালী নিউরাল সিগন্যাল"
        elif output < -0.4:
            return "🧠 দুর্বল নিউরাল সিগন্যাল"
        else:
            return "🧠 নিউট্রাল নিউরাল প্যাটার্ন"


class SequenceAnalyzer:
    """সিকোয়েন্স অ্যানালাইজার - প্যাটার্ন খুঁজে বের করে"""
    
    def __init__(self):
        self.patterns = defaultdict(list)
        self.max_pattern_length = CONFIG["PATTERN_DEPTH"]
    
    def find_patterns(self, history):
        """ইতিহাসে প্যাটার্ন খুঁজে বের করে"""
        patterns = []
        
        for length in range(2, min(self.max_pattern_length, len(history) // 2)):
            for i in range(len(history) - length * 2):
                pattern = history[i:i+length]
                next_seq = history[i+length:i+length*2]
                
                similarity = self.calculate_similarity(pattern, next_seq)
                if similarity > 0.6:
                    patterns.append({
                        'pattern': pattern,
                        'next': next_seq,
                        'similarity': similarity,
                        'length': length
                    })
        
        return patterns
    
    def calculate_similarity(self, seq1, seq2):
        """দুটি সিকোয়েন্সের মিল ক্যালকুলেট করে"""
        if len(seq1) != len(seq2) or len(seq1) == 0:
            return 0
        
        diffs = [abs(seq1[i] - seq2[i]) / (max(seq1[i], seq2[i]) + 0.1) 
                for i in range(len(seq1))]
        avg_diff = np.mean(diffs) if diffs else 1
        return max(0, 1 - avg_diff)
    
    def predict(self, history):
        """প্যাটার্ন-ভিত্তিক প্রেডিকশন"""
        try:
            if len(history) < 4:
                return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'পর্যাপ্ত প্যাটার্ন নেই'}
            
            patterns = self.find_patterns(history)
            
            if not patterns:
                return {
                    'prediction': 1.5,
                    'confidence': 0.4,
                    'analysis': '🔗 কোন প্যাটার্ন মেলেনি'
                }
            
            # সেরা প্যাটার্ন নির্বাচন
            best_pattern = max(patterns, key=lambda p: p['similarity'] * p['length'])
            
            # প্যাটার্ন থেকে প্রেডিকশন
            trend = (best_pattern['pattern'][-1] - best_pattern['pattern'][0]) / len(best_pattern['pattern'])
            prediction = best_pattern['pattern'][-1] + trend
            
            # অ্যাডজাস্টমেন্ট
            prediction = max(1.05, min(12.0, prediction))
            
            confidence = best_pattern['similarity'] * 0.8
            
            analysis = f"🔗 {best_pattern['length']}-দৈর্ঘ্যের প্যাটার্ন ({int(best_pattern['similarity']*100)}% মিল)"
            
            return {
                'prediction': float(prediction),
                'confidence': float(confidence),
                'analysis': analysis
            }
        except Exception as e:
            print(f"SequenceAnalyzer error: {e}")
            return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'প্যাটার্ন অ্যানালাইসিস এরর'}


class MarkovChain:
    """মার্কভ চেইন - স্টেট ট্রানজিশন প্রেডিক্ট করে"""
    
    def __init__(self):
        self.transition_matrix = defaultdict(lambda: defaultdict(float))
        self.states = ['খুব_ছোট', 'ছোট', 'মাঝারি', 'বড়', 'পিঙ্ক']
        
    def discretize(self, value):
        """ভ্যালুকে স্টেটে রূপান্তর করে"""
        if value < 1.3:
            return 'খুব_ছোট'
        elif value < 1.8:
            return 'ছোট'
        elif value < 2.5:
            return 'মাঝারি'
        elif value < CONFIG["PINK_THRESHOLD"]:
            return 'বড়'
        else:
            return 'পিঙ্ক'
    
    def build_model(self, history):
        """ট্রানজিশন ম্যাট্রিক্স বিল্ড করে"""
        self.transition_matrix.clear()
        
        for i in range(len(history) - 1):
            current = self.discretize(history[i])
            next_state = self.discretize(history[i + 1])
            self.transition_matrix[current][next_state] += 1
        
        # নরমালাইজেশন
        for state in self.transition_matrix:
            total = sum(self.transition_matrix[state].values())
            if total > 0:
                for next_state in self.transition_matrix[state]:
                    self.transition_matrix[state][next_state] /= total
    
    def predict(self, history):
        """মার্কভ চেইন থেকে প্রেডিকশন"""
        try:
            if len(history) < 2:
                return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'পর্যাপ্ত ডাটা নেই'}
            
            self.build_model(history)
            
            current_state = self.discretize(history[0])
            next_state_probs = self.transition_matrix.get(current_state, {})
            
            if not next_state_probs:
                # ডিফল্ট প্রোবাবিলিটি
                next_state_probs = {
                    'খুব_ছোট': 0.2,
                    'ছোট': 0.4,
                    'মাঝারি': 0.25,
                    'বড়': 0.1,
                    'পিঙ্ক': 0.05
                }
            
            # স্টেট ভ্যালু ম্যাপিং
            state_values = {
                'খুব_ছোট': 1.15,
                'ছোট': 1.5,
                'মাঝারি': 2.2,
                'বড়': 2.8,
                'পিঙ্ক': 4.5
            }
            
            # ওয়েটেড এভারেজ
            prediction = 0
            total_prob = 0
            for s, prob in next_state_probs.items():
                prediction += state_values.get(s, 1.5) * prob
                total_prob += prob
            
            if total_prob > 0:
                prediction /= total_prob
            else:
                prediction = 1.5
            
            confidence = max(next_state_probs.values()) * 0.9 if next_state_probs else 0.3
            
            most_likely = max(next_state_probs.items(), key=lambda x: x[1])[0] if next_state_probs else 'ছোট'
            
            analysis = f"⛓️ মার্কভ: {current_state}{most_likely} ({int(max(next_state_probs.values())*100)}%)"
            
            return {
                'prediction': float(prediction),
                'confidence': float(confidence),
                'analysis': analysis
            }
        except Exception as e:
            print(f"MarkovChain error: {e}")
            return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'মার্কভ এরর'}


class StatisticalModel:
    """পরিসংখ্যানিক মডেল - গড়, মিডিয়ান, ট্রেন্ড"""
    
    def predict(self, history):
        """পরিসংখ্যানিক প্রেডিকশন"""
        try:
            if len(history) < 3:
                return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'পর্যাপ্ত ডাটা নেই'}
            
            recent = history[:10]
            
            # বিভিন্ন পরিসংখ্যানিক মাপক
            mean_val = np.mean(recent) if recent else 1.5
            median_val = np.median(recent) if recent else 1.5
            std_val = np.std(recent) if recent else 0.2
            
            # ট্রেন্ড
            if len(recent) >= 3:
                x = np.arange(len(recent))
                z = np.polyfit(x, recent, 1)
                trend = z[0]
            else:
                trend = 0
            
            # প্রেডিকশন (মিডিয়ান + ট্রেন্ড)
            prediction = median_val + trend * 2
            
            # স্ট্যান্ডার্ড ডিভিয়েশন অনুযায়ী অ্যাডজাস্টমেন্ট
            if std_val > 1.0:
                prediction += random.uniform(-0.5, 0.5)
            
            prediction = max(1.05, min(10.0, prediction))
            
            # কনফিডেন্স
            confidence = min(0.8, 0.5 + (len(history) / 200) - (std_val / 10))
            
            # অ্যানালাইসিস
            if abs(trend) > 0.2:
                trend_text = "উর্ধ্বমুখী" if trend > 0 else "নিম্নমুখী"
                analysis = f"📊 {trend_text} ট্রেন্ড, মিডিয়ান: {median_val:.2f}x"
            else:
                analysis = f"📊 স্থিতিশীল, গড়: {mean_val:.2f}x"
            
            return {
                'prediction': float(prediction),
                'confidence': float(confidence),
                'analysis': analysis
            }
        except Exception as e:
            print(f"StatisticalModel error: {e}")
            return {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'স্ট্যাটিস্টিক্যাল এরর'}


class EnsemblePredictor:
    """এনসেম্বল মডেল - সব মডেলকে একত্রে ব্যবহার করে"""
    
    def __init__(self):
        self.models = {
            'neuralNetwork': NeuralNetwork(),
            'sequencePattern': SequenceAnalyzer(),
            'markovChain': MarkovChain(),
            'statisticalModel': StatisticalModel()
        }
        self.weights = CONFIG["ENSEMBLE_WEIGHTS"].copy()
        self.pattern_memory = {}
        self.performance = {'overall': []}
    
    def predict(self, history):
        """এনসেম্বল প্রেডিকশন"""
        try:
            if len(history) < 5:
                return self.get_default_prediction()
            
            predictions = {}
            total_weighted = 0
            total_weight = 0
            
            for name, model in self.models.items():
                try:
                    pred = model.predict(history)
                    predictions[name] = pred
                    
                    weight = self.weights.get(name, 0.2) * pred['confidence']
                    total_weighted += pred['prediction'] * weight
                    total_weight += weight
                    
                except Exception as e:
                    print(f"Error in {name}: {e}")
                    predictions[name] = {'prediction': 1.5, 'confidence': 0.3, 'analysis': 'এরর'}
            
            # এনসেম্বল প্রেডিকশন
            ensemble_pred = total_weighted / total_weight if total_weight > 0 else 1.5
            
            # টাইম উইন্ডো এফেক্ট
            time_window = self.get_current_time_window()
            if time_window:
                ensemble_pred *= time_window['multiplier']
                ensemble_pred = min(15.0, ensemble_pred)
            
            # কনফিডেন্স ক্যালকুলেশন
            confidences = [p.get('confidence', 0.3) for p in predictions.values()]
            avg_confidence = np.mean(confidences) if confidences else 0.3
            
            # পিঙ্ক সম্ভাবনা
            pink_prob = self.calculate_pink_probability(history, predictions, time_window)
            is_pink_expected = pink_prob > 0.4
            
            # প্রেডিকশন ইন্টারভ্যাল
            interval = self.calculate_prediction_interval(predictions, ensemble_pred, avg_confidence)
            
            # রিস্ক লেভেল
            risk = self.calculate_risk(interval, avg_confidence)
            
            # অ্যানালাইসিস জেনারেট
            analysis = self.generate_analysis(predictions, time_window, is_pink_expected)
            
            # মডেল ডিটেইলস স্ট্রিং ফরম্যাটে
            model_details_str = self.format_model_details(predictions)
            
            return {
                'prediction': float(ensemble_pred),
                'interval_lower': float(interval['lower']),
                'interval_upper': float(interval['upper']),
                'confidence': float(avg_confidence),
                'risk': risk,
                'is_pink_expected': is_pink_expected,
                'pink_probability': float(pink_prob),
                'analysis': analysis,
                'model_details': model_details_str,
                'time_window': time_window['name'] if time_window else None
            }
        except Exception as e:
            print(f"EnsemblePredictor error: {e}")
            return self.get_default_prediction()
    
    def format_model_details(self, predictions):
        """মডেল ডিটেইলসকে স্ট্রিং ফরম্যাটে কনভার্ট করে"""
        lines = []
        for name, pred in predictions.items():
            model_names = {
                'neuralNetwork': '🧠 নিউরাল',
                'sequencePattern': '🔗 সিকোয়েন্স',
                'markovChain': '⛓️ মার্কভ',
                'statisticalModel': '📊 স্ট্যাট'
            }
            display_name = model_names.get(name, name)
            lines.append(f"{display_name}: {pred.get('prediction', 1.5):.2f}x (কনফি: {int(pred.get('confidence', 0)*100)}%) - {pred.get('analysis', '')[:30]}")
        return "\n".join(lines)
    
    def get_default_prediction(self):
        """ডিফল্ট প্রেডিকশন"""
        return {
            'prediction': 1.5,
            'interval_lower': 1.3,
            'interval_upper': 1.7,
            'confidence': 0.3,
            'risk': 'মাঝারি ⚖️',
            'is_pink_expected': False,
            'pink_probability': 0.1,
            'analysis': '🧠 পর্যাপ্ত ডাটা নেই। কমপক্ষে ৫টি রাউন্ড প্রয়োজন।',
            'model_details': 'কোন মডেল ডাটা নেই',
            'time_window': None
        }
    
    def get_current_time_window(self):
        """বর্তমান টাইম উইন্ডো চেক করে"""
        now = datetime.now()
        minutes = now.minute
        
        for window in TIME_WINDOWS:
            if window['start'] <= window['end']:
                if minutes >= window['start'] and minutes <= window['end']:
                    return window
            else:
                if minutes >= window['start'] or minutes <= window['end']:
                    return window
        return None
    
    def calculate_pink_probability(self, history, predictions, time_window):
        """পিঙ্ক হওয়ার সম্ভাবনা ক্যালকুলেট করে"""
        factors = []
        
        # টাইম উইন্ডো ফ্যাক্টর
        if time_window:
            factors.append(time_window['probability'])
        
        # মডেল কনসেনসাস
        if predictions:
            pink_predictions = sum(1 for p in predictions.values() 
                                  if p.get('prediction', 1.5) >= CONFIG["PINK_THRESHOLD"])
            factors.append(pink_predictions / len(predictions))
        
        # হিস্টোরিক্যাল ফ্যাক্টর
        if history:
            recent_pinks = sum(1 for v in history[:20] if v >= CONFIG["PINK_THRESHOLD"])
            factors.append(min(0.9, recent_pinks / 20))
        
        return np.mean(factors) if factors else 0.2
    
    def calculate_prediction_interval(self, predictions, center, confidence):
        """প্রেডিকশন ইন্টারভ্যাল ক্যালকুলেট করে"""
        pred_values = [p.get('prediction', 1.5) for p in predictions.values()]
        std = np.std(pred_values) if len(pred_values) > 1 else 0.2
        
        spread = std * (2 - confidence)
        spread = max(0.1, min(1.5, spread))
        
        return {
            'lower': max(1.01, center - spread/2),
            'upper': center + spread/2
        }
    
    def calculate_risk(self, interval, confidence):
        """রিস্ক লেভেল ক্যালকুলেট করে"""
        spread = interval['upper'] - interval['lower']
        avg = (interval['lower'] + interval['upper']) / 2
        relative_spread = spread / avg if avg > 0 else 0.5
        
        if relative_spread < 0.2 and confidence > 0.7:
            return "নিম্ন ✅"
        elif relative_spread > 0.5 or confidence < 0.45:
            return "উচ্চ ⚠️"
        else:
            return "মাঝারি ⚖️"
    
    def generate_analysis(self, predictions, time_window, is_pink):
        """অ্যানালাইসিস টেক্সট জেনারেট করে"""
        parts = []
        
        # মডেল অ্যানালাইসিস
        for name, pred in predictions.items():
            if pred.get('confidence', 0) > 0.6:
                parts.append(pred.get('analysis', ''))
        
        # টাইম উইন্ডো
        if time_window:
            parts.append(f"⏰ {time_window['name']}")
        
        # পিঙ্ক
        if is_pink:
            parts.append("🌸 পিঙ্ক সম্ভাবনা")
        
        if not parts:
            return "🧠 মডেল অ্যানালাইসিস চলছে..."
        
        return " | ".join(parts[:3])  # সর্বোচ্চ ৩টি অংশ


# ==================== মেইন অ্যাপ্লিকেশন ====================

class AviatorPredictorApp:
    """মেইন অ্যাপ্লিকেশন ক্লাস"""
    
    def __init__(self):
        self.history = []
        self.predictor = EnsemblePredictor()
        self.last_prediction = None
    
    def add_round(self, multiplier):
        """নতুন রাউন্ড যোগ করে"""
        if multiplier <= 0:
            return False
        
        self.history.insert(0, float(multiplier))
        if len(self.history) > CONFIG["HISTORY_LIMIT"]:
            self.history = self.history[:CONFIG["HISTORY_LIMIT"]]
        
        return True
    
    def reset(self):
        """হিস্ট্রি রিসেট করে"""
        self.history = []
        self.last_prediction = None
        # ২০টি র‍্যান্ডম রাউন্ড জেনারেট করে
        for _ in range(20):
            self.history.append(round(random.uniform(1.0, 3.5), 2))
        self.history.sort(reverse=True)
    
    def get_prediction(self):
        """বর্তমান প্রেডিকশন রিটার্ন করে"""
        if len(self.history) < 3:
            return self.predictor.get_default_prediction()
        
        self.last_prediction = self.predictor.predict(self.history)
        return self.last_prediction
    
    def get_history_table(self):
        """হিস্ট্রি টেবিল রিটার্ন করে (শুধু ডাটা, কোন বুলিয়ান না)"""
        return [[i+1, f"{val:.2f}x"] for i, val in enumerate(self.history[:50])]
    
    def get_time_status(self):
        """টাইম স্ট্যাটাস রিটার্ন করে"""
        now = datetime.now()
        time_str = now.strftime("%H:%M:%S")
        
        current = self.predictor.get_current_time_window()
        
        if current:
            status = f"🌸 {current['name']} একটিভ! 🌸"
            detail = f"বুস্ট: +{int((current['multiplier']-1)*100)}% | {int(current['probability']*100)}% পিঙ্ক সম্ভাবনা"
            css_class = "active-window"
        else:
            # পরবর্তী উইন্ডো খুঁজে বের করা
            next_window = None
            min_minutes = 60
            
            for window in TIME_WINDOWS:
                if window['start'] <= window['end']:
                    if now.minute < window['start']:
                        minutes = window['start'] - now.minute
                    else:
                        minutes = (60 - now.minute) + window['start']
                else:
                    if now.minute >= window['start']:
                        minutes = (60 - now.minute) + window['end']
                    elif now.minute <= window['end']:
                        minutes = 0
                    else:
                        minutes = window['start'] - now.minute
                
                if 0 < minutes < min_minutes:
                    min_minutes = minutes
                    next_window = window
            
            if next_window and min_minutes <= 2:
                status = f"⚠️ {next_window['name']} আসছে {min_minutes} মিনিটে"
                detail = "প্রস্তুত হোন!"
                css_class = "approaching-window"
            else:
                status = "পিঙ্ক জোনের অপেক্ষায়..."
                detail = next_window and f"পরবর্তী: {next_window['name']} {min_minutes} মিনিটে" or "কোন উইন্ডো নেই"
                css_class = "waiting-window"
        
        return f"""
        <div class="time-box {css_class}">
            <div style="font-size: 24px; font-weight: 700;">{time_str}</div>
            <div style="font-size: 16px; font-weight: 600; margin: 5px 0;">{status}</div>
            <div style="font-size: 12px; opacity: 0.9;">{detail}</div>
        </div>
        """
    
    def get_confidence_html(self):
        """কনফিডেন্স বার এইচটিএমএল রিটার্ন করে"""
        if not self.last_prediction:
            conf = 0.3
            is_pink = False
        else:
            conf = self.last_prediction.get('confidence', 0.3)
            is_pink = self.last_prediction.get('is_pink_expected', False)
        
        color_class = "pink-text" if is_pink else ""
        pink_text = "🌸 পিঙ্ক মোড" if is_pink else ""
        
        return f"""
        <div style="margin: 10px 0;">
            <div style="display: flex; justify-content: space-between; margin-bottom: 4px;">
                <span class="{color_class}">এমএল কনফিডেন্স: {int(conf*100)}%</span>
                <span class="{color_class}">{pink_text}</span>
            </div>
            <div class="confidence-bar">
                <div class="confidence-fill" style="width: {int(conf*100)}%;"></div>
            </div>
        </div>
        """


# ==================== কাস্টম সিএসএস ====================
CUSTOM_CSS = """
/* ডার্ক মোড বেস */
.gradio-container {
    background: #0a0a0f !important;
    color: #ffffff !important;
}
footer {visibility: hidden}

/* টাইম বক্স স্টাইল */
.time-box {
    text-align: center;
    padding: 15px;
    border-radius: 10px;
    margin-bottom: 20px;
    transition: all 0.3s;
    font-weight: 500;
}
.active-window {
    background: linear-gradient(135deg, #ff1493, #ff69b4) !important;
    color: white !important;
    box-shadow: 0 0 20px #ff1493;
}
.approaching-window {
    background: linear-gradient(135deg, #ffa726, #ff9800) !important;
    color: white !important;
    box-shadow: 0 0 20px #ffa726;
}
.waiting-window {
    background: linear-gradient(135deg, #1a1a2e, #16213e) !important;
    color: white !important;
    border: 1px solid #00d4ff;
}

/* পিঙ্ক টেক্সট */
.pink-text {
    color: #ff1493 !important;
    font-weight: bold !important;
}

/* কনফিডেন্স বার */
.confidence-bar {
    height: 8px;
    background: #333;
    border-radius: 4px;
    overflow: hidden;
    margin: 5px 0;
}
.confidence-fill {
    height: 100%;
    background: linear-gradient(90deg, #00d4ff, #ff1493);
    transition: width 0.3s;
}

/* প্রেডিকশন ইন্টারভ্যাল */
.prediction-interval {
    font-size: 32px;
    font-weight: 700;
    text-align: center;
    margin: 10px 0;
    padding: 15px;
    background: rgba(255,255,255,0.05);
    border-radius: 10px;
}
.prediction-interval.pink-mode {
    color: #ff1493;
    text-shadow: 0 0 10px #ff1493;
}
.interval-lower {
    color: #00d4ff;
}
.interval-upper {
    color: #ffa726;
}
.interval-separator {
    color: white;
    margin: 0 10px;
}

/* টেক্সটবক্স স্টাইল */
.gr-box {
    border: 1px solid #333 !important;
    background: rgba(255,255,255,0.05) !important;
}
.gr-box label {
    color: #00d4ff !important;
}

/* বাটন স্টাইল */
.gr-button-primary {
    background: linear-gradient(135deg, #00d4ff, #0088ff) !important;
    border: none !important;
}
.gr-button-secondary {
    background: rgba(255,255,255,0.1) !important;
    border: 1px solid #00d4ff !important;
}

/* ডাটাফ্রেম */
.gr-dataframe {
    background: rgba(255,255,255,0.05) !important;
}
"""

# ==================== গ্র্যাডিও ইন্টারফেস ====================

# অ্যাপ ইনিশিয়ালাইজ
app = AviatorPredictorApp()
app.reset()  # শুরুতেই কিছু ডাটা জেনারেট করে

# গ্র্যাডিও ইন্টারফেস
with gr.Blocks(css=CUSTOM_CSS) as demo:
    # হেডার
    gr.HTML("""
    <div style="text-align: center; margin-bottom: 20px;">
        <h1 style="color: #00d4ff; font-size: 48px; margin: 0; text-shadow: 0 0 10px #00d4ff;">✈️ AVOLD ML</h1>
        <p style="color: #888; font-size: 14px;">এনসেম্বল এমএল এভিয়েটর প্রেডিক্টর v3.0</p>
    </div>
    """)
    
    # টাইম সেকশন
    time_html = gr.HTML(value=app.get_time_status())
    
    # ইনপুট রো
    with gr.Row():
        new_multiplier = gr.Number(
            label="নতুন মাল্টিপ্লায়ার",
            value=1.0,
            step=0.1,
            minimum=1.0,
            maximum=100.0
        )
        add_btn = gr.Button("➕ যোগ করুন", variant="primary")
    
    # রিসেট বাটন
    reset_btn = gr.Button("🔄 রিসেট ডাটা", variant="secondary")
    
    # প্রেডিকশন ডিসপ্লে
    with gr.Row():
        with gr.Column():
            gr.Markdown("### 🎯 প্রেডিকশন ইন্টারভ্যাল")
            prediction_html = gr.HTML()
    
    # তিনটি আউটপুট এক লাইনে
    with gr.Row():
        expected_out = gr.Textbox(label="📊 এক্সপেক্টেড", interactive=False)
        analysis_out = gr.Textbox(label="📈 অ্যানালাইসিস", interactive=False)
        decision_out = gr.Textbox(label="🎯 ডিসিশন", interactive=False)
    
    # কনফিডেন্স এবং রিস্ক
    with gr.Row():
        with gr.Column(scale=3):
            confidence_html = gr.HTML(value=app.get_confidence_html())
        with gr.Column(scale=1):
            risk_out = gr.Textbox(label="রিস্ক লেভেল", interactive=False)
    
    # পিঙ্ক প্রোবাবিলিটি
    pink_prob_out = gr.Textbox(label="🌸 পিঙ্ক সম্ভাবনা", interactive=False)
    
    # মডেল ডিটেইলস (টেক্সট বক্স হিসেবে - JSON এরর এড়াতে)
    model_details = gr.Textbox(
        label="🧠 মডেল ডিটেইলস", 
        interactive=False,
        lines=5
    )
    
    # টেবিল
    rounds_table = gr.Dataframe(
        label="📜 শেষ ৫০ রাউন্ড",
        headers=["রাউন্ড", "মাল্টিপ্লায়ার"],
        row_count=10,
        interactive=False
    )
    
    # ---------- আপডেট ফাংশন ----------
    def update_all():
        """সব ডিসপ্লে আপডেট করে - শুধু ডাটা রিটার্ন করে, কোন বুলিয়ান না"""
        try:
            pred = app.get_prediction()
            history_table = app.get_history_table()
            time_status = app.get_time_status()
            confidence = app.get_confidence_html()
            
            # প্রেডিকশন এইচটিএমএল
            pink_class = "pink-mode" if pred.get('is_pink_expected', False) else ""
            prediction_html_str = f"""
            <div class="prediction-interval {pink_class}">
                <span class="interval-lower">{pred.get('interval_lower', 1.3):.2f}x</span>
                <span class="interval-separator">—</span>
                <span class="interval-upper">{pred.get('interval_upper', 1.7):.2f}x</span>
            </div>
            """
            
            # ডিসিশন
            if pred.get('is_pink_expected', False):
                decision = "পিঙ্ক আশান্বিত 🌸"
            elif pred.get('prediction', 1.5) > 3.0:
                decision = "বড় মাল্টিপ্লায়ার 🚀"
            elif pred.get('prediction', 1.5) > 1.8:
                decision = "মাঝারি 💪"
            else:
                decision = "ছোট 🎯"
            
            # পিঙ্ক প্রোবাবিলিটি টেক্সট
            pink_prob_text = f"{int(pred.get('pink_probability', 0)*100)}%"
            
            # মডেল ডিটেইলস
            model_text = pred.get('model_details', 'কোন ডাটা নেই')
            
            return [
                history_table,      # dataframe
                time_status,        # html
                confidence,         # html
                prediction_html_str, # html
                f"{pred.get('prediction', 1.5):.2f}x",  # textbox
                pred.get('analysis', 'অ্যানালাইসিস নেই'),  # textbox
                decision,           # textbox
                pred.get('risk', 'মাঝারি ⚖️'),  # textbox
                pink_prob_text,     # textbox
                model_text          # textbox
            ]
        except Exception as e:
            print(f"Update error: {e}")
            traceback.print_exc()
            # এরর হলে ডিফল্ট ভ্যালু রিটার্ন
            default_table = [[1, "1.00x"]]
            default_time = '<div class="time-box waiting-window"><div style="font-size:24px;">--:--:--</div><div>আপডেট এরর</div></div>'
            default_conf = '<div>এমএল কনফিডেন্স: 0%</div>'
            default_pred = '<div class="prediction-interval"><span class="interval-lower">1.30x</span><span class="interval-separator">—</span><span class="interval-upper">1.70x</span></div>'
            
            return [
                default_table,
                default_time,
                default_conf,
                default_pred,
                "1.50x",
                "আপডেট এরর",
                "ছোট 🎯",
                "মাঝারি ⚖️",
                "0%",
                "মডেল লোড করতে সমস্যা হয়েছে"
            ]
    
    # অ্যাড বাটন ক্লিক
    add_btn.click(
        fn=lambda x: (
            app.add_round(x),  # এইটা True রিটার্ন করে, কিন্তু আমরা ইউজ করছি না
            *update_all()       # শুধু আপডেট ডাটা রিটার্ন করছি
        )[-10:],  # শুধু শেষ ১০টি ভ্যালু নিচ্ছি (ডাটাফ্রেম থেকে শুরু)
        inputs=[new_multiplier],
        outputs=[rounds_table, time_html, confidence_html, prediction_html, 
                 expected_out, analysis_out, decision_out, risk_out, 
                 pink_prob_out, model_details]
    )
    
    # রিসেট বাটন ক্লিক
    reset_btn.click(
        fn=lambda: (
            app.reset(),
            *update_all()
        )[-10:],
        outputs=[rounds_table, time_html, confidence_html, prediction_html,
                 expected_out, analysis_out, decision_out, risk_out,
                 pink_prob_out, model_details]
    )
    
    # ইনিশিয়াল লোড
    demo.load(
        fn=update_all,
        outputs=[rounds_table, time_html, confidence_html, prediction_html,
                 expected_out, analysis_out, decision_out, risk_out,
                 pink_prob_out, model_details]
    )

# ==================== অ্যাপ রান ====================
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )