Soltane777 commited on
Commit
ab17da2
·
verified ·
1 Parent(s): 200c447

Update frontend/script.js

Browse files
Files changed (1) hide show
  1. frontend/script.js +222 -79
frontend/script.js CHANGED
@@ -1,99 +1,242 @@
1
- async function uploadFile(endpoint) {
2
- const fileInput = document.getElementById("fileInput");
3
- const file = fileInput.files[0];
4
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  if (!file) {
6
- alert("Please select a file first.");
7
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
9
-
10
- const formData = new FormData();
11
- formData.append("file", file);
12
-
 
13
  try {
14
- const response = await fetch(`https://soltane777-textgeneration.hf.space/${endpoint}`, { // ✅ تم تصحيح المسار
15
- method: "POST",
16
- body: formData
17
- });
18
-
19
- const data = await response.json();
20
- document.getElementById("output").innerText = JSON.stringify(data, null, 2);
21
  } catch (error) {
22
- console.error("Error:", error);
23
- document.getElementById("output").innerText = "Error fetching data!";
 
 
24
  }
25
- }
26
-
27
- async function processText(endpoint) {
28
- const text = document.getElementById("textInput").value;
29
-
 
 
30
  if (!text) {
31
- alert("Please enter some text.");
32
- return;
33
  }
34
-
35
- const formData = new FormData();
36
- formData.append("text", text);
37
-
 
 
 
38
  try {
39
- const response = await fetch(`https://soltane777-textgeneration.hf.space/${endpoint}`, { // ✅ تم تصحيح المسار
40
- method: "POST",
41
- body: formData
42
- });
43
-
44
- const data = await response.json();
45
- document.getElementById("output").innerText = JSON.stringify(data, null, 2);
46
  } catch (error) {
47
- console.error("Error:", error);
48
- document.getElementById("output").innerText = "Error fetching data!";
 
 
49
  }
50
- }
51
-
52
- async function askQuestion() {
53
- const context = document.getElementById("contextInput").value;
54
- const question = document.getElementById("questionInput").value;
55
-
56
  if (!context || !question) {
57
- alert("يرجى إدخال كل من النص المرجعي والسؤال.");
58
- return;
59
  }
60
-
 
61
  try {
62
- const response = await fetch("https://soltane777-textgeneration.hf.space/qa/", {
63
- method: "POST",
64
- headers: { "Content-Type": "application/json" },
65
- body: JSON.stringify({ context: context, question: question })
66
- });
67
-
68
- const data = await response.json();
69
- document.getElementById("output").innerText = JSON.stringify(data, null, 2);
70
  } catch (error) {
71
- console.error("Error:", error);
72
- document.getElementById("output").innerText = "Error fetching data!";
 
 
73
  }
74
- }
75
-
76
- async function generateCode() {
77
- const prompt = document.getElementById("codeInput").value;
78
-
79
  if (!prompt) {
80
- alert("يرجى إدخال وصف للرسم البياني.");
81
- return;
82
  }
83
-
84
- const formData = new FormData();
85
- formData.append("prompt", prompt);
86
-
 
87
  try {
88
- const response = await fetch("https://soltane777-textgeneration.hf.space/generate_code/", {
89
- method: "POST",
90
- body: formData
91
- });
92
-
93
- const data = await response.json();
94
- document.getElementById("output").innerText = JSON.stringify(data, null, 2);
95
  } catch (error) {
96
- console.error("Error:", error);
97
- document.getElementById("output").innerText = "Error fetching data!";
 
 
98
  }
99
- }
 
 
1
+ // Tab switching functionality
2
+ document.addEventListener("DOMContentLoaded", () => {
3
+ // Tab switching
4
+ const tabButtons = document.querySelectorAll(".tab-button")
5
+ const tabContents = document.querySelectorAll(".tab-content")
6
+
7
+ tabButtons.forEach((button) => {
8
+ button.addEventListener("click", () => {
9
+ // Remove active class from all buttons and hide all contents
10
+ tabButtons.forEach((btn) => btn.classList.remove("active"))
11
+ tabContents.forEach((content) => content.classList.add("hidden"))
12
+
13
+ // Add active class to clicked button and show corresponding content
14
+ button.classList.add("active")
15
+ const contentId = "content-" + button.id.split("-")[1]
16
+ document.getElementById(contentId).classList.remove("hidden")
17
+ })
18
+ })
19
+
20
+ // File input handling
21
+ const fileInput = document.getElementById("fileInput")
22
+ const fileName = document.getElementById("fileName")
23
+
24
+ fileInput.addEventListener("change", () => {
25
+ if (fileInput.files.length > 0) {
26
+ fileName.textContent = fileInput.files[0].name
27
+ } else {
28
+ fileName.textContent = "No file chosen"
29
+ }
30
+ })
31
+
32
+ // Copy output button
33
+ const copyButton = document.getElementById("copyOutput")
34
+ copyButton.addEventListener("click", () => {
35
+ const output = document.getElementById("output")
36
+ navigator.clipboard
37
+ .writeText(output.textContent)
38
+ .then(() => {
39
+ showNotification("Results copied successfully!")
40
+ })
41
+ .catch((err) => {
42
+ console.error("Failed to copy text: ", err)
43
+ })
44
+ })
45
+
46
+ // Translation options toggle
47
+ const translateBtn = document.getElementById("translateBtn")
48
+ const translationOptions = document.getElementById("translationOptions")
49
+
50
+ translateBtn.addEventListener("click", () => {
51
+ // Toggle translation options visibility
52
+ if (translationOptions.classList.contains("hidden")) {
53
+ translationOptions.classList.remove("hidden")
54
+ } else {
55
+ // If options are already visible, perform translation
56
+ translateText()
57
+ }
58
+ })
59
+ })
60
+
61
+ // Show notification function
62
+ function showNotification(message) {
63
+ // Create notification element
64
+ const notification = document.createElement("div")
65
+ notification.className =
66
+ "notification fixed top-4 right-4 bg-green-100 border-l-4 border-green-500 text-green-700 p-4 rounded shadow-md"
67
+ notification.innerHTML = message
68
+
69
+ // Add to document
70
+ document.body.appendChild(notification)
71
+
72
+ // Remove after 3 seconds
73
+ setTimeout(() => {
74
+ notification.remove()
75
+ }, 3000)
76
+ }
77
+
78
+ // Show/hide loader
79
+ function toggleLoader(show) {
80
+ const loader = document.getElementById("loader")
81
+ if (show) {
82
+ loader.classList.remove("hidden")
83
+ } else {
84
+ loader.classList.add("hidden")
85
+ }
86
+ }
87
+
88
+ // API Functions
89
+ async function uploadFile(endpoint) {
90
+ const fileInput = document.getElementById("fileInput")
91
+ const file = fileInput.files[0]
92
+
93
  if (!file) {
94
+ showNotification("Please select a file first.")
95
+ return
96
+ }
97
+
98
+ toggleLoader(true)
99
+ const formData = new FormData()
100
+ formData.append("file", file)
101
+
102
+ try {
103
+ const response = await fetch(`https://soltane777-textgeneration.hf.space/${endpoint}`, {
104
+ method: "POST",
105
+ body: formData,
106
+ })
107
+
108
+ const data = await response.json()
109
+ document.getElementById("output").innerText = JSON.stringify(data, null, 2)
110
+ } catch (error) {
111
+ console.error("Error:", error)
112
+ document.getElementById("output").innerText = "Error fetching data!"
113
+ } finally {
114
+ toggleLoader(false)
115
+ }
116
+ }
117
+
118
+ async function processText(endpoint) {
119
+ // If endpoint is translate, we now handle it separately
120
+ if (endpoint === "translate") {
121
+ // Show translation options if they're hidden
122
+ if (document.getElementById("translationOptions").classList.contains("hidden")) {
123
+ document.getElementById("translationOptions").classList.remove("hidden")
124
+ return
125
+ }
126
+ return translateText()
127
+ }
128
+
129
+ const text = document.getElementById("textInput").value
130
+
131
+ if (!text) {
132
+ showNotification("Please enter text first.")
133
+ return
134
  }
135
+
136
+ toggleLoader(true)
137
+ const formData = new FormData()
138
+ formData.append("text", text)
139
+
140
  try {
141
+ const response = await fetch(`https://soltane777-textgeneration.hf.space/${endpoint}`, {
142
+ method: "POST",
143
+ body: formData,
144
+ })
145
+
146
+ const data = await response.json()
147
+ document.getElementById("output").innerText = JSON.stringify(data, null, 2)
148
  } catch (error) {
149
+ console.error("Error:", error)
150
+ document.getElementById("output").innerText = "Error fetching data!"
151
+ } finally {
152
+ toggleLoader(false)
153
  }
154
+ }
155
+
156
+ async function translateText() {
157
+ const text = document.getElementById("textInput").value
158
+ const sourceLanguage = document.getElementById("sourceLanguage").value
159
+ const targetLanguage = document.getElementById("targetLanguage").value
160
+
161
  if (!text) {
162
+ showNotification("Please enter text to translate.")
163
+ return
164
  }
165
+
166
+ toggleLoader(true)
167
+ const formData = new FormData()
168
+ formData.append("text", text)
169
+ formData.append("source_lang", sourceLanguage)
170
+ formData.append("target_lang", targetLanguage)
171
+
172
  try {
173
+ const response = await fetch("https://soltane777-textgeneration.hf.space/translate", {
174
+ method: "POST",
175
+ body: formData,
176
+ })
177
+
178
+ const data = await response.json()
179
+ document.getElementById("output").innerText = JSON.stringify(data, null, 2)
180
  } catch (error) {
181
+ console.error("Error:", error)
182
+ document.getElementById("output").innerText = "Error fetching translation data!"
183
+ } finally {
184
+ toggleLoader(false)
185
  }
186
+ }
187
+
188
+ async function askQuestion() {
189
+ const context = document.getElementById("contextInput").value
190
+ const question = document.getElementById("questionInput").value
191
+
192
  if (!context || !question) {
193
+ showNotification("Please enter both reference text and question.")
194
+ return
195
  }
196
+
197
+ toggleLoader(true)
198
  try {
199
+ const response = await fetch("https://soltane777-textgeneration.hf.space/qa/", {
200
+ method: "POST",
201
+ headers: { "Content-Type": "application/json" },
202
+ body: JSON.stringify({ context: context, question: question }),
203
+ })
204
+
205
+ const data = await response.json()
206
+ document.getElementById("output").innerText = JSON.stringify(data, null, 2)
207
  } catch (error) {
208
+ console.error("Error:", error)
209
+ document.getElementById("output").innerText = "Error fetching data!"
210
+ } finally {
211
+ toggleLoader(false)
212
  }
213
+ }
214
+
215
+ async function generateCode() {
216
+ const prompt = document.getElementById("codeInput").value
217
+
218
  if (!prompt) {
219
+ showNotification("Please enter a description for the code.")
220
+ return
221
  }
222
+
223
+ toggleLoader(true)
224
+ const formData = new FormData()
225
+ formData.append("prompt", prompt)
226
+
227
  try {
228
+ const response = await fetch("https://soltane777-textgeneration.hf.space/generate_code/", {
229
+ method: "POST",
230
+ body: formData,
231
+ })
232
+
233
+ const data = await response.json()
234
+ document.getElementById("output").innerText = JSON.stringify(data, null, 2)
235
  } catch (error) {
236
+ console.error("Error:", error)
237
+ document.getElementById("output").innerText = "Error fetching data!"
238
+ } finally {
239
+ toggleLoader(false)
240
  }
241
+ }
242
+