Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit c87c2bc

Browse files
Merge pull request #10 from remla25-team21/small-frontend-fix
Small frontend fix
2 parents 7547232 + 6fdfcce commit c87c2bc

1 file changed

Lines changed: 48 additions & 44 deletions

File tree

app-frontend/predict.js

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
async function send_review() {
2-
const reviewText = document.getElementById("review").value.trim();
3-
const predictionBox = document.getElementById("prediction-box");
4-
const predictionSpan = document.getElementById("prediction");
2+
const reviewText = document.getElementById("review").value.trim();
3+
const predictionBox = document.getElementById("prediction-box");
4+
const predictionSpan = document.getElementById("prediction");
55

6-
if (!reviewText) {
7-
alert("Please enter a review.");
8-
return;
9-
}
6+
if (!reviewText) {
7+
alert("Please enter a review.");
8+
return;
9+
}
1010

11-
const requestBody = {
12-
data: reviewText
13-
};
11+
const requestBody = {
12+
data: reviewText,
13+
};
1414

15-
try {
16-
// Use relative URL or determine API URL based on current hostname
17-
// This works in both development and production environments
18-
const apiUrl = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
19-
? 'http://localhost:5000/predict' // For local development
20-
: 'http://app-service:5000/predict'; // For docker network
15+
try {
16+
// Use relative URL or determine API URL based on current hostname
17+
// This works in both development and production environments
18+
const apiUrl =
19+
window.location.hostname === "localhost" ||
20+
window.location.hostname === "127.0.0.1"
21+
? "http://localhost:5000/predict" // For local development
22+
: "http://app-service:5000/predict"; // For docker network
2123

22-
const response = await fetch(apiUrl, {
23-
method: "POST",
24-
headers: {
25-
"Content-Type": "application/json"
26-
},
27-
body: JSON.stringify(requestBody)
28-
});
24+
const response = await fetch(apiUrl, {
25+
method: "POST",
26+
headers: {
27+
"Content-Type": "application/json",
28+
},
29+
body: JSON.stringify(requestBody),
30+
});
2931

30-
const result = await response.json();
32+
const result = await response.json();
33+
console.log(result.prediction);
3134

32-
if (response.ok && result.prediction) {
33-
const sentiment = result.prediction.toLowerCase();
34-
predictionSpan.textContent = sentiment;
35+
if (response.ok && (result.prediction === 0 || result.prediction === 1)) {
36+
const sentiment = result.prediction;
37+
predictionSpan.textContent = sentiment === 1 ? "Positive" : "Negative";
3538

36-
// Style prediction result
37-
predictionBox.style.display = "block";
38-
if (sentiment.includes("positive")) {
39-
predictionBox.className = "prediction-box prediction-positive";
40-
} else if (sentiment.includes("negative")) {
41-
predictionBox.className = "prediction-box prediction-negative";
42-
predictionSpan.style.color = "#dc3545";
43-
} else {
44-
predictionBox.className = "prediction-box";
45-
}
46-
} else {
47-
throw new Error(result.error || "Unknown prediction error");
48-
}
49-
} catch (error) {
50-
predictionBox.style.display = "block";
39+
// Style prediction result
40+
predictionBox.style.display = "block";
41+
if (sentiment === 1) {
42+
predictionBox.className = "prediction-box prediction-positive";
43+
predictionSpan.style.color = "#28a745"; // Green for positive
44+
} else if (sentiment === 0) {
5145
predictionBox.className = "prediction-box prediction-negative";
52-
predictionSpan.style.color = "#dc3545";
53-
predictionSpan.textContent = "Error: " + error.message;
46+
predictionSpan.style.color = "#dc3545"; // Red for negative
47+
} else {
48+
predictionBox.className = "prediction-box"; // Default style if result is not 0 or 1
49+
}
50+
} else {
51+
throw new Error(result.error || "Unknown prediction error");
5452
}
53+
} catch (error) {
54+
predictionBox.style.display = "block";
55+
predictionBox.className = "prediction-box prediction-negative";
56+
predictionSpan.style.color = "#dc3545";
57+
predictionSpan.textContent = "Error: " + error.message;
58+
}
5559
}

0 commit comments

Comments
 (0)