forked from oscardcamargo/VideoTextReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
langchain.js
144 lines (119 loc) · 4.84 KB
/
langchain.js
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
import { Buffer } from 'buffer';
import { DetectDocumentTextCommand, TextractClient } from "@aws-sdk/client-textract";
import { PromptTemplate } from "langchain/prompts";
import { ChatOpenAI } from "langchain/chat_models/openai";
// import fs from "fs";
// import AWS from "aws-sdk";
// @ts-ignore
window.Buffer = Buffer;
// import { YoutubeTranscript } from 'youtube-transcript';
const res = await chrome.storage.local.get(["OpenAIAPIKey", "AWSAPIKey", "AWSAPISecret"]);
let OpenAIAPIKey = res.OpenAIAPIKey;
let AWSAPIKey = res.AWSAPIKey;
let AWSAPISecret = res.AWSAPISecret;
// let YouTubeAPIKey = "AIzaSyBS0Dzli0ZvKpqRlU5RbEjzE_40zHQ01Rg";
const model = new ChatOpenAI({
modelName: 'gpt-3.5-turbo',
openAIApiKey: OpenAIAPIKey
});
const client = new TextractClient({
region: "us-east-1",
credentials: {
secretAccessKey: AWSAPISecret,
accessKeyId: AWSAPIKey,
}
});
// Initialize Textract client
async function run(base64Data) {
// Remove the base64 heading if present
base64Data = base64Data.split(',')[1] || base64Data;
// Convert base64 to Buffer
const fileBuffer = Buffer.from(base64Data, 'base64');
// Prepare command
const command = new DetectDocumentTextCommand({
Document: {
Bytes: fileBuffer,
},
});
try {
// Run Textract
const result = await client.send(command);
// Parse result
const rawText = result.Blocks.map((block) => block.Text).join(" ");
console.log("Extracted Text:", rawText);
return rawText;
} catch (err) {
console.error("An error occurred:", err);
return "error";
}
}
async function genExplanation(imageData) {
console.log("genExplanation");
const prompt = PromptTemplate.fromTemplate(
'Explain in one short paragraph {extractedText}. Describe key insights. Write a practice questions with an answer. Output only what I ask and nothing else'
);
const chain = prompt.pipe(model);
const extractedText = await run(imageData);
const result = await chain.invoke({
extractedText: extractedText,
});
return result.content
}
// function extractVideoId(url) {
// // Regular expression to extract the video_id from the YouTube URL
// const regex = /[?&]v=([^&]+)/;
// const match = url.match(regex);
// if (match) {
// return match[1];
// } else {
// return null; // Video ID not found in the URL
// }
// }
async function genStudyGuide() {
console.log("genStudyGuide");
// Video ID of the YouTube video you want to get the transcript for
// const videoId = extractVideoId(location.href);
// if (videoId) {
// // Now you have the video_id.
// console.log(videoId);
// } else {
// console.log("Video ID not found in the URL.");
// }
// URL to fetch captions for a video
// const captionsUrl = `https://www.googleapis.com/youtube/v3/captions?part=snippet&videoId=${videoId}&key=${YouTubeAPIKey}`;
// const captionsUrl = await fetch(`https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.captions.list?part=snippet&videoId=${videoId}`);
// const captionsUrl = await fetch(`https://youtube.googleapis.com/youtube/v3/captions?part=snippet&videoId=${videoId}&key=${YouTubeAPIKey}`);
// const captionsUrl = await fetch(`https://googleapis.com/youtube/v3/captions?part=snippet&videoId=${videoId}&key=${YouTubeAPIKey}`);
// console.log(captionsUrl);
// Make a GET request to the captions URL
// fetch(captionsUrl)
// .then(response => response.json())
// .then(data => {
// console.log(data);
// // Data contains information about the video's captions, including transcript URL.
// const transcriptUrl = data.items[0].snippet.url;
// // Fetch the transcript content
// fetch(transcriptUrl)
// .then(response => response.text())
// .then(transcript => {
// // Here, you can work with the transcript data as needed.
// console.log(transcript);
// })
// .catch(error => {
// console.error('Error fetching transcript:', error);
// });
// })
// .catch(error => {
// console.error('Error fetching captions:', error);
// });
// const prompt = PromptTemplate.fromTemplate(
// 'Create a summary of {extractedText} and write three short questions based on the text. Output only what I ask and nothing else. Can make assumptions if you do not understand something'
// );
// const chain = prompt.pipe(model);
// const extractedText = await run();
// const result = await chain.invoke({
// extractedText: extractedText
// });
return "result.content"
}
export { genExplanation, genStudyGuide };