Skip to content

Commit c33d649

Browse files
authored
Merge pull request #31 from ajcwebdev/next
`setup.sh` script and Fastify server integration
2 parents 83a3363 + af7938d commit c33d649

40 files changed

+774
-800
lines changed

README.md

+1-9
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ git clone https://github.com/ggerganov/whisper.cpp.git && \
7272

7373
> Replace `base` with `large-v2` for the largest model, `medium` for a middle sized model, or `tiny` for the smallest model.
7474
75-
### Clone Llama Repo
76-
77-
```bash
78-
git clone https://github.com/ggerganov/llama.cpp && \
79-
make -C llama.cpp && \
80-
cp .github/llama.Dockerfile llama.cpp/Dockerfile
81-
```
82-
8375
## Run Autoshow Node Scripts
8476

8577
Run on a single YouTube video.
@@ -123,7 +115,7 @@ Use 3rd party LLM providers.
123115
```bash
124116
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --chatgpt GPT_4o_MINI
125117
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --claude CLAUDE_3_5_SONNET
126-
npm run as -- --video "https://www.youtube.com/watch?v=h41DF9GUqx4" --gemini GEMINI_1_5_PRO
118+
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --gemini GEMINI_1_5_PRO
127119
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --cohere COMMAND_R_PLUS
128120
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --mistral MISTRAL_LARGE
129121
npm run as -- --video "https://www.youtube.com/watch?v=MORMZXEaONk" --octo LLAMA_3_1_405B

docker-compose.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
- whisper
1414
- ollama
1515
environment:
16-
- OLLAMA_HOST=localhost
16+
- OLLAMA_HOST=ollama
1717
- OLLAMA_PORT=11434
1818
whisper:
1919
build:
@@ -29,5 +29,10 @@ services:
2929
image: ollama/ollama
3030
ports:
3131
- "11434:11434"
32-
volumes:
33-
whisper:
32+
volumes:
33+
- ./ollama:/root/.ollama
34+
healthcheck:
35+
test: ["CMD", "curl", "-f", "http://localhost:11434/healthz"]
36+
interval: 10s
37+
timeout: 5s
38+
retries: 5

docs/examples.md

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ Run on multiple YouTube videos in a playlist.
4848
npm run as -- --playlist "https://www.youtube.com/playlist?list=PLCVnrVv4KhXPz0SoAVu8Rc1emAdGPbSbr"
4949
```
5050

51+
Run on playlist URL and generate JSON info file with markdown metadata of each video in the playlist:
52+
53+
```bash
54+
npm run as -- --playlist "https://www.youtube.com/playlist?list=PLCVnrVv4KhXPz0SoAVu8Rc1emAdGPbSbr" --info
55+
```
56+
5157
### Process Multiple Videos Specified in a URLs File
5258

5359
Run on an arbitrary list of URLs in `example-urls.md`.
@@ -56,6 +62,12 @@ Run on an arbitrary list of URLs in `example-urls.md`.
5662
npm run as -- --urls "content/example-urls.md"
5763
```
5864

65+
Run on URLs file and generate JSON info file with markdown metadata of each video:
66+
67+
```bash
68+
npm run as -- --urls "content/example-urls.md" --info
69+
```
70+
5971
### Process Single Audio or Video File
6072

6173
Run on `audio.mp3` on the `content` directory:

docs/server.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ curl --json '{
3737
}' http://localhost:3000/video
3838
```
3939

40-
41-
4240
Use LLM.
4341

