Spaces:
Runtime error
Runtime error
File size: 5,913 Bytes
bcfd653 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentiment Analysis</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
textarea {
width: 100%;
min-height: 120px;
padding: 15px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 16px;
resize: vertical;
}
button {
background-color: #007bff;
color: white;
padding: 12px 24px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 10px 5px;
}
button:hover {
background-color: #0056b3;
}
.result {
margin-top: 20px;
padding: 15px;
border-radius: 5px;
font-weight: bold;
}
.positive {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.negative {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.confidence {
margin-top: 10px;
font-size: 14px;
}
.api-info {
margin-top: 30px;
padding: 20px;
background-color: #e9ecef;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>🎭 Sentiment Analysis</h1>
<p>Enter your text below to analyze its sentiment:</p>
<textarea id="textInput" placeholder="Type your text here...">I love this new product! It's amazing and works perfectly.</textarea>
<div style="text-align: center;">
<button onclick="predictSentiment()">Predict Sentiment</button>
<button onclick="getProbabilities()">Get Probabilities</button>
</div>
<div id="result"></div>
<div class="api-info">
<h3>API Endpoints:</h3>
<ul>
<li><strong>/predict</strong> - Get sentiment prediction (0 or 1)</li>
<li><strong>/predict_proba</strong> - Get prediction probabilities</li>
<li><strong>/docs</strong> - Interactive API documentation</li>
</ul>
</div>
</div>
<script>
async function predictSentiment() {
const text = document.getElementById('textInput').value;
if (!text.trim()) {
alert('Please enter some text');
return;
}
try {
const response = await fetch('/predict', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: text })
});
const result = await response.json();
displayResult(result, 'prediction');
} catch (error) {
console.error('Error:', error);
displayError('Error making prediction');
}
}
async function getProbabilities() {
const text = document.getElementById('textInput').value;
if (!text.trim()) {
alert('Please enter some text');
return;
}
try {
const response = await fetch('/predict_proba', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: text })
});
const result = await response.json();
displayResult(result, 'probability');
} catch (error) {
console.error('Error:', error);
displayError('Error getting probabilities');
}
}
function displayResult(result, type) {
const resultDiv = document.getElementById('result');
const sentimentClass = result.sentiment === 'positive' ? 'positive' : 'negative';
let html = `<div class="result ${sentimentClass}">
<div>Sentiment: ${result.sentiment.toUpperCase()} (${result.prediction})</div>`;
if (type === 'prediction') {
html += `<div class="confidence">Confidence: ${(result.confidence * 100).toFixed(1)}%</div>`;
} else if (type === 'probability') {
html += `<div class="confidence">
Probabilities: Negative ${(result.probabilities[0] * 100).toFixed(1)}%,
Positive ${(result.probabilities[1] * 100).toFixed(1)}%
</div>`;
}
html += `</div>`;
resultDiv.innerHTML = html;
}
function displayError(message) {
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = `<div class="result" style="background-color: #f8d7da; color: #721c24;">${message}</div>`;
}
</script>
</body>
</html>
|