File size: 1,940 Bytes
9f48b74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>AI Quote Generator (Offline)</title>
  <script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0"></script>
  <style>
    body { font-family: Arial, sans-serif; text-align: center; padding: 30px; background: #f4f4f4; }
    input, button { padding: 10px; font-size: 16px; }
    #output { margin-top: 20px; padding: 15px; background: white; border-radius: 10px; }
  </style>
</head>
<body>
  <h1>🧠 AI Quote Generator</h1>
  <p>Runs 100% in your browser using Transformers.js</p>

  <input type="text" id="topic" placeholder="Enter a topic (love, success...)" />
  <button onclick="generate()">Generate Quote</button>

  <div id="loading" style="display:none; color:green;">Loading model, please wait...</div>
  <div id="output"></div>

  <script>
    let pipeline, loaded = false;

    async function loadModel() {
      document.getElementById("loading").style.display = "block";
      pipeline = await window.transformers.pipeline("text-generation", "Xenova/distilgpt2");
      loaded = true;
      document.getElementById("loading").style.display = "none";
    }

    async function generate() {
      if (!loaded) {
        alert("Please wait, model is still loading...");
        return;
      }

      const topic = document.getElementById("topic").value.trim();
      if (!topic) {
        alert("Please enter a topic.");
        return;
      }

      document.getElementById("output").innerText = "Generating...";
      const result = await pipeline(`Quote about ${topic}:`, {
        max_new_tokens: 50,
        temperature: 0.9,
        top_k: 50
      });
      const text = result[0].generated_text.replace(/Quote about .*?:/, "").trim();
      document.getElementById("output").innerText = "“" + text + "”";
    }

    loadModel();
  </script>
</body>
</html>