-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (64 loc) · 3.02 KB
/
Copy pathserver.js
File metadata and controls
80 lines (64 loc) · 3.02 KB
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
const readline = require('readline');
const YouTubeService = require('./youtube.service.js');
const OpenAiApi = require('./openAiApi'); // Import OpenAiApi class
const BartApi = require('./bartApi'); // Import BartApi class
const PegasusApi = require('./pegasusApi'); // Import PegasusApi class
const TextSummarizationApi = require('./textSummarizationApi');
const LedLargeBookSummaryApi = require('./ledLargeBookSummaryApi');
require('dotenv').config();
const { processText, truncateContent } = require('./textpreprocessing.service');
const openaiApiKey = process.env.OPENAI_API_KEY;
const huggingApiKey = process.env.HUGGINGFACE_API_TOKEN
import('node-fetch').then(module => {
const fetch = module.default;
const youtubeService = new YouTubeService(fetch);
// Factory for API endpoints
function getApiEndpoint(apiName) {
switch (apiName) {
case 'openai':
return new OpenAiApi(openaiApiKey);
case 'huggingface-bart':
return new BartApi(huggingApiKey);
case 'huggingface-pegasus':
return new PegasusApi(huggingApiKey);
case 'huggingface-textsummarization':
return new TextSummarizationApi(huggingApiKey);
case 'huggingface-ledlargebooksummarization':
return new LedLargeBookSummaryApi(huggingApiKey);
// Add other cases for additional APIs
default:
throw new Error('Unknown API endpoint');
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function handleUserInput() {
try {
const videoId = await askQuestion('Enter YouTube video ID: ');
const captionType = await askQuestion('Enter caption type (SRT/TXT): ');
console.log('Available models: openai, huggingface-bart, huggingface-pegasus, huggingface-textsummarization');
const apiName = await askQuestion('Enter API endpoint name: ');
const prompt = await askQuestion('Enter prompt for the summary: ');
const captions = await youtubeService.fetchCaptions(videoId, captionType);
const processedCaptions = processText(captions);
const truncatedCaptions = truncateContent(processedCaptions, 1000); // Adjust the maxSize as needed (bart breaks with large inputs)
console.log(truncatedCaptions);
const apiEndpoint = getApiEndpoint(apiName);
// Process and send the transcript to the API
const summary = await apiEndpoint.generateSummary(prompt, truncatedCaptions);
console.log("Summary: "+ summary);
} catch (error) {
console.error(error.message);
} finally {
rl.close();
}
}
function askQuestion(query) {
return new Promise(resolve => rl.question(query, resolve));
}
handleUserInput();
}).catch(err => {
console.error("Failed to load 'node-fetch' module", err);
});