NitinBot001 commited on
Commit
abde558
·
verified ·
1 Parent(s): 9f07cc4

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +260 -260
static/script.js CHANGED
@@ -1,261 +1,261 @@
1
- const API_BASE = 'http://localhost:7860';
2
-
3
- // DOM Elements
4
- const statusBar = document.getElementById('statusBar');
5
- const statusIndicator = document.getElementById('statusIndicator');
6
- const statusText = document.getElementById('statusText');
7
- const initSection = document.getElementById('initSection');
8
- const mainContent = document.getElementById('mainContent');
9
- const queryInput = document.getElementById('queryInput');
10
- const submitBtn = document.getElementById('submitBtn');
11
- const resultsSection = document.getElementById('resultsSection');
12
- const answerContent = document.getElementById('answerContent');
13
- const tokenInfo = document.getElementById('tokenInfo');
14
- const timeInfo = document.getElementById('timeInfo');
15
- const sourcesSection = document.getElementById('sourcesSection');
16
- const sourcesList = document.getElementById('sourcesList');
17
- const loading = document.getElementById('loading');
18
- const statsBtn = document.getElementById('statsBtn');
19
- const statsModal = document.getElementById('statsModal');
20
- const statsContent = document.getElementById('statsContent');
21
- const apiKeyInput = document.getElementById('apiKeyInput');
22
- const initBtn = document.getElementById('initBtn');
23
- const fileInput = document.getElementById('fileInput');
24
- const uploadBtn = document.getElementById('uploadBtn');
25
-
26
- // Check system status on load
27
- window.addEventListener('load', checkSystemStatus);
28
-
29
- // Event Listeners
30
- submitBtn.addEventListener('click', submitQuery);
31
- queryInput.addEventListener('keypress', (e) => {
32
- if (e.key === 'Enter' && !e.shiftKey) {
33
- e.preventDefault();
34
- submitQuery();
35
- }
36
- });
37
-
38
- // Sample query buttons
39
- document.querySelectorAll('.sample-btn').forEach(btn => {
40
- btn.addEventListener('click', () => {
41
- queryInput.value = btn.dataset.query;
42
- submitQuery();
43
- });
44
- });
45
-
46
- // Initialize button
47
- initBtn.addEventListener('click', initializeSystem);
48
-
49
- // Stats modal
50
- statsBtn.addEventListener('click', showStats);
51
- document.querySelector('.close').addEventListener('click', () => {
52
- statsModal.style.display = 'none';
53
- });
54
-
55
- // File upload
56
- uploadBtn.addEventListener('click', uploadDocument);
57
-
58
- // Functions
59
- async function checkSystemStatus() {
60
- try {
61
- const response = await fetch(`${API_BASE}/api/status`);
62
- const data = await response.json();
63
-
64
- if (data.is_initialized) {
65
- statusIndicator.className = 'status-indicator ready';
66
- statusText.textContent = 'System Ready';
67
- mainContent.style.display = 'block';
68
- initSection.style.display = 'none';
69
- } else {
70
- statusIndicator.className = 'status-indicator not-ready';
71
- statusText.textContent = 'System Not Initialized';
72
- mainContent.style.display = 'none';
73
- initSection.style.display = 'block';
74
- }
75
- } catch (error) {
76
- console.error('Error checking status:', error);
77
- statusIndicator.className = 'status-indicator not-ready';
78
- statusText.textContent = 'Connection Error';
79
- }
80
- }
81
-
82
- async function initializeSystem() {
83
- const apiKey = apiKeyInput.value.trim();
84
- if (!apiKey) {
85
- alert('Please enter an API key');
86
- return;
87
- }
88
-
89
- loading.style.display = 'block';
90
- initSection.style.display = 'none';
91
-
92
- try {
93
- const response = await fetch(`${API_BASE}/api/initialize`, {
94
- method: 'POST',
95
- headers: {
96
- 'Content-Type': 'application/json',
97
- },
98
- body: JSON.stringify({ api_key: apiKey })
99
- });
100
-
101
- if (response.ok) {
102
- const data = await response.json();
103
- apiKeyInput.value = '';
104
- await checkSystemStatus();
105
- } else {
106
- const error = await response.json();
107
- alert(`Initialization failed: ${error.detail}`);
108
- initSection.style.display = 'block';
109
- }
110
- } catch (error) {
111
- console.error('Error initializing system:', error);
112
- alert('Failed to initialize system');
113
- initSection.style.display = 'block';
114
- } finally {
115
- loading.style.display = 'none';
116
- }
117
- }
118
-
119
- async function submitQuery() {
120
- const query = queryInput.value.trim();
121
- if (!query) {
122
- alert('Please enter a question');
123
- return;
124
- }
125
-
126
- loading.style.display = 'block';
127
- resultsSection.style.display = 'none';
128
-
129
- try {
130
- const response = await fetch(`${API_BASE}/api/query`, {
131
- method: 'POST',
132
- headers: {
133
- 'Content-Type': 'application/json',
134
- },
135
- body: JSON.stringify({ query: query })
136
- });
137
-
138
- if (response.ok) {
139
- const data = await response.json();
140
- displayResults(data);
141
- } else {
142
- const error = await response.json();
143
- answerContent.innerHTML = `<div class="error-message">Error: ${error.detail}</div>`;
144
- resultsSection.style.display = 'block';
145
- }
146
- } catch (error) {
147
- console.error('Error submitting query:', error);
148
- answerContent.innerHTML = `<div class="error-message">Failed to process query</div>`;
149
- resultsSection.style.display = 'block';
150
- } finally {
151
- loading.style.display = 'none';
152
- }
153
- }
154
-
155
- function displayResults(data) {
156
- answerContent.textContent = data.answer;
157
-
158
- // Display token info
159
- if (data.token_usage && Object.keys(data.token_usage).length > 0) {
160
- tokenInfo.innerHTML = `
161
- <strong>Tokens:</strong>
162
- Prompt: ${data.token_usage.prompt_tokens || 0},
163
- Completion: ${data.token_usage.completion_tokens || 0},
164
- Total: ${data.token_usage.total_tokens || 0}
165
- `;
166
- } else {
167
- tokenInfo.innerHTML = '';
168
- }
169
-
170
- // Display processing time
171
- timeInfo.innerHTML = `<strong>Processing Time:</strong> ${data.processing_time}s`;
172
-
173
- // Display sources
174
- if (data.sources && data.sources.length > 0) {
175
- sourcesList.innerHTML = data.sources.map(source =>
176
- `<div class="source-item">${source}</div>`
177
- ).join('');
178
- sourcesSection.style.display = 'block';
179
- } else {
180
- sourcesSection.style.display = 'none';
181
- }
182
-
183
- resultsSection.style.display = 'block';
184
- }
185
-
186
- async function showStats() {
187
- try {
188
- const response = await fetch(`${API_BASE}/api/token-stats`);
189
- const data = await response.json();
190
-
191
- if (data.total_calls !== undefined) {
192
- statsContent.innerHTML = `
193
- <div class="stat-item">
194
- <span>Total API Calls:</span>
195
- <span>${data.total_calls}</span>
196
- </div>
197
- <div class="stat-item">
198
- <span>Total Prompt Tokens:</span>
199
- <span>${data.total_prompt_tokens}</span>
200
- </div>
201
- <div class="stat-item">
202
- <span>Total Completion Tokens:</span>
203
- <span>${data.total_completion_tokens}</span>
204
- </div>
205
- <div class="stat-item">
206
- <span>Total Tokens:</span>
207
- <span>${data.total_tokens}</span>
208
- </div>
209
- `;
210
- } else {
211
- statsContent.innerHTML = '<p>No statistics available</p>';
212
- }
213
-
214
- statsModal.style.display = 'flex';
215
- } catch (error) {
216
- console.error('Error fetching stats:', error);
217
- alert('Failed to fetch statistics');
218
- }
219
- }
220
-
221
- async function uploadDocument() {
222
- const file = fileInput.files[0];
223
- if (!file) {
224
- alert('Please select a file');
225
- return;
226
- }
227
-
228
- loading.style.display = 'block';
229
-
230
- const formData = new FormData();
231
- formData.append('file', file);
232
-
233
- try {
234
- const response = await fetch(`${API_BASE}/api/upload-document`, {
235
- method: 'POST',
236
- body: formData
237
- });
238
-
239
- if (response.ok) {
240
- const data = await response.json();
241
- alert(data.message);
242
- fileInput.value = '';
243
- await checkSystemStatus();
244
- } else {
245
- const error = await response.json();
246
- alert(`Upload failed: ${error.detail}`);
247
- }
248
- } catch (error) {
249
- console.error('Error uploading document:', error);
250
- alert('Failed to upload document');
251
- } finally {
252
- loading.style.display = 'none';
253
- }
254
- }
255
-
256
- // Close modal when clicking outside
257
- window.addEventListener('click', (event) => {
258
- if (event.target === statsModal) {
259
- statsModal.style.display = 'none';
260
- }
261
  });
 
