-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update package versions and add support for YouTube transcripts
- Loading branch information
Showing
11 changed files
with
315 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FastifyRequest, FastifyReply } from "fastify"; | ||
import { GetYoutubeTranscript } from "./types"; | ||
import { YtTranscript } from "yt-transcript" | ||
|
||
const getYoutubeTranscript = async (url: string) => { | ||
const transcript = new YtTranscript({ url }); | ||
const data = await transcript.listAllTranscripts(); | ||
return data; | ||
} | ||
|
||
export const getYoutubeTranscriptHandler = async ( | ||
request: FastifyRequest<GetYoutubeTranscript>, | ||
reply: FastifyReply | ||
) => { | ||
const { url } = request.query; | ||
|
||
const available = await getYoutubeTranscript(url); | ||
|
||
return { | ||
data: available as any | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./get.handler" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type GetYoutubeTranscript = { | ||
Querystring: { | ||
url: string; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { BaseDocumentLoader } from "langchain/document_loaders/base"; | ||
import { Document } from "langchain/document"; | ||
import { YtTranscript } from "yt-transcript"; | ||
|
||
export interface YoutubeTranscriptParams { | ||
url: string; | ||
language_code: string; | ||
} | ||
|
||
export class DialoqbaseYoutubeTranscript | ||
extends BaseDocumentLoader | ||
implements YoutubeTranscriptParams { | ||
language_code: string; | ||
url: string; | ||
|
||
constructor({ language_code, url }: YoutubeTranscriptParams) { | ||
super(); | ||
this.language_code = language_code; | ||
this.url = url | ||
} | ||
|
||
|
||
|
||
async load(): Promise<Document<Record<string, any>>[]> { | ||
const ytTranscript = new YtTranscript({ | ||
url: this.url, | ||
}) | ||
|
||
const script = await ytTranscript.getTranscript(this.language_code) | ||
|
||
if (!script) throw new Error("No script found") | ||
|
||
|
||
let text = "" | ||
|
||
script.forEach((item) => { | ||
text += item.text + " " | ||
}) | ||
|
||
|
||
return [ | ||
{ | ||
metadata: { | ||
source: this.url, | ||
audio: { chunks: script } | ||
}, | ||
pageContent: text | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.