JibayAi commited on
Commit
27e7f5f
·
verified ·
1 Parent(s): c1bc5b7

Upload index.php

Browse files
Files changed (1) hide show
  1. index.php +230 -0
index.php ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ header('Content-Type: application/json; charset=utf-8');
3
+
4
+ $text = isset($_GET['text']) ? trim($_GET['text']) : '';
5
+ if ($text === '') {
6
+ echo json_encode(["error" => "Text parameter not provided."], JSON_UNESCAPED_UNICODE);
7
+ exit;
8
+ }
9
+
10
+ $tokenizerPath = 'tokenizer.json';
11
+ if (!file_exists($tokenizerPath)) {
12
+ echo json_encode(["error" => "tokenizer.json file not found."], JSON_UNESCAPED_UNICODE);
13
+ exit;
14
+ }
15
+
16
+ $tokenizer = json_decode(file_get_contents($tokenizerPath), true);
17
+ if (!$tokenizer) {
18
+ echo json_encode(["error" => "Error reading tokenizer.json."], JSON_UNESCAPED_UNICODE);
19
+ exit;
20
+ }
21
+
22
+ $allChars = $tokenizer['letters'] ?? [];
23
+ if (isset($tokenizer['additional_characters'])) {
24
+ foreach ($tokenizer['additional_characters'] as $set) {
25
+ if (is_array($set)) {
26
+ $allChars = array_merge($allChars, $set);
27
+ }
28
+ }
29
+ }
30
+
31
+ $valid = false;
32
+ foreach ($allChars as $ch) {
33
+ if (mb_strpos($text, $ch) !== false) {
34
+ $valid = true;
35
+ break;
36
+ }
37
+ }
38
+
39
+ if (!$valid) {
40
+ echo json_encode(["error" => "Text does not contain any valid letters or characters."], JSON_UNESCAPED_UNICODE);
41
+ exit;
42
+ }
43
+
44
+ $models = [];
45
+ for ($i = 1; $i <= 8; $i++) {
46
+ $pathJson = "model-jibay1-000{$i}.json";
47
+ $pathJsonl = "model-jibay1-000{$i}.jsonl";
48
+ if (file_exists($pathJson)) $models[] = $pathJson;
49
+ elseif (file_exists($pathJsonl)) $models[] = $pathJsonl;
50
+ }
51
+
52
+ if (empty($models)) {
53
+ echo json_encode(["error" => "No model files found."], JSON_UNESCAPED_UNICODE);
54
+ exit;
55
+ }
56
+
57
+ function similarity($a, $b) {
58
+ similar_text($a, $b, $percent);
59
+ return $percent;
60
+ }
61
+
62
+ $englishGreetings = [
63
+ "Dear", "Bro", "Your answer", "Friend", "My flower", "My good friend", "My dear", "Respected user",
64
+ "Valued companion", "My brother", "My lovely sister", "Sir", "My lord", "Good boy", "My queen", "Sweetheart",
65
+ "Cool friend", "My sympathizer", "Old friend", "Dear friend", "Dad's flower", "Dear soul", "Champion", "Smart one",
66
+ "Master", "My intelligent", "Good colleague", "Lovely", "Smart", "Noble one", "My dear companion",
67
+ "Hero", "Intelligent one", "Good-natured flower", "Specially smart", "Smart friend", "Genius", "Your intelligence",
68
+ "Flower friend", "Little smart", "Cute smart", "My smart one", "Special intelligence", "Kind smart", "Real smart",
69
+ "Awesome smart", "Special smart", "Professional smart", "Golden smart", "Smartest", "My dear smart",
70
+ "Amazingly smart", "Lovable smart", "Flower smart", "Genius smart", "Kind smart",
71
+ "Amazing smart", "Legendary smart", "Charming smart", "Beautiful smart", "My special smart", "Cute smart",
72
+ "Cool smart", "Excellent smart", "Unique smart", "Creative smart", "Sharp-eyed smart", "Sweet smart",
73
+ "Strong smart", "Civilized smart", "Understanding smart", "Logical smart", "Professional smart", "Fantastic smart",
74
+ "My flower smart", "Pleasant smart", "Well-mannered smart", "Cool smart", "First-class smart", "Skilled smart",
75
+ "Respected smart", "Cute smart", "Sweet smart", "Dear smart", "Special smart", "Beloved smart",
76
+ "Beautiful smart", "Strong-hearted smart", "Polite smart", "Heartfelt smart", "My professional smart"
77
+ ];
78
+
79
+ $englishFarewells = [
80
+ "If you have any questions, ask 😊", "I'm at your service 🌹", "If you have a question, ask my dear 🙌", "Always at your service ❤️",
81
+ "Ready for your next question 😉", "If you need any other help, let me know 🌷", "If you have another question, I'm here 😎",
82
+ "If something was unclear, ask 😌", "Proudly at your service 💪", "Ask again, friend 🧠",
83
+ "Waiting for your next question 📘", "If the answer wasn't enough, let me know 👂", "I'm always ready 🔥",
84
+ "At the service of my dear student 🎓", "My good friend, ask again 🤝", "Whenever you want, ask 💬",
85
+ "I'm ready 🌈", "Ask again 🌟", "Need help? I'm here 🧩", "Your questions are excellent 💎",
86
+ "Serving Iranian intelligence 🇮🇷", "Come visit again 😊", "Until next question 👋", "Always here to help 💡",
87
+ "Happy to answer 🌸", "Next question please 😄", "If you want to continue 🚀",
88
+ "Serving science 💫", "Hope to see you again 📚", "Waiting for the next question 🌼",
89
+ "At your service 🌹", "Your questions help growth 🌱", "Continue, you're doing great 💪",
90
+ "Write again 🌻", "At your service 🙏", "Excellent question 👏", "Next question is ready 🔔",
91
+ "Serving your learning 📖", "Ask again so we can learn 🌟", "You asked excellently 🌸",
92
+ "Always at your service ❤️", "Ask again, professor 👨‍🏫", "Happy to answer with pleasure 😍",
93
+ "Always ready to answer 🌙", "Serving the knowledgeable 💎", "Continue to become stronger 💪",
94
+ "Ask again, dear 🌼", "Serving the love of knowledge ❤️", "Ready to help anytime 🌟"
95
+ ];
96
+
97
+ $persianGreetings = [
98
+ "عزیزم", "داداش", "جواب شما", "رفیق", "گل من", "دوست خوبم", "عزیز دلم", "کاربر محترم",
99
+ "همراه گرامی", "برادر من", "خواهر گلم", "جناب", "سرورم", "گل پسر", "ملکه من", "عزیز دل",
100
+ "رفیق باصفا", "همدل من", "یار قدیمی", "دوست نازنین", "گل بابا", "عزیز جان", "پهلوان", "باهوش",
101
+ "استاد", "باهوش من", "همکار خوبم", "دوست داشتنی", "باهوش", "بزرگوار", "هم‌راه عزیزم",
102
+ "قهرمان", "باهوشی", "گل خوش‌اخلاق", "باهوشی خاص", "رفیق باهوش", "نابغه", "باهوشی تو",
103
+ "دوست گل", "باهوش کوچولو", "باهوش ناز", "باهوش منی", "باهوشی خاص", "باهوش مهربون", "باهوش واقعی",
104
+ "باهوش خفن", "باهوش خاص", "باهوش حرفه‌ای", "باهوش طلایی", "باهوش ترین", "باهوش عزیزم",
105
+ "باهوش فوق‌العاده", "باهوش دوست‌داشتنی", "باهوش گل", "باهوش نابغه", "باهوش مهربان",
106
+ "باهوش شگفت‌انگیز", "باهوش افسانه‌ای", "باهوش دلبر", "باهوش قشنگ", "باهوش خاصم", "باهوش بانمک",
107
+ "باهوش باحال", "باهوش عالی", "باهوش بی‌نظیر", "باهوش خوش‌فکر", "باهوش تیزبین", "باهوش نازنین",
108
+ "باهوش قوی", "باهوش متمدن", "باهوش فهیم", "باهوش منطقی", "باهوش حرفه‌ای", "باهوش معرکه",
109
+ "باهوش گل من", "باهوش دلپذیر", "باهوش بامرام", "باهوش باصفا", "باهوش درجه یک", "باهوش کاربلد",
110
+ "باهوش محترم", "باهوش بانمک", "باهوش شیرین", "باهوش جان", "باهوش خاصی", "باهوش دلبند",
111
+ "باهوش قشنگ", "باهوش قوی‌دل", "باهوش مودب", "باهوش اهل دل", "باهوش حرفه‌ای من"
112
+ ];
113
+
114
+ $persianFarewells = [
115
+ "اگر سوالی بود بپرس 😊", "در خدمتت هستم 🌹", "سوالی بود بپرس عزیزم 🙌", "درخدمتم همیشه ❤️",
116
+ "آماده سوال بعدی‌ام 😉", "اگه کمک دیگه‌ای خواستی بگو 🌷", "بازم سوال داشتی من اینجام 😎",
117
+ "اگه چیزی مبهم بود بپرس 😌", "با افتخار در خدمتتم 💪", "بازم بپرس رفیق 🧠",
118
+ "منتظر سوال بعدی‌ات هستم 📘", "اگه جواب کافی نبود، بگو 👂", "همیشه آماده‌ام 🔥",
119
+ "در خدمت دانشجوی عزیزم 🎓", "دوست خوبم، باز هم بپرس 🤝", "هر وقت خواستی بپرس 💬",
120
+ "من آماده‌ام 🌈", "بازم بپرس 🌟", "کمک می‌خوای؟ من هستم 🧩", "سوالاتت عالی‌ان 💎",
121
+ "در خدمت هوش ایرانی 🇮🇷", "بازم بیا سر بزن 😊", "تا سوال بعدی 👋", "همیشه اینجام برای کمک 💡",
122
+ "با لذت جواب میدم 🌸", "سوال بعدی لطفاً 😄", "اگه خواستی ادامه بده 🚀",
123
+ "در خدمت علم 💫", "به امید دیدار دوباره 📚", "منتظر سوال بعدی 🌼",
124
+ "در خدمت شما 🌹", "پرسش‌هات باعث رشد میشه 🌱", "ادامه بده، خوب پیش میری 💪",
125
+ "بازم بنویس 🌻", "درخدمت شما هستم 🙏", "پرسش عالی بود 👏", "سوال بعدی آماده‌ست 🔔",
126
+ "در خدمت یادگیری تو 📖", "باز هم بپرس تا یاد بگیریم 🌟", "عالی پرسیدی 🌸",
127
+ "درخدمت تو همیشه ❤️", "بازم بپرس استاد 👨‍🏫", "با کمال میل پاسخ میدم 😍",
128
+ "همیشه آماده پاسخ دادن 🌙", "در خدمت اهل دانایی 💎", "ادامه بده تا قوی‌تر شی 💪",
129
+ "بازم سوال کن عزیز 🌼", "در خدمت عشق به دانش ❤️", "هر لحظه آماده کمکم 🌟"
130
+ ];
131
+
132
+ $isEnglish = preg_match('/[a-zA-Z]/', $text);
133
+ $greetings = $isEnglish ? $englishGreetings : $persianGreetings;
134
+ $farewells = $isEnglish ? $englishFarewells : $persianFarewells;
135
+
136
+ $bestMatch = null;
137
+ $bestScore = 0;
138
+
139
+ foreach ($models as $file) {
140
+ $ext = pathinfo($file, PATHINFO_EXTENSION);
141
+ $content = file_get_contents($file);
142
+ if (!$content) continue;
143
+
144
+ if ($ext === 'jsonl') {
145
+ $lines = explode("\n", trim($content));
146
+ foreach ($lines as $line) {
147
+ $data = json_decode($line, true);
148
+ if (!$data) continue;
149
+ $q = $data['question'] ?? $data['user'] ?? '';
150
+ $a = $data['answer'] ?? $data['assistant'] ?? '';
151
+ if (is_array($a)) $a = implode(' | ', $a);
152
+
153
+ $score = similarity($text, $q);
154
+ if ($score > $bestScore && $score >= 80) {
155
+ $bestScore = $score;
156
+ $bestMatch = ["question" => $q, "answer" => $a];
157
+ }
158
+ }
159
+ } else {
160
+ $json = json_decode($content, true);
161
+ if (!$json) continue;
162
+
163
+ if (isset($json[0]['user'])) {
164
+ foreach ($json as $item) {
165
+ $q = $item['user'] ?? '';
166
+ $a = $item['assistant'] ?? '';
167
+ $score = similarity($text, $q);
168
+ if ($score > $bestScore && $score >= 80) {
169
+ $bestScore = $score;
170
+ $bestMatch = ["question" => $q, "answer" => $a];
171
+ }
172
+ }
173
+ }
174
+ elseif (isset($json['data'][0]['paragraphs'][0]['qas'])) {
175
+ foreach ($json['data'] as $block) {
176
+ foreach ($block['paragraphs'] as $p) {
177
+ foreach ($p['qas'] as $qa) {
178
+ $q = $qa['question'] ?? '';
179
+ $answers = [];
180
+ foreach ($qa['answers'] ?? [] as $ans) {
181
+ if (isset($ans['text'])) $answers[] = $ans['text'];
182
+ }
183
+ $a = implode(' | ', $answers);
184
+ $score = similarity($text, $q);
185
+ if ($score > $bestScore && $score >= 80) {
186
+ $bestScore = $score;
187
+ $bestMatch = ["question" => $q, "answer" => $a];
188
+ }
189
+ }
190
+ }
191
+ }
192
+ }
193
+ elseif (isset($json['data'][0]['questions'])) {
194
+ foreach ($json['data'] as $block) {
195
+ foreach ($block['questions'] as $qitem) {
196
+ $q = $qitem['input_text'] ?? '';
197
+ $matchAnswer = '';
198
+ foreach ($block['answers'] as $ans) {
199
+ if (($ans['turn_id'] ?? null) === ($qitem['turn_id'] ?? null)) {
200
+ $matchAnswer = $ans['input_text'] ?? ($ans['span_text'] ?? '');
201
+ break;
202
+ }
203
+ }
204
+ $score = similarity($text, $q);
205
+ if ($score > $bestScore && $score >= 80) {
206
+ $bestScore = $score;
207
+ $bestMatch = ["question" => $q, "answer" => $matchAnswer];
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ if ($bestMatch) {
216
+ $greet = $greetings[array_rand($greetings)];
217
+ $farewell = $farewells[array_rand($farewells)];
218
+ $finalAnswer = trim("{$greet}، {$bestMatch['answer']} {$farewell}");
219
+
220
+ echo json_encode([
221
+ [
222
+ "thinking" => "Well, the user said '{$text}'. I should try to review, collect, analyze and present the best information to the user, the best answer is ('{$finalAnswer}').",
223
+ "question" => $bestMatch['question'],
224
+ "answer" => $finalAnswer
225
+ ]
226
+ ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
227
+ } else {
228
+ echo json_encode(["message" => "No suitable answer found."], JSON_UNESCAPED_UNICODE);
229
+ }
230
+ ?>