-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
102 lines (87 loc) · 3.77 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Text Sentiment Predictor</title>
</head>
<body>
<h1>Text Sentiment Prediction</h1>
<form id="predictionForm">
<input type="file" id="csvFileInput" accept=".csv">
<textarea id="textInput" placeholder="Enter text..."></textarea>
<button type="button" onclick="predict()">Predict</button>
<button id="downloadBtn" style="display:none" onclick="downloadPredictions()">Download Predictions</button>
</form>
<div id="predictionResult"></div>
<div id="graphContainer"></div>
<script>
function predict() {
// Check if CSV file is present
var csvFileInput = document.getElementById("csvFileInput");
var textInput = document.getElementById("textInput");
var predictionResult = document.getElementById("predictionResult");
var graphContainer = document.getElementById("graphContainer");
if (csvFileInput.files.length > 0) {
// Upload CSV file
var formData = new FormData();
formData.append("file", csvFileInput.files[0]);
fetch("http://localhost:5000/predict", {
method: "POST",
body: formData
})
.then(response => {
if (response.headers.get('X-Graph-Exists') === 'true') {
console.log("Graph")
var graphData = response.headers.get('X-Graph-Data');
displayGraph(graphData);
}
return response.blob();
})
.then(blob => {
console.log("Blob:", blob);
document.getElementById("downloadBtn").style.display = "block";
document.getElementById("downloadBtn").onclick = function () {
console.log("Downloading...");
var url = URL.createObjectURL(blob);
console.log("URL:", url);
var a = document.createElement("a");
a.href = url;
a.download = "Predictions.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
})
.catch(error => {
console.error("Error:", error);
});
} else if (textInput.value.trim() !== "") {
// Predict on single sentence
fetch("http://localhost:5000/predict", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ "text": textInput.value.trim() })
})
.then(response => response.json())
.then(data => {
console.log(data)
predictionResult.innerHTML = "Predicted sentiment: " + data.prediction;
});
}
}
function downloadPredictions() {
console.log("Download prediction")
}
function displayGraph(graphData) {
var graphUrl = "data:image/png;base64," + graphData;
var img = document.createElement('img');
img.src = graphUrl;
graphContainer.appendChild(img);
}
</script>
</body>
</html>