akhaliq HF Staff commited on
Commit
796efe2
·
verified ·
1 Parent(s): efd2c0c

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +66 -52
index.js CHANGED
@@ -177,68 +177,82 @@ async function processVideo() {
177
 
178
  // Generate output
179
  let generatedText = '';
180
- const outputs = await model.generate({
181
- ...inputs,
182
- max_new_tokens: 256,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  do_sample: false,
184
  streamer: new TextStreamer(processor.tokenizer, {
185
  skip_prompt: true,
186
  skip_special_tokens: false,
187
  callback_function: (text) => {
188
- generatedText += text;
189
- frameCard.querySelector('.frame-description').innerHTML = `<p>${generatedText}</p>`;
 
190
  },
191
  }),
192
  });
193
-
194
- // Decode output
195
- const decoded = processor.batch_decode(
196
- outputs.slice(null, [inputs.input_ids.dims.at(-1), null]),
197
- { skip_special_tokens: true },
198
- );
199
-
200
- frameDescriptions.push({
201
- frame: i + 1,
202
- time: frames[i].time,
203
- description: decoded[0]
204
- });
205
  }
206
 
207
- // Generate overall summary
208
- updateProgress(80, 'Generating video summary...');
209
- const summaryCard = document.getElementById('summaryCard');
210
- const summaryContent = document.getElementById('summaryContent');
211
-
212
- const summaryMessages = [
213
- {
214
- role: "user",
215
- content: `Based on these video frame descriptions: ${frameDescriptions.map(f => f.description).join('. ')}. Provide a comprehensive summary of the entire video content.`,
216
- },
217
- ];
218
- const summaryPrompt = processor.apply_chat_template(summaryMessages, {
219
- add_generation_prompt: true,
220
- });
221
-
222
- const summaryInputs = await processor(frames[0].image, summaryPrompt, {
223
- add_special_tokens: false,
224
- });
225
-
226
- let summaryText = '';
227
- const summaryOutputs = await model.generate({
228
- ...summaryInputs,
229
- max_new_tokens: 512,
230
- do_sample: false,
231
- streamer: new TextStreamer(processor.tokenizer, {
232
- skip_prompt: true,
233
- skip_special_tokens: false,
234
- callback_function: (text) => {
235
- summaryText += text;
236
- summaryContent.innerHTML = `<p>${summaryText}</p>`;
237
- summaryCard.classList.remove('hidden');
238
- },
239
- }),
240
- });
241
-
242
  updateProgress(100, 'Analysis complete!');
243
 
244
  // Show results
 
177
 
178
  // Generate output
179
  let generatedText = '';
180
+ try {
181
+ const outputs = await model.generate({
182
+ ...inputs,
183
+ max_new_tokens: 256,
184
+ do_sample: false,
185
+ streamer: new TextStreamer(processor.tokenizer, {
186
+ skip_prompt: true,
187
+ skip_special_tokens: false,
188
+ callback_function: (text) => {
189
+ generatedText += text;
190
+ frameCard.querySelector('.frame-description').innerHTML = `<p>${generatedText}</p>`;
191
+ },
192
+ }),
193
+ });
194
+
195
+ // Decode output
196
+ const decoded = processor.batch_decode(
197
+ outputs.slice(null, [inputs.input_ids.dims.at(-1), null]),
198
+ { skip_special_tokens: true },
199
+ );
200
+
201
+ frameDescriptions.push({
202
+ frame: i + 1,
203
+ time: frames[i].time,
204
+ description: decoded[0] || generatedText
205
+ });
206
+ } catch (frameError) {
207
+ console.error(`Error processing frame ${i + 1}:`, frameError);
208
+ frameDescriptions.push({
209
+ frame: i + 1,
210
+ time: frames[i].time,
211
+ description: 'Failed to analyze this frame'
212
+ });
213
+ frameCard.querySelector('.frame-description').innerHTML = `<p style="color: #ef4444;">Failed to analyze this frame</p>`;
214
+ }
215
+ }
216
+
217
+ // Generate overall summary
218
+ updateProgress(80, 'Generating video summary...');
219
+ const summaryCard = document.getElementById('summaryCard');
220
+ const summaryContent = document.getElementById('summaryContent');
221
+
222
+ // Create a summary based on the frame descriptions
223
+ if (frameDescriptions.length > 0) {
224
+ const summaryMessages = [
225
+ {
226
+ role: "user",
227
+ content: `<image>Based on what you see in this video frame and knowing that the video contains the following sequence: ${frameDescriptions.map(f => `Frame ${f.frame}: ${f.description}`).join('; ')}. Provide a comprehensive summary of what the entire video is about.`,
228
+ },
229
+ ];
230
+ const summaryPrompt = processor.apply_chat_template(summaryMessages, {
231
+ add_generation_prompt: true,
232
+ });
233
+
234
+ // Use the last frame's image for context
235
+ const summaryInputs = await processor(frames[frames.length - 1].image, summaryPrompt, {
236
+ add_special_tokens: false,
237
+ });
238
+
239
+ let summaryText = '';
240
+ const summaryOutputs = await model.generate({
241
+ ...summaryInputs,
242
+ max_new_tokens: 512,
243
  do_sample: false,
244
  streamer: new TextStreamer(processor.tokenizer, {
245
  skip_prompt: true,
246
  skip_special_tokens: false,
247
  callback_function: (text) => {
248
+ summaryText += text;
249
+ summaryContent.innerHTML = `<p>${summaryText}</p>`;
250
+ summaryCard.classList.remove('hidden');
251
  },
252
  }),
253
  });
 
 
 
 
 
 
 
 
 
 
 
 
254
  }
255
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  updateProgress(100, 'Analysis complete!');
257
 
258
  // Show results