test / index.html
aringad's picture
Update index.html
28048a3 verified
raw
history blame
3.57 kB
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>OpenBuddy Text Generation</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>OpenBuddy Text Generation</h1>
<form id="generateForm">
<div class="form-group">
<label for="promptInput">Enter your prompt:</label>
<textarea id="promptInput" rows="5" cols="50" required></textarea>
</div>
<button type="submit">Generate Text</button>
</form>
<div id="resultContainer" style="display: none;">
<h2>Generated Text:</h2>
<p id="generatedText"></p>
</div>
<div id="debugContainer">
<h2>Debug Information:</h2>
<pre id="debugOutput"></pre>
</div>
</div>
<script>
document.getElementById('generateForm').addEventListener('submit', function(e) {
e.preventDefault();
const promptInput = document.getElementById('promptInput');
const prompt = promptInput.value.trim();
if (prompt !== '') {
generateText(prompt);
}
});
function generateText(prompt) {
const apiUrl = 'https://api-inference.huggingface.co/models/OpenBuddy/openbuddy-llama3-8b-v21.1-8k';
const apiKey = '{{API_KEY}}'; // La chiave API verrà sostituita dalla variabile d'ambiente
const requestData = {
inputs: prompt,
parameters: {
max_new_tokens: 100
}
};
// Display request data in the debug output
document.getElementById('debugOutput').textContent = 'Request Data:\n' + JSON.stringify(requestData, null, 2);
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify(requestData)
})
.then(response => {
// Display response status and headers in the debug output
document.getElementById('debugOutput').textContent += '\n\nResponse Status: ' + response.status;
document.getElementById('debugOutput').textContent += '\nResponse Headers:\n' + JSON.stringify(Object.fromEntries(response.headers), null, 2);
if (!response.ok) {
throw new Error('Request failed with status: ' + response.status);
}
return response.json();
})
.then(data => {
// Display response data in the debug output
document.getElementById('debugOutput').textContent += '\n\nResponse Data:\n' + JSON.stringify(data, null, 2);
const generatedText = data[0].generated_text;
document.getElementById('generatedText').textContent = generatedText;
document.getElementById('resultContainer').style.display = 'block';
})
.catch(error => {
console.error('Error:', error);
// Display error in the debug output
document.getElementById('debugOutput').textContent += '\n\nError:\n' + error.message;
alert('An error occurred while generating text.');
});
}
</script>
</body>
</html>