|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>Shopping Assistant Demo</title> |
|
<style> |
|
body { |
|
font-family: Arial, sans-serif; |
|
max-width: 800px; |
|
margin: 0 auto; |
|
padding: 20px; |
|
line-height: 1.6; |
|
} |
|
h1 { |
|
color: #2c3e50; |
|
text-align: center; |
|
} |
|
.container { |
|
border: 1px solid #ddd; |
|
border-radius: 5px; |
|
padding: 20px; |
|
margin-top: 20px; |
|
} |
|
.form-group { |
|
margin-bottom: 15px; |
|
} |
|
label { |
|
display: block; |
|
margin-bottom: 5px; |
|
font-weight: bold; |
|
} |
|
input[type="text"] { |
|
width: 100%; |
|
padding: 8px; |
|
border: 1px solid #ddd; |
|
border-radius: 4px; |
|
box-sizing: border-box; |
|
} |
|
button { |
|
background-color: #3498db; |
|
color: white; |
|
border: none; |
|
padding: 10px 15px; |
|
border-radius: 4px; |
|
cursor: pointer; |
|
} |
|
button:hover { |
|
background-color: #2980b9; |
|
} |
|
#result { |
|
margin-top: 20px; |
|
padding: 15px; |
|
border: 1px solid #ddd; |
|
border-radius: 4px; |
|
background-color: #f9f9f9; |
|
white-space: pre-wrap; |
|
display: none; |
|
} |
|
.loading { |
|
text-align: center; |
|
margin-top: 20px; |
|
display: none; |
|
} |
|
.category { |
|
display: inline-block; |
|
margin: 5px; |
|
padding: 5px 10px; |
|
border-radius: 15px; |
|
color: white; |
|
font-size: 14px; |
|
} |
|
.electronics { background-color: #3498db; } |
|
.clothing { background-color: #9b59b6; } |
|
.home { background-color: #2ecc71; } |
|
.kitchen { background-color: #e74c3c; } |
|
.toys { background-color: #f39c12; } |
|
.other { background-color: #95a5a6; } |
|
</style> |
|
</head> |
|
<body> |
|
<h1>Shopping Assistant Demo</h1> |
|
|
|
<div class="container"> |
|
<p> |
|
This demo shows how to use the Shopping Assistant model to classify shopping queries into categories. |
|
Enter a shopping query below to see which categories it belongs to. |
|
</p> |
|
|
|
<div class="form-group"> |
|
<label for="query">Shopping Query:</label> |
|
<input type="text" id="query" placeholder="e.g., I'm looking for headphones" value="I'm looking for headphones"> |
|
</div> |
|
|
|
<button id="submit">Classify Query</button> |
|
|
|
<div class="loading" id="loading"> |
|
<p>Classifying your query...</p> |
|
</div> |
|
|
|
<div id="result"></div> |
|
</div> |
|
|
|
<script> |
|
document.getElementById('submit').addEventListener('click', async () => { |
|
const query = document.getElementById('query').value.trim(); |
|
if (!query) { |
|
alert('Please enter a shopping query'); |
|
return; |
|
} |
|
|
|
const loading = document.getElementById('loading'); |
|
const result = document.getElementById('result'); |
|
|
|
loading.style.display = 'block'; |
|
result.style.display = 'none'; |
|
|
|
try { |
|
|
|
const modelId = 'selvaonline/shopping-assistant'; |
|
const response = await fetch( |
|
`https://api-inference.huggingface.co/models/${modelId}`, |
|
{ |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
|
|
|
|
}, |
|
body: JSON.stringify({ |
|
inputs: query, |
|
options: { |
|
wait_for_model: true |
|
} |
|
}) |
|
} |
|
); |
|
|
|
if (!response.ok) { |
|
throw new Error(`HTTP error! status: ${response.status}`); |
|
} |
|
|
|
const data = await response.json(); |
|
|
|
|
|
const categories = ["electronics", "clothing", "home", "kitchen", "toys", "other"]; |
|
const probabilities = data[0].map(logit => 1 / (1 + Math.exp(-logit))); |
|
|
|
|
|
const topCategories = []; |
|
for (let i = 0; i < probabilities.length; i++) { |
|
if (probabilities[i] > 0.5) { |
|
topCategories.push({ |
|
category: categories[i], |
|
score: probabilities[i] |
|
}); |
|
} |
|
} |
|
|
|
|
|
topCategories.sort((a, b) => b.score - a.score); |
|
|
|
|
|
let resultHtml = `<h3>Results for: "${query}"</h3>`; |
|
|
|
if (topCategories.length > 0) { |
|
resultHtml += '<p>Top categories:</p>'; |
|
resultHtml += '<div>'; |
|
for (const item of topCategories) { |
|
resultHtml += `<span class="category ${item.category}">${item.category}: ${item.score.toFixed(4)}</span>`; |
|
} |
|
resultHtml += '</div>'; |
|
|
|
resultHtml += `<p>Based on your query, I would recommend looking for deals in the <strong>${topCategories[0].category}</strong> category.</p>`; |
|
} else { |
|
resultHtml += '<p>No categories found for your query. Please try a different query.</p>'; |
|
} |
|
|
|
result.innerHTML = resultHtml; |
|
result.style.display = 'block'; |
|
} catch (error) { |
|
console.error('Error:', error); |
|
result.innerHTML = `<p>Error: ${error.message}</p><p>Make sure the model is available and you have the correct permissions.</p>`; |
|
result.style.display = 'block'; |
|
} finally { |
|
loading.style.display = 'none'; |
|
} |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
|