Spaces:
Runtime error
Runtime error
| <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> | |