chenguittiMaroua commited on
Commit
892aeb4
·
verified ·
1 Parent(s): 2393498

Update static/test.js

Browse files
Files changed (1) hide show
  1. static/test.js +104 -113
static/test.js CHANGED
@@ -1,150 +1,145 @@
1
  document.addEventListener('DOMContentLoaded', function() {
2
- // Tab switching functionality
3
- const tabBtns = document.querySelectorAll('.tab-btn');
4
  const tabContents = document.querySelectorAll('.tab-content');
5
 
6
- tabBtns.forEach(btn => {
7
- btn.addEventListener('click', () => {
8
- tabBtns.forEach(b => b.classList.remove('active'));
9
- tabContents.forEach(c => c.classList.remove('active'));
10
- btn.classList.add('active');
11
- const tabId = btn.getAttribute('data-tab');
 
 
 
12
  document.getElementById(tabId).classList.add('active');
13
  });
14
  });
15
-
16
- // API configuration
17
- const API_BASE_URL = window.location.origin;
18
- const API_PREFIX = '/api';
19
- ...
20
- fetch(`${API_BASE_URL}${API_PREFIX}/summarize`, ...)
21
 
22
- // Summarize document
 
 
 
 
23
  document.getElementById('summarize-btn').addEventListener('click', async function() {
24
  const fileInput = document.getElementById('summarize-file');
25
  const resultArea = document.getElementById('summarize-result');
26
 
27
- if (!fileInput.files.length) {
28
- showError(resultArea, 'Please select a file first');
29
  return;
30
  }
31
-
 
 
 
 
 
32
  try {
33
- showLoading(resultArea, 'Processing document...');
34
-
35
- const formData = new FormData();
36
- formData.append('file', fileInput.files[0]);
37
-
38
- const response = await fetch(`${API_BASE_URL}${API_PREFIX}/summarize`, {
39
  method: 'POST',
40
  body: formData
41
  });
42
 
43
  if (!response.ok) {
44
- const error = await response.json();
45
- throw new Error(error.detail || 'Failed to summarize document');
46
  }
47
 
48
  const data = await response.json();
49
- showResult(resultArea, data.summary);
 
 
 
 
50
  } catch (error) {
51
- showError(resultArea, error.message);
 
52
  }
53
  });
54
-
55
- // Ask question
56
  document.getElementById('ask-btn').addEventListener('click', async function() {
57
  const fileInput = document.getElementById('ask-file');
58
  const questionInput = document.getElementById('ask-question');
59
  const resultArea = document.getElementById('ask-result');
60
 
61
- if (!fileInput.files.length && !questionInput.value.trim()) {
62
- showError(resultArea, 'Please select a file or enter text first');
63
- return;
64
- }
65
-
66
  if (!questionInput.value.trim()) {
67
- showError(resultArea, 'Please enter a question');
68
  return;
69
  }
 
 
 
 
 
70
 
 
 
 
 
71
  try {
72
- showLoading(resultArea, 'Processing your question...');
73
-
74
- const formData = new FormData();
75
- if (fileInput.files.length) {
76
- formData.append('file', fileInput.files[0]);
77
- }
78
- formData.append('question', questionInput.value.trim());
79
-
80
- const response = await fetch(`${API_BASE_URL}${API_PREFIX}/ask`, {
81
  method: 'POST',
82
  body: formData
83
  });
84
 
85
  if (!response.ok) {
86
- const error = await response.json();
87
- throw new Error(error.detail || 'Failed to get answer');
88
  }
89
 
90
  const data = await response.json();
91
- const resultHtml = `
92
  <h3>Answer:</h3>
93
  <p>${data.answer}</p>
94
  <p class="confidence">Confidence: ${(data.confidence * 100).toFixed(1)}%</p>
95
  ${data.context_used ? `<details><summary>Context used</summary><p>${data.context_used}</p></details>` : ''}
96
  `;
97
- showResult(resultArea, resultHtml);
98
  } catch (error) {
99
- showError(resultArea, error.message);
 
100
  }
101
  });
102
-
103
- // Generate image caption
104
  document.getElementById('caption-btn').addEventListener('click', async function() {
105
  const fileInput = document.getElementById('caption-file');
106
  const resultArea = document.getElementById('caption-result');
107
 
108
- if (!fileInput.files.length) {
109
- showError(resultArea, 'Please select an image first');
110
  return;
111
  }
112
-
 
 
 
 
 
113
  try {
114
- showLoading(resultArea, 'Generating caption...');
115
-
116
- const formData = new FormData();
117
- formData.append('file', fileInput.files[0]);
118
-
119
- const response = await fetch(`${API_BASE_URL}${API_PREFIX}/caption`, {
120
  method: 'POST',
121
  body: formData
122
  });
123
 
124
  if (!response.ok) {
125
- const error = await response.json();
126
- throw new Error(error.detail || 'Failed to generate caption');
127
  }
128
 
129
  const data = await response.json();
130
- const reader = new FileReader();
131
- reader.onload = function(e) {
132
- const resultHtml = `
133
- <div class="image-preview">
134
- <img src="${e.target.result}" alt="Uploaded image">
135
- </div>
136
- <h3>Caption:</h3>
137
- <p>${data.caption}</p>
138
- `;
139
- showResult(resultArea, resultHtml);
140
- };
141
- reader.readAsDataURL(fileInput.files[0]);
142
  } catch (error) {
143
- showError(resultArea, error.message);
 
144
  }
145
  });
146
-
147
- // Translate text
148
  document.getElementById('translate-btn').addEventListener('click', async function() {
149
  const textInput = document.getElementById('translate-text');
150
  const sourceLang = document.getElementById('source-lang').value;
@@ -152,52 +147,48 @@ fetch(`${API_BASE_URL}${API_PREFIX}/summarize`, ...)
152
  const resultArea = document.getElementById('translate-result');
153
 
154
  if (!textInput.value.trim()) {
155
- showError(resultArea, 'Please enter text to translate');
156
  return;
157
  }
158
-
 
 
 
 
 
 
 
159
  try {
160
- showLoading(resultArea, 'Translating...');
161
-
162
- const response = await fetch(`${API_BASE_URL}${API_PREFIX}/translate`, {
163
  method: 'POST',
164
- headers: {
165
- 'Content-Type': 'application/x-www-form-urlencoded',
166
- },
167
- body: new URLSearchParams({
168
- 'text': textInput.value.trim(),
169
- 'target_lang': targetLang,
170
- 'src_lang': sourceLang
171
- })
172
  });
173
 
174
  if (!response.ok) {
175
- const error = await response.json();
176
- throw new Error(error.detail || 'Failed to translate text');
177
  }
178
 
179
  const data = await response.json();
180
- showResult(resultArea, data.translated_text);
 
 
 
 
181
  } catch (error) {
182
- showError(resultArea, error.message);
 
183
  }
184
  });
185
-
186
- // Helper functions
187
- function showLoading(element, message) {
188
- element.innerHTML = `
189
- <div class="loading-container">
190
- <span class="loading"></span>
191
- <span>${message}</span>
192
- </div>
193
- `;
194
- }
195
-
196
- function showResult(element, content) {
197
- element.innerHTML = `<div class="result">${content}</div>`;
198
- }
199
-
200
- function showError(element, message) {
201
- element.innerHTML = `<div class="error">Error: ${message}</div>`;
202
- }
203
  });
 
