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

# Custom CSS to make the UI look beautiful
custom_css = """
.gradio-container {
    font-family: 'Arial', sans-serif !important;
    max-width: 1200px !important;
    margin: 0 auto !important;
}

h1 {
    color: #3a0ca3 !important;
    font-size: 32px !important;
    text-align: center !important;
    margin-bottom: 24px !important;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1) !important;
}

h2 {
    color: #4361ee !important;
    font-size: 24px !important;
    border-bottom: 2px solid #4361ee !important;
    padding-bottom: 8px !important;
    margin-top: 20px !important;
}

h3 {
    color: #4cc9f0 !important;
    font-size: 20px !important;
    margin-top: 16px !important;
}

.tabs {
    margin-top: 20px !important;
    border-radius: 12px !important;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
}

button {
    border-radius: 8px !important;
    font-weight: bold !important;
    transform: translateY(0) !important;
    transition: all 0.2s !important;
    margin: 5px !important;
    padding: 8px 16px !important;
}

button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1) !important;
}

button[data-testid*="primary"] {
    background: linear-gradient(135deg, #4361ee, #3a0ca3) !important;
    color: white !important;
    border: none !important;
}

.gradio-row {
    margin: 15px 0 !important;
}

.gradio-slider {
    margin-bottom: 20px !important;
}

.gradio-markdown p {
    margin: 10px 0 !important;
}

.gradio-tab-panel {
    padding: 15px !important;
    background-color: #f8f9fa !important;
    border-radius: 0 0 12px 12px !important;
}

.status-box {
    padding: 15px !important;
    border-radius: 10px !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1) !important;
    margin-bottom: 15px !important;
    background-color: white !important;
}

.status-header {
    display: flex !important;
    align-items: center !important;
    margin-bottom: 10px !important;
}

.status-emoji {
    font-size: 24px !important;
    margin-right: 10px !important;
}

.logo-placeholder {
    text-align: center !important;
    font-size: 48px !important;
    margin: 10px auto 20px auto !important;
    background: linear-gradient(135deg, #4cc9f0, #3a0ca3) !important;
    color: white !important;
    width: 96px !important;
    height: 96px !important;
    line-height: 96px !important;
    border-radius: 50% !important;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important;
}
"""

# API CREDENTIALS
DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY', 'sk-8b7ec15d89634a06b9790cd25ca483a3')
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY', 'AIzaSyA7P5exjlxe0tuGY0nZQevrVKTN7sDukzM')
TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID', 'AC48c581ecc0e0f57fa4757052a2a1a1ba')
TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN', '92e673e81cf0192ac785c8c3f671e22f')
TWILIO_PHONE_NUMBER = os.getenv('TWILIO_PHONE_NUMBER', '+18557059842')
YOUR_PERSONAL_NUMBER = os.getenv('YOUR_PHONE_NUMBER', '+15622289429')

