-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
137 lines (104 loc) · 3.62 KB
/
server.mjs
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
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { YoutubeTranscript } from 'youtube-transcript';
const app = express();
const port = 3000;
import OpenAI from 'openai';
app.use(bodyParser.json());
app.use(cors());
app.post('/generate-summary', async (req, res) => {
try {
console.log('Request received');
const videoUrl = req.body.videoUrl;
console.log('Video URL:', videoUrl);
if (!videoUrl) {
return res.status(400).json({ error: 'Missing videoUrl in the request body' });
}
const transcript = await getVideoTranscript(videoUrl);
console.log("transcript complete");
const summary = await generateSummary('summarize the following in english ' + transcript);
console.log('Sending response',summary);
res.json({ summary });
} catch (error) {
console.error('Unexpected error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.post('/generate-notes', async (req, res) => {
try {
console.log('Request received');
const videoUrl = req.body.videoUrl;
console.log('Video URL:', videoUrl);
if (!videoUrl) {
return res.status(400).json({ error: 'Missing videoUrl in the request body' });
}
const transcript = await getVideoTranscript(videoUrl);
console.log("transcript complete");
const summary = await generateNotes(' make notes from following in english' + transcript);
console.log('Sending response',summary);
res.json({ summary });
} catch (error) {
console.error('Unexpected error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
export async function getVideoTranscript(videoUrl) {
try {
const transcript = await YoutubeTranscript.fetchTranscript(videoUrl);
if (transcript) {
const transcriptText = transcript.map((item) => item.text).join(' ');
return transcriptText;
} else {
return null;
}
} catch (error) {
console.error('Error fetching video transcript:', error.message);
return null;
}
}
async function generateSummary(text) {
try {
console.log("in function");
const chatCompletion = await openai.chat.completions.create({
messages: [
{"role": "user", "content": text},
],
model: "gpt-3.5-turbo",
});
if (chatCompletion.choices && chatCompletion.choices.length > 0) {
const generatedText = chatCompletion.choices[0].message.content;
return generatedText
} else {
console.error('No choices found in the response.');
}
return "unable to do";
} catch (error) {
console.error('Error generating summary: \n', error);
throw new Error('Error generating summary');
}
}
async function generateNotes(text) {
try {
console.log("in function");
const chatCompletion = await openai.chat.completions.create({
messages: [
{"role": "user", "content": text},
],
model: "gpt-3.5-turbo",
});
if (chatCompletion.choices && chatCompletion.choices.length > 0) {
const generatedText = chatCompletion.choices[0].message.content;
return generatedText
} else {
console.error('No choices found in the response.');
}
return "unable to do";
} catch (error) {
console.error('Error generating notes: \n', error);
throw new Error('Error generating notes');
}
}
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});