4442
```bash
@@ -54,15 +52,15 @@ curl --json '{
5452
```bash
5553
curl --json '{
5654
"playlistUrl": "https://www.youtube.com/playlist?list=PLCVnrVv4KhXPz0SoAVu8Rc1emAdGPbSbr"
57-
}' http://localhost:3000/playlist
55+
}' http://localhost:3000/playlist
5856
```
5957

6058
```bash
6159
curl --json '{
6260
"playlistUrl": "https://www.youtube.com/playlist?list=PLCVnrVv4KhXPz0SoAVu8Rc1emAdGPbSbr",
6361
"whisperModel": "tiny",
6462
"llm": "llama"
65-
}' http://localhost:3000/playlist
63+
}' http://localhost:3000/playlist
6664
```
6765

6866
### URLs Endpoint
@@ -271,14 +269,14 @@ curl --json '{
271269
```bash
272270
curl --json '{
273271
"youtubeUrl": "https://www.youtube.com/watch?v=MORMZXEaONk",
274-
"transcriptionService": "deepgram"
272+
"transcriptService": "deepgram"
275273
}' http://localhost:3000/video
276274
```
277275

278276
```bash
279277
curl --json '{
280278
"youtubeUrl": "https://www.youtube.com/watch?v=MORMZXEaONk",
281-
"transcriptionService": "deepgram",
279+
"transcriptService": "deepgram",
282280
"llm": "llama"
283281
}' http://localhost:3000/video
284282
```
@@ -288,30 +286,30 @@ curl --json '{
288286
```bash
289287
curl --json '{
290288
"youtubeUrl": "https://www.youtube.com/watch?v=MORMZXEaONk",
291-
"transcriptionService": "assembly"
289+
"transcriptService": "assembly"
292290
}' http://localhost:3000/video
293291
```
294292

295293
```bash
296294
curl --json '{
297295
"youtubeUrl": "https://www.youtube.com/watch?v=MORMZXEaONk",
298-
"transcriptionService": "assembly",
296+
"transcriptService": "assembly",
299297
"llm": "llama"
300298
}' http://localhost:3000/video
301299
```
302300

303301
```bash
304302
curl --json '{
305303
"youtubeUrl": "https://ajc.pics/audio/fsjam-short.mp3",
306-
"transcriptionService": "assembly",
304+
"transcriptService": "assembly",
307305
"speakerLabels": true
308306
}' http://localhost:3000/video
309307
```
310308

311309
```bash
312310
curl --json '{
313311
"youtubeUrl": "https://ajc.pics/audio/fsjam-short.mp3",
314-
"transcriptionService": "assembly",
312+
"transcriptService": "assembly",
315313
"speakerLabels": true,
316314
"llm": "llama"
317315
}' http://localhost:3000/video

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"autoshow": "./autoshow.js"
1919
},
2020
"scripts": {
21+
"setup": "bash ./setup.sh",
2122
"autoshow": "node --env-file=.env --no-warnings src/autoshow.js",
2223
"as": "node --env-file=.env --no-warnings src/autoshow.js",
2324
"bun-as": "bun --env-file=.env --no-warnings src/autoshow.js",
@@ -34,13 +35,15 @@
3435
"dependencies": {
3536
"@anthropic-ai/sdk": "^0.26.0",
3637
"@deepgram/sdk": "^3.5.1",
38+
"@fastify/cors": "^10.0.1",
3739
"@google/generative-ai": "^0.17.1",
3840
"@mistralai/mistralai": "^1.0.2",
3941
"@octoai/sdk": "^1.5.1",
4042
"assemblyai": "^4.6.1",
4143
"cohere-ai": "^7.12.0",
4244
"commander": "^12.1.0",
4345
"fast-xml-parser": "^4.4.1",
46+
"fastify": "^5.0.0",
4447
"ffmpeg-static": "^5.2.0",
4548
"file-type": "^19.4.1",
4649
"inquirer": "^10.2.2",

server/index.js

+34-50
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,49 @@
11
// server/index.js
22

3-
import http from 'node:http'
3+
import Fastify from 'fastify'
4+
import cors from '@fastify/cors'
45
import { handleVideoRequest } from './routes/video.js'
56
import { handlePlaylistRequest } from './routes/playlist.js'
67
import { handleURLsRequest } from './routes/urls.js'
78
import { handleFileRequest } from './routes/file.js'
89
import { handleRSSRequest } from './routes/rss.js'
910
import { env } from 'node:process'
1011

12+
// Set the port from environment variable or default to 3000
1113
const port = env.PORT || 3000
1214

13-
const server = http.createServer(async (req, res) => {
14-
console.log(`[${new Date().toISOString()}] Received ${req.method} request for ${req.url}`)
15-
res.setHeader('Access-Control-Allow-Origin', '*')
16-
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
17-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
18-
console.log('CORS headers set')
15+
async function start() {
16+
// Create a Fastify instance with logging enabled
17+
const fastify = Fastify({ logger: true })
1918

20-
if (req.method === 'OPTIONS') {
21-
console.log('Handling OPTIONS preflight request')
22-
res.writeHead(204)
23-
res.end()
24-
return
25-
}
19+
// Register CORS plugin to handle CORS headers and preflight requests
20+
await fastify.register(cors, {
21+
origin: '*',
22+
methods: ['GET', 'POST', 'OPTIONS'],
23+
allowedHeaders: ['Content-Type'],
24+
})
25+
26+
// Log each incoming request
27+
fastify.addHook('onRequest', async (request, reply) => {
28+
console.log(
29+
`[${new Date().toISOString()}] Received ${request.method} request for ${request.url}`
30+
)
31+
})
32+
33+
// Define route handlers
34+
fastify.post('/video', handleVideoRequest)
35+
fastify.post('/playlist', handlePlaylistRequest)
36+
fastify.post('/urls', handleURLsRequest)
37+
fastify.post('/file', handleFileRequest)
38+
fastify.post('/rss', handleRSSRequest)
2639

27-
if (req.method === 'POST') {
28-
switch (req.url) {
29-
case '/video':
30-
console.log('Routing to handleVideoRequest')
31-
await handleVideoRequest(req, res)
32-
break
33-
case '/playlist':
34-
console.log('Routing to handlePlaylistRequest')
35-
await handlePlaylistRequest(req, res)
36-
break
37-
case '/urls':
38-
console.log('Routing to handleURLsRequest')
39-
await handleURLsRequest(req, res)
40-
break
41-
case '/file':
42-
console.log('Routing to handleFileRequest')
43-
await handleFileRequest(req, res)
44-
break
45-
case '/rss':
46-
console.log('Routing to handleRSSRequest')
47-
await handleRSSRequest(req, res)
48-
break
49-
default:
50-
console.log('Unknown route, sending 404')
51-
res.statusCode = 404
52-
res.setHeader('Content-Type', 'application/json')
53-
res.end(JSON.stringify({ error: 'Not Found' }))
54-
}
55-
} else {
56-
console.log(`Method ${req.method} not allowed, sending 405`)
57-
res.statusCode = 405
58-
res.setHeader('Content-Type', 'application/json')
59-
res.end(JSON.stringify({ error: 'Method Not Allowed' }))
40+
try {
41+
await fastify.listen({ port })
42+
console.log(`Server running at http://localhost:${port}`)
43+
} catch (err) {
44+
fastify.log.error(err)
45+
process.exit(1)
6046
}
61-
})
47+
}
6248

63-
server.listen(port, () => {
64-
console.log(`Server running at http://localhost:${port}`)
65-
})
49+
start()

server/routes/file.js

+29-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
// server/routes/file.js
22

33
import { processFile } from '../../src/commands/processFile.js'
4+
import { reqToOpts } from '../utils/reqToOpts.js'
45

5-
const handleFileRequest = async (req, res) => {
6+
// Handler for /file route
7+
const handleFileRequest = async (request, reply) => {
68
console.log('Entered handleFileRequest')
7-
let body = ''
8-
req.on('data', chunk => {
9-
body += chunk.toString()
10-
console.log('Received chunk:', chunk.toString())
11-
})
12-
req.on('end', async () => {
13-
console.log('Request body complete:', body)
14-
try {
15-
const { filePath, model = 'base', llm, options = {} } = JSON.parse(body)
16-
console.log('Parsed request body:', { filePath, model, llm, options })
17-
if (!filePath) {
18-
console.log('File path not provided, sending 400')
19-
res.statusCode = 400
20-
res.setHeader('Content-Type', 'application/json')
21-
res.end(JSON.stringify({ error: 'File path is required' }))
22-
return
23-
}
24-
const llmOpt = llm || null
25-
await processFile(filePath, llmOpt, model, options)
26-
console.log('processFile completed successfully')
27-
res.statusCode = 200
28-
res.setHeader('Content-Type', 'application/json')
29-
res.end(JSON.stringify({
30-
message: 'File processed successfully.'
31-
}))
32-
} catch (error) {
33-
console.error('Error processing file:', error)
34-
res.statusCode = 500
35-
res.setHeader('Content-Type', 'application/json')
36-
res.end(JSON.stringify({ error: 'An error occurred while processing the file' }))
9+
10+
try {
11+
// Access parsed request body
12+
const requestData = request.body
13+
console.log('Parsed request body:', requestData)
14+
15+
// Extract file path
16+
const { filePath } = requestData
17+
18+
if (!filePath) {
19+
console.log('File path not provided, sending 400')
20+
reply.status(400).send({ error: 'File path is required' })
21+
return
3722
}
38-
})
23+
24+
// Map request data to processing options
25+
const { options, llmOpt, transcriptOpt } = reqToOpts(requestData)
26+
console.log('Calling processFile with params:', { filePath, llmOpt, transcriptOpt, options })
27+
28+
await processFile(filePath, llmOpt, transcriptOpt, options)
29+
30+
console.log('processFile completed successfully')
31+
reply.send({ message: 'File processed successfully.' })
32+
} catch (error) {
33+
console.error('Error processing file:', error)
34+
reply.status(500).send({ error: 'An error occurred while processing the file' })
35+
}
3936
}
4037

4138
export { handleFileRequest }

0 commit comments

Comments
 (0)