ApnaAI / index.html
qwerty998999's picture
Create index.html
8baced6 verified
Raw
History Blame Contribute Delete
2.85 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Text Generator (GPT-2)</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f4f4f9;
}
.card {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
padding: 10px;
box-sizing: border-box;
}
button {
padding: 10px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:disabled {
background-color: #cccccc;
}
#output {
margin-top: 15px;
padding: 10px;
background: #e9ecef;
border-radius: 4px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<div class="card">
<h2>AI Text Generator (GPT-2)</h2>
<textarea id="promptInput" placeholder="Enter your prompt here..."></textarea>
<button id="generateBtn" onclick="generateText()">Generate Text</button>
<div id="output">Output will appear here...</div>
</div>
<script type="module">
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2';
// Allow remote model loading
env.allowLocalModels = false;
let generator = null;
window.generateText = async function() {
const prompt = document.getElementById("promptInput").value;
const outputDiv = document.getElementById("output");
const btn = document.getElementById("generateBtn");
if (!prompt) {
alert("Please enter a prompt!");
return;
}
btn.disabled = true;
try {
if (!generator) {
outputDiv.innerText = "Downloading model to browser cache... (this takes a moment on first run)";
generator = await pipeline('text-generation', 'Xenova/gpt2');
}
outputDiv.innerText = "Generating text...";
const result = await generator(prompt, {
max_new_tokens: 50,
temperature: 0.7
});
outputDiv.innerText = result[0].generated_text;
} catch (error) {
outputDiv.innerText = "Error: " + error.message;
console.error(error);
} finally {
btn.disabled = false;
}
};
</script>
</body>
</html>