Spaces:
No application file
No application file
File size: 6,006 Bytes
60b4e60 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Intent Recognition API Client</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; }
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px; height: 36px; border-radius: 50%;
border-left-color: #09f;
animation: spin 1s ease infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="w-full max-w-2xl mx-auto bg-white rounded-xl shadow-lg p-8">
<div class="text-center mb-8">
<h1 class="text-3xl font-bold text-gray-800">Intent Recognizer</h1>
<p class="text-gray-500 mt-2">Enter a sentence to find its top intents using the API.</p>
</div>
<div class="space-y-4">
<textarea id="query-input" class="w-full h-24 p-4 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500" placeholder="e.g., can you reduce the price on the house in islington"></textarea>
<button id="submit-btn" class="w-full bg-blue-600 text-white font-semibold py-3 px-4 rounded-lg hover:bg-blue-700">
Recognize Intent
</button>
</div>
<div id="results-container" class="mt-8 hidden">
<h2 class="text-2xl font-semibold text-gray-700 mb-4 border-b pb-2">API Response</h2>
<div id="results-content" class="space-y-6 bg-gray-50 p-6 rounded-lg">
<!-- Results will be injected here -->
</div>
</div>
<div id="loading-spinner" class="hidden justify-center mt-8">
<div class="spinner"></div>
</div>
<div id="error-message" class="hidden mt-6 p-4 bg-red-100 text-red-700 border border-red-300 rounded-lg"></div>
</div>
<script>
const submitBtn = document.getElementById('submit-btn');
const queryInput = document.getElementById('query-input');
const resultsContainer = document.getElementById('results-container');
const resultsContent = document.getElementById('results-content');
const loadingSpinner = document.getElementById('loading-spinner');
const errorMessage = document.getElementById('error-message');
const API_URL = "http://127.0.0.1:8000/recognize-intent/";
submitBtn.addEventListener('click', async () => {
const query = queryInput.value.trim();
if (!query) { return; }
loadingSpinner.style.display = 'flex';
resultsContainer.style.display = 'none';
errorMessage.style.display = 'none';
resultsContent.innerHTML = '';
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: query }),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `API request failed: ${response.status}`);
}
const data = await response.json();
displayResults(data);
} catch (error) {
displayError(error);
} finally {
loadingSpinner.style.display = 'none';
}
});
function displayResults(data) {
resultsContainer.style.display = 'block';
// FIX: The API now returns a `predictions` array. We must loop through it.
// The error occurs when trying to access `data.confidence_score` which no longer exists.
// The correct value is inside each `prediction` object as `prediction.score`.
data.predictions.forEach((prediction, index) => {
const intent = prediction.intent;
const score = prediction.score.toFixed(4); // Accessing score from the prediction object
const resultBlock = `
<div>
<div class="flex justify-between items-center">
<span class="text-sm font-bold text-gray-500">Prediction #${index + 1}</span>
</div>
<div class="mt-2 grid grid-cols-2 gap-4">
<div class="flex flex-col">
<span class="text-sm font-medium text-gray-500">Recognized Intent</span>
<span class="text-lg font-semibold text-gray-800">${intent}</span>
</div>
<div class="flex flex-col">
<span class="text-sm font-medium text-gray-500">Confidence Score</span>
<span class="text-lg font-semibold text-gray-800">${score}</span>
</div>
</div>
</div>
`;
// Add a divider if it's not the last item
resultsContent.innerHTML += resultBlock + (index < data.predictions.length - 1 ? '<hr class="my-3">' : '');
});
}
function displayError(error) {
errorMessage.style.display = 'block';
errorMessage.textContent = `An error occurred: ${error.message}. Please ensure the API server is running.`;
}
</script>
</body>
</html>
|