Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Simple Text Generation</title> | |
| <script type="module"> | |
| import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]'; | |
| let generator; | |
| // Load the model once | |
| async function loadModel() { | |
| document.getElementById("status").innerText = "β³ Loading model..."; | |
| try { | |
| generator = await pipeline("text-generation", "Xenova/distilgpt2"); | |
| document.getElementById("status").innerText = "β Model loaded!"; | |
| document.getElementById("generateBtn").disabled = false; | |
| } catch (error) { | |
| document.getElementById("status").innerText = "β Error loading model."; | |
| console.error("Model load error:", error); | |
| } | |
| } | |
| async function generateText() { | |
| const inputText = document.getElementById("inputText").value.trim(); | |
| if (!inputText) { | |
| alert("Please enter some text!"); | |
| return; | |
| } | |
| document.getElementById("output").innerText = "β³ Generating..."; | |
| try { | |
| const output = await generator(inputText, { max_new_tokens: 50, do_sample: true }); | |
| document.getElementById("output").innerText = output[0].generated_text; | |
| } catch (error) { | |
| document.getElementById("output").innerText = "β Error generating text."; | |
| console.error("Generation error:", error); | |
| } | |
| } | |
| window.addEventListener("DOMContentLoaded", loadModel); | |
| window.generateText = generateText; | |
| </script> | |
| </head> | |
| <body> | |
| <h2>Simple Text Generation</h2> | |
| <p id="status">β³ Loading model...</p> | |
| <textarea id="inputText" rows="3" cols="50" placeholder="Enter text..."></textarea> | |
| <br><br> | |
| <button id="generateBtn" onclick="generateText()" disabled>Generate</button> | |
| <h3>Output:</h3> | |
| <p id="output"></p> | |
| </body> | |
| </html> | |