File size: 2,813 Bytes
e93ea23
 
 
 
 
 
 
129e2a9
e93ea23
 
 
 
 
 
 
129e2a9
e93ea23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129e2a9
e93ea23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129e2a9
 
e93ea23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129e2a9
e93ea23
 
 
 
 
 
 
 
 
 
 
 
 
 
129e2a9
 
e93ea23
129e2a9
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const generateBtn = document.getElementById("generateBtn");
const topicInput = document.getElementById("topicInput");
const resultsSection = document.getElementById("resultsSection");
const generatedTitle = document.getElementById("generatedTitle");
const generatedTags = document.getElementById("generatedTags");
const copyBtn = document.getElementById("copyBtn");


const API_BASE = "http://127.0.0.1:5000";

const ENDPOINTS = {
  GENERATE: `${API_BASE}/generate`,
  HEALTH: `${API_BASE}/health`,
};


async function checkHealth() {
  try {
    const res = await fetch(ENDPOINTS.HEALTH);
    if (!res.ok) return false;
    const data = await res.json();
    return data.status === "ok";
  } catch (err) {
    console.error("Health check failed:", err);
    return false;
  }
}

async function generateTitleAndTags(description) {
  try {
    const healthy = await checkHealth();
    if (!healthy) throw new Error("Backend not available");

    const res = await fetch(ENDPOINTS.GENERATE, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ description }), // ✅ matches Flask app.py
    });

    if (!res.ok) throw new Error("Failed to fetch results");

    return await res.json();
  } catch (err) {
    console.error("Error generating:", err);
    throw err;
  }
}


generateBtn.addEventListener("click", async () => {
  const userPrompt = topicInput.value.trim();
  if (!userPrompt) {
    alert("⚠️ Please enter a video description.");
    return;
  }

  generatedTitle.textContent = "⏳ Generating...";
  generatedTags.innerHTML = "";
  resultsSection.style.display = "block";

  try {
    const { title, tags } = await generateTitleAndTags(userPrompt);
    displayResults(title, tags);
  } catch (err) {
    generatedTitle.textContent = "❌ Error generating content.";
    generatedTags.textContent = "";
  }
});



function displayResults(title, tags) {
  generatedTitle.textContent = title || "No title generated.";
  generatedTags.innerHTML = "";

  if (Array.isArray(tags) && tags.length > 0) {
    tags.forEach((tag) => {
      const tagEl = document.createElement("span");
      tagEl.className = "tag";
      tagEl.textContent = "#" + tag;
      generatedTags.appendChild(tagEl);
    });
  } else {
    generatedTags.textContent = "No tags generated.";
  }

  resultsSection.style.display = "block";
}


if (copyBtn) {
  copyBtn.addEventListener("click", () => {
    const title = generatedTitle.textContent;
    const tags = Array.from(generatedTags.children)
      .map((tag) => tag.textContent)
      .join(" ");
    const text = `Title: ${title}\nTags: ${tags}`;

    navigator.clipboard.writeText(text).then(() => {
      showNotification("📋 Copied!");
    });
  });
}



function showNotification(message) {
  alert(message); 
}