Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

use api chat/completions for summary

Christian Fei04578d76e6d1
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index 47cf40d..52fa49d 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -7,9 +7,14 @@ export async function summarizeVideo(id, repo, callback = () => {}) {
let cleanedTranscript = cleanTranscript(transcript)
console.log('cleanedTranscript.length', cleanedTranscript.length)
repo.setVideoTranscript(id, cleanedTranscript)
+ cleanedTranscript = cleanedTranscript.replace('\n', ' ')
+ // if (process.env.KEEP_SUBTITLES === 'true')
fs.writeFileSync(`./data/videos/${id}-sub.en.txt`, cleanedTranscript)
- const prompt = `Summarize the following video transcript, in max 10 sentences, keep it short, don't repeat yourself:\n${cleanedTranscript}`
+ const prompt = `
+Extract the key points from the following video transcript:
+
+${cleanedTranscript.replace('\n', ' ')}`
console.log('prompt', prompt)
const summary = await fetchAIResponseLMSTUDIO(prompt)
console.log('summary', summary)
@@ -32,18 +37,21 @@ export function cleanTranscript (transcript) {
async function fetchAIResponseLMSTUDIO (prompt) {
- if (prompt.length > 5000) prompt = prompt.substring(0, 5000)
+ if (prompt.length > 10000) prompt = prompt.substring(0, 10000)
try {
- const response = await fetch('http://localhost:1234/v1/completions', {
+ const response = await fetch('http://0.0.0.0:1234/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
- "model": "llama-3.2-3b-instruct",
- "prompt": prompt,
+ "model": "meta-llama-3-8b-instruct",
+ "messages": [
+ {"role": "system", "content": "You're a helpful assistant that sumarizes a video transcript in max 10 sentences, extracting the key points"},
+ {"role": "user", "content": prompt}
+ ],
"temperature": 0.9,
- "max_tokens": 1000,
+ // "max_tokens": 2000,
"stream": false,
// "stop": "\n"
})
@@ -52,35 +60,9 @@ async function fetchAIResponseLMSTUDIO (prompt) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
const data = await response.json()
- console.log(data)
- return data.choices[data.choices.length - 1]?.text
+ console.log(JSON.stringify(data, null, 2))
+ return data.choices[data.choices.length - 1]?.message.content
} catch (error) {
console.error('Error summarizing text:', error)
}
-}
-
-/*
-async function fetchAIResponseOLLAMA (prompt) {
- try {
- const response = await fetch('http://localhost:11434/api/generate', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- "model": "llama3.2",
- "prompt": prompt,
- "stream": false
- })
- })
- if (!response.ok) {
- throw new Error(`HTTP error! Status: ${response.status}`)
- }
- const data = await response.json()
- // console.log(data)
- return data
- } catch (error) {
- console.error('Error summarizing text:', error)
- }
-}
-*/
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/lib/youtube.js b/lib/youtube.js
index 9f51e60..869f540 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -22,6 +22,7 @@ export async function getVideoSubtitles (id, callback = () => {}) {
}
const transcriptPath = `./data/videos/${id}-sub.en.srt`
const transcript = fs.readFileSync(transcriptPath, 'utf-8')
+ // if (process.env.KEEP_SUBTITLES !== 'true') fs.rmSync(transcriptPath)
return transcript
}
diff --git a/test/youtube.test.js b/test/youtube.test.js
new file mode 100644
index 0000000..eb9e254
--- /dev/null
+++ b/test/youtube.test.js
@@ -0,0 +1,18 @@
+import { test } from 'node:test'
+import assert from 'assert';
+import { getVideosFor } from '../lib/youtube.js'
+
+test('gets videos for channel', async () => {
+ const videos = await getVideosFor('veritasium')
+ const video = videos[0]
+ assert.ok(video.channelName)
+ assert.ok(video.title)
+ assert.ok(video.url)
+ assert.ok(video.thumbnail)
+ assert.ok(video.description)
+ assert.ok(video.id)
+ assert.ok(video.publishedTime)
+ assert.ok(video.viewCount)
+ assert.ok(video.duration)
+ assert.strictEqual(videos.length, 30)
+})
\ No newline at end of file
diff --git a/test/yt.test.js b/test/yt.test.js
deleted file mode 100644
index dab336c..0000000
--- a/test/yt.test.js
+++ /dev/null
@@ -1,31 +0,0 @@
-import {test} from 'node:test'
-import assert from 'assert';
-// read json file data/mr_rip.json
-// import fs module to read the file
-import fs, { chownSync } from 'fs';
-import path from 'path';
-
-const __dirname = new URL('.', import.meta.url).pathname;
-
-const filePath = path.join(__dirname, 'data', 'veritasium.json');
-const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
-
-test('extracts videos', () => {
- const videoTab = jsonData.contents.twoColumnBrowseResultsRenderer.tabs.find(t => t.tabRenderer.title === 'Video')
- assert.ok(videoTab);
- const videoContents = videoTab.tabRenderer.content.richGridRenderer.contents
- assert.ok(videoContents)
- const v = videoContents.filter(v => {
- return v.richItemRenderer?.content?.videoRenderer?.title?.runs[0]?.text
- }).map(v => ({
- title: v.richItemRenderer.content.videoRenderer.title.runs[0].text,
- url: `https://www.youtube.com/watch?v=${v.richItemRenderer.content.videoRenderer.videoId}`,
- thumbnail: v.richItemRenderer.content.videoRenderer.thumbnail.thumbnails[0].url,
- description: v.richItemRenderer.content.videoRenderer.descriptionSnippet.runs[0].text,
- videoId: v.richItemRenderer.content.videoRenderer.videoId,
- publishedTime: v.richItemRenderer.content.videoRenderer.publishedTimeText.simpleText,
- viewCount: v.richItemRenderer.content.videoRenderer.viewCountText.simpleText,
- duration: v.richItemRenderer.content.videoRenderer.lengthText ? v.richItemRenderer.content.videoRenderer.lengthText.simpleText : null,
- }))
- console.log(v)
-});
\ No newline at end of file