File size: 33,085 Bytes
b5bd1aa
26a5842
 
c5987cc
58b97f2
b91d93b
a750190
9aeacca
d4637e4
 
a8b2fd4
 
32696d3
980018a
 
 
 
de9239a
5d5cb52
a8b2fd4
 
f0ba455
81e0f3f
35e6a69
f0ba455
383a137
a8b2fd4
f0ba455
 
 
 
 
 
 
 
 
 
9deb7e1
 
f0ba455
 
 
 
9deb7e1
 
 
f0ba455
 
 
 
 
 
9deb7e1
 
f0ba455
9deb7e1
 
 
 
f0ba455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9deb7e1
f0ba455
 
 
 
 
9deb7e1
f0ba455
 
 
9deb7e1
 
 
f0ba455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9deb7e1
 
 
f0ba455
9deb7e1
 
f0ba455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9deb7e1
 
 
f0ba455
 
 
 
 
 
 
 
 
 
 
9deb7e1
0611560
f0ba455
 
 
 
 
 
9deb7e1
f0ba455
 
 
 
 
 
 
 
 
 
 
9deb7e1
 
 
 
 
 
 
 
 
f0ba455
9deb7e1
f0ba455
 
9deb7e1
 
 
f0ba455
 
9deb7e1
 
f0ba455
 
 
 
 
9deb7e1
 
f0ba455
 
 
9deb7e1
 
f0ba455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9deb7e1
f0ba455
 
9deb7e1
 
f0ba455
 
9deb7e1
 
f0ba455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9deb7e1
 
383a137
35e6a69
383a137
 
0b69bda
 
 
35e6a69
3eb256b
383a137
32696d3
f0ba455
35e6a69
9aeacca
 
de9239a
 
35e6a69
de9239a
 
 
 
 
 
9aeacca
de9239a
 
9aeacca
35e6a69
 
9782585
 
35e6a69
 
 
 
 
d9be852
9782585
35e6a69
 
 
 
 
d9be852
9782585
35e6a69
 
 
 
 
 
 
 
 
 
d9be852
9782585
35e6a69
 
 
 
 
a8b2fd4
9782585
35e6a69
 
 
 
 
 
d9be852
53e1625
 
 
35e6a69
53e1625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16e8b2c
 
 
 
 
 
50da573
16e8b2c
 
 
50da573
16e8b2c
50da573
 
 
16e8b2c
 
 
 
53e1625
16e8b2c
 
 
 
 
53e1625
16e8b2c
35e6a69
6f91bff
 
 
 
fb18efe
6f91bff
 
 
 
60e6faa
53e1625
0611560
53e1625
 
 
 
3c2118a
53e1625
 
3c2118a
53e1625
 
3c2118a
53e1625
 
3c2118a
53e1625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16e8b2c
 
35e6a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d05c16
 
 
 
 
 
 
3eb256b
35e6a69
6d65a62
 
3fd75cd
6d65a62
3c2118a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50da573
 
 
 
 
 
59a8f26
 
 
 
 
 
 
 
d8bfff4
 
 
 
 
 
59a8f26
 
d8bfff4
35e6a69
50da573
 
 
 
35e6a69
 
3c2118a
 
 
b91d93b
35e6a69
 
 
b91d93b
35e6a69
 
0b69bda
df426cf
d4637e4
 
 
 
 
 
 
 
 
35e6a69
b91d93b
3c2118a
 
 
 
5a6b076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50da573
 
5a6b076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1a12ee
 
 
 
 
55fd7b2
 
 
 
 
e1a12ee
55fd7b2
 
 
 
 
 
 
e1a12ee
 
f1ce1c6
 
 
35e6a69
4a53fd8
9b5077c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a53fd8
35e6a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0497130
 
8cc0898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0497130
 
35e6a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c347a4
35e6a69
de9239a
 
 
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
import streamlit as st
from pathlib import Path
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import time
from transformers import AutoModelForCausalLM, AutoTokenizer
import seaborn as sns
from io import BytesIO
import base64
from streamlit_drawable_canvas import st_canvas
import io
import torch
import cv2
import mediapipe as mp
import base64
import gc
import  accelerate


# Set page config
st.set_page_config(page_title="NeuraSense AI", page_icon="🧠", layout="wide")