1
  document.addEventListener('DOMContentLoaded', function() {
2
+ // Tab functionality
3
+ const tabButtons = document.querySelectorAll('.tab-btn');
4
  const tabContents = document.querySelectorAll('.tab-content');
5
 
6
+ tabButtons.forEach(button => {
7
+ button.addEventListener('click', () => {
8
+ // Remove active class from all buttons and contents
9
+ tabButtons.forEach(btn => btn.classList.remove('active'));
10
+ tabContents.forEach(content => content.classList.remove('active'));
11
+
12
+ // Add active class to clicked button and corresponding content
13
+ button.classList.add('active');
14
+ const tabId = button.getAttribute('data-tab');
15
  document.getElementById(tabId).classList.add('active');
16
  });
17
  });
 
 
 
 
 
 
18
 
19
+ // API base URL - replace with your Hugging Face Space URL
20
+ const API_BASE_URL = "https://your-username-your-space-name.hf.space";
21
+ // For local testing: "http://localhost:7860"
22
+
23
+ // Summarize functionality
24
  document.getElementById('summarize-btn').addEventListener('click', async function() {
25
  const fileInput = document.getElementById('summarize-file');
26
  const resultArea = document.getElementById('summarize-result');
27
 
28
+ if (!fileInput.files[0]) {
29
+ resultArea.textContent = "Please select a file first";
30
  return;
31
  }
32
+
33
+ resultArea.textContent = "Processing document...";
34
+
35
+ const formData = new FormData();
36
+ formData.append('file', fileInput.files[0]);
37
+
38
  try {
39
+ const response = await fetch(`${API_BASE_URL}/ask`, {
 
 
 
 
 
40
  method: 'POST',
41
  body: formData
42
  });
43
 
44
  if (!response.ok) {
45
+ throw new Error(`HTTP error! status: ${response.status}`);
 
46
  }
47
 
48
  const data = await response.json();
49
+ resultArea.innerHTML = `
50
+ <h3>Summary:</h3>
51
+ <p>${data.answer}</p>
52
+ <p class="confidence">Confidence: ${(data.confidence * 100).toFixed(1)}%</p>
53
+ `;
54
  } catch (error) {
55
+ resultArea.textContent = `Error: ${error.message}`;
56
+ console.error('Summarization error:', error);
57
  }
58
  });
59
+
60
+ // Question Answering functionality
61
  document.getElementById('ask-btn').addEventListener('click', async function() {
62
  const fileInput = document.getElementById('ask-file');
63
  const questionInput = document.getElementById('ask-question');
64
  const resultArea = document.getElementById('ask-result');
65
 
 
 
 
 
 
66
  if (!questionInput.value.trim()) {
67
+ resultArea.textContent = "Please enter a question";
68
  return;
69
  }
70
+
71
+ resultArea.textContent = "Processing your question...";
72
+
73
+ const formData = new FormData();
74
+ formData.append('question', questionInput.value);
75
 
76
+ if (fileInput.files[0]) {
77
+ formData.append('file', fileInput.files[0]);
78
+ }
79
+
80
  try {
81
+ const response = await fetch(`${API_BASE_URL}/ask`, {
 
 
 
 
 
 
 
 
82
  method: 'POST',
83
  body: formData
84
  });
85
 
86
  if (!response.ok) {
87
+ throw new Error(`HTTP error! status: ${response.status}`);
 
88
  }
89
 
90
  const data = await response.json();
91
+ resultArea.innerHTML = `
92
  <h3>Answer:</h3>
93
  <p>${data.answer}</p>
94
  <p class="confidence">Confidence: ${(data.confidence * 100).toFixed(1)}%</p>
95
  ${data.context_used ? `<details><summary>Context used</summary><p>${data.context_used}</p></details>` : ''}
96
  `;
 
97
  } catch (error) {
98
+ resultArea.textContent = `Error: ${error.message}`;
99
+ console.error('Question answering error:', error);
100
  }
101
  });
102
+
103
+ // Image Captioning functionality
104
  document.getElementById('caption-btn').addEventListener('click', async function() {
105
  const fileInput = document.getElementById('caption-file');
106
  const resultArea = document.getElementById('caption-result');
107
 
108
+ if (!fileInput.files[0]) {
109
+ resultArea.textContent = "Please select an image first";
110
  return;
111
  }
112
+
113
+ resultArea.textContent = "Generating caption...";
114
+
115
+ const formData = new FormData();
116
+ formData.append('file', fileInput.files[0]);
117
+
118
  try {
119
+ const response = await fetch(`${API_BASE_URL}/api/caption`, {
 
 
 
 
 
120
  method: 'POST',
121
  body: formData
122
  });
123
 
124
  if (!response.ok) {
125
+ throw new Error(`HTTP error! status: ${response.status}`);
 
126
  }
127
 
128
  const data = await response.json();
129
+ resultArea.innerHTML = `
130
+ <h3>Caption:</h3>
131
+ <p>${data.caption}</p>
132
+ <div class="image-preview">
133
+ <img src="${URL.createObjectURL(fileInput.files[0])}" alt="Uploaded image">
134
+ </div>
135
+ `;
 
 
 
 
 
136
  } catch (error) {
137
+ resultArea.textContent = `Error: ${error.message}`;
138
+ console.error('Captioning error:', error);
139
  }
140
  });
141
+
142
+ // Translation functionality
143
  document.getElementById('translate-btn').addEventListener('click', async function() {
144
  const textInput = document.getElementById('translate-text');
145
  const sourceLang = document.getElementById('source-lang').value;
 
147
  const resultArea = document.getElementById('translate-result');
148
 
149
  if (!textInput.value.trim()) {
150
+ resultArea.textContent = "Please enter text to translate";
151
  return;
152
  }
153
+
154
+ resultArea.textContent = "Translating...";
155
+
156
+ const formData = new FormData();
157
+ formData.append('text', textInput.value);
158
+ formData.append('target_lang', targetLang);
159
+ formData.append('src_lang', sourceLang);
160
+
161
  try {
162
+ const response = await fetch(`${API_BASE_URL}/translate`, {
 
 
163
  method: 'POST',
164
+ body: formData
 
 
 
 
 
 
 
165
  });
166
 
167
  if (!response.ok) {
168
+ throw new Error(`HTTP error! status: ${response.status}`);
 
169
  }
170
 
171
  const data = await response.json();
172
+ resultArea.innerHTML = `
173
+ <h3>Translation:</h3>
174
+ <p>${data.translated_text}</p>
175
+ <p class="language-info">Translated from ${document.getElementById('source-lang').options[document.getElementById('source-lang').selectedIndex].text} to ${document.getElementById('target-lang').options[document.getElementById('target-lang').selectedIndex].text}</p>
176
+ `;
177
  } catch (error) {
178
+ resultArea.textContent = `Error: ${error.message}`;
179
+ console.error('Translation error:', error);
180
  }
181
  });
182
+
183
+ // File input styling
184
+ document.querySelectorAll('input[type="file"]').forEach(input => {
185
+ input.addEventListener('change', function() {
186
+ const label = this.nextElementSibling;
187
+ if (this.files[0]) {
188
+ label.textContent = this.files[0].name;
189
+ } else {
190
+ label.textContent = label.getAttribute('data-default') || 'Choose a file';
191
+ }
192
+ });
193
+ });
 
 
 
 
 
 
194
  });