1
+ const API_BASE = 'https://nitinbot001-crop-rag-api.hf.space';
2
+
3
+ // DOM Elements
4
+ const statusBar = document.getElementById('statusBar');
5
+ const statusIndicator = document.getElementById('statusIndicator');
6
+ const statusText = document.getElementById('statusText');
7
+ const initSection = document.getElementById('initSection');
8
+ const mainContent = document.getElementById('mainContent');
9
+ const queryInput = document.getElementById('queryInput');
10
+ const submitBtn = document.getElementById('submitBtn');
11
+ const resultsSection = document.getElementById('resultsSection');
12
+ const answerContent = document.getElementById('answerContent');
13
+ const tokenInfo = document.getElementById('tokenInfo');
14
+ const timeInfo = document.getElementById('timeInfo');
15
+ const sourcesSection = document.getElementById('sourcesSection');
16
+ const sourcesList = document.getElementById('sourcesList');
17
+ const loading = document.getElementById('loading');
18
+ const statsBtn = document.getElementById('statsBtn');
19
+ const statsModal = document.getElementById('statsModal');
20
+ const statsContent = document.getElementById('statsContent');
21
+ const apiKeyInput = document.getElementById('apiKeyInput');
22
+ const initBtn = document.getElementById('initBtn');
23
+ const fileInput = document.getElementById('fileInput');
24
+ const uploadBtn = document.getElementById('uploadBtn');
25
+
26
+ // Check system status on load
27
+ window.addEventListener('load', checkSystemStatus);
28
+
29
+ // Event Listeners
30
+ submitBtn.addEventListener('click', submitQuery);
31
+ queryInput.addEventListener('keypress', (e) => {
32
+ if (e.key === 'Enter' && !e.shiftKey) {
33
+ e.preventDefault();
34
+ submitQuery();
35
+ }
36
+ });
37
+
38
+ // Sample query buttons
39
+ document.querySelectorAll('.sample-btn').forEach(btn => {
40
+ btn.addEventListener('click', () => {
41
+ queryInput.value = btn.dataset.query;
42
+ submitQuery();
43
+ });
44
+ });
45
+
46
+ // Initialize button
47
+ initBtn.addEventListener('click', initializeSystem);
48
+
49
+ // Stats modal
50
+ statsBtn.addEventListener('click', showStats);
51
+ document.querySelector('.close').addEventListener('click', () => {
52
+ statsModal.style.display = 'none';
53
+ });
54
+
55
+ // File upload
56
+ uploadBtn.addEventListener('click', uploadDocument);
57
+
58
+ // Functions
59
+ async function checkSystemStatus() {
60
+ try {
61
+ const response = await fetch(`${API_BASE}/api/status`);
62
+ const data = await response.json();
63
+
64
+ if (data.is_initialized) {
65
+ statusIndicator.className = 'status-indicator ready';
66
+ statusText.textContent = 'System Ready';
67
+ mainContent.style.display = 'block';
68
+ initSection.style.display = 'none';
69
+ } else {
70
+ statusIndicator.className = 'status-indicator not-ready';
71
+ statusText.textContent = 'System Not Initialized';
72
+ mainContent.style.display = 'none';
73
+ initSection.style.display = 'block';
74
+ }
75
+ } catch (error) {
76
+ console.error('Error checking status:', error);
77
+ statusIndicator.className = 'status-indicator not-ready';
78
+ statusText.textContent = 'Connection Error';
79
+ }
80
+ }
81
+
82
+ async function initializeSystem() {
83
+ const apiKey = apiKeyInput.value.trim();
84
+ if (!apiKey) {
85
+ alert('Please enter an API key');
86
+ return;
87
+ }
88
+
89
+ loading.style.display = 'block';
90
+ initSection.style.display = 'none';
91
+
92
+ try {
93
+ const response = await fetch(`${API_BASE}/api/initialize`, {
94
+ method: 'POST',
95
+ headers: {
96
+ 'Content-Type': 'application/json',
97
+ },
98
+ body: JSON.stringify({ api_key: apiKey })
99
+ });
100
+
101
+ if (response.ok) {
102
+ const data = await response.json();
103
+ apiKeyInput.value = '';
104
+ await checkSystemStatus();
105
+ } else {
106
+ const error = await response.json();
107
+ alert(`Initialization failed: ${error.detail}`);
108
+ initSection.style.display = 'block';
109
+ }
110
+ } catch (error) {
111
+ console.error('Error initializing system:', error);
112
+ alert('Failed to initialize system');
113
+ initSection.style.display = 'block';
114
+ } finally {
115
+ loading.style.display = 'none';
116
+ }
117
+ }
118
+
119
+ async function submitQuery() {
120
+ const query = queryInput.value.trim();
121
+ if (!query) {
122
+ alert('Please enter a question');
123
+ return;
124
+ }
125
+
126
+ loading.style.display = 'block';
127
+ resultsSection.style.display = 'none';
128
+
129
+ try {
130
+ const response = await fetch(`${API_BASE}/api/query`, {
131
+ method: 'POST',
132
+ headers: {
133
+ 'Content-Type': 'application/json',
134
+ },
135
+ body: JSON.stringify({ query: query })
136
+ });
137
+
138
+ if (response.ok) {
139
+ const data = await response.json();
140
+ displayResults(data);
141
+ } else {
142
+ const error = await response.json();
143
+ answerContent.innerHTML = `<div class="error-message">Error: ${error.detail}</div>`;
144
+ resultsSection.style.display = 'block';
145
+ }
146
+ } catch (error) {
147
+ console.error('Error submitting query:', error);
148
+ answerContent.innerHTML = `<div class="error-message">Failed to process query</div>`;
149
+ resultsSection.style.display = 'block';
150
+ } finally {
151
+ loading.style.display = 'none';
152
+ }
153
+ }
154
+
155
+ function displayResults(data) {
156
+ answerContent.textContent = data.answer;
157
+
158
+ // Display token info
159
+ if (data.token_usage && Object.keys(data.token_usage).length > 0) {
160
+ tokenInfo.innerHTML = `
161
+ <strong>Tokens:</strong>
162
+ Prompt: ${data.token_usage.prompt_tokens || 0},
163
+ Completion: ${data.token_usage.completion_tokens || 0},
164
+ Total: ${data.token_usage.total_tokens || 0}
165
+ `;
166
+ } else {
167
+ tokenInfo.innerHTML = '';
168
+ }
169
+
170
+ // Display processing time
171
+ timeInfo.innerHTML = `<strong>Processing Time:</strong> ${data.processing_time}s`;
172
+
173
+ // Display sources
174
+ if (data.sources && data.sources.length > 0) {
175
+ sourcesList.innerHTML = data.sources.map(source =>
176
+ `<div class="source-item">${source}</div>`
177
+ ).join('');
178
+ sourcesSection.style.display = 'block';
179
+ } else {
180
+ sourcesSection.style.display = 'none';
181
+ }
182
+
183
+ resultsSection.style.display = 'block';
184
+ }
185
+
186
+ async function showStats() {
187
+ try {
188
+ const response = await fetch(`${API_BASE}/api/token-stats`);
189
+ const data = await response.json();
190
+
191
+ if (data.total_calls !== undefined) {
192
+ statsContent.innerHTML = `
193
+ <div class="stat-item">
194
+ <span>Total API Calls:</span>
195
+ <span>${data.total_calls}</span>
196
+ </div>
197
+ <div class="stat-item">
198
+ <span>Total Prompt Tokens:</span>
199
+ <span>${data.total_prompt_tokens}</span>
200
+ </div>
201
+ <div class="stat-item">
202
+ <span>Total Completion Tokens:</span>
203
+ <span>${data.total_completion_tokens}</span>
204
+ </div>
205
+ <div class="stat-item">
206
+ <span>Total Tokens:</span>
207
+ <span>${data.total_tokens}</span>
208
+ </div>
209
+ `;
210
+ } else {
211
+ statsContent.innerHTML = '<p>No statistics available</p>';
212
+ }
213
+
214
+ statsModal.style.display = 'flex';
215
+ } catch (error) {
216
+ console.error('Error fetching stats:', error);
217
+ alert('Failed to fetch statistics');
218
+ }
219
+ }
220
+
221
+ async function uploadDocument() {
222
+ const file = fileInput.files[0];
223
+ if (!file) {
224
+ alert('Please select a file');
225
+ return;
226
+ }
227
+
228
+ loading.style.display = 'block';
229
+
230
+ const formData = new FormData();
231
+ formData.append('file', file);
232
+
233
+ try {
234
+ const response = await fetch(`${API_BASE}/api/upload-document`, {
235
+ method: 'POST',
236
+ body: formData
237
+ });
238
+
239
+ if (response.ok) {
240
+ const data = await response.json();
241
+ alert(data.message);
242
+ fileInput.value = '';
243
+ await checkSystemStatus();
244
+ } else {
245
+ const error = await response.json();
246
+ alert(`Upload failed: ${error.detail}`);
247
+ }
248
+ } catch (error) {
249
+ console.error('Error uploading document:', error);
250
+ alert('Failed to upload document');
251
+ } finally {
252
+ loading.style.display = 'none';
253
+ }
254
+ }
255
+
256
+ // Close modal when clicking outside
257
+ window.addEventListener('click', (event) => {
258
+ if (event.target === statsModal) {
259
+ statsModal.style.display = 'none';
260
+ }
261
  });