File size: 3,986 Bytes
2b022c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fileInput = document.getElementById('fileUpload');
const urlInput  = document.getElementById('urlInput');
const analyzeBtn = document.getElementById('analyzeBtn');
const resultBox  = document.getElementById('result');
const resultText = document.getElementById('resultText');
const previewBox = document.getElementById('preview');
const logList    = document.getElementById('logList');

analyzeBtn.addEventListener('click', () => {
  resultBox.classList.add('hidden');
  resultText.textContent = '';
  previewBox.innerHTML = '';

  const urlVal = urlInput.value.trim();

  if (fileInput.files.length > 0) {
    const file = fileInput.files[0];
    renderPreview(file);
    analyzeBackend({ file });
  } else if (urlVal) {
    renderURLPreview(urlVal);
    analyzeBackend({ url: urlVal });
  } else {
    alert('Please upload a file or enter a URL.');
  }
});

function renderPreview(file) {
  const fileURL = URL.createObjectURL(file);

  if (file.type.startsWith('video')) {
    const video = document.createElement('video');
    video.src = fileURL;
    video.controls = true;
    video.width = 400;
    previewBox.appendChild(video);
  } else if (file.type.startsWith('audio')) {
    const audio = document.createElement('audio');
    audio.src = fileURL;
    audio.controls = true;
    previewBox.appendChild(audio);

    const canvas = document.createElement('canvas');
    canvas.id = 'waveform';
    canvas.width = 400;
    canvas.height = 100;
    canvas.style.marginTop = '10px';
    previewBox.appendChild(canvas);

    visualizeAudio(audio, canvas);
  }
}

function renderURLPreview(url) {
  const link = document.createElement('a');
  link.href = url;
  link.textContent = url;
  link.target = '_blank';
  previewBox.appendChild(link);
}

async function analyzeBackend({ file = null, url = '' }) {
  const fd = new FormData();
  if (file) fd.append('file', file);
  if (url) fd.append('url', url);

  resultText.textContent = 'Analyzing...\\n';
  resultBox.classList.remove('hidden');

  try {
    const res = await fetch("/api/analyze", {
      method: 'POST',
      body: fd
    });

    if (!res.ok) throw new Error('Server error');

    const data = await res.json();
    displayResults(data);
  } catch (err) {
    resultText.textContent += `Error: ${err.message}`;
  }
}

function displayResults(data) {
  const { summary, transcript = [], subliminal_flags = [] } = data;
  resultText.textContent = summary + '\\n\\n';
  if (subliminal_flags.length) {
    subliminal_flags.forEach(flag => {
      resultText.textContent += `⚠ ${flag.type} at ${flag.timestamp}${flag.content}\\n`;
    });
    resultText.textContent += '\\n';
  }
  resultText.textContent += '--- Transcript ---\\n' + transcript.join('\\n');

  const li = document.createElement('li');
  li.textContent = summary;
  logList.appendChild(li);
}

function visualizeAudio(audioEl, canvasEl) {
  const ctx = canvasEl.getContext('2d');
  const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  const analyser = audioCtx.createAnalyser();
  analyser.fftSize = 2048;

  const source = audioCtx.createMediaElementSource(audioEl);
  source.connect(analyser);
  analyser.connect(audioCtx.destination);

  const bufferLength = analyser.fftSize;
  const dataArray = new Uint8Array(bufferLength);

  function draw() {
    requestAnimationFrame(draw);

    analyser.getByteTimeDomainData(dataArray);

    ctx.fillStyle = '#ffffff';
    ctx.fillRect(0, 0, canvasEl.width, canvasEl.height);

    ctx.lineWidth = 2;
    ctx.strokeStyle = '#3498db';
    ctx.beginPath();

    const sliceWidth = canvasEl.width * 1.0 / bufferLength;
    let x = 0;

    for (let i = 0; i < bufferLength; i++) {
      const v = dataArray[i] / 128.0;
      const y = v * canvasEl.height / 2;

      if (i === 0) {
        ctx.moveTo(x, y);
      } else {
        ctx.lineTo(x, y);
      }

      x += sliceWidth;
    }

    ctx.lineTo(canvasEl.width, canvasEl.height / 2);
    ctx.stroke();
  }

  draw();
}