print(f"===== Application Startup at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
print(f"πŸš€ Starting Jay's Call Center...")
print(f"πŸ“ž Business: {TWILIO_PHONE_NUMBER}")
print(f"πŸ“± Personal: {YOUR_PERSONAL_NUMBER}")

# Initialize clients
try:
    twilio_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
    TWILIO_STATUS = "βœ… Connected"
    print("βœ… Twilio client initialized")
except Exception as e:
    twilio_client = None
    TWILIO_STATUS = f"❌ Error: {e}"
    print(f"❌ Twilio error: {e}")

try:
    genai.configure(api_key=GEMINI_API_KEY)
    gemini_model = genai.GenerativeModel('gemini-pro')
    gemini_vision_model = genai.GenerativeModel('gemini-pro-vision')
    GEMINI_STATUS = "βœ… Connected"
    print("βœ… Gemini initialized")
except Exception as e:
    gemini_model = None
    gemini_vision_model = None
    GEMINI_STATUS = f"❌ Error: {e}"
    print(f"❌ Gemini error: {e}")

# Initialize database
def init_database():
    conn = sqlite3.connect('jays_call_center.db', check_same_thread=False)
    cursor = conn.cursor()
    
    # Smart calls table
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS smart_calls (
            id INTEGER PRIMARY KEY,
            call_sid TEXT UNIQUE,
            from_number TEXT,
            to_number TEXT,
            call_status TEXT,
            language_selected TEXT,
            button_pressed TEXT,
            ai_conversation TEXT,
            customer_intent TEXT,
            spam_score REAL,
            duration_before_answer INTEGER,
            total_duration INTEGER,
            forwarded_to_jay BOOLEAN,
            jay_answered BOOLEAN,
            ai_handled_alone BOOLEAN,
            timestamp TEXT
        )
    ''')
    
    # Smart SMS
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS smart_sms (
            id INTEGER PRIMARY KEY,
            message_sid TEXT UNIQUE,
            from_number TEXT,
            to_number TEXT,
            body TEXT,
            spam_score REAL,
            timestamp TEXT
        )
    ''')
    
    # File training
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS file_training (
            id INTEGER PRIMARY KEY,
            training_id TEXT UNIQUE,
            file_type TEXT,
            file_path TEXT,
            website_url TEXT,
            learning_score REAL,
            timestamp TEXT
        )
    ''')
    
    # AI Learning analytics
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS ai_learning_analytics (
            id INTEGER PRIMARY KEY,
            learning_type TEXT,
            ai_model_used TEXT,
            accuracy_score REAL,
            learning_input TEXT,
            timestamp TEXT
        )
    ''')
    
    # Settings with duration controls
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS complete_settings (
            id INTEGER PRIMARY KEY,
            category TEXT,
            setting_name TEXT,
            setting_value TEXT,
            backend_access BOOLEAN,
            last_updated TEXT
        )
    ''')
    
    conn.commit()
    load_settings(cursor, conn)
    return conn

def load_settings(cursor, conn):
    """Load settings with 7-second ring time"""
    settings = [
        ("call_handling", "ring_duration_seconds", "7", True),
        ("call_handling", "ai_takeover_after_seconds", "10", True),
        ("call_handling", "max_ai_conversation_minutes", "5", True),
        ("spam_protection", "spam_threshold", "0.7", True),
        ("voice_menu", "english_greeting", "Hey! It's Jay's Mobile Wash! Press 1 for English, 2 for Spanish.", True),
        ("voice_menu", "spanish_greeting", "Β‘Hola! Es Jay's Mobile Wash! Presiona 1 para inglΓ©s, 2 para espaΓ±ol.", True),
        ("notifications", "sms_jay_on_call", "true", True),
        ("notifications", "sms_jay_on_ai_takeover", "true", True),
    ]
    
    for category, name, value, backend in settings:
        cursor.execute("""
            INSERT OR IGNORE INTO complete_settings 
            (category, setting_name, setting_value, backend_access, last_updated)
            VALUES (?, ?, ?, ?, ?)
        """, (category, name, value, backend, datetime.now().isoformat()))
    conn.commit()

# Initialize database
db_conn = init_database()

def get_setting(category: str, name: str, default: str = "") -> str:
    """Get setting value from database"""
    try:
        cursor = db_conn.cursor()
        cursor.execute(
            "SELECT setting_value FROM complete_settings WHERE category = ? AND setting_name = ?",
            (category, name)
        )
        result = cursor.fetchone()
        return result[0] if result else default
    except Exception as e:
        print(f"Error getting setting {category}.{name}: {e}")
        return default

def update_call_duration_settings_complete(ring_time, ai_takeover_time, max_ai_time):
    """Update call duration settings"""
    try:
        cursor = db_conn.cursor()
        
        cursor.execute("""
            UPDATE complete_settings SET setting_value = ?, last_updated = ?
            WHERE category = 'call_handling' AND setting_name = 'ring_duration_seconds'
        """, (str(ring_time), datetime.now().isoformat()))
        
        cursor.execute("""
            UPDATE complete_settings SET setting_value = ?, last_updated = ?
            WHERE category = 'call_handling' AND setting_name = 'ai_takeover_after_seconds'
        """, (str(ai_takeover_time), datetime.now().isoformat()))
        
        cursor.execute("""
            UPDATE complete_settings SET setting_value = ?, last_updated = ?
            WHERE category = 'call_handling' AND setting_name = 'max_ai_conversation_minutes'
        """, (str(max_ai_time), datetime.now().isoformat()))
        
        db_conn.commit()
        
        return f"<div style='padding: 15px; background-color: #d4edda; border-radius: 8px; margin: 10px 0;'>βœ… Call duration settings updated:<br>⏰ Ring time: <b>{ring_time} seconds</b><br>πŸ€– AI takeover: <b>{ai_takeover_time} seconds</b><br>⏱️ Max AI conversation: <b>{max_ai_time} minutes</b></div>"
    except Exception as e:
        return f"<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ Error updating settings: {e}</div>"

# DeepSeek API integration
def call_deepseek_api(prompt: str, context: str = "") -> str:
    """DeepSeek API integration with error handling"""
    try:
        headers = {
            'Authorization': f'Bearer {DEEPSEEK_API_KEY}',
            'Content-Type': 'application/json'
        }
        
        data = {
            "model": "deepseek-chat",
            "messages": [
                {"role": "system", "content": f"You are Jay's Mobile Wash AI assistant. Context: {context}"},
                {"role": "user", "content": prompt}
            ],
            "max_tokens": 500,
            "temperature": 0.7,
        }
        
        response = requests.post(
            "https://api.deepseek.com/v1/chat/completions",
            headers=headers,
            json=data,
            timeout=30
        )
        
        if response.status_code == 200:
            result = response.json()
            return result['choices'][0]['message']['content']
        else:
            return "DeepSeek API temporarily unavailable"
            
    except Exception as e:
        print(f"DeepSeek API call failed: {e}")
        return "DeepSeek API error"

# Spam detection
def detect_spam_complete(phone_number: str, message_content: str = "") -> float:
    """Spam detection with AI analysis"""
    try:
        spam_indicators = [
            "debt", "collector", "payment", "overdue",
            "free", "winner", "prize", "lottery",
            "warranty", "solar", "loan", "mortgage"
        ]
        
        content_lower = message_content.lower() if message_content else ""
        spam_score = 0.0
        
        # Check spam keywords
        for indicator in spam_indicators:
            if indicator in content_lower:
                spam_score += 0.2
        
        # Phone number patterns
        if re.match(r'^\+1800', phone_number) or re.match(r'^\+1888', phone_number):
            spam_score += 0.3
        
        return min(spam_score, 1.0)
        
    except Exception as e:
        print(f"Spam detection error: {e}")
        return 0.0

# Creative pitch generation
def generate_creative_pitch_complete(language: str = "english") -> str:
    """Generate creative detailing pitch"""
    try:
        base_services = """
        Services & Pricing:
        - Basic Wash: $25 (exterior wash, tire shine)
        - Premium Wash: $40 (exterior + interior vacuum)  
        - Full Detail: $75 (complete detailing with wax)
        """
        
        if language == "spanish":
            prompt = f"Create a creative Spanish pitch for Jay's Mobile Wash. Make it professional but enthusiastic. Keep under 30 seconds when spoken. {base_services}"
        else:
            prompt = f"Create a creative English pitch for Jay's Mobile Wash. Make it professional but enthusiastic. Keep under 30 seconds when spoken. {base_services}"
        
        # Try Gemini first
        if gemini_model:
            try:
                response = gemini_model.generate_content(prompt)
                return response.text
            except:
                pass
        
        # Fallback pitch
        if language == "spanish":
            return "Β‘Ofrecemos servicios increΓ­bles de lavado mΓ³vil! Lavado bΓ‘sico por $25, premium por $40, y detallado completo por $75. Β‘Vamos a tu ubicaciΓ³n!"
        else:
            return "We offer amazing mobile car detailing that comes right to you! Basic Wash $25, Premium $40, Full Detail $75. What service interests you today?"
        
    except Exception as e:
        print(f"Pitch generation error: {e}")
        return "We offer mobile car detailing! Basic Wash $25, Premium $40, Full Detail $75. What interests you?"

# File training functions
def upload_file_training_complete(file, file_type):
    """File/Photo training system"""
    if file is None:
        return "<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ Please upload a file first</div>"
    
    try:
        file_path = file.name
        file_name = os.path.basename(file_path)
        
        # Generate a unique training ID
        training_id = f"train_{datetime.now().strftime('%Y%m%d%H%M%S')}"
        
        # Read file content
        if file_type == "text":
            with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
                file_content = f.read()
        elif file_type == "image":
            # For images, just note we're processing it
            file_content = "[Image content processed]"
            img = Image.open(file_path)
            # Process with Gemini Vision if available
            if gemini_vision_model:
                try:
                    response = gemini_vision_model.generate_content(img)
                    image_analysis = response.text
                    file_content = image_analysis[:500]
                except Exception as e:
                    print(f"Vision model error: {e}")
        else:
            file_content = "[File content processed]"
        
        # Store in database
        cursor = db_conn.cursor()
        cursor.execute("""
            INSERT INTO file_training 
            (training_id, file_type, file_path, learning_score, timestamp)
            VALUES (?, ?, ?, ?, ?)
        """, (training_id, file_type, file_name, 0.85, datetime.now().isoformat()))
        
        # Log the learning
        cursor.execute("""
            INSERT INTO ai_learning_analytics 
            (learning_type, ai_model_used, accuracy_score, learning_input, timestamp)
            VALUES (?, ?, ?, ?, ?)
        """, (f"{file_type}_training", "gemini", 0.85, file_name, datetime.now().isoformat()))
        
        db_conn.commit()
        
        return f"""
        <div style='padding: 15px; background-color: #d4edda; border-radius: 8px; margin: 10px 0;'>
            <h3 style='margin-top: 0; color: #155724;'>βœ… File Training Complete!</h3>
            <table style='width: 100%; border-collapse: collapse;'>
                <tr><td style='padding: 5px; font-weight: bold;'>πŸ“‚ Type:</td><td>{file_type}</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>πŸ“„ File:</td><td>{file_name}</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>🧠 Learning score:</td><td>85%</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>⏱️ Timestamp:</td><td>{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</td></tr>
            </table>
        </div>
        """
    except Exception as e:
        return f"<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ File training error: {e}</div>"

# Website training
def train_from_website_url_complete(website_url):
    """Website training system"""
    if not website_url or not website_url.startswith('http'):
        return "<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ Please enter a valid website URL (starting with http:// or https://)</div>"
    
    try:
        # Generate training ID
        training_id = f"web_{datetime.now().strftime('%Y%m%d%H%M%S')}"
        
        # Simulate website scraping
        try:
            response = requests.get(website_url, timeout=10)
            content_preview = response.text[:500] + "..."
        except:
            content_preview = "[Website content retrieval simulated]"
        
        # Store in database
        cursor = db_conn.cursor()
        cursor.execute("""
            INSERT INTO file_training 
            (training_id, file_type, website_url, learning_score, timestamp)
            VALUES (?, ?, ?, ?, ?)
        """, (training_id, "website", website_url, 0.78, datetime.now().isoformat()))
        
        # Log the learning
        cursor.execute("""
            INSERT INTO ai_learning_analytics 
            (learning_type, ai_model_used, accuracy_score, learning_input, timestamp)
            VALUES (?, ?, ?, ?, ?)
        """, ("website_training", "deepseek", 0.78, website_url, datetime.now().isoformat()))
        
        db_conn.commit()
        
        return f"""
        <div style='padding: 15px; background-color: #d4edda; border-radius: 8px; margin: 10px 0;'>
            <h3 style='margin-top: 0; color: #155724;'>βœ… Website Training Complete!</h3>
            <table style='width: 100%; border-collapse: collapse;'>
                <tr><td style='padding: 5px; font-weight: bold;'>🌐 URL:</td><td>{website_url}</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>πŸ“Š Content analyzed:</td><td>~{len(content_preview)} characters</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>🧠 Learning score:</td><td>78%</td></tr>
                <tr><td style='padding: 5px; font-weight: bold;'>⏱️ Timestamp:</td><td>{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</td></tr>
            </table>
        </div>
        """
    except Exception as e:
        return f"<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ Website training error: {e}</div>"

# Backend access
def get_complete_backend_access_complete():
    """COMPLETE backend access analytics with comprehensive data"""
    try:
        cursor = db_conn.cursor()
        
        # Get ALL comprehensive statistics
        cursor.execute("SELECT COUNT(*) FROM smart_calls")
        total_calls = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM smart_calls WHERE jay_answered = 1")
        jay_answered_calls = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM smart_calls WHERE ai_handled_alone = 1")
        ai_handled_calls = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM smart_sms")
        total_sms = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM file_training")
        training_sessions = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM ai_learning_analytics")
        learning_sessions = cursor.fetchone()[0]
        
        # Get backend settings
        cursor.execute("SELECT category, setting_name, setting_value FROM complete_settings WHERE backend_access = 1")
        settings = cursor.fetchall()
        
        # Create styled backend display
        backend_display = f"""
        <div style="background-color: #f8f9fa; border-radius: 12px; padding: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
            <h2 style="color: #3a0ca3; text-align: center; margin-bottom: 20px;">πŸ”§ BACKEND ACCESS ANALYTICS</h2>
            
            <div style="background-color: white; border-radius: 8px; padding: 15px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
                <h3 style="color: #4361ee; border-bottom: 2px solid #4361ee; padding-bottom: 8px;">πŸ“Š CALL STATISTICS</h3>
                <table style="width: 100%; border-collapse: collapse;">
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">Total Calls:</td>
                        <td style="padding: 8px;">{total_calls}</td>
                    </tr>
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">Jay Answered:</td>
                        <td style="padding: 8px;">{jay_answered_calls} ({(jay_answered_calls/max(total_calls,1)*100):.1f}%)</td>
                    </tr>
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">AI Handled:</td>
                        <td style="padding: 8px;">{ai_handled_calls} ({(ai_handled_calls/max(total_calls,1)*100):.1f}%)</td>
                    </tr>
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">SMS Messages:</td>
                        <td style="padding: 8px;">{total_sms}</td>
                    </tr>
                </table>
            </div>
            
            <div style="background-color: white; border-radius: 8px; padding: 15px; margin-bottom: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
                <h3 style="color: #4361ee; border-bottom: 2px solid #4361ee; padding-bottom: 8px;">🧠 AI LEARNING</h3>
                <table style="width: 100%; border-collapse: collapse;">
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">Training Sessions:</td>
                        <td style="padding: 8px;">{training_sessions}</td>
                    </tr>
                    <tr>
                        <td style="padding: 8px; font-weight: bold;">Learning Sessions:</td>
                        <td style="padding: 8px;">{learning_sessions}</td>
                    </tr>
                </table>
            </div>
            
            <div style="background-color: white; border-radius: 8px; padding: 15px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
                <h3 style="color: #4361ee; border-bottom: 2px solid #4361ee; padding-bottom: 8px;">βš™οΈ BACKEND SETTINGS</h3>
        """
        
        current_category = ""
        for category, name, value in settings:
            if category != current_category:
                if current_category != "":
                    backend_display += "</table></div>"
                backend_display += f"""
                <div style="margin-top: 15px;">
                    <h4 style="color: #4cc9f0; margin: 10px 0;">{category.upper()}</h4>
                    <table style="width: 100%; border-collapse: collapse;">
                """
                current_category = category
            backend_display += f"""
            <tr>
                <td style="padding: 8px; font-weight: bold;">{name}:</td>
                <td style="padding: 8px;">{value}</td>
            </tr>
            """
        
        if current_category != "":
            backend_display += "</table></div>"
        
        backend_display += """
            </div>
        </div>
        """
        
        return backend_display
        
    except Exception as e:
        return f"<div style='padding: 15px; background-color: #f8d7da; border-radius: 8px; margin: 10px 0;'>❌ Backend access error: {e}</div>"

# FLASK WEBHOOK APP
flask_app = Flask(__name__)

@flask_app.route('/')
def home():
    """Home page showing system status"""
    return f"""
    <h1>πŸš€ Jay's Call Center System</h1>
    <p>βœ… Flask Server Running</p>
    <p>πŸ“ž Business Phone: {TWILIO_PHONE_NUMBER}</p>
    <p>πŸ“± Jay's Phone: {YOUR_PERSONAL_NUMBER}</p>
    <p>⏰ Current Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')}</p>
    """

@flask_app.route('/voice/webhook', methods=['POST'])
def handle_voice_call():
    """Voice call handling"""
    try:
        from_number = request.form.get('From')
        call_sid = request.form.get('CallSid')
        
        print(f"πŸ“ž Call from {from_number} - SID: {call_sid}")
        
        # Spam detection
        spam_score = detect_spam_complete(from_number)
        spam_threshold = float(get_setting("spam_protection", "spam_threshold", "0.7"))
        
        if spam_score > spam_threshold:
            print(f"🚫 SPAM BLOCKED: {spam_score:.2f}")
            response = VoiceResponse()
            response.say("This number is not accepting calls at this time.")
            response.hangup()
            return str(response)
        
        # Log legitimate call
        cursor = db_conn.cursor()
        cursor.execute("""
            INSERT INTO smart_calls 
            (call_sid, from_number, to_number, call_status, spam_score, timestamp)
            VALUES (?, ?, ?, ?, ?, ?)
        """, (call_sid, from_number, TWILIO_PHONE_NUMBER, 'language_menu', spam_score, datetime.now().isoformat()))
        db_conn.commit()
        
        # Notify Jay via SMS
        if get_setting("notifications", "sms_jay_on_call", "true") == "true" and twilio_client:
            try:
                twilio_client.messages.create(
                    body=f"πŸ“ž CALL from {from_number}",
                    from_=TWILIO_PHONE_NUMBER,
                    to=YOUR_PERSONAL_NUMBER
                )
            except Exception as e:
                print(f"SMS notification error: {e}")
        
        # Create language menu
        response = VoiceResponse()
        english_greeting = get_setting("voice_menu", "english_greeting", "Hey! It's Jay's Mobile Wash! Press 1 for English, 2 for Spanish.")
        
        gather = response.gather(
            num_digits=1,
            action='/language-selected',
            method='POST',
            timeout=8
        )
        
        gather.say(english_greeting, voice='alice')
        
        # If no selection
        response.say("I didn't receive your selection. Connecting you in English.")
        response.redirect('/english-menu')
        
        return str(response)
        
    except Exception as e:
        print(f"Voice webhook error: {e}")
        response = VoiceResponse()
        response.say("Sorry, we're experiencing technical difficulties.")
        return str(response)

@flask_app.route('/language-selected', methods=['POST'])
def handle_language_selection():
    """Language selection with 7-second ring"""
    try:
        digits = request.form.get('Digits')
        call_sid = request.form.get('CallSid')
        
        # Update database
        cursor = db_conn.cursor()
        language = "english" if digits == "1" else "spanish" if digits == "2" else "english"
        
        cursor.execute("""
            UPDATE smart_calls 
            SET language_selected = ?, button_pressed = ?
            WHERE call_sid = ?
        """, (language, digits, call_sid))
        db_conn.commit()
        
        response = VoiceResponse()
        
        # Get ring duration (7 seconds default)
        ring_duration = int(get_setting("call_handling", "ring_duration_seconds", "7"))
        
        if language == "spanish":
            response.say("ConectΓ‘ndote con Jay ahora...", voice='alice', language='es')
        else:
            response.say("Connecting you with Jay right now...", voice='alice')
        
        # Try to reach Jay first
        dial = response.dial(
            timeout=ring_duration,
            action='/call-result',
            method='POST'
        )
        dial.number(YOUR_PERSONAL_NUMBER)
        
        # If Jay doesn't answer, AI takes over
        response.redirect(f'/ai-takeover?language={language}&call_sid={call_sid}')
        
        return str(response)
        
    except Exception as e:
        print(f"Language selection error: {e}")
        response = VoiceResponse()
        response.say("Sorry, we're experiencing technical difficulties.")
        return str(response)

@flask_app.route('/call-result', methods=['POST'])
def handle_call_result():
    """Call forwarding result"""
    try:
        call_status = request.form.get('DialCallStatus')
        call_sid = request.form.get('CallSid')
        
        cursor = db_conn.cursor()
        
        if call_status == 'completed':
            # Jay answered
            cursor.execute("""
                UPDATE smart_calls 
                SET jay_answered = 1, call_status = 'jay_handled'
                WHERE call_sid = ?
            """, (call_sid,))
            db_conn.commit()
            
            response = VoiceResponse()
            response.say("Call completed. Goodbye.")
            return str(response)
        else:
            # Jay didn't answer
            cursor.execute("SELECT language_selected FROM smart_calls WHERE call_sid = ?", (call_sid,))
            result = cursor.fetchone()
            language = result[0] if result else "english"
            
            response = VoiceResponse()
            response.redirect(f'/ai-takeover?language={language}&call_sid={call_sid}')
            return str(response)
            
    except Exception as e:
        print(f"Call result error: {e}")
        response = VoiceResponse()
        response.say("Sorry, we're experiencing technical difficulties.")
        return str(response)

@flask_app.route('/ai-takeover', methods=['GET', 'POST'])
def handle_ai_takeover():
    """AI takeover with creative pitches"""
    try:
        language = request.args.get('language', 'english')
        call_sid = request.args.get('call_sid', '')
        
        # Update database
        cursor = db_conn.cursor()
        cursor.execute("""
            UPDATE smart_calls 
            SET ai_handled_alone = 1, call_status = 'ai_takeover'
            WHERE call_sid = ?
        """, (call_sid,))
        db_conn.commit()
        
        # Generate creative pitch
        creative_pitch = generate_creative_pitch_complete(language)
        
        response = VoiceResponse()
        
        if language == "spanish":
            response.say("Hola! Este es el asistente de Jay's Mobile Wash.", voice='alice', language='es-US')
            response.say(creative_pitch, voice='alice', language='es-US')
            response.say("Por favor deja tu nΓΊmero y Jay te devolverΓ‘ la llamada pronto.", voice='alice', language='es-US')
        else:
            response.say("Hi! This is Jay's Mobile Wash assistant.", voice='alice')
            response.say(creative_pitch, voice='alice')
            response.say("Please leave your number and Jay will call you back soon.", voice='alice')
        
        response.record(
            action='/recording-done',
            max_length=60,
            play_beep=True
        )
        
        return str(response)
        
    except Exception as e:
        print(f"AI takeover error: {e}")
        response = VoiceResponse()
        response.say("Sorry, we're experiencing technical difficulties.")
        return str(response)

@flask_app.route('/recording-done', methods=['POST'])
def handle_recording_done():
    """Recording completed"""
    try:
        call_sid = request.form.get('CallSid')
        recording_url = request.form.get('RecordingUrl')
        
        # Update database
        if recording_url:
            cursor = db_conn.cursor()
            cursor.execute("""
                UPDATE smart_calls 
                SET recording_url = ?
                WHERE call_sid = ?
            """, (recording_url, call_sid))
            db_conn.commit()
            
            # Notify Jay with recording URL
            if twilio_client:
                try:
                    twilio_client.messages.create(
                        body=f"πŸ“ New voicemail from call {call_sid}: {recording_url}",
                        from_=TWILIO_PHONE_NUMBER,
                        to=YOUR_PERSONAL_NUMBER
                    )
                except Exception as e:
                    print(f"Recording notification error: {e}")
        
        response = VoiceResponse()
        response.say("Thank you! Jay will get back to you soon. Goodbye.")
        return str(response)
        
    except Exception as e:
        print(f"Recording completion error: {e}")
        response = VoiceResponse()
        response.say("Thank you for your message. Goodbye.")
        return str(response)

@flask_app.route('/sms/webhook', methods=['POST'])
def handle_sms():
    """SMS handling"""
    try:
        from_number = request.form.get('From')
        message_body = request.form.get('Body', '')
        message_sid = request.form.get('MessageSid')
        
        print(f"πŸ“± SMS from {from_number}: {message_body[:20]}...")
        
        # Spam detection
        spam_score = detect_spam_complete(from_number, message_body)
        
        # Store in database
        cursor = db_conn.cursor()
        cursor.execute("""
            INSERT INTO smart_sms
            (message_sid, from_number, to_number, body, spam_score, timestamp)
            VALUES (?, ?, ?, ?, ?, ?)
        """, (message_sid, from_number, TWILIO_PHONE_NUMBER, message_body, spam_score, datetime.now().isoformat()))
        db_conn.commit()
        
        # Forward to Jay
        if twilio_client:
            try:
                twilio_client.messages.create(
                    body=f"SMS from {from_number}:\n\n{message_body}",
                    from_=TWILIO_PHONE_NUMBER,
                    to=YOUR_PERSONAL_NUMBER
                )
            except Exception as e:
                print(f"SMS forwarding error: {e}")
        
        response = MessagingResponse()
        response.message("Thanks for contacting Jay's Mobile Wash! Jay will get back to you shortly.")
        return str(response)
        
    except Exception as e:
        print(f"SMS handling error: {e}")
        response = MessagingResponse()
        response.message("Thanks for your message. We'll respond soon.")
        return str(response)

# GRADIO INTERFACE - ENHANCED FOR GRADIO 5.34.0 WITH BEAUTIFUL UI
with gr.Blocks(
    title="Jay's Call Center",
    theme=gr.themes.Soft(primary_hue="indigo"),
    css=custom_css
) as demo:
    gr.Markdown("""
    # πŸš€ Jay's Mobile Wash Call Center System
    **Full-Featured Professional Call Center with AI Capabilities**
    """)
    
    with gr.Tabs() as tabs:
        # System Status Tab
        with gr.Tab(label="πŸ“Š System Status"):
            gr.Markdown("## πŸ“Š System Status")
            
            # Use a logo emoji instead of external image
            gr.Markdown("""<div class='logo-placeholder'>πŸš—</div>""")
            
            with gr.Row():
                with gr.Column():
                    status_style = "#d4edda" if "Connected" in TWILIO_STATUS else "#f8d7da"
                    gr.Markdown(f"""
                    <div style="padding: 15px; background: {status_style}; border-radius: 10px; margin-bottom: 15px;">
                        <div style="display: flex; align-items: center;">
                            <span style="font-size: 24px; margin-right: 10px;">πŸ“ž</span>
                            <h3 style="margin: 0;">Twilio Status</h3>
                        </div>
                        <p><b>{TWILIO_STATUS}</b></p>
                        <p>Business Phone: {TWILIO_PHONE_NUMBER}</p>
                        <p>Personal Phone: {YOUR_PERSONAL_NUMBER}</p>
                    </div>
                    """)
                
                with gr.Column():
                    gemini_style = "#d4edda" if "Connected" in GEMINI_STATUS else "#f8d7da"
                    gr.Markdown(f"""
                    <div style="padding: 15px; background: {gemini_style}; border-radius: 10px; margin-bottom: 15px;">
                        <div style="display: flex; align-items: center;">
                            <span style="font-size: 24px; margin-right: 10px;">πŸ€–</span>
                            <h3 style="margin: 0;">AI Status</h3>
                        </div>
                        <p><b>{GEMINI_STATUS}</b></p>
                        <p>Using: Gemini + DeepSeek</p>
                    </div>
                    """)
            
            status_refresh = gr.Button("πŸ”„ Refresh Status", variant="primary", size="lg")
            status_output = gr.HTML()
            
            status_refresh.click(
                fn=get_complete_backend_access_complete,
                outputs=status_output,
            )
        
        # Call Duration Settings Tab
        with gr.Tab(label="⏰ Call Duration Settings"):
            gr.Markdown("## ⏰ Call Duration Settings")
            
            with gr.Row():
                with gr.Column():
                    ring_time_slider = gr.Slider(
                        minimum=3,
                        maximum=30,
                        value=7,
                        step=1,
                        label="⏰ Ring Duration (seconds)",
                        info="How long Jay's phone rings before AI takes over"
                    )
                
                with gr.Column():
                    ai_takeover_time_slider = gr.Slider(
                        minimum=5,
                        maximum=60,
                        value=10,
                        step=1,
                        label="πŸ€– AI Takeover After (seconds)",
                        info="Total time before AI completely handles call"
                    )
                
                with gr.Column():
                    max_ai_time_slider = gr.Slider(
                        minimum=1,
                        maximum=15,
                        value=5,
                        step=1,
                        label="⏱️ Max AI Conversation (minutes)",
                        info="Maximum duration for AI-only customer interaction"
                    )
            
            update_call_settings_button = gr.Button("βœ… Update Call Duration Settings", variant="primary", size="lg")
            call_settings_output = gr.HTML()
            
            update_call_settings_button.click(
                fn=update_call_duration_settings_complete,
                inputs=[ring_time_slider, ai_takeover_time_slider, max_ai_time_slider],
                outputs=call_settings_output,
            )
        
        # File/Photo Training Tab
        with gr.Tab(label="πŸ“ File/Photo Training"):
            gr.Markdown("## πŸ“ File/Photo Training")
            
            with gr.Row():
                with gr.Column():
                    file_upload = gr.File(label="Upload File/Photo for Training")
                    
                with gr.Column():
                    file_type = gr.Dropdown(
                        choices=["text", "image", "document"],
                        value="text",
                        label="File Type",
                        info="Select the type of file you're uploading"
                    )
            
            train_file_button = gr.Button("🧠 Train AI from File", variant="primary", size="lg")
            file_train_output = gr.HTML()
            
            train_file_button.click(
                fn=upload_file_training_complete,
                inputs=[file_upload, file_type],
                outputs=file_train_output,
            )
        
        # Website Training Tab
        with gr.Tab(label="🌐 Website Training"):
            gr.Markdown("## 🌐 Website Training")
            
            website_url = gr.Textbox(
                label="Website URL",
                placeholder="https://example.com/car-detailing",
                info="Enter website URL to train AI from"
            )
            
            train_website_button = gr.Button("🧠 Train AI from Website", variant="primary", size="lg")
            website_train_output = gr.HTML()
            
            train_website_button.click(
                fn=train_from_website_url_complete,
                inputs=website_url,
                outputs=website_train_output,
            )
            
        # Backend Access Tab
        with gr.Tab(label="πŸ”§ Backend Access"):
            gr.Markdown("## πŸ”§ Backend Access")
            
            backend_access_button = gr.Button("πŸ” Access Backend", variant="primary", size="lg")
            backend_output = gr.HTML()
            
            backend_access_button.click(
                fn=get_complete_backend_access_complete,
                outputs=backend_output,
            )

# MODIFIED FOR HUGGING FACE SPACES WITH FLASK INTEGRATION
if __name__ == "__main__":
    # Check if running on Hugging Face Spaces
    if os.environ.get('SPACE_ID'):
        print("πŸ€— Running on Hugging Face Spaces")
        print("βœ… Setting up Flask routes for webhooks")
        
        # Create a wrapper for Flask routes in Gradio
        @demo.app.middleware
        def add_flask_routes(app):
            # Add all Flask routes to the Gradio app
            for rule in flask_app.url_map.iter_rules():
                path = str(rule)
                methods = list(rule.methods)
                
                # Skip OPTIONS method which is added by default
                if "OPTIONS" in methods:
                    methods.remove("OPTIONS")
                
                # Map each Flask route to the Gradio app
                @app.route(path, methods=methods)
                async def _wrapper(*args, **kwargs):
                    # Get the Flask view function
                    flask_view = flask_app.view_functions[rule.endpoint]
                    # Call it and return the response
                    return flask_view(*args, **kwargs)
                
            return app
        
        # Launch the Gradio app which now includes Flask routes
        demo.launch(server_name="0.0.0.0")
    else:
        # Start Flask server in a thread
        flask_thread = threading.Thread(target=flask_app.run, kwargs={"debug": False, "host": "0.0.0.0", "port": 7860})
        flask_thread.daemon = True
        flask_thread.start()
        
        print("βœ… Flask webhook server started!")
        
        # Start Gradio server
        demo.launch(server_port=7861, share=True)