|
1 | 1 | 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"); |
5 | 5 |
|
6 | | - if (!reviewText) { |
7 | | - alert("Please enter a review."); |
8 | | - return; |
9 | | - } |
| 6 | + if (!reviewText) { |
| 7 | + alert("Please enter a review."); |
| 8 | + return; |
| 9 | + } |
10 | 10 |
|
11 | | - const requestBody = { |
12 | | - data: reviewText |
13 | | - }; |
| 11 | + const requestBody = { |
| 12 | + data: reviewText, |
| 13 | + }; |
14 | 14 |
|
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 |
21 | 23 |
|
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 | + }); |
29 | 31 |
|
30 | | - const result = await response.json(); |
| 32 | + const result = await response.json(); |
| 33 | + console.log(result.prediction); |
31 | 34 |
|
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"; |
35 | 38 |
|
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) { |
51 | 45 | 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"); |
54 | 52 | } |
| 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 | + } |
55 | 59 | } |
0 commit comments