gebsunamy commited on
Commit
fc6286e
·
verified ·
1 Parent(s): 2620423

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +67 -0
static/script.js ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const chatBox = document.getElementById('chat-box');
2
+ const chatForm = document.getElementById('chat-form');
3
+ const userInput = document.getElementById('user-input');
4
+ const trainingOverlay = document.getElementById('training-overlay');
5
+
6
+ // التحقق من حالة التدريب كل ثانية
7
+ function checkStatus() {
8
+ fetch('/training-status')
9
+ .then(r => r.json())
10
+ .then(data => {
11
+ if (data.is_training) {
12
+ trainingOverlay.classList.remove('hidden');
13
+ document.getElementById('progress-fill').style.width = data.progress + '%';
14
+ document.getElementById('progress-text').innerText = data.progress + '%';
15
+ document.getElementById('task-name').innerText = data.current_task;
16
+ } else {
17
+ trainingOverlay.classList.add('hidden');
18
+ }
19
+ });
20
+ }
21
+
22
+ setInterval(checkStatus, 1500);
23
+
24
+ chatForm.onsubmit = async (e) => {
25
+ e.preventDefault();
26
+ const msg = userInput.value;
27
+ appendMsg(msg, 'user');
28
+ userInput.value = '';
29
+
30
+ const res = await fetch('/chat', {
31
+ method: 'POST',
32
+ headers: {'Content-Type': 'application/json'},
33
+ body: JSON.stringify({message: msg})
34
+ });
35
+
36
+ const data = await res.json();
37
+ if (data.error) appendMsg(data.error, 'bot');
38
+ else appendMsg(data.response_text, 'bot');
39
+ };
40
+
41
+ function appendMsg(text, side) {
42
+ const div = document.createElement('div');
43
+ div.className = `msg ${side}`;
44
+ div.innerText = text;
45
+ chatBox.appendChild(div);
46
+ chatBox.scrollTop = chatBox.scrollHeight;
47
+ }
48
+
49
+ // أزرار المودال
50
+ document.getElementById('add-data-btn').onclick = () => document.getElementById('modal').classList.remove('hidden');
51
+ document.getElementById('close-btn').onclick = () => document.getElementById('modal').classList.add('hidden');
52
+
53
+ document.getElementById('save-btn').onclick = async () => {
54
+ const q = document.getElementById('new-q').value;
55
+ const a = document.getElementById('new-a').value;
56
+ if(!q || !a) return;
57
+
58
+ await fetch('/save', {
59
+ method: 'POST',
60
+ headers: {'Content-Type': 'application/json'},
61
+ body: JSON.stringify([{question: q, answer: a}])
62
+ });
63
+
64
+ document.getElementById('modal').classList.add('hidden');
65
+ document.getElementById('new-q').value = '';
66
+ document.getElementById('new-a').value = '';
67
+ };