Update index.html
Browse files- index.html +37 -21
index.html
CHANGED
@@ -108,39 +108,55 @@
|
|
108 |
scoreText.innerText = "";
|
109 |
improvedText.innerText = "";
|
110 |
|
111 |
-
let body;
|
112 |
-
let headers = {};
|
113 |
-
|
114 |
-
if (audioFile) {
|
115 |
-
const formData = new FormData();
|
116 |
-
formData.append("data", audioFile); // audio file
|
117 |
-
formData.append("data", ""); // empty text
|
118 |
-
body = formData;
|
119 |
-
} else {
|
120 |
-
headers["Content-Type"] = "application/json";
|
121 |
-
body = JSON.stringify({
|
122 |
-
data: [null, text]
|
123 |
-
});
|
124 |
-
}
|
125 |
-
|
126 |
try {
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
const json = await response.json();
|
|
|
|
|
|
|
|
|
|
|
134 |
const [baseline, score, improved] = json.data;
|
135 |
|
136 |
baselineText.innerText = baseline;
|
137 |
scoreText.innerText = `${score}%`;
|
138 |
improvedText.innerText = improved;
|
|
|
139 |
} catch (err) {
|
140 |
baselineText.innerText = "❌ Error calling API.";
|
141 |
-
console.error(err);
|
142 |
}
|
143 |
}
|
144 |
</script>
|
|
|
|
|
145 |
</body>
|
146 |
</html>
|
|
|
108 |
scoreText.innerText = "";
|
109 |
improvedText.innerText = "";
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
try {
|
112 |
+
let response;
|
113 |
+
|
114 |
+
if (audioFile) {
|
115 |
+
// FormData for file + text input
|
116 |
+
const formData = new FormData();
|
117 |
+
formData.append("data", audioFile); // audio file
|
118 |
+
formData.append("data", ""); // empty string for text
|
119 |
+
|
120 |
+
response = await fetch(API_URL, {
|
121 |
+
method: "POST",
|
122 |
+
body: formData
|
123 |
+
});
|
124 |
+
} else {
|
125 |
+
// JSON body when no file
|
126 |
+
response = await fetch(API_URL, {
|
127 |
+
method: "POST",
|
128 |
+
headers: {
|
129 |
+
"Content-Type": "application/json"
|
130 |
+
},
|
131 |
+
body: JSON.stringify({
|
132 |
+
data: [null, text]
|
133 |
+
})
|
134 |
+
});
|
135 |
+
}
|
136 |
+
|
137 |
+
if (!response.ok) {
|
138 |
+
throw new Error("Network response was not OK");
|
139 |
+
}
|
140 |
|
141 |
const json = await response.json();
|
142 |
+
|
143 |
+
if (!json || !json.data || json.data.length < 3) {
|
144 |
+
throw new Error("Unexpected API response format");
|
145 |
+
}
|
146 |
+
|
147 |
const [baseline, score, improved] = json.data;
|
148 |
|
149 |
baselineText.innerText = baseline;
|
150 |
scoreText.innerText = `${score}%`;
|
151 |
improvedText.innerText = improved;
|
152 |
+
|
153 |
} catch (err) {
|
154 |
baselineText.innerText = "❌ Error calling API.";
|
155 |
+
console.error("API call error:", err);
|
156 |
}
|
157 |
}
|
158 |
</script>
|
159 |
+
|
160 |
+
|
161 |
</body>
|
162 |
</html>
|