File size: 5,549 Bytes
6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 2042ce5 6af3f66 |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
const form = document.getElementById("analyze-form");
const statusBox = document.getElementById("status");
const metaBox = document.getElementById("meta");
const mHashtag = document.getElementById("m-hashtag");
const mGemini = document.getElementById("m-gemini");
const mFallback = document.getElementById("m-fallback");
const mModels = document.getElementById("m-models");
const pieDiv = document.getElementById("pie");
const lineDiv = document.getElementById("line");
const tableDiv = document.getElementById("table");
// Parallax cursor
const cursor = document.getElementById("parallax-cursor");
window.addEventListener("mousemove", (e) => {
const x = e.clientX, y = e.clientY;
cursor.style.opacity = ".9";
cursor.style.left = x + "px";
cursor.style.top = y + "px";
});
// Helpers
function fmtPct(n){ return (Math.round(n * 100) / 100).toFixed(2); }
function renderMeta(meta) {
mHashtag.textContent = meta.hashtag;
mGemini.textContent = `Gemini: ${meta.generated_by.gemini}`;
mFallback.textContent = `Fallback: ${meta.generated_by.fallback}`;
mModels.textContent = `Gen: ${meta.model.generation} • Sentiment: ${meta.model.sentiment}`;
// meta fade-in animation
metaBox.style.opacity = "0";
metaBox.style.transform = "translateY(10px)";
requestAnimationFrame(() => {
metaBox.style.transition = "all .6s ease";
metaBox.style.opacity = "1";
metaBox.style.transform = "translateY(0)";
});
}
function renderPie(percent) {
const data = [{
values: [percent.positive, percent.neutral, percent.negative],
labels: ['Positive', 'Neutral', 'Negative'],
type: 'pie',
textinfo: 'label+percent',
hoverinfo: 'label+percent',
hole: .35
}];
const layout = {
paper_bgcolor: 'rgba(0,0,0,0)',
plot_bgcolor: 'rgba(0,0,0,0)',
font: {color: '#eaf2ff'},
margin: {l: 4, r: 4, t: 0, b: 0},
showlegend: false,
transition: {duration: 500, easing: "cubic-in-out"}
};
Plotly.newPlot(pieDiv, data, layout, {displayModeBar:false, responsive:true}).then(() => {
pieDiv.style.opacity = "0";
pieDiv.style.transform = "scale(0.9)";
requestAnimationFrame(() => {
pieDiv.style.transition = "all .6s ease";
pieDiv.style.opacity = "1";
pieDiv.style.transform = "scale(1)";
});
});
}
function renderLine(rolling) {
const data = [{
x: [...Array(rolling.length).keys()].map(i => i+1),
y: rolling,
type: 'scatter',
mode: 'lines+markers',
line: {shape: 'spline', smoothing: 1.3}
}];
const layout = {
paper_bgcolor: 'rgba(0,0,0,0)',
plot_bgcolor: 'rgba(0,0,0,0)',
font: {color: '#eaf2ff'},
margin: {l: 30, r: 10, t: 0, b: 24},
yaxis: {range:[0,1], tickformat: '.0%'},
transition: {duration: 600, easing: "cubic-in-out"}
};
Plotly.newPlot(lineDiv, data, layout, {displayModeBar:false, responsive:true}).then(() => {
lineDiv.style.opacity = "0";
lineDiv.style.transform = "translateY(20px)";
requestAnimationFrame(() => {
lineDiv.style.transition = "all .7s ease";
lineDiv.style.opacity = "1";
lineDiv.style.transform = "translateY(0)";
});
});
}
function renderTable(rows) {
tableDiv.innerHTML = "";
rows.forEach((r, i) => {
const row = document.createElement("div");
row.className = "row";
// text
const c1 = document.createElement("div");
c1.className = "cell";
c1.textContent = r.text;
// source chip
const c2 = document.createElement("div");
c2.className = "cell";
const chip = document.createElement("span");
chip.className = "chip " + (r.source === "gemini" ? "chip-gemini" : "chip-fallback");
chip.textContent = r.source === "gemini" ? "Gemini" : "Fallback";
c2.appendChild(chip);
// sentiment badge
const c3 = document.createElement("div");
c3.className = "cell";
const badge = document.createElement("span");
const s = r.sentiment;
badge.className = "badge " + (s === "POSITIVE" ? "pos" : s === "NEGATIVE" ? "neg" : "neu");
badge.textContent = s + " " + (r.score.toFixed(2));
c3.appendChild(badge);
row.appendChild(c1);
row.appendChild(c2);
row.appendChild(c3);
// add animation
row.style.opacity = "0";
row.style.transform = "translateX(-15px)";
setTimeout(() => {
row.style.transition = "all .4s ease";
row.style.opacity = "1";
row.style.transform = "translateX(0)";
}, 100 * i);
tableDiv.appendChild(row);
});
}
form.addEventListener("submit", async (e) => {
e.preventDefault();
const hashtag = document.getElementById("hashtag").value.trim();
const count = parseInt(document.getElementById("count").value || "20", 10);
if(!hashtag){
alert("Please enter a hashtag (e.g., #gla)");
return;
}
statusBox.classList.remove("hidden");
metaBox.classList.add("hidden");
try {
const resp = await fetch("/api/analyze", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({hashtag, count})
});
if(!resp.ok){
const err = await resp.json().catch(()=>({}));
throw new Error(err.error || `HTTP ${resp.status}`);
}
const data = await resp.json();
// META
renderMeta(data.meta);
metaBox.classList.remove("hidden");
// CHARTS
renderPie(data.aggregate.percent);
renderLine(data.aggregate.rolling);
// TABLE
renderTable(data.rows);
} catch (err) {
console.error(err);
alert("Failed: " + err.message);
} finally {
statusBox.classList.add("hidden");
}
});
|