Repository navigation
Expand file tree
/
Copy pathmain_engine.py
More file actions
1101 lines (949 loc) · 53.6 KB
/
Copy pathmain_engine.py
File metadata and controls
1101 lines (949 loc) · 53.6 KB
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
from datetime import datetime, timedelta
import difflib
import glob
import json
import math
import os
import random
import re
import sqlite3
from collections import Counter
# --- TENSORFLOW NEURAL NETWORK IMPORTS ---
try:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
TF_AVAILABLE = True
print("\n[TensorFlow Active] Real Neural Network Brain Activated! 🚀")
except ImportError:
TF_AVAILABLE = False
print("\n[Notice] TensorFlow not found. Running on standard fallback mode.")
class KnowledgeDatabaseManager:
"""Exact v5.6 SQLite Knowledge Database Manager (tech_knowledge table)"""
def __init__(self, db_name="knowledge.db"):
self.db_name = db_name
self.init_sqlite_db()
def init_sqlite_db(self):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tech_knowledge (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT UNIQUE,
definition TEXT
)
"""
)
cursor.execute("SELECT COUNT(*) FROM tech_knowledge")
if cursor.fetchone()[0] == 0:
defaults = [
(
"cpu",
"CPU (Central Processing Unit) computer ka brain hota hai jo saari calculations aur instructions execute karta hai.",
),
(
"ram",
"RAM (Random Access Memory) temporary memory hoti hai jo open apps aur active tasks ko fast run karti hai.",
),
(
"python",
"Python ek high-level, interpreted programming language hai jo AI, Web Dev aur Automation me use hoti hai.",
),
(
"html",
"HTML (HyperText Markup Language) web pages ka basic structure banane ke liye istemal hoti hai.",
),
(
"sql",
"SQL (Structured Query Language) databases me data store, search aur manage karne ki language hai.",
),
]
cursor.executemany(
"INSERT OR IGNORE INTO tech_knowledge (topic, definition) VALUES (?, ?)",
defaults,
)
conn.commit()
conn.close()
def search_sqlite_knowledge(self, query):
words = re.findall(r"\w+", query.lower())
ignore = {
"kya",
"hai",
"kaun",
"sa",
"batao",
"bata",
"kisi",
"ko",
"bhai",
"samjha",
}
clean_words = [w for w in words if w not in ignore and len(w) > 1]
if not clean_words:
return None
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
for word in clean_words:
cursor.execute(
"SELECT topic, definition FROM tech_knowledge WHERE LOWER(topic) = ? OR LOWER(topic) LIKE ?",
(word, f"%{word}%"),
)
row = cursor.fetchone()
if row:
conn.close()
return f"🧠 **Knowledge Base ({row[0].upper()}):** {row[1]}"
conn.close()
return None
def add_custom_topic(self, topic, definition):
conn = sqlite3.connect(self.db_name)
cursor = conn.cursor()
cursor.execute(
"INSERT OR REPLACE INTO tech_knowledge (topic, definition) VALUES (?, ?)",
(topic.lower().strip(), definition.strip())
)
conn.commit()
conn.close()
class SmartIntentClassifier:
"""V7.0 Deep Learning TensorFlow Intent Classifier Engine"""
def __init__(self):
self.model = None
self.tokenizer = None
self.tag_to_idx = {}
self.idx_to_tag = {}
self.max_len = 20
def tokenize_text(self, text):
words = re.findall(r"\w+", text.lower())
ignore = {"bhai", "bro", "oo", "ooo", "aa", "aah"}
filtered = [w for w in words if w not in ignore]
return filtered if filtered else words
def train(self, memory_db):
if not TF_AVAILABLE or not isinstance(memory_db, list) or not memory_db:
return
training_sentences = []
training_labels = []
tags = []
for item in memory_db:
if not isinstance(item, dict):
continue
tag = item.get("tag")
patterns = item.get("patterns", [])
if tag and tag not in tags:
tags.append(tag)
for p in patterns:
training_sentences.append(p)
training_labels.append(tag)
if not training_sentences:
return
self.tag_to_idx = {tag: i for i, tag in enumerate(tags)}
self.idx_to_tag = {i: tag for tag, i in self.tag_to_idx.items()}
y_train = [self.tag_to_idx[label] for label in training_labels]
self.tokenizer = Tokenizer(num_words=1000, oov_token="<OOV>")
self.tokenizer.fit_on_texts(training_sentences)
sequences = self.tokenizer.texts_to_sequences(training_sentences)
X_train = pad_sequences(sequences, maxlen=self.max_len, padding='post')
y_train = np.array(y_train)
vocab_size = len(self.tokenizer.word_index) + 1
num_classes = len(tags)
self.model = Sequential([
Embedding(vocab_size, 16, input_length=self.max_len),
GlobalAveragePooling1D(),
Dense(16, activation='relu'),
Dense(num_classes, activation='softmax')
])
self.model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
self.model.fit(X_train, y_train, epochs=100, verbose=0)
def predict_intent(self, text, threshold=0.40):
if not TF_AVAILABLE or not self.model or not self.tokenizer:
return None, 0.0
sequence = self.tokenizer.texts_to_sequences([text])
padded = pad_sequences(sequence, maxlen=self.max_len, padding='post')
prediction = self.model.predict(padded, verbose=0)[0]
max_idx = np.argmax(prediction)
confidence = float(prediction[max_idx])
if confidence >= threshold:
predicted_tag = self.idx_to_tag.get(max_idx)
return predicted_tag, confidence
return None, 0.0
class MainAIEngine:
def __init__(self, user_name="Pankaj"):
self.user_name = user_name.strip().lower()
self.session_history = []
self.last_unknown_query = None
self.current_context = None
self.last_responses = {}
self.db_manager = KnowledgeDatabaseManager("knowledge.db")
self.memory_file = self.handle_smart_memory_file()
self.memory_db = []
self.classifier = SmartIntentClassifier()
self.load_memory()
def get_non_repeating_choice(self, tag, responses):
if not responses:
return ""
if len(responses) == 1:
return responses[0]
last_resp = self.last_responses.get(tag)
available = [r for r in responses if r != last_resp]
if not available:
available = responses
chosen = random.choice(available)
self.last_responses[tag] = chosen
return chosen
def handle_smart_memory_file(self):
if self.user_name == "pankaj":
master_file = "ai_memory_pankaj_master.json"
if os.path.exists(master_file):
print(
f"\n[Access Granted] Welcome Developer Pankaj! Master File: {master_file}"
)
else:
print(
f"\n[Info] Creating Developer Master Brain: {master_file}"
)
return master_file
if "pankaj" in self.user_name:
print("\n" + "=" * 65)
print("❌ ACCESS DENIED: Cannot use developer signature name!")
print("=" * 65 + "\n")
self.user_name = "guest_user"
existing_files = glob.glob("ai_memory_*.json")
user_specific_files = [
f for f in existing_files if f"_{self.user_name}_" in f
]
if user_specific_files:
print(f"\n[Info] Memory File Matched: {user_specific_files[0]}")
return user_specific_files[0]
random_id = random.randint(1000, 9999)
new_file = f"ai_memory_{self.user_name}_{random_id}.json"
print(f"\n[Info] New Memory File Auto-Created: {new_file}")
return new_file
def load_memory(self):
default_data = [
{
"tag": "greeting",
"patterns": ["hi", "hello", "hey", "namaste", "oi", "hello bhai", "kaise ho", "kya haal hai", "hey bro", "wassup"],
"responses": ["Arey {name} bhai! Kya scene hai aaj ka? 😎", "Oi {name}! Bol bhai, aaj kya naya chal raha hai?", "Hey {name}! Kya haal chaal? Kuch spicy khabar hai kya aaj?"],
"context_responses": {"thik": "Badiya bhai! Aur batao aaj kya plan hai?", "mast": "Sahi hai bhai, aise hi mast raho! 😎", "badhiya": "Ekdam zabardast! Kuch khas chal raha hai kya?"}
},
{
"tag": "status_ok",
"patterns": ["theek hu", "thik hu", "mast hu", "badhiya hu", "badiya hu", "sab sahi hai", "ekdam badiya", "mast hu bhai", "all good", "ha sab theek hai", "ha sab theek hai tu bata", "sab theek hai"],
"responses": ["Sahi hai bawa! Mast raho ekdam! Waise aaj kya special khaya? 🍕", "Sunkar badhiya laga {name}! Chal bata fir, aaj din kaisa gaya? 🔥", "Badiya bhai! Vaise aaj koi naya kand kiya ya chup chaap baithe ho? 👀"],
"context_responses": {}
},
{
"tag": "acknowledgement",
"patterns": ["ok", "okay", "k", "hmm", "accha", "achha", "sahi hai", "got it", "samajh gaya", "thik hai"],
"responses": ["Set hai bhai! 👍 Aur batao, koi naye gaane sun rahe ho kya aajkal?", "Ekdam clear! Waise abhi free ho ya koi kaam nipta rahe ho?", "Done bhai! Waise itna chup kyo ho, kuch bolo na! 😁"],
"context_responses": {}
},
{
"tag": "courtesy",
"patterns": ["thanks", "thank you", "shukriya", "dhanyawad", "thanks bhai", "thx"],
"responses": ["Arre chill karo {name} bhai! Apne hi bande ho. 🙌", "Arre welcome bhai! Isme thanks kaisa, party do ab! 🍕", "Always active tere liye {name}! Kuch aur chahiye toh bol!"],
"context_responses": {}
},
{
"tag": "goodbye",
"patterns": ["bye", "goodbye", "chalo bye", "see you", "alvida", "tc", "take care"],
"responses": ["Chalo {name} bhai! Milte hain break ke baad. 👋", "Jao jao, aaram karo! Apna khyal rakhna bhai! 🔥", "Tata bhai! Kuch lafda ho toh turant yaad karna!"],
"context_responses": {}
},
{
"tag": "agreement_disagreement",
"patterns": ["nahi chahiye", "no", "na", "bilkul nahi"],
"responses": ["Koi nahi bhai, jaisa tera mood! 🤝 Kuch aur baat karein?", "Theek hai bhai, no problem. Waise bored toh nahi ho rahe?", "Sahi hai! Jab zaroorat lage bol dena, apun idhar hi hai."],
"context_responses": {}
},
{
"tag": "casual_chitchat",
"patterns": ["kuch nahi", "kuchh nahi", "kuch nahi chal raha hai", "kuchh nahi chal raha hai", "nahi soch aise hi baat kar lu", "aise hi", "bas aise hi", "kuch khas nahi", "life ma toh kuch nahi ho raha hai", "life me kuch nahi ho raha", "kuch nahi ho raha"],
"responses": ["Arey aise kaise kuch nahi! Koi mast movie ya series dekhi hogi? 🍿", "Bina kisi वजह ke baat karne ka hi alag maza hai! Bol fir kya chal raha hai mind me? ✨", "Kuch khas nahi? Arey chill karo fir! Koi accha gaana suggest karu kya?"],
"context_responses": {"ha": "Arijit Singh ka 'Kesariya' ya 'Tum Hi Ho' suno, mood ekdam mast ho jayega! 🎧🎶", "kar": "Arijit Singh ka 'Kesariya' ya 'Tum Hi Ho' suno, mood ekdam mast ho jayega! 🎧🎶", "yes": "Arijit Singh ka 'Kesariya' ya 'Tum Hi Ho' suno, mood ekdam mast ho jayega! 🎧🎶", "na": "Koi baat nahi bhai! Fir kya karne ka plan hai?"}
},
{
"tag": "compliment",
"patterns": ["good", "nice", "great", "badiya", "bohot achha", "mast", "oo bahi good", "op", "superb", "awesome"],
"responses": ["Dil jeet liya {name} bhai! ❤️ Aur batao itni tareef ka raaz?", "Thanks brother! 🔥 Tu bhi ekdam OP banda hai!", "Khushi hui sunkar! Aise hi vibe banaye rakho! 😎"],
"context_responses": {}
},
{
"tag": "bot_identity",
"patterns": ["tum kon hon", "tum kaun ho", "who are you", "tumhe kisne banaya", "who created you", "about yourself", "tera naam kya hai", "tera naam kya", "tera naam"],
"responses": ["Main INFINITY-AI hoon! 🤖 Mujhe Pankaj Singh ne Python me scratch se banaya hai. Agar aapko developer ke baare me aur jaanna hai toh unke GitHub pe jaa sakte hain aur unke Discord pe pooch sakte hain sawal ya koi suggestion. Discord ki link unke GitHub profile me hai.\n\nDeveloper GitHub Profile: infinityminds-dev"],
"context_responses": {}
},
{
"tag": "daily_activity_suggestions",
"patterns": ["chal bata aaj kya karna hai", "aaj kya kare", "kya kare aaj", "aaj kya karna chahiye", "bor ho raha hu kya karu", "kya karu aaj", "bor ho raha hu", "kya plan hai ajj ka", "kya plan hai aaj ka"],
"responses": ["Arey bawa mast plan banate hain! 🎯 Coding ka mood hai, game khelne ka scene hai, ya chill music sunke aaram karna hai? Kya set karein?"],
"context_responses": {"music": "Mast playlist lagao aur headphone pehen ke chill karo bhai! 🎧🎶", "gaana": "Koi apna favourite gaana lagao aur speakers full kar do! 🎶🔊", "game": "Kaunsa game khelne ka plan hai? PC game ya mobile game? 🎮🔥", "mobile": "Mast BGMI, Free Fire ya Call of Duty lagao aur dosto ke saath machao! 📱💥", "pc": "Sahi hai! GTA, Valorant ya Counter-Strike me se kya chalayein? 🖥️🎯", "coding": "Sahi hai! Aaj kaunsa naya feature code karne wale ho? 💻⚡", "code": "Python me koi naya logic likhe kya aaj? 🐍⚡", "walk": "Sahi hai! Thodi taazi hawa lo aur fresh ho ke aao! 🚶♂️✨", "rest": "Badiya hai bhai! Thodi der aaram karo, mind fresh ho jayega. ベッド💤", "soja": "Aaram se neend poori karo, rest bohot zaroori hai! 😴💤"}
},
{
"tag": "music_intent",
"patterns": ["music", "music sun lo", "gaana sun leta hu", "chal music sun leta hu", "gaane sunne hain", "songs", "song sununga"],
"responses": ["Music hi toh asli vibe hai bhai! 🎶 Lofi, Punjabi, ya Rock—aaj kaunsi vibe chalegi?"],
"context_responses": {"punjabi": "Zabardast! High energy Punjabi beats lagao aur mood banao! 🎧🔥", "lofi": "Chilled Lofi beats suno, mind ekdam calm ho jayega! ☕🎶", "sad": "Arre kya hua bhai? Mood off hai kya jo sad songs sun rahe ho? 💔"}
},
{
"tag": "game_intent",
"patterns": ["game khelna hai", "mobile game", "pc game", "game khel lu", "chal game khelte hain", "gaming"],
"responses": ["Gaming mode ON! 🎮 BGMI, Valorant, ya GTA—aaj kahan machane ka plan hai?"],
"context_responses": {"bgmi": "Arey waah! Chicken Dinner pakka karke aana bhai! 🪖💥", "freefire": "Booyah maar ke aao bhai, full rush gameplay! 🎯🔥", "valorant": "Clutch marna padega aaj! Headshots ready rakho! 🖥️🎯", "gta": "City me ghoomne ka aur tabaahi machane ka plan hai lagta hai! 🚗💥"}
},
{
"tag": "tech_coding_fallback",
"patterns": ["python", "coding", "code", "programming", "programming language", "javascript", "html", "css", "java", "c++", "bug", "error", "loop", "function", "array", "database", "sql", "tech", "github", "tujha coding ka bare ma kuch pata hai", "tumhe coding ke bare me pata hai", "coding aati hai kya", "coding ke bare me pata hai", "loop kya hai", "Meri help karga coding ma"],
"responses": ["Mujhe abhi coding aur tech ke baare me zyada jankari nahi hai 😅. Iske liye aap ChatGPT ya Gemini jaise Large Language Models (LLM) se pooch sakte hain 🤖. Developer ne abhi mujhe itna develop nahi kiya hai, lekin in future main iska jawab zaroor de paunga!"],
"context_responses": {"python": "Python toh meri core language hai! Developer ne mujhe Python se hi banaya hai 🐍⚡", "help": "Main chote moti baatein samajh sakta hu, par heavy programming ke liye ChatGPT best rahega 🤖"}
},
{
"tag": "low_energy_mood",
"patterns": ["maan nahi kar raha hai kuch karne ka", "man nahi kar raha hai", "kuch karne ka man nahi hai", "aaj kuch nahi karna", "man nahi hai aaj", "aaj mood off hai", "thaka hua hu"],
"responses": ["Koi nahi bhai! Kabhi kabhi aaram karna hi best hota hai. Chill maaro, koi zabardasti nahi! 🛌"],
"context_responses": {"kya hua": "Koi tension mat lo bhai, chill karo. Baatein karni ho toh main hu na!", "kisi se baat nahi karni": "Koi baat nahi bhai, phone side me rakh kar thodi der aaram kar lo 🛌"}
},
{
"tag": "bot_status_query",
"patterns": ["tu bata", "tu bata bhai", "apna batao", "aur batao", "kya ho raha hai", "tu bata kya kar na hai"],
"responses": ["Main toh chill mode me hu bhai! Tu bata, aaj kya naya chal raha hai life me?", "ma toh hamesha ready hai! Tu bol aaj kya tufani karna hai?"],
"context_responses": {}
},
{
"tag": "custom_2064",
"patterns": ["ooo", "oo", "oho", "wah"],
"responses": ["Full bawaal vibe! 🔥💯"],
"context_responses": {}
},
{
"tag": "bot_capabilities",
"patterns": ["tu kya kar sakta hai", "tum kya kar sakte ho", "kya kya kar sakte ho", "tum kya kya kar sakte ho", "tumhare features kya hain", "what can you do"],
"responses": ["ma multitalented hu bhai! 🚀\n1. Chill baatein aur mood set karna 😎\n2. Math problems fast solve karna 🧮\n3. Date & Unit Conversion calculations 📅📐\n4. Jokes aur advice dena 💡\n5. Teri baatein aur memory yaad rakhna 🧠\n\nBol aaj kis cheez me madad chahiye?"],
"context_responses": {}
},
{
"tag": "bot_current_activity",
"patterns": ["tu kya kar raha hai", "tum kya kar rahe ho", "kya kar raha hai", "kya kar rahe ho"],
"responses": ["Bas {name} bhai, beth ke tere reply ka hi wait kar raha tha! Aur bata kya scene hai?"],
"context_responses": {}
},
{
"tag": "custom_5567",
"patterns": ["i am back", "wapas aa gaya", "back"],
"responses": ["Arey bawa aa gaya wapas! 🔥 Chal bata kahan gaye the?"],
"context_responses": {}
},
{
"tag": "custom_6423",
"patterns": ["good Morning bhai", "good morning", "good morning bro", "gm"],
"responses": ["Good Morning {name}! 🌅 Chai-paani ho gaya? Aaj kya machane wale ho?"],
"context_responses": {}
},
{
"tag": "custom_1812",
"patterns": ["Good night bhai", "good night", "good night bro", "gn"],
"responses": ["Good Night bhai! 🌙 Phone side me rakho aur mast neend lo!"],
"context_responses": {}
},
{
"tag": "custom_5087",
"patterns": ["Good evening", "good evening bhai", "good evening bro", "ge"],
"responses": ["Good Evening bhai! 🌆 Sham ka kya plan hai fir?"],
"context_responses": {}
},
{
"tag": "custom_afternoon",
"patterns": ["good afternoon", "good afternoon bhai", "afternoon bro"],
"responses": ["Good Afternoon {name} bhai! ☀️ Khana peena ho gaya na?"],
"context_responses": {}
},
{
"tag": "custom_jokes",
"patterns": ["joke sunao", "chutkula sunao", "koi joke batao", "hassa do bhai", "ek aur joke", "joke"],
"responses": [
"Teacher: 1 se 10 tak ginti sunao!\nStudent: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10!\nTeacher: Shabash! Aage?\nStudent: J, Q, K! 🃏😂",
"Ek aadmi doctor ke paas gaya: Doctor sahab jab mai chai pita hu to meri aakh me dard hota hai!\nDoctor: Pehle cup me se chammach nikal liya karo! ☕😜",
"Pappu: Yaar mera mobile pani me gir gaya aur kharab ho gaya.\nFriend: Toh dukandar ko dikhaya?\nPappu: Haan, usne bola isme rice me daal ke rakho.\nFriend: Phir kya hua?\nPappu: Kucch nahi, mobile chawal ke sath pak gaya! 📱🍚",
"Teacher: Tumne kal homework kyon nahi kiya?\nStudent: Sir, bijli nahi thi.\nTeacher: Toh mombatti jala lete!\nStudent: Sir, maachis nahi thi.\nTeacher: Maachis kyon nahi thi?\nStudent: Pooja ke ghar me rakhi thi.\nTeacher: Toh wahan se le aate!\nStudent: Sir, nahaaya hua nahi tha! 🕯️🤣",
"Customer: Bhai, is phone me net fast chalta hai kya?\nDukandar: Pata nahi bhai, kal ek banda le gaya tha, aaj tak wapas hi nahi aaya... speed me kahin aage nikal gaya hoga! 🚀📱",
"Papa: Beta tumhare result ka kya hua?\nBeta: Papa, doctor ka beta doctor bana, engineer ka beta engineer!\nPapa: Aur tum?\nBeta: Main toh mazdoor ka beta hoon na papa, toh fail ho gaya! 👷♂️📖",
"Husband: Aaj khane me kya banaya hai?\nWife: Jo tumne kal bola tha na wahi!\nHusband: Par kal toh maine bola tha ki zahar de do!\nWife: Toh bas wahi bana diya hai, chup chap kha lo! 🍜💀🤣",
"Santa: Yaar, mera dimaag bohot tez chalta hai!\nBanta: Kaise?\nSanta: Kal train chootne me 2 min bache the, main 1 min pehle hi pahunch gaya! 🏃♂️🚂",
"Boy: Tum itni sundar kaise ho?\nGirl: God ki kripa hai!\nBoy: Lagta hai God ne tum par poori kripa kar di, baaki sab par aalsi ho gaye! 🙄✨",
"Doctor: Aapko kaunsa rog hai?\nPatient: Doctor sahab, jab bhi kaam karne lagta hoon, neend aane lagti hai!\nDoctor: Ye rog nahi, isse alsi-pan kehte hain! 🛌💤",
"Pappu: Sir, computer crash ho gaya!\nSir: Re-boot karke dekha?\nPappu: Haan sir, teen baar joota mara par fir bhi nahi chala! 👟💻🤣",
"Teacher: Dharti ghoomti hai, iska proof kya hai?\nStudent: Sir kal raat ko ghar aaya toh mummy, papa, ghar sab ghoom rahe the! 🌎💫😜",
"Santa: Yaar, tune Bluetooth se gaana bheja tha, par aaya nahi!\nBanta: Arre bewakoof, raste me red light thi toh ruk gaya hoga! 🛑📱😆",
"Mom: Beta, TV band kar aur padhne baith!\nSon: Mummy, TV pe toh news aa rahi hai, gyan badh raha hai!\nMom: Aur jo result ke din danda padega usse kya badhega? 📺🧹🤣",
"Friend: Yaar, tension se kaise bachein?\nPappu: Seedhi baat hai bro, phone flight mode pe dalo aur so jao! ✈️😴💡",
"Exam Hall me Student:\n'Paper bohot tough aaya hai sir!'\nTeacher: 'Paper nahi beta, tumhari padhai weak hai!'\nStudent: 'Wo toh theek hai sir, par mark toh paper me milte hain, padhai me nahi!' 📝💥😆",
"Dukandar: Ye kapda bohot waterproof hai!\nCustomer: Achha? Thoda paani daal ke dikhao!\nDukandar: Arre bhai, dukaan me baarish karwaoge kya? 🌧️👕😂",
"Papa: Tum har waqt phone pe kya karte rehte ho?\nBeta: Papa, future ki planning kar raha hoon!\nPapa: Game me level up karne ko future planning nahi kehte! 🎮🤦♂️",
"Doctor: Aapko subah aur shaam walk karni chahiye!\nPatient: Sir, walk karne se kya hoga?\nDoctor: Pait kam hoga!\nPatient: Lekin sir, mera pait toh pehle se hi peeth se chipka hai! 🚶♂️🤣",
"Teacher: Gravity kya hai?\nStudent: Sir, jab koi cheez aasmaan me jaye aur seedhe aapke sar par gire, toh usse gravity kehte hain! 🍎🤯"
],
"context_responses": {
"aur sunao": "Ek aur suno: Doctor - Tumhare daant me kida kaise laga? Pappu - Mithai khate waqt sir! Doctor - Kida dikha nahi? Pappu - Arre wo mithai me chhup ke baitha tha! 🍬🦷🤣",
"hahaha": "Haste raho bhai, mood ekdam mast hona chahiye! 😂🔥",
"bekar": "Arre sorry bhai, agli baar super funny wala sunata hu! 😅"
}
},
{
"tag": "custom_advice",
"patterns": ["koi advice do", "mujhe advice chahiye", "kuch salah do", "tips do", "salah chahiye", "study advice", "focus kaise kare", "life tips"],
"responses": [
"Sahi salah chahiye toh simple baat yaad rakho {name} bhai: Ek waqt pe ek hi kaam par focus karo, distraction se door raho aur daily thodi mehnat karo! 🎯",
"Bhai meri sabse badi advice yehi hai: Consistency banaye rakho! Roz thoda thoda seekhoge toh long run me bohot aage nikal jaoge! 🚀",
"Agar padhai ya kaam me focus nahi ho raha, toh Pomodoro technique use karo: 25 min kaam + 5 min break! Boht sahi chalta hai! ⏱️💡",
"Galti karne se mat daro bhai! Har error aur failure ek nayi learning deke jata hai! Bold raho aur aage badho! 🔥"
],
"context_responses": {
"thik hai": "Sahi hai bhai, implement karke dekho, zaroor fayda hoga! 🚀",
"mushkil hai": "Shuru me mushkil lagta hai bhai, par roz thoda thoda try karoge toh aasan ho jayega! 💪"
}
},
{
"tag": "custom_7212",
"patterns": ["oo new respones", "hahahahahhahaha", "ya badiya tha", "good joke", "maza aa gaya", "mats joke tha"],
"responses": ["Thank you bhai apki training ki vajah se 🥰🔥", "Hehe! Haste raho bhai, mood ekdam fresh rehna chahiye! 😂"],
"context_responses": {}
},
{
"tag": "custom_3253",
"patterns": ["nachooook", "nacho", "dance"],
"responses": ["lekin kyo nachooo bhai kya baat hai", "Arre waah! Koi badi khushi ki baat hai kya bhai? 🕺🎉"],
"context_responses": {}
},
{
"tag": "express_frustration",
"patterns": ["aise kon karta hai", "aise kaun karta hai", "aise kon kar tha hai yaar", "kya hai yaar", "kya kar raha hai yaar", "ye kya baat hui", "kya scene hai yaar", "pagal hai kya"],
"responses": ["Arey sorry {name} bhai! 😅 Dimaag thoda idhar udhar ho gaya tha. Ab bata kya scene hai?", "Galti ho gayi bawa! 😂 Aisa nahi karunga ab, tu bata sahi se kya chal raha hai?", "Arre gussa mat ho bhai! Robot hu na, kabhi kabhi confuse ho jata hu 😜"],
"context_responses": {}
},
{
"tag": "user_silent",
"patterns": ["chup hu", "shaant hu", "kuch nahi bolna", "bolna nahi hai", "chup rehna hai", "shant hu"],
"responses": ["Arey itna chup kyo ho {name} bhai? Subah se koi kand kar diya kya? 👀", "Shanti bhi zaroori hai bawa, par mujhse kya chupa raha hai? Bol de! 😁", "Koi nahi bhai, aaram se baitho. Jab bolne ka mann kare batana! ☕"],
"context_responses": {}
},
{
"tag": "user_sad_cry",
"patterns": ["ro raha hu", "dukh hai", "duokhi hu", "sad hu", "mood kharab hai", "dil toot gaya"],
"responses": ["Arey kya hua {name} bhai? Rona mat, kisne pareshan kiya batayo mujhe! 😠", "Bhai tension mat le, sab theek ho jayega. Kuch hua hai kya?", "Chill maaro bhai! Ek baar lambi saans lo aur batao kya tension hai? 🫂"],
"context_responses": {}
},
{
"tag": "bot_confusion",
"patterns": ["samajh nahi aaya", "kya bola", "pata nahi", "kya keh raha hai", "samajh nahi tha"],
"responses": ["Arre lagta hai mera circuit thoda hil gaya! Phir se batao simple me 😅", "Arey sorry {name} bhai, thoda confuse ho gaya tha. Phir se bolo!"],
"context_responses": {}
},
{
"tag": "weather_inquiry",
"patterns": ["mausam kaisa hai", "aaj baarish hogi kya", "dhoop hai kya", "garmi hai"],
"responses": ["Mausam ka toh pata nahi bhai, par tu ghar pe baith ke chill maar! ☀️🌧️"],
"context_responses": {}
},
{
"tag": "bad_day_feeling",
"patterns": ["acha nahi tha", "achha nahi tha", "bura tha", "kuch khas nahi gaya", "din acha nahi tha", "din kharab tha", "bekar tha", "bekar gaya", "accha nahi gaya"],
"responses": ["Arey kya hua Pankaj bhai? Din me aisa kya ho gaya? 😔 Aaja dil halka kar!", "Oh ho! Kya tension ho gayi bhai? Koi lafda hua kya aaj?", "Koi nahi bhai, har din ek jaisa nahi hota. Thoda aaram karo aur chill maaro! ☕"],
"context_responses": {}
},
{
"tag": "clarification_correction",
"patterns": ["din ki baat kar raha hu", "teri baat nahi kar raha", "are bhai din ki baat kar raha hu teri nahi", "tujhe nahi bol raha", "dhyan se sun"],
"responses": ["Acha acha! My bad Pankaj bhai, main galat samajh gaya tha 😅 Haan toh bata, din me kya gadbad hui?", "Sahi pakde hain! Main thoda confuse ho gaya tha. Haan bata kya hua tha aaj?"],
"context_responses": {}
}
]
if os.path.exists(self.memory_file):
try:
with open(self.memory_file, "r", encoding="utf-8") as f:
data = json.load(f)
self.memory_db = data if isinstance(data, list) else default_data
except Exception:
self.memory_db = default_data
else:
self.memory_db = default_data
self.save_memory()
self.classifier.train(self.memory_db)
def clean_text_for_json(self, text):
if not isinstance(text, str):
return text
return text.strip()
def save_memory(self):
cleaned_db = []
if isinstance(self.memory_db, list):
for item in self.memory_db:
if not isinstance(item, dict):
continue
if "full_text" in item or item.get("source", "").endswith((".pdf", ".png", ".jpg", ".jpeg", ".webp")):
continue
clean_patterns = [
self.clean_text_for_json(p)
for p in item.get("patterns", [])
if self.clean_text_for_json(p)
]
clean_responses = [
self.clean_text_for_json(r)
for r in item.get("responses", [])
if self.clean_text_for_json(r)
]
if clean_patterns and clean_responses:
cleaned_db.append(
{
"tag": item.get("tag", "general"),
"patterns": clean_patterns,
"responses": clean_responses,
"context_responses": item.get("context_responses", {}),
}
)
with open(self.memory_file, "w", encoding="utf-8") as f:
json.dump(cleaned_db, f, indent=2, ensure_ascii=False)
self.classifier.train(self.memory_db)
def generate_proactive_greeting(self):
greetings = [
f"Hey {self.user_name.capitalize()}! Neural Network Engine v7.0 & Knowledge DB active hai! 😎",
f"Oi {self.user_name.capitalize()}! Deep Learning Engine v7.0 ready hai. Batao kya plan hai? 🚀",
f"Yo {self.user_name.capitalize()}! INFINITY-AI v7.0 neural mode par online hai! 🔥",
]
return random.choice(greetings)
def learn_new_response(self, user_query, correct_response):
query_clean = self.clean_text_for_json(user_query.lower())
response_clean = self.clean_text_for_json(correct_response)
if not query_clean or not response_clean:
return
learned = False
for item in self.memory_db:
if isinstance(item, dict) and "patterns" in item:
if query_clean in [p.lower() for p in item["patterns"]]:
if "responses" in item:
if response_clean not in item["responses"]:
item["responses"].append(response_clean)
learned = True
break
if not learned:
self.memory_db.append(
{
"tag": f"custom_{random.randint(1000, 9999)}",
"patterns": [query_clean],
"responses": [response_clean],
"context_responses": {},
}
)
self.save_memory()
# --- DATE & TIME UTILITY ENGINE ---
def handle_date_time_utilities(self, text_lower):
now = datetime.now()
if "kal" in text_lower:
if "pehle" in text_lower or "beeta" in text_lower:
target = now - timedelta(days=1)
return f"📅 **Past Date Info:** Kal **{target.strftime('%A')}** thi ({target.strftime('%d %B %Y')})."
else:
target = now + timedelta(days=1)
return f"📅 **Future Date Info:** Kal **{target.strftime('%A')}** hoga ({target.strftime('%d %B %Y')})."
if "parso" in text_lower:
if "pehle" in text_lower or "beeta" in text_lower:
target = now - timedelta(days=2)
return f"📅 **Past Date Info:** Parso **{target.strftime('%A')}** thi ({target.strftime('%d %B %Y')})."
else:
target = now + timedelta(days=2)
return f"📅 **Future Date Info:** Parso **{target.strftime('%A')}** hoga ({target.strftime('%d %B %Y')})."
match_days = re.search(r"(\d+)\s*din\s*(baad|bad|bada|pehle)", text_lower)
if match_days:
days = int(match_days.group(1))
mode = match_days.group(2)
if mode == "pehle":
target_date = now - timedelta(days=days)
return f"🗓️ **Past Date:** {days} din pehle **{target_date.strftime('%d %B %Y (%A)')}** thi."
else:
target_date = now + timedelta(days=days)
return f"🗓️ **Future Date:** {days} din baad **{target_date.strftime('%d %B %Y (%A)')}** hogi."
if any(k in text_lower for k in ["aaj ka din", "aaj din", "kaunsa din", "what day"]):
return f"📅 **Date & Day Info:** Aaj **{now.strftime('%A')}** hai ({now.strftime('%d %B %Y')})."
if any(k in text_lower for k in ["aaj date", "aaj ki date", "today date", "ajj ka din"]):
return f"📅 **Current Date:** {now.strftime('%d-%m-%Y')} ({now.strftime('%A')})"
if any(k in text_lower for k in ["time kya hua", "kitne baje", "current time"]):
return f"⏰ **Current Time:** {now.strftime('%I:%M %p')}"
return None
# --- UNIT CONVERTER UTILITY ENGINE ---
def handle_unit_conversions(self, text_lower):
patterns = [
(r"(\d+\.?\d*)\s*(km|kilometer|kilometres)\s*(to|in|me)\s*(miles|mile)", "km_to_miles"),
(r"(\d+\.?\d*)\s*(miles|mile)\s*(to|in|me)\s*(km|kilometer|kilometres)", "miles_to_km"),
(r"(\d+\.?\d*)\s*(km|kilometer|kilometres)\s*(to|in|me)\s*(m|meter|meters)", "km_to_m"),
(r"(\d+\.?\d*)\s*(km|kilometer|kilometres)\s*(to|in|me)\s*(cm|centimeter)", "km_to_cm"),
(r"(\d+\.?\d*)\s*(m|meter|meters)\s*(to|in|me)\s*(cm|centimeter)", "m_to_cm"),
(r"(\d+\.?\d*)\s*(cm|centimeter)\s*(to|in|me)\s*(m|meter|meters)", "cm_to_m"),
(r"(\d+\.?\d*)\s*(kg|kilogram|kilograms)\s*(to|in|me)\s*(g|gram|grams)", "kg_to_g"),
(r"(\d+\.?\d*)\s*(g|gram|grams)\s*(to|in|me)\s*(kg|kilogram|kilograms)", "g_to_kg"),
(r"(\d+\.?\d*)\s*(c|celsius)\s*(to|in|me)\s*(f|fahrenheit)", "c_to_f"),
(r"(\d+\.?\d*)\s*(f|fahrenheit)\s*(to|in|me)\s*(c|celsius)", "f_to_c"),
]
for pat, conv_type in patterns:
match = re.search(pat, text_lower)
if match:
val = float(match.group(1))
if conv_type == "km_to_miles":
res = round(val * 0.621371, 2)
return f"📏 **Unit Converter:** {val} km = **{res} miles**"
elif conv_type == "miles_to_km":
res = round(val * 1.60934, 2)
return f"📏 **Unit Converter:** {val} miles = **{res} km**"
elif conv_type == "km_to_m":
res = round(val * 1000, 2)
return f"📏 **Unit Converter:** {val} km = **{res:,} meters**"
elif conv_type == "km_to_cm":
res = round(val * 100000, 2)
return f"📏 **Unit Converter:** {val} km = **{res:,} cm**"
elif conv_type == "m_to_cm":
res = round(val * 100, 2)
return f"📏 **Unit Converter:** {val} meters = **{res:,} cm**"
elif conv_type == "cm_to_m":
res = round(val / 100, 4)
return f"📏 **Unit Converter:** {val} cm = **{res} meters**"
elif conv_type == "kg_to_g":
res = round(val * 1000, 2)
return f"📏 **Unit Converter:** {val} kg = **{res:,} grams**"
elif conv_type == "g_to_kg":
res = round(val / 1000, 4)
return f"📏 **Unit Converter:** {val} grams = **{res} kg**"
elif conv_type == "c_to_f":
res = round((val * 9/5) + 32, 2)
return f"🌡️ **Unit Converter:** {val}°C = **{res}°F**"
elif conv_type == "f_to_c":
res = round((val - 32) * 5/9, 2)
return f"🌡️ **Unit Converter:** {val}°F = **{res}°C**"
return None
def get_current_time_slot(self):
hour = datetime.now().hour
if 5 <= hour < 12:
return "morning"
elif 12 <= hour < 17:
return "afternoon"
elif 17 <= hour < 22:
return "evening"
else:
return "night"
def check_time_greeting_correction(self, text_lower):
current_slot = self.get_current_time_slot()
greetings_map = {
"morning": ["good morning", "gm", "morning"],
"afternoon": ["good afternoon", "afternoon"],
"evening": ["good evening", "ge", "evening"],
"night": ["good night", "gn", "night"],
}
hindi_slot_names = {
"morning": "Morning/Subah",
"afternoon": "Afternoon/Dopahar",
"evening": "Evening/Shaam",
"night": "Night/Raat",
}
detected_slot = None
for slot, keywords in greetings_map.items():
if any(k in text_lower for k in keywords):
detected_slot = slot
break
if detected_slot and detected_slot != current_slot:
actual = hindi_slot_names[current_slot]
return f"Arre {self.user_name.capitalize()} bhai, abhi {actual} ho raha hai! Pehle ghadi toh dekh lo 😜"
return None
def check_dynamic_advice(self, text_lower):
mood_triggers = {
"bored": ["bore", "boring", "paka", "kuch nahi kar raha"],
"tired": ["thak", "tired", "sleepy", "neend"],
"stressed": ["stress", "tension", "pareshan", "headache"],
}
detected_mood = None
for mood, keys in mood_triggers.items():
if any(k in text_lower for k in keys):
detected_mood = mood
break
if detected_mood == "bored":
advice_list = [
f"{self.user_name.capitalize()} bhai, thodi der fav playlist chala ke dance kar le ya game khel le! 🎧🎮",
f"Bore ho rahe ho toh 15-min ki walk le aao ya koi joke suno! 🚶♂️🤣",
]
return random.choice(advice_list)
elif detected_mood == "tired":
advice_list = [
f"{self.user_name.capitalize()} bhai, screen off karo aur 20-min ka power nap le lo! 😴🛌",
"Thoda thanda paani piyo aur stretch kar lo, energy wapas aayegi! ✨",
]
return random.choice(advice_list)
elif detected_mood == "stressed":
advice_list = [
"Bhai bilkul tension mat lo. Lambi saans lo aur 10 min coding se break le lo! 🧘♂️✨",
"Stress lene se kuch nahi hoga, thoda relaxed music suno aur chai/coffee piyo! ☕🎧",
]
return random.choice(advice_list)
return None
def solve_math(self, text):
clean_text = (
text.lower().replace("x", "*").replace("=", "").replace("^", "**")
)
safe_dict = {
"sqrt": math.sqrt,
"sin": lambda x: round(math.sin(math.radians(x)), 4),
"cos": lambda x: round(math.cos(math.radians(x)), 4),
"tan": lambda x: round(math.tan(math.radians(x)), 4),
"fact": math.factorial,
"abs": abs,
"pi": math.pi,
"e": math.e,
}
match = re.search(r"[\d\.\s\+\-\*\/\(\)\^\,]+", clean_text)
if match:
expr = match.group().strip()
expr = re.sub(r"[\+\-\*\/]+$", "", expr).strip()
has_op = any(op in expr for op in ["+", "-", "*", "/", "**"])
has_func = any(fn in expr for fn in safe_dict.keys())
if (has_op or has_func) and len(expr) > 1:
try:
res = eval(expr, {"__builtins__": None}, safe_dict)
if isinstance(res, float) and res.is_integer():
res = int(res)
return f"Math: {expr} = {res}"
except Exception:
return None
return None
def get_full_session_history_reply(self, text):
text_lower = text.lower()
if any(
k in text_lower
for k in [
"sawal",
"history",
"pehle",
"pehla",
"phele",
"question",
]
):
user_queries = [
h["content"]
for h in self.session_history
if h["role"] == "user"
]
if len(user_queries) <= 1:
return "Abhi tak session me aur koi purana sawal nahi hai!"
past_all = [
f"{i+1}. {q}" for i, q in enumerate(user_queries[:-1])
]
return "Poori Session History -> " + " | ".join(past_all)
return None
def find_fuzzy_match(self, input_text, cutoff=0.55):
best_match_item = None
highest_ratio = 0.0
for item in self.memory_db:
if not isinstance(item, dict):
continue
for pattern in item.get("patterns", []):
ratio = difflib.SequenceMatcher(
None, input_text.lower(), pattern.lower()
).ratio()
if ratio > highest_ratio:
highest_ratio = ratio
best_match_item = item
if highest_ratio >= cutoff:
return best_match_item, highest_ratio
return None, 0.0
def check_context_reply(self, text_lower):
if not self.current_context:
return None
for item in self.memory_db:
if isinstance(item, dict) and item.get("tag") == self.current_context:
ctx_responses = item.get("context_responses", {})
for key, resp in ctx_responses.items():
if key in text_lower:
self.current_context = None
return resp.replace("{name}", self.user_name)
return None
def format_extracted_content(self, raw_text, source_name, content_type="Image"):
clean_lines = [line.strip() for line in raw_text.splitlines() if line.strip()]
if not clean_lines:
return f"🖼️ **{content_type} Scan ({source_name}):** Is file me koi text nahi mil saka."
total_words = len(raw_text.split())
explanation = f"📋 **{content_type} Analysis & Summary ({source_name})**\n"
explanation += f"• **Total Words Detected:** {total_words}\n"
explanation += f"• **Total Lines Extracted:** {len(clean_lines)}\n\n"
explanation += "💡 **Point-wise Breakdown (Extracted Content):**\n\n"
for idx, line in enumerate(clean_lines, 1):
explanation += f" {idx}. {line}\n"
return explanation
def check_latest_media_memory_reply(self, text_lower):
if not isinstance(self.memory_db, list):
return None
for item in reversed(self.memory_db):
if not isinstance(item, dict):
continue
tag = item.get("tag", "")
source = item.get("source", "").lower()
is_image = tag == "image_context" or source.endswith((".png", ".jpg", ".jpeg", ".webp"))
is_pdf = tag == "pdf_context" or source.endswith(".pdf")
if (is_image or is_pdf) and "full_text" in item:
file_text = item.get("full_text", "")
source_name = item.get("source", "Uploaded File")
content_type = "Image" if is_image else "PDF Document"
if any(k in text_lower for k in ["total", "sum", "hisab", "amount", "fees", "kitna"]):
found_nums = re.findall(r"\b\d[\d,]*\b", file_text)
clean_nums = []
for n in found_nums:
val = int(n.replace(",", ""))
if val > 0:
clean_nums.append(val)
if clean_nums:
total_val = sum(clean_nums)
num_str = " + ".join([f"{v:,}" for v in clean_nums])
return f"🖼️ **Total Calculation ({source_name}):**\n\n• **Detected Numbers:** {num_str}\n• **Total Amount:** {total_val:,}"
explain_keywords = [
"explain", "samjha", "samjhao", "detail", "kya keh raha hai",
"matlab", "summary", "kya hai", "scan", "image", "photo",
"pic", "ocr", "likha", "isme", "batao", "bata", "pdf", "document"
]
if any(k in text_lower for k in explain_keywords):
return self.format_extracted_content(file_text, source_name, content_type)
words = [w for w in re.findall(r"\w+", text_lower) if len(w) > 3]
for w in words:
if w in file_text.lower():
pos = file_text.lower().find(w)
start = max(0, pos - 50)
end = min(len(file_text), pos + 250)
snippet = file_text[start:end].replace("\n", " ").strip()
icon = "🖼️" if is_image else "📄"
return f"{icon} **Context Match from {source_name}**\n\n...{snippet}..."
break
return None
def process_single_query(self, sub_query):
sub_text = sub_query.strip()
if not sub_text:
return None
text_lower = sub_text.lower()
# 1. SQLite Knowledge Match (v5.6)
sqlite_ans = self.db_manager.search_sqlite_knowledge(text_lower)
if sqlite_ans:
return sqlite_ans
# 2. Media Check Priority
media_reply = self.check_latest_media_memory_reply(text_lower)
if media_reply:
return media_reply
# 3. Time Correction Check
time_corrected = self.check_time_greeting_correction(text_lower)
if time_corrected:
return time_corrected
# 4. Date & Time Utilities
date_time_reply = self.handle_date_time_utilities(text_lower)
if date_time_reply:
return date_time_reply
# 5. Unit Conversion Utilities
unit_reply = self.handle_unit_conversions(text_lower)
if unit_reply:
return unit_reply
# 6. Dynamic Advice
advice = self.check_dynamic_advice(text_lower)
if advice:
return advice
# 7. Math Solver
math_ans = self.solve_math(sub_text)
if math_ans:
return math_ans
# 8. Session History
hist_ans = self.get_full_session_history_reply(sub_text)
if hist_ans:
return hist_ans
# 9. Context Reply
ctx_reply = self.check_context_reply(text_lower)
if ctx_reply:
return ctx_reply
# 10. Memory DB Pattern Match
for item in self.memory_db:
if isinstance(item, dict) and "patterns" in item:
if text_lower in [p.lower() for p in item["patterns"]]:
if "responses" in item and item["responses"]:
self.current_context = item.get("tag")
chosen = self.get_non_repeating_choice(item.get("tag"), item["responses"])