# Enhanced Custom CSS for a hyper-cyberpunk realistic look
custom_css = """
<style>
    @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&family=Roboto+Mono:wght@400;700&display=swap');

    :root {
        --neon-blue: #00FFFF;
        --neon-pink: #FF00FF;
        --neon-green: #39FF14;
        --dark-bg: #0a0a0a;
        --darker-bg: #050505;
        --light-text: #E0E0E0;
    }

    body {
        color: var(--light-text);
        background-color: var(--dark-bg);
        font-family: 'Roboto Mono', monospace;
        overflow-x: hidden;
    }

    .stApp {
        background: 
            linear-gradient(45deg, var(--darker-bg) 0%, var(--dark-bg) 100%),
            repeating-linear-gradient(45deg, #000 0%, #000 2%, transparent 2%, transparent 4%),
            repeating-linear-gradient(-45deg, #111 0%, #111 1%, transparent 1%, transparent 3%);
        background-blend-mode: overlay;
        animation: backgroundPulse 20s infinite alternate;
    }

    @keyframes backgroundPulse {
        0% { background-position: 0% 50%; }
        100% { background-position: 100% 50%; }
    }

    h1, h2, h3 {
        font-family: 'Orbitron', sans-serif;
        position: relative;
        text-shadow: 
            0 0 5px var(--neon-blue),
            0 0 10px var(--neon-blue),
            0 0 20px var(--neon-blue),
            0 0 40px var(--neon-blue);
        animation: textGlitch 5s infinite alternate;
    }

    @keyframes textGlitch {
        0% { transform: skew(0deg); }
        20% { transform: skew(5deg); text-shadow: 3px 3px 0 var(--neon-pink); }
        40% { transform: skew(-5deg); text-shadow: -3px -3px 0 var(--neon-green); }
        60% { transform: skew(3deg); text-shadow: 2px -2px 0 var(--neon-blue); }
        80% { transform: skew(-3deg); text-shadow: -2px 2px 0 var(--neon-pink); }
        100% { transform: skew(0deg); }
    }

    .stButton>button {
        color: var(--neon-blue);
        border: 2px solid var(--neon-blue);
        border-radius: 5px;
        background: linear-gradient(45deg, rgba(0,255,255,0.1), rgba(0,255,255,0.3));
        box-shadow: 0 0 15px var(--neon-blue);
        transition: all 0.3s ease;
        text-transform: uppercase;
        letter-spacing: 2px;
        backdrop-filter: blur(5px);
    }

    .stButton>button:hover {
        transform: scale(1.05) translateY(-3px);
        box-shadow: 0 0 30px var(--neon-blue);
        text-shadow: 0 0 5px var(--neon-blue);
    }

    .stTextInput>div>div>input, .stTextArea>div>div>textarea, .stSelectbox>div>div>div {
        background-color: rgba(0, 255, 255, 0.1);
        border: 1px solid var(--neon-blue);
        border-radius: 5px;
        color: var(--neon-blue);
        backdrop-filter: blur(5px);
    }

    .stTextInput>div>div>input:focus, .stTextArea>div>div>textarea:focus, .stSelectbox>div>div>div:focus {
        box-shadow: 0 0 20px var(--neon-blue);
    }

    .stSlider>div>div>div>div {
        background-color: var(--neon-blue);
    }

    .stSlider>div>div>div>div>div {
        background-color: var(--neon-pink);
        box-shadow: 0 0 10px var(--neon-pink);
    }

    ::-webkit-scrollbar {
        width: 10px;
        height: 10px;
    }

    ::-webkit-scrollbar-track {
        background: var(--darker-bg);
        border-radius: 5px;
    }

    ::-webkit-scrollbar-thumb {
        background: var(--neon-blue);
        border-radius: 5px;
        box-shadow: 0 0 5px var(--neon-blue);
    }

    ::-webkit-scrollbar-thumb:hover {
        background: var(--neon-pink);
        box-shadow: 0 0 5px var(--neon-pink);
    }

    .stPlot, .stDataFrame {
        border: 1px solid var(--neon-blue);
        border-radius: 5px;
        overflow: hidden;
        box-shadow: 0 0 15px rgba(0, 255, 255, 0.3);
    }

    .stImage, .stIcon {
        filter: drop-shadow(0 0 5px var(--neon-blue));
    }

    .stSidebar, .stContainer {
        background: 
            linear-gradient(45deg, var(--darker-bg) 0%, var(--dark-bg) 100%),
            repeating-linear-gradient(45deg, #000 0%, #000 2%, transparent 2%, transparent 4%);
        animation: sidebarPulse 10s infinite alternate;
    }

    @keyframes sidebarPulse {
        0% { background-position: 0% 50%; }
        100% { background-position: 100% 50%; }
    }

    .element-container {
        position: relative;
    }

    .element-container::before {
        content: '';
        position: absolute;
        top: -5px;
        left: -5px;
        right: -5px;
        bottom: -5px;
        border: 1px solid var(--neon-blue);
        border-radius: 10px;
        opacity: 0.5;
        pointer-events: none;
    }

    .stMarkdown a {
        color: var(--neon-pink);
        text-decoration: none;
        position: relative;
        transition: all 0.3s ease;
    }

    .stMarkdown a::after {
        content: '';
        position: absolute;
        width: 100%;
        height: 1px;
        bottom: -2px;
        left: 0;
        background-color: var(--neon-pink);
        transform: scaleX(0);
        transform-origin: bottom right;
        transition: transform 0.3s ease;
    }

    .stMarkdown a:hover::after {
        transform: scaleX(1);
        transform-origin: bottom left;
    }

    /* Cyberpunk-style progress bar */
    .stProgress > div > div {
        background-color: var(--neon-blue);
        background-image: linear-gradient(
            45deg, 
            var(--neon-pink) 25%, 
            transparent 25%, 
            transparent 50%, 
            var(--neon-pink) 50%, 
            var(--neon-pink) 75%, 
            transparent 75%, 
            transparent
        );
        background-size: 40px 40px;
        animation: progress-bar-stripes 1s linear infinite;
    }

    @keyframes progress-bar-stripes {
        0% { background-position: 40px 0; }
        100% { background-position: 0 0; }
    }

    /* Glowing checkbox */
    .stCheckbox > label > div {
        border-color: var(--neon-blue);
        transition: all 0.3s ease;
    }

    .stCheckbox > label > div[data-checked="true"] {
        background-color: var(--neon-blue);
        box-shadow: 0 0 10px var(--neon-blue);
    }

    /* Futuristic radio button */
    .stRadio > div {
        background-color: rgba(0, 255, 255, 0.1);
        border-radius: 10px;
        padding: 10px;
    }

    .stRadio > div > label > div {
        border-color: var(--neon-blue);
        transition: all 0.3s ease;
    }

    .stRadio > div > label > div[data-checked="true"] {
        background-color: var(--neon-blue);
        box-shadow: 0 0 10px var(--neon-blue);
    }

    /* Cyberpunk-style tables */
    .stDataFrame table {
        border-collapse: separate;
        border-spacing: 0;
        border: 1px solid var(--neon-blue);
        border-radius: 10px;
        overflow: hidden;
    }

    .stDataFrame th {
        background-color: rgba(0, 255, 255, 0.2);
        color: var(--neon-blue);
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    .stDataFrame td {
        border-bottom: 1px solid rgba(0, 255, 255, 0.2);
    }

    .stDataFrame tr:last-child td {
        border-bottom: none;
    }

    /* Futuristic file uploader */
    .stFileUploader > div {
        border: 2px dashed var(--neon-blue);
        border-radius: 10px;
        background-color: rgba(0, 255, 255, 0.05);
        transition: all 0.3s ease;
    }

    .stFileUploader > div:hover {
        background-color: rgba(0, 255, 255, 0.1);
        box-shadow: 0 0 15px rgba(0, 255, 255, 0.3);
    }

    /* Cyberpunk-style tooltips */
    .stTooltipIcon {
        color: var(--neon-pink);
        transition: all 0.3s ease;
    }

    .stTooltipIcon:hover {
        color: var(--neon-blue);
        text-shadow: 0 0 5px var(--neon-blue);
    }

    /* Futuristic date input */
    .stDateInput > div > div > input {
        background-color: rgba(0, 255, 255, 0.1);
        border: 1px solid var(--neon-blue);
        border-radius: 5px;
        color: var(--neon-blue);
        backdrop-filter: blur(5px);
    }

    .stDateInput > div > div > input:focus {
        box-shadow: 0 0 20px var(--neon-blue);
    }

    /* Cyberpunk-style code blocks */
    .stCodeBlock {
        background-color: rgba(0, 0, 0, 0.6);
        border: 1px solid var(--neon-green);
        border-radius: 5px;
        color: var(--neon-green);
        font-family: 'Roboto Mono', monospace;
        padding: 10px;
        position: relative;
        overflow: hidden;
    }

    .stCodeBlock::before {
        content: '';
        position: absolute;
        top: -10px;
        left: -10px;
        right: -10px;
        bottom: -10px;
        background: linear-gradient(45deg, var(--neon-green), transparent);
        opacity: 0.1;
        z-index: -1;
    }
</style>
"""

# Apply the custom CSS
st.markdown(custom_css, unsafe_allow_html=True)
AVATAR_WIDTH = 600
AVATAR_HEIGHT = 800



# Your Streamlit app code goes here
st.title("NeuraSense AI")


# Set up DialoGPT model
@st.cache_resource
def load_tokenizer():
    return AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")

@st.cache_resource
def load_model():
    model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium", 
                                                 device_map="auto", 
                                                 torch_dtype=torch.float16)
    return model

tokenizer = load_tokenizer()
model = load_model()



# Advanced Sensor Classes
class QuantumSensor:
   @staticmethod
   def measure(x, y, sensitivity):
       return np.sin(x/20) * np.cos(y/20) * sensitivity * np.random.normal(1, 0.1)



class NanoThermalSensor:
   @staticmethod
   def measure(base_temp, pressure, duration):
       return base_temp + 10 * pressure * (1 - np.exp(-duration / 3)) + np.random.normal(0, 0.001)



class AdaptiveTextureSensor:
   textures = [
       "nano-smooth", "quantum-rough", "neuro-bumpy", "plasma-silky",
       "graviton-grainy", "zero-point-soft", "dark-matter-hard", "bose-einstein-condensate"
   ]
  
   @staticmethod
   def measure(x, y):
       return AdaptiveTextureSensor.textures[hash((x, y)) % len(AdaptiveTextureSensor.textures)]



class EMFieldSensor:
   @staticmethod
   def measure(x, y, sensitivity):
       return (np.sin(x / 30) * np.cos(y / 30) + np.random.normal(0, 0.1)) * 10 * sensitivity



class NeuralNetworkSimulator:
   @staticmethod
   def process(inputs):
       weights = np.random.rand(len(inputs))
       return np.dot(inputs, weights) / np.sum(weights)



# Set up MediaPipe Pose
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)

def detect_humanoid(image):
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results = pose.process(image_rgb)
    
    if results.pose_landmarks:
        landmarks = results.pose_landmarks.landmark
        image_height, image_width, _ = image.shape
        keypoints = []
        for landmark in landmarks:
            x = int(landmark.x * image_width)
            y = int(landmark.y * image_height)
            keypoints.append((x, y))
        return keypoints
    return []

def apply_touch_points(image, keypoints):
    draw = ImageDraw.Draw(image)
    for point in keypoints:
        draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill='red')
    return image

def create_sensation_map(width, height, keypoints):
    sensation_map = np.zeros((height, width, 12))
    for y in range(height):
        for x in range(width):
            base_sensitivities = np.random.rand(12) * 0.5 + 0.5
            
            # Enhance sensitivities near keypoints
            for kp in keypoints:
                distance = np.sqrt((x - kp[0])**2 + (y - kp[1])**2)
                if distance < 30:  # Adjust this value to change the area of influence
                    base_sensitivities *= 1.5
            
            sensation_map[y, x, 0] = base_sensitivities[0] * np.random.rand()  # Pain
            sensation_map[y, x, 1] = base_sensitivities[1] * np.random.rand()  # Pleasure
            sensation_map[y, x, 2] = base_sensitivities[2] * np.random.rand()  # Pressure
            sensation_map[y, x, 3] = base_sensitivities[3] * (np.random.rand() * 10 + 30)  # Temperature
            sensation_map[y, x, 4] = base_sensitivities[4] * np.random.rand()  # Texture
            sensation_map[y, x, 5] = base_sensitivities[5] * np.random.rand()  # EM field
            sensation_map[y, x, 6] = base_sensitivities[6] * np.random.rand()  # Tickle
            sensation_map[y, x, 7] = base_sensitivities[7] * np.random.rand()  # Itch
            sensation_map[y, x, 8] = base_sensitivities[8] * np.random.rand()  # Quantum
            sensation_map[y, x, 9] = base_sensitivities[9] * np.random.rand()  # Neural
            sensation_map[y, x, 10] = base_sensitivities[10] * np.random.rand()  # Proprioception
            sensation_map[y, x, 11] = base_sensitivities[11] * np.random.rand()  # Synesthesia

    return sensation_map

def create_heatmap(sensation_map, sensation_type):
    plt.figure(figsize=(10, 15))
    sns.heatmap(sensation_map[:, :, sensation_type], cmap='viridis')
    
def create_heatmap(sensation_map, sensation_type):
    plt.figure(figsize=(10, 15))
    sns.heatmap(sensation_map[:, :, sensation_type], cmap='viridis')
    plt.title(f'{["Pain", "Pleasure", "Pressure", "Temperature", "Texture", "EM Field", "Tickle", "Itch", "Quantum", "Neural", "Proprioception", "Synesthesia"][sensation_type]} Sensation Map')
    plt.axis('off')
    
    # Instead of displaying, save to a buffer
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)
    plt.close()  # Close the figure to free up memory
    
    # Create an image from the buffer
    heatmap_img = Image.open(buf)
    return heatmap_img

def generate_ai_response(keypoints, sensation_map):
    num_keypoints = len(keypoints)
    avg_sensations = np.mean(sensation_map, axis=(0, 1))
    
    response = f"I detect {num_keypoints} key points on the humanoid figure. "
    response += "The average sensations across the body are:\n"
    for i, sensation in enumerate(["Pain", "Pleasure", "Pressure", "Temperature", "Texture", "EM Field", 
                                   "Tickle", "Itch", "Quantum", "Neural", "Proprioception", "Synesthesia"]):
        response += f"{sensation}: {avg_sensations[i]:.2f}\n"
    
    return response

# Create and display avatar with heatmap
st.subheader("Avatar with Sensation Heatmap")

# You need to define sensation_map and sensation_type before this
sensation_map = np.random.rand(AVATAR_HEIGHT, 600, AVATAR_WIDTH, 300)  # Example random sensation map
sensation_type = 0  # Example sensation type (0 for Pain)

avatar_with_heatmap = create_avatar_with_heatmap(sensation_map, sensation_type)
st.image(avatar_with_heatmap, use_column_width=True)

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Read the image
    image = Image.open(uploaded_file)
    image_np = np.array(image)
    
    # Detect humanoid keypoints
    keypoints = detect_humanoid(image_np)
    
    # Apply touch points to the image
    processed_image = apply_touch_points(image.copy(), keypoints)
    
    # Display the processed image
    st.image(processed_image, caption='Processed Image with Touch Points', use_column_width=True)
    
    # Create sensation map
    sensation_map = create_sensation_map(image.width, image.height, keypoints)
    
    # Display heatmaps for different sensations
    sensation_types = ["Pain", "Pleasure", "Pressure", "Temperature", "Texture", "EM Field", 
                       "Tickle", "Itch", "Quantum", "Neural", "Proprioception", "Synesthesia"]
    
    selected_sensation = st.selectbox("Select a sensation to view:", sensation_types)
    heatmap = create_heatmap(sensation_map, sensation_types.index(selected_sensation))
    st.image(heatmap, use_column_width=True)

    # Generate AI response based on the image and sensations
    if st.button("Generate AI Response"):
        response = generate_ai_response(keypoints, sensation_map)
        st.write("AI Response:", response)


   


# Create futuristic human-like avatar
def create_avatar():
   img = Image.new('RGBA', (AVATAR_WIDTH, AVATAR_HEIGHT), color=(0, 0, 0, 0))
   draw = ImageDraw.Draw(img)
  
   # Body
   draw.polygon([(300, 100), (200, 250), (250, 600), (300, 750), (350, 600), (400, 250)], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
  
   # Head
   draw.ellipse([250, 50, 350, 150], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
  
   # Eyes
   draw.ellipse([275, 80, 295, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
   draw.ellipse([305, 80, 325, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
  
   # Nose
   draw.polygon([(300, 90), (290, 110), (310, 110)], fill=(0, 255, 255, 150))
  
   # Mouth
   draw.arc([280, 110, 320, 130], 0, 180, fill=(0, 255, 255, 200), width=2)
  
  # Arms
   draw.line([(200, 250), (150, 400)], fill=(0, 255, 255, 200), width=5)
   draw.line([(400, 250), (450, 400)], fill=(0, 255, 255, 200), width=5)
  
   # Hands
   draw.ellipse([140, 390, 160, 410], fill=(0, 255, 255, 150))
   draw.ellipse([440, 390, 460, 410], fill=(0, 255, 255, 150))
  
   # Fingers
   for i in range(5):
       draw.line([(150 + i*5, 400), (145 + i*5, 420)], fill=(0, 255, 255, 200), width=2)
       draw.line([(450 - i*5, 400), (455 - i*5, 420)], fill=(0, 255, 255, 200), width=2)
  
   # Legs
   draw.line([(250, 600), (230, 780)], fill=(0, 255, 255, 200), width=5)
   draw.line([(350, 600), (370, 780)], fill=(0, 255, 255, 200), width=5)
  
   # Feet
   draw.ellipse([220, 770, 240, 790], fill=(0, 255, 255, 150))
   draw.ellipse([360, 770, 380, 790], fill=(0, 255, 255, 150))
  
   # Toes
   for i in range(5):
       draw.line([(225 + i*3, 790), (223 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
       draw.line([(365 + i*3, 790), (363 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
  
def generate_neural_network_lines(img, draw):
    # Neural network lines
    for _ in range(100):
        start = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
        end = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
        draw.line([start, end], fill=(0, 255, 255, 50), width=1)
    return img
    

# Create and display avatar with heatmap
st.subheader("Avatar with Sensation Heatmap")
avatar_with_heatmap = create_avatar_with_heatmap()
st.image(avatar_with_heatmap, use_column_width=True)
# Create avatar function
def create_avatar():
    img = Image.new('RGBA', (AVATAR_WIDTH, AVATAR_HEIGHT), color=(0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # Body
    draw.polygon([(300, 100), (200, 250), (250, 600), (300, 750), (350, 600), (400, 250)], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
    
    # Head
    draw.ellipse([250, 50, 350, 150], fill=(0, 255, 255, 100), outline=(0, 255, 255, 255))
    
    # Eyes
    draw.ellipse([275, 80, 295, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
    draw.ellipse([305, 80, 325, 100], fill=(255, 255, 255, 200), outline=(0, 255, 255, 255))
    
    # Nose
    draw.polygon([(300, 90), (290, 110), (310, 110)], fill=(0, 255, 255, 150))
    
    # Mouth
    draw.arc([280, 110, 320, 130], 0, 180, fill=(0, 255, 255, 200), width=2)
    
    # Arms
    draw.line([(200, 250), (150, 400)], fill=(0, 255, 255, 200), width=5)
    draw.line([(400, 250), (450, 400)], fill=(0, 255, 255, 200), width=5)
    
    # Hands
    draw.ellipse([140, 390, 160, 410], fill=(0, 255, 255, 150))
    draw.ellipse([440, 390, 460, 410], fill=(0, 255, 255, 150))
    
    # Fingers
    for i in range(5):
        draw.line([(150 + i*5, 400), (145 + i*5, 420)], fill=(0, 255, 255, 200), width=2)
        draw.line([(450 - i*5, 400), (455 - i*5, 420)], fill=(0, 255, 255, 200), width=2)
    
    # Legs
    draw.line([(250, 600), (230, 780)], fill=(0, 255, 255, 200), width=5)
    draw.line([(350, 600), (370, 780)], fill=(0, 255, 255, 200), width=5)
    
    # Feet
    draw.ellipse([220, 770, 240, 790], fill=(0, 255, 255, 150))
    draw.ellipse([360, 770, 380, 790], fill=(0, 255, 255, 150))
    
    # Toes
    for i in range(5):
        draw.line([(225 + i*3, 790), (223 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
        draw.line([(365 + i*3, 790), (363 + i*3, 800)], fill=(0, 255, 255, 200), width=2)
    
    # Neural network lines
    for _ in range(100):
        start = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
        end = (np.random.randint(0, AVATAR_WIDTH), np.random.randint(0, AVATAR_HEIGHT))
        draw.line([start, end], fill=(0, 255, 255, 50), width=1)
    
    return img
def create_avatar_with_heatmap(show_heatmap=True):
    # Load avatar image
    avatar_img = Image.open("avatar.png").resize((AVATAR_WIDTH, AVATAR_HEIGHT))
    
    if not show_heatmap:
        return avatar_img  # Return the avatar image without heatmap
    
    # Create a heatmap
    heatmap_img = create_heatmap(sensation_map, sensation_type)
    
    # Resize heatmap to match avatar size
    heatmap_img = heatmap_img.resize((AVATAR_WIDTH, AVATAR_HEIGHT))
    
    # Adjust alpha channel of heatmap
    data = np.array(heatmap_img)
    if data.shape[2] == 3:  # If RGB, add an alpha channel
        data = np.concatenate([data, np.full((data.shape[0], data.shape[1], 1), 255, dtype=np.uint8)], axis=2)
    data[:, :, 3] = data[:, :, 3] * 0.5  # Reduce opacity to 50%
    heatmap_img = Image.fromarray(data)
    
    # Combine avatar and heatmap
    combined_img = Image.alpha_composite(avatar_img.convert('RGBA'), heatmap_img.convert('RGBA'))
    return combined_img

# Create and display avatar with optional heatmap
st.subheader("Avatar with Optional Sensation Heatmap")
avatar_with_heatmap = create_avatar_with_heatmap(show_heatmap)
st.image(avatar_with_heatmap, use_column_width=True)



# Create three columns
col1, col2, col3 = st.columns(3)



# Avatar display with touch interface
with col1:
   st.subheader("Humanoid Avatar Interface")
  
 # Use st_canvas for touch input
canvas_result = st_canvas(
        fill_color="rgba(0, 255, 255, 0.3)",
        stroke_width=2,
        stroke_color="#00FFFF",
        background_image=avatar_with_heatmap,
        height=AVATAR_HEIGHT,
        width=AVATAR_WIDTH,
        drawing_mode="point",
        key="canvas",
    )


with col3:
    st.subheader("Sensation Heatmap")
    heatmap = create_heatmap(avatar_sensation_map)
    st.image(heatmap, use_column_width=True)
        # Touch controls and output
with col2:
    st.subheader("Neural Interface Controls")
    
    # Touch duration
    touch_duration = st.slider("Interaction Duration (s)", 0.1, 5.0, 1.0, 0.1)
    
    # Touch pressure
    touch_pressure = st.slider("Interaction Intensity", 0.1, 2.0, 1.0, 0.1)
    
    # Toggle quantum feature
    use_quantum = st.checkbox("Enable Quantum Sensing", value=True)
    
    # Toggle synesthesia
    use_synesthesia = st.checkbox("Enable Synesthesia", value=False)
    # Add this with your other UI elements
    show_heatmap = st.checkbox("Show Sensation Heatmap", value=True)
    
    if canvas_result.json_data is not None:
        objects = canvas_result.json_data["objects"]
        if len(objects) > 0:
            last_touch = objects[-1]
            touch_x, touch_y = last_touch["left"], last_touch["top"]
            
            sensation = avatar_sensation_map[int(touch_y), int(touch_x)]
            (
                pain, pleasure, pressure_sens, temp_sens, texture_sens,
                em_sens, tickle_sens, itch_sens, quantum_sens, neural_sens,
                proprioception_sens, synesthesia_sens
            ) = sensation

            measured_pressure = QuantumSensor.measure(touch_x, touch_y, pressure_sens) * touch_pressure
            measured_temp = NanoThermalSensor.measure(37, touch_pressure, touch_duration)
            measured_texture = AdaptiveTextureSensor.measure(touch_x, touch_y)
            measured_em = EMFieldSensor.measure(touch_x, touch_y, em_sens)
            
            if use_quantum:
                quantum_state = QuantumSensor.measure(touch_x, touch_y, quantum_sens)
            else:
                quantum_state = "N/A"

            # Calculate overall sensations
            pain_level = pain * measured_pressure * touch_pressure
            pleasure_level = pleasure * (measured_temp - 37) / 10
            tickle_level = tickle_sens * (1 - np.exp(-touch_duration / 0.5))
            itch_level = itch_sens * (1 - np.exp(-touch_duration / 1.5))
            
            # Proprioception (sense of body position)
            proprioception = proprioception_sens * np.linalg.norm([touch_x - AVATAR_WIDTH/2, touch_y - AVATAR_HEIGHT/2]) / (AVATAR_WIDTH/2)
            
            # Synesthesia (mixing of senses)
            if use_synesthesia:
                synesthesia = synesthesia_sens * (measured_pressure + measured_temp + measured_em) / 3
            else:
                synesthesia = "N/A"
            
            # Neural network simulation
            neural_inputs = [pain_level, pleasure_level, measured_pressure, measured_temp, measured_em, tickle_level, itch_level, proprioception]
            neural_response = NeuralNetworkSimulator.process(neural_inputs)

            st.write("### Sensory Data Analysis")
            st.write(f"Interaction Point: ({touch_x:.1f}, {touch_y:.1f})")
            st.write(f"Duration: {touch_duration:.1f} s | Intensity: {touch_pressure:.2f}")  
        
# Create a futuristic data display
data_display = (
    "```\n"
    "+---------------------------------------------+\n"
    f"| Pressure     : {measured_pressure:.2f}".ljust(45) + "|\n"
    f"| Temperature  : {measured_temp:.2f}°C".ljust(45) + "|\n"
    f"| Texture      : {measured_texture}".ljust(45) + "|\n"
    f"| EM Field     : {measured_em:.2f} μT".ljust(45) + "|\n"
    f"| Quantum State: {quantum_state:.2f}".ljust(45) + "|\n"
    "+---------------------------------------------+\n"
    f"| Pain Level   : {pain_level:.2f}".ljust(45) + "|\n"
    f"| Pleasure     : {pleasure_level:.2f}".ljust(45) + "|\n"
    f"| Tickle       : {tickle_level:.2f}".ljust(45) + "|\n"
    f"| Itch         : {itch_level:.2f}".ljust(45) + "|\n"
    f"| Proprioception: {proprioception:.2f}".ljust(44) + "|\n"
    f"| Synesthesia  : {synesthesia}".ljust(45) + "|\n"
    f"| Neural Response: {neural_response:.2f}".ljust(43) + "|\n"
    "+---------------------------------------------+\n"
    "```"
)

st.code(data_display, language="")

# Generate description
prompt = (
    "Human: Analyze the sensory input for a hyper-advanced AI humanoid:\n"
    "    Location: (" + str(round(touch_x, 1)) + ", " + str(round(touch_y, 1)) + ")\n"
    "    Duration: " + str(round(touch_duration, 1)) + "s, Intensity: " + str(round(touch_pressure, 2)) + "\n"
    "    Pressure: " + str(round(measured_pressure, 2)) + "\n"
    "    Temperature: " + str(round(measured_temp, 2)) + "°C\n"
    "    Texture: " + measured_texture + "\n"
    "    EM Field: " + str(round(measured_em, 2)) + " μT\n"
    "    Quantum State: " + str(quantum_state) + "\n"
    "    Resulting in:\n"
    "    Pain: " + str(round(pain_level, 2)) + ", Pleasure: " + str(round(pleasure_level, 2)) + "\n"
    "    Tickle: " + str(round(tickle_level, 2)) + ", Itch: " + str(round(itch_level, 2)) + "\n"
    "    Proprioception: " + str(round(proprioception, 2)) + "\n"
    "    Synesthesia: " + synesthesia + "\n"
    "    Neural Response: " + str(round(neural_response, 2)) + "\n"
    "    Provide a detailed, scientific analysis of the AI's experience.\n"
    "    AI:"
)


input_ids = tokenizer.encode(prompt, return_tensors="pt")



output = model.generate(
   input_ids,
   max_length=400,
   num_return_sequences=1,
   no_repeat_ngram_size=2,
   top_k=50,
   top_p=0.95,
   temperature=0.7
)



response = tokenizer.decode(output[0], skip_special_tokens=True).split("AI:")[-1].strip()



st.write("### AI's Sensory Analysis:")
st.write(response)



# Visualize sensation map
st.subheader("Quantum Neuro-Sensory Map")
fig, axs = plt.subplots(3, 4, figsize=(20, 15))
titles = [
   'Pain', 'Pleasure', 'Pressure', 'Temperature', 'Texture',
   'EM Field', 'Tickle', 'Itch', 'Quantum', 'Neural',
   'Proprioception', 'Synesthesia'
]



for i, title in enumerate(titles):
   ax = axs[i // 4, i % 4]
   im = ax.imshow(avatar_sensation_map[:, :, i], cmap='plasma')
   ax.set_title(title)
   fig.colorbar(im, ax=ax)



plt.tight_layout()
st.pyplot(fig)



st.write("The quantum neuro-sensory map illustrates the varying sensitivities across the AI's body. Brighter areas indicate heightened responsiveness to specific stimuli.")



# Add information about the AI's advanced capabilities
st.subheader("NeuraSense AI: Cutting-Edge Sensory Capabilities")

st.write("This hyper-advanced AI humanoid incorporates revolutionary sensory technology:")

capabilities = [
    "1. Quantum-Enhanced Pressure Sensors: Utilize quantum tunneling effects for unparalleled sensitivity.",
    "2. Nano-scale Thermal Detectors: Capable of detecting temperature variations to 0.001°C.",
    "3. Adaptive Texture Analysis: Employs machine learning to continually refine texture perception.",
    "4. Electromagnetic Field Sensors: Can detect and analyze complex EM patterns in the environment.",
    "5. Quantum State Detector: Interprets quantum phenomena, adding a new dimension to sensory input.",
    "6. Neural Network Integration: Simulates complex interplay of sensations, creating emergent experiences.",
    "7. Proprioception Simulation: Accurately models the AI's sense of body position and movement.",
    "8. Synesthesia Emulation: Allows for cross-modal sensory experiences, mixing different sensory inputs.",
    "9. Tickle and Itch Simulation: Replicates these unique sensations with quantum-level precision.",
    "10. Adaptive Pain and Pleasure Modeling: Simulates complex emotional and physical responses to stimuli."
]

for capability in capabilities:
    st.write(capability)

st.write("The AI's responses are generated using an advanced language model, providing detailed scientific analysis of its sensory experiences.")
st.write("This simulation showcases the potential for creating incredibly sophisticated and responsive artificial sensory systems that go beyond human capabilities.")



# Interactive sensory exploration
st.subheader("Interactive Sensory Exploration")
exploration_type = st.selectbox("Choose a sensory exploration:",
                               ["Quantum Field Fluctuations", "Synesthesia Experience", "Proprioceptive Mapping"])



if exploration_type == "Quantum Field Fluctuations":
   st.write("Observe how quantum fields fluctuate across the AI's body.")
   quantum_field = np.array([[QuantumSensor.measure(x, y, 1) for x in range(AVATAR_WIDTH)] for y in range(AVATAR_HEIGHT)])
  
   # Save the plot to an in-memory buffer
   buf = io.BytesIO()
   plt.figure(figsize=(8, 6))
   plt.imshow(quantum_field, cmap='viridis')
   plt.savefig(buf, format='png')
  
   # Create a PIL Image object from the buffer
   quantum_image = Image.open(buf)
  
   # Display the image using st.image()
   st.image(quantum_image, use_column_width=True)



elif exploration_type == "Synesthesia Experience":
   st.write("Experience how the AI might perceive colors as sounds or textures as tastes.")
   synesthesia_map = np.random.rand(AVATAR_HEIGHT, AVATAR_WIDTH, 3)
   st.image(Image.fromarray((synesthesia_map * 255).astype(np.uint8)), use_column_width=True)



elif exploration_type == "Proprioceptive Mapping":
   st.write("Explore the AI's sense of body position and movement.")
   proprioceptive_map = np.array([[np.linalg.norm([x - AVATAR_WIDTH/2, y - AVATAR_HEIGHT/2]) / (AVATAR_WIDTH/2)
                                   for x in range(AVATAR_WIDTH)] for y in range(AVATAR_HEIGHT)])
  
   # Save the plot to an in-memory buffer
   buf = io.BytesIO()
   plt.figure(figsize=(8, 6))
   plt.imshow(proprioceptive_map, cmap='coolwarm')
   plt.savefig(buf, format='png')
  
   # Create a PIL Image object from the buffer
   proprioceptive_image = Image.open(buf)
  
   # Display the image using st.image()
   st.image(proprioceptive_image, use_column_width=True)
# Footer
st.write("---")
st.write("NeuraSense AI: Quantum-Enhanced Sensory Simulation v4.0")
st.write("Disclaimer: This is an advanced simulation and does not represent current technological capabilities.""")

# After processing
torch.cuda.empty_cache()
gc.collect()