From 96f1c69fd90a2ed539c9528f4d875e97dcdfb2d0 Mon Sep 17 00:00:00 2001 From: Paccoco <99041736+Paccoco@users.noreply.github.com> Date: Sat, 9 May 2026 21:10:23 -0500 Subject: [PATCH] fix: chunk size limit + keep MP3s + Respond Accepted fix; add rocinante stack - Chunk Text: word-level fallback for sentences > 600 chars - watch.py: keep MP3 after transcription - Respond Accepted: remove cross-node reference - rocinante/: Whisper CUDA + watcher stack for RTX 4090 transcription --- .../08-class-recording-rag-v1.2.json | 538 ++++++++++++++++++ .../09-class-transcript-ingest-v1.1.json | 423 ++++++++++++++ rocinante/docker-compose.yaml | 40 ++ rocinante/watcher/Dockerfile | 12 + rocinante/watcher/watch.py | 213 +++++++ 5 files changed, 1226 insertions(+) create mode 100644 n8n-workflows/08-class-recording-rag-v1.2.json create mode 100644 n8n-workflows/09-class-transcript-ingest-v1.1.json create mode 100644 rocinante/docker-compose.yaml create mode 100644 rocinante/watcher/Dockerfile create mode 100644 rocinante/watcher/watch.py diff --git a/n8n-workflows/08-class-recording-rag-v1.2.json b/n8n-workflows/08-class-recording-rag-v1.2.json new file mode 100644 index 0000000..ac40158 --- /dev/null +++ b/n8n-workflows/08-class-recording-rag-v1.2.json @@ -0,0 +1,538 @@ +{ + "updatedAt": "2026-05-07T01:16:50.340Z", + "createdAt": "2026-05-07T01:16:50.340Z", + "id": "91I278E72FAaQenD", + "name": "Class Recording \u2192 RAG v1.2", + "description": null, + "active": true, + "isArchived": false, + "nodes": [ + { + "parameters": { + "httpMethod": "POST", + "path": "class/upload", + "responseMode": "lastNode", + "responseData": "allEntries", + "options": { + "rawBody": true + } + }, + "id": "e475f17b-8560-4376-852e-a613ba6b300e", + "name": "Class Upload Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 2, + "position": [ + 0, + 0 + ], + "webhookId": "d0ee8226-db56-477e-8a3f-572a9129aea9" + }, + { + "parameters": { + "jsCode": "const data = $input.first().json;\nconst body = data.body || data;\n\n// Class name comes from:\n// 1. Form field 'class_name' in multipart upload\n// 2. Query parameter ?class=CS101\n// 3. Header X-Class-Name\nconst className = body.class_name || data.headers?.['x-class-name'] || data.query?.class || 'unclassified';\n\n// Lecture title from:\n// 1. Form field 'lecture_title'\n// 2. Query param ?title=...\n// 3. Header X-Lecture-Title\n// 4. Form field 'filename' (original filename)\nconst rawTitle = body.lecture_title || data.headers?.['x-lecture-title'] || data.query?.title || body.filename || 'Untitled Lecture';\nconst lectureTitle = rawTitle.replace(/\\.(wav|mp3|m4a|ogg)$/i, '').replace(/[-_]/g, ' ');\n\nconst classSlug = className.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');\n\nreturn [{\n json: {\n className,\n classSlug,\n lectureTitle,\n uploadedAt: new Date().toISOString()\n },\n binary: $input.first().binary\n}];" + }, + "id": "24e4a726-ab50-4767-a144-1751616c781d", + "name": "Extract Class Info", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 224, + 0 + ] + }, + { + "parameters": { + "method": "POST", + "url": "http://10.5.1.16:8786/v1/audio/transcriptions", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Accept", + "value": "application/json" + } + ] + }, + "sendBody": true, + "contentType": "multipart-form-data", + "bodyParameters": { + "parameters": [ + { + "parameterType": "formBinaryData", + "name": "file", + "inputDataFieldName": "data" + }, + { + "name": "model", + "value": "Systran/faster-distil-whisper-small.en" + }, + { + "name": "language", + "value": "en" + }, + { + "name": "response_format", + "value": "verbose_json" + } + ] + }, + "options": { + "timeout": 3600000 + } + }, + "id": "fcd1062d-8f60-4214-85ee-f53b914abcbb", + "name": "Whisper Transcribe", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 448, + 0 + ] + }, + { + "parameters": { + "jsCode": "const result = $input.first().json;\nconst classInfo = $('Extract Class Info').first().json;\n\nconst segments = result.segments || [];\nconst fullText = result.text || segments.map(s => s.text).join(' ');\nconst duration = result.duration || segments[segments.length - 1]?.end || 0;\n\nconst timestamped = segments.map(s => {\n const mins = Math.floor(s.start / 60);\n const secs = Math.floor(s.start % 60);\n return `[${mins}:${String(secs).padStart(2, '0')}] ${s.text.trim()}`;\n}).join('\\n');\n\nreturn [{\n json: {\n className: classInfo.className,\n classSlug: classInfo.classSlug,\n lectureTitle: classInfo.lectureTitle,\n fullText: fullText.trim(),\n timestampedText: timestamped,\n durationMinutes: Math.round(duration / 60),\n segmentCount: segments.length\n }\n}];" + }, + "id": "ecf09ad2-327e-4a82-90f3-95da5590e0ac", + "name": "Format Transcript", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 672, + 0 + ] + }, + { + "parameters": { + "method": "POST", + "url": "http://10.5.1.6:4000/v1/chat/completions", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + }, + { + "name": "Authorization", + "value": "=Bearer {{ $env.LITELLM_API_KEY }}" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"model\": \"medium\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"You are an academic note-taking assistant. Given a class lecture transcription, provide:\\n1. A concise summary (3-5 sentences)\\n2. Key concepts covered (as a list)\\n3. Important definitions or formulas\\n4. Any assignments or deadlines mentioned\\n\\nRespond in JSON with keys: summary, key_concepts (array), definitions (array of {term, definition}), assignments (array)\"\n },\n {\n \"role\": \"user\",\n \"content\": \"Class: {{ $json.className }}\\nLecture: {{ $json.lectureTitle }}\\n\\nTranscription (first 3000 chars):\\n{{ $json.fullText.substring(0, 3000) }}\"\n }\n ],\n \"max_tokens\": 600,\n \"temperature\": 0.2,\n \"response_format\": { \"type\": \"json_object\" }\n}", + "options": {} + }, + "id": "e7407d06-f7f4-4105-a817-055d4703d59d", + "name": "AI Extract Notes", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 880, + 0 + ] + }, + { + "parameters": { + "jsCode": "const llmResponse = $input.first().json;\nconst transcriptData = $('Format Transcript').first().json;\n\nlet notes;\ntry {\n notes = JSON.parse(llmResponse.choices?.[0]?.message?.content || '{}');\n} catch(e) {\n notes = { summary: 'Could not parse notes', key_concepts: [], definitions: [], assignments: [] };\n}\n\nconst ragText = [\n `Class: ${transcriptData.className}`,\n `Lecture: ${transcriptData.lectureTitle}`,\n `Duration: ${transcriptData.durationMinutes} minutes`,\n `Summary: ${notes.summary}`,\n `Key Concepts: ${(notes.key_concepts || []).join(', ')}`,\n '',\n 'Full Transcript:',\n transcriptData.fullText\n].join('\\n');\n\nreturn [{\n json: {\n className: transcriptData.className,\n classSlug: transcriptData.classSlug,\n lectureTitle: transcriptData.lectureTitle,\n durationMinutes: transcriptData.durationMinutes,\n fullText: transcriptData.fullText,\n notes,\n ragText,\n source: `class-${transcriptData.classSlug}-${Date.now()}`,\n metadata: {\n class_name: transcriptData.className,\n class_slug: transcriptData.classSlug,\n lecture_title: transcriptData.lectureTitle,\n duration_minutes: transcriptData.durationMinutes,\n type: 'class_recording',\n recorded_at: new Date().toISOString()\n }\n }\n}];" + }, + "id": "71b0807d-03ba-45b2-b75b-b6fbeee5687e", + "name": "Prepare for RAG", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1104, + 0 + ] + }, + { + "parameters": { + "jsCode": "const input = $input.first().json;\nconst text = input.ragText || '';\nconst source = input.source;\nconst metadata = input.metadata;\n\nconst CHUNK_SIZE = 400;\nconst MAX_CHUNK = 600;\nconst OVERLAP = 50;\n\nconst chunks = [];\nconst sentences = text.split(/(?<=[.!?])\\s+/);\nlet current = '';\n\nfor (const sentence of sentences) {\n // If a single sentence exceeds MAX_CHUNK, split it by words first\n const parts = [];\n if (sentence.length > MAX_CHUNK) {\n const words = sentence.split(' ');\n let part = '';\n for (const word of words) {\n if ((part + ' ' + word).length > CHUNK_SIZE && part.length > 0) {\n parts.push(part.trim());\n part = word;\n } else {\n part = part ? part + ' ' + word : word;\n }\n }\n if (part.trim()) parts.push(part.trim());\n } else {\n parts.push(sentence);\n }\n\n for (const part of parts) {\n if ((current + ' ' + part).length > CHUNK_SIZE && current.length > 0) {\n chunks.push(current.trim());\n const words = current.split(' ');\n current = words.slice(-OVERLAP).join(' ') + ' ' + part;\n } else {\n current = current ? current + ' ' + part : part;\n }\n }\n}\nif (current.trim()) chunks.push(current.trim());\n\nreturn chunks.map((chunk, i) => ({\n json: {\n chunk,\n chunkIndex: i,\n totalChunks: chunks.length,\n source,\n metadata\n }\n}));" + }, + "id": "9e4bbe70-72b0-4955-848f-0d1b62ae94a4", + "name": "Chunk Text", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1328, + 0 + ] + }, + { + "parameters": { + "method": "POST", + "url": "http://10.5.1.6:11434/v1/embeddings", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"model\": \"nomic-embed-text\",\n \"input\": {{ JSON.stringify($json.chunk) }}\n}", + "options": { + "batching": { + "batch": { + "batchSize": 5, + "batchInterval": 500 + } + } + } + }, + "id": "4ae1ab11-c76f-4478-84e0-7bcdfb20b83b", + "name": "Get Embedding", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 1552, + 0 + ] + }, + { + "parameters": { + "jsCode": "const embeddingResponse = $input.first().json;\nconst chunkData = $('Chunk Text').item;\nconst embedding = embeddingResponse.data?.[0]?.embedding || [];\n\nconst pointId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {\n const r = Math.random() * 16 | 0;\n return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);\n});\n\nreturn [{\n json: {\n id: pointId,\n vector: embedding,\n payload: {\n text: chunkData.json.chunk,\n source: chunkData.json.source,\n chunkIndex: chunkData.json.chunkIndex,\n totalChunks: chunkData.json.totalChunks,\n metadata: chunkData.json.metadata,\n ingested_at: new Date().toISOString()\n }\n }\n}];" + }, + "id": "d4232b02-23fc-4cbd-ac2d-2fdbfbe9583a", + "name": "Prepare Qdrant Point", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1760, + 0 + ] + }, + { + "parameters": { + "method": "PUT", + "url": "http://10.5.1.6:6333/collections/knowledge_base/points", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"points\": [\n {\n \"id\": \"{{ $json.id }}\",\n \"vector\": {{ JSON.stringify($json.vector) }},\n \"payload\": {{ JSON.stringify($json.payload) }}\n }\n ]\n}", + "options": {} + }, + "id": "845a2aa0-2546-431d-96e8-b36875d1fe7d", + "name": "Upsert to Qdrant", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 1984, + 0 + ] + }, + { + "parameters": { + "jsCode": "const rag = $('Prepare for RAG').first().json;\nreturn [{\n json: {\n title: `\ud83c\udf93 ${rag.className} Lecture Processed`,\n message: `${rag.lectureTitle} (${rag.durationMinutes}min)\\nSummary: ${rag.notes.summary}\\nKey concepts: ${(rag.notes.key_concepts || []).slice(0, 5).join(', ')}`,\n priority: 5\n }\n}];" + }, + "id": "fmt08-0001-0000-0000-000000000008", + "name": "Format Gotify Message", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1968, + 0 + ] + }, + { + "parameters": { + "message": "={{ $json.message }}", + "additionalFields": { + "priority": "={{ $json.priority }}", + "title": "={{ $json.title }}" + }, + "options": { + "contentType": "text/plain" + } + }, + "id": "5b31b660-c414-40d1-82a5-d68511203133", + "name": "Notify via Gotify", + "type": "n8n-nodes-base.gotify", + "typeVersion": 1, + "position": [ + 2208, + 0 + ], + "credentials": { + "gotifyApi": { + "id": "SETUP_REQUIRED", + "name": "Class Recordings" + } + } + }, + { + "parameters": { + "jsCode": "const rag = $('Prepare for RAG').first().json;\nconst notes = rag.notes || {};\nconst concepts = (notes.key_concepts || []).map((c,i) => `${i+1}. ${c}`).join('\\n');\nconst defList = notes.definitions || [];\nconst defs = Array.isArray(defList)\n ? defList.map(d => `${d.term}: ${d.definition}`).join('\\n')\n : Object.entries(defList).map(([k,v]) => `${k}: ${v}`).join('\\n');\nconst assignments = (notes.assignments || []).map((a,i) => `${i+1}. ${a}`).join('\\n');\nconst content = [\n `Lecture Notes: ${rag.lectureTitle}`,\n `Class: ${rag.className}`,\n `Duration: ${rag.durationMinutes} minutes`,\n `Date: ${new Date().toISOString()}`, '',\n `Summary`, `=======`, notes.summary || '', '',\n `Key Concepts`, `=============`, concepts,\n ...(defs ? ['', `Definitions`, `===========`, defs] : []),\n ...(assignments ? ['', `Assignments`, `===========`, assignments] : []),\n '', `Full Transcript`, `================`, rag.fullText || '',\n].join('\\n');\nconst title = `${rag.className} \u2014 ${rag.lectureTitle}`;\nreturn [{ binary: { document: { data: Buffer.from(content,'utf-8').toString('base64'), mimeType:'text/plain', fileName:`${title}.txt` }}, json: { title, className: rag.className } }];\n" + }, + "id": "95aea3a7-3b15-4196-9e4a-dc8f6ed8d49e", + "name": "Prepare Notes Doc", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 2208, + -200 + ] + }, + { + "parameters": { + "method": "POST", + "url": "https://paperless.paccoco.com/api/documents/post_document/", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Authorization", + "value": "=Token {{ $env.PAPERLESS_API_TOKEN }}" + } + ] + }, + "sendBody": true, + "contentType": "multipart-form-data", + "bodyParameters": { + "parameters": [ + { + "parameterType": "formBinaryData", + "name": "document", + "inputDataFieldName": "document" + }, + { + "name": "title", + "value": "={{ $json.title }}" + }, + { + "name": "tags[]", + "value": "={{ $env.PAPERLESS_TAG_LECTURE_NOTES }}" + } + ] + }, + "options": { + "response": { + "response": { + "responseFormat": "text" + } + } + } + }, + "id": "c902a153-ab26-4518-af2a-585f0b72fe6c", + "name": "Save Notes to Paperless", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 2432, + -200 + ], + "continueOnFail": true + } + ], + "connections": { + "Class Upload Webhook": { + "main": [ + [ + { + "node": "Extract Class Info", + "type": "main", + "index": 0 + } + ] + ] + }, + "Extract Class Info": { + "main": [ + [ + { + "node": "Whisper Transcribe", + "type": "main", + "index": 0 + } + ] + ] + }, + "Whisper Transcribe": { + "main": [ + [ + { + "node": "Format Transcript", + "type": "main", + "index": 0 + } + ] + ] + }, + "Format Transcript": { + "main": [ + [ + { + "node": "AI Extract Notes", + "type": "main", + "index": 0 + } + ] + ] + }, + "AI Extract Notes": { + "main": [ + [ + { + "node": "Prepare for RAG", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare for RAG": { + "main": [ + [ + { + "node": "Chunk Text", + "type": "main", + "index": 0 + } + ] + ] + }, + "Chunk Text": { + "main": [ + [ + { + "node": "Get Embedding", + "type": "main", + "index": 0 + } + ] + ] + }, + "Get Embedding": { + "main": [ + [ + { + "node": "Prepare Qdrant Point", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare Qdrant Point": { + "main": [ + [ + { + "node": "Upsert to Qdrant", + "type": "main", + "index": 0 + } + ] + ] + }, + "Upsert to Qdrant": { + "main": [ + [ + { + "node": "Format Gotify Message", + "type": "main", + "index": 0 + }, + { + "node": "Prepare Notes Doc", + "type": "main", + "index": 0 + } + ] + ] + }, + "Format Gotify Message": { + "main": [ + [ + { + "node": "Notify via Gotify", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare Notes Doc": { + "main": [ + [ + { + "node": "Save Notes to Paperless", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "settings": { + "executionOrder": "v1", + "binaryMode": "separate" + }, + "staticData": null, + "meta": null, + "pinData": {}, + "versionId": "d64293a7-5537-4b01-a11b-6b838d6cacbf", + "activeVersionId": "d64293a7-5537-4b01-a11b-6b838d6cacbf", + "versionCounter": 11, + "triggerCount": 1, + "tags": [ + { + "updatedAt": "2026-05-07T00:49:56.081Z", + "createdAt": "2026-05-07T00:49:56.081Z", + "id": "Q9lCYtCOuy9XmeyL", + "name": "school" + }, + { + "updatedAt": "2026-05-06T22:39:59.716Z", + "createdAt": "2026-05-06T22:39:59.716Z", + "id": "tuPxDoKFqpVrrQoX", + "name": "whisper" + }, + { + "updatedAt": "2026-05-06T22:39:33.284Z", + "createdAt": "2026-05-06T22:39:33.284Z", + "id": "7uuEyQrIlcuruZBk", + "name": "rag" + }, + { + "updatedAt": "2026-05-06T22:38:51.678Z", + "createdAt": "2026-05-06T22:38:51.678Z", + "id": "S9FxzVfHLsHmyzyt", + "name": "ai" + } + ], + "shared": [ + { + "updatedAt": "2026-05-07T01:16:50.340Z", + "createdAt": "2026-05-07T01:16:50.340Z", + "role": "workflow:owner", + "workflowId": "91I278E72FAaQenD", + "projectId": "dxCRnBdX5uJizCGa", + "project": { + "updatedAt": "2026-05-06T01:10:37.638Z", + "createdAt": "2026-05-06T01:08:07.820Z", + "id": "dxCRnBdX5uJizCGa", + "name": "Wilfred Fizzlepoof ", + "type": "personal", + "icon": null, + "description": null, + "creatorId": "47b2227f-2fc2-4a08-bd35-ea157b92df0d" + } + } + ], + "versionMetadata": { + "name": "Version d64293a7", + "description": "" + } +} \ No newline at end of file diff --git a/n8n-workflows/09-class-transcript-ingest-v1.1.json b/n8n-workflows/09-class-transcript-ingest-v1.1.json new file mode 100644 index 0000000..a6aae51 --- /dev/null +++ b/n8n-workflows/09-class-transcript-ingest-v1.1.json @@ -0,0 +1,423 @@ +{ + "name": "Class Transcript Ingest v1.1", + "nodes": [ + { + "parameters": { + "httpMethod": "POST", + "path": "class/ingest", + "responseMode": "responseNode", + "options": {} + }, + "id": "a1b2c3d4-0001-0001-0001-000000000001", + "name": "Class Ingest Webhook", + "type": "n8n-nodes-base.webhook", + "typeVersion": 2, + "position": [ + 0, + 0 + ], + "webhookId": "class-ingest-rocinante-v1" + }, + { + "parameters": { + "respondWith": "json", + "responseBody": "={ \"status\": \"accepted\" }", + "options": {} + }, + "id": "a1b2c3d4-0001-0001-0001-000000000002", + "name": "Respond Accepted", + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1, + "position": [ + 224, + -200 + ] + }, + { + "parameters": { + "jsCode": "const body = $input.first().json.body || $input.first().json;\n\nconst className = body.class_name || 'unclassified';\nconst lectureTitle = body.lecture_title || 'Untitled Lecture';\nconst fullText = body.transcript || '';\nconst timestampedText = body.timestamped_transcript || fullText;\nconst durationMinutes = Math.round((body.duration_seconds || 0) / 60);\nconst classSlug = className.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');\n\nreturn [{\n json: {\n className,\n classSlug,\n lectureTitle,\n fullText,\n timestampedText,\n durationMinutes\n }\n}];" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000003", + "name": "Extract Class Info", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 224, + 0 + ] + }, + { + "parameters": { + "method": "POST", + "url": "http://10.5.1.6:4000/v1/chat/completions", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + }, + { + "name": "Authorization", + "value": "=Bearer {{ $env.LITELLM_API_KEY }}" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"model\": \"medium\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"You are an academic note-taking assistant. Given a class lecture transcription, provide:\\n1. A concise summary (3-5 sentences)\\n2. Key concepts covered (as a list)\\n3. Important definitions or formulas\\n4. Any assignments or deadlines mentioned\\n\\nRespond in JSON with keys: summary, key_concepts (array), definitions (array of {term, definition}), assignments (array)\"\n },\n {\n \"role\": \"user\",\n \"content\": \"Class: {{ $json.className }}\\nLecture: {{ $json.lectureTitle }}\\n\\nTranscription (first 4000 chars):\\n{{ $json.fullText.substring(0, 4000) }}\"\n }\n ],\n \"max_tokens\": 600,\n \"temperature\": 0.2,\n \"response_format\": { \"type\": \"json_object\" }\n}", + "options": {} + }, + "id": "a1b2c3d4-0001-0001-0001-000000000004", + "name": "AI Extract Notes", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 448, + 0 + ] + }, + { + "parameters": { + "jsCode": "const llmResponse = $input.first().json;\nconst classInfo = $('Extract Class Info').first().json;\n\nlet notes;\ntry {\n notes = JSON.parse(llmResponse.choices?.[0]?.message?.content || '{}');\n} catch(e) {\n notes = { summary: 'Could not parse notes', key_concepts: [], definitions: [], assignments: [] };\n}\n\nconst ragText = [\n `Class: ${classInfo.className}`,\n `Lecture: ${classInfo.lectureTitle}`,\n `Duration: ${classInfo.durationMinutes} minutes`,\n `Summary: ${notes.summary}`,\n `Key Concepts: ${(notes.key_concepts || []).join(', ')}`,\n '',\n 'Full Transcript:',\n classInfo.fullText\n].join('\\n');\n\nreturn [{\n json: {\n className: classInfo.className,\n classSlug: classInfo.classSlug,\n lectureTitle: classInfo.lectureTitle,\n durationMinutes: classInfo.durationMinutes,\n fullText: classInfo.fullText,\n notes,\n ragText,\n source: `class-${classInfo.classSlug}-${Date.now()}`,\n metadata: {\n class_name: classInfo.className,\n class_slug: classInfo.classSlug,\n lecture_title: classInfo.lectureTitle,\n duration_minutes: classInfo.durationMinutes,\n type: 'class_recording',\n recorded_at: new Date().toISOString()\n }\n }\n}];" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000005", + "name": "Prepare for RAG", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 672, + 0 + ] + }, + { + "parameters": { + "jsCode": "const input = $input.first().json;\nconst text = input.ragText || '';\nconst source = input.source;\nconst metadata = input.metadata;\n\nconst CHUNK_SIZE = 400;\nconst MAX_CHUNK = 600;\nconst OVERLAP = 50;\n\nconst chunks = [];\nconst sentences = text.split(/(?<=[.!?])\\s+/);\nlet current = '';\n\nfor (const sentence of sentences) {\n // If a single sentence exceeds MAX_CHUNK, split it by words first\n const parts = [];\n if (sentence.length > MAX_CHUNK) {\n const words = sentence.split(' ');\n let part = '';\n for (const word of words) {\n if ((part + ' ' + word).length > CHUNK_SIZE && part.length > 0) {\n parts.push(part.trim());\n part = word;\n } else {\n part = part ? part + ' ' + word : word;\n }\n }\n if (part.trim()) parts.push(part.trim());\n } else {\n parts.push(sentence);\n }\n\n for (const part of parts) {\n if ((current + ' ' + part).length > CHUNK_SIZE && current.length > 0) {\n chunks.push(current.trim());\n const words = current.split(' ');\n current = words.slice(-OVERLAP).join(' ') + ' ' + part;\n } else {\n current = current ? current + ' ' + part : part;\n }\n }\n}\nif (current.trim()) chunks.push(current.trim());\n\nreturn chunks.map((chunk, i) => ({\n json: { chunk, chunkIndex: i, totalChunks: chunks.length, source, metadata }\n}));" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000006", + "name": "Chunk Text", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 896, + 0 + ] + }, + { + "parameters": { + "method": "POST", + "url": "http://10.5.1.6:11434/v1/embeddings", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"model\": \"nomic-embed-text\",\n \"input\": {{ JSON.stringify($json.chunk) }}\n}", + "options": { + "batching": { + "batch": { + "batchSize": 5, + "batchInterval": 500 + } + } + } + }, + "id": "a1b2c3d4-0001-0001-0001-000000000007", + "name": "Get Embedding", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 1120, + 0 + ] + }, + { + "parameters": { + "jsCode": "const embeddingResponse = $input.first().json;\nconst chunkData = $('Chunk Text').item;\nconst embedding = embeddingResponse.data?.[0]?.embedding || [];\n\nconst pointId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {\n const r = Math.random() * 16 | 0;\n return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);\n});\n\nreturn [{\n json: {\n id: pointId,\n vector: embedding,\n payload: {\n text: chunkData.json.chunk,\n source: chunkData.json.source,\n chunkIndex: chunkData.json.chunkIndex,\n totalChunks: chunkData.json.totalChunks,\n metadata: chunkData.json.metadata,\n ingested_at: new Date().toISOString()\n }\n }\n}];" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000008", + "name": "Prepare Qdrant Point", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1344, + 0 + ] + }, + { + "parameters": { + "method": "PUT", + "url": "http://10.5.1.6:6333/collections/knowledge_base/points", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Content-Type", + "value": "application/json" + } + ] + }, + "sendBody": true, + "specifyBody": "json", + "jsonBody": "={\n \"points\": [\n {\n \"id\": \"{{ $json.id }}\",\n \"vector\": {{ JSON.stringify($json.vector) }},\n \"payload\": {{ JSON.stringify($json.payload) }}\n }\n ]\n}", + "options": {} + }, + "id": "a1b2c3d4-0001-0001-0001-000000000009", + "name": "Upsert to Qdrant", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 1568, + 0 + ] + }, + { + "parameters": { + "jsCode": "const rag = $('Prepare for RAG').first().json;\nreturn [{\n json: {\n title: `\ud83c\udf93 ${rag.className} Lecture Processed`,\n message: `${rag.lectureTitle} (${rag.durationMinutes}min)\\nSummary: ${rag.notes.summary}\\nKey concepts: ${(rag.notes.key_concepts || []).slice(0, 5).join(', ')}`,\n priority: 5\n }\n}];" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000010", + "name": "Format Gotify Message", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1552, + -200 + ] + }, + { + "parameters": { + "message": "={{ $json.message }}", + "additionalFields": { + "priority": "={{ $json.priority }}", + "title": "={{ $json.title }}" + }, + "options": { + "contentType": "text/plain" + } + }, + "id": "a1b2c3d4-0001-0001-0001-000000000011", + "name": "Notify via Gotify", + "type": "n8n-nodes-base.gotify", + "typeVersion": 1, + "position": [ + 1776, + -200 + ], + "credentials": { + "gotifyApi": { + "id": "SETUP_REQUIRED", + "name": "Class Recordings" + } + } + }, + { + "parameters": { + "jsCode": "const rag = $('Prepare for RAG').first().json;\nconst notes = rag.notes || {};\nconst concepts = (notes.key_concepts || []).map((c,i) => `${i+1}. ${c}`).join('\\n');\nconst defList = notes.definitions || [];\nconst defs = Array.isArray(defList)\n ? defList.map(d => `${d.term}: ${d.definition}`).join('\\n')\n : Object.entries(defList).map(([k,v]) => `${k}: ${v}`).join('\\n');\nconst assignments = (notes.assignments || []).map((a,i) => `${i+1}. ${a}`).join('\\n');\n\nconst content = [\n `Lecture Notes: ${rag.lectureTitle}`,\n `Class: ${rag.className}`,\n `Duration: ${rag.durationMinutes} minutes`,\n `Date: ${new Date().toISOString()}`, '',\n `Summary`, `=======`, notes.summary || '', '',\n `Key Concepts`, `=============`, concepts,\n ...(defs ? ['', `Definitions`, `===========`, defs] : []),\n ...(assignments ? ['', `Assignments`, `===========`, assignments] : []),\n '', `Full Transcript`, `================`, rag.fullText || '',\n].join('\\n');\n\nconst title = `${rag.className} \u2014 ${rag.lectureTitle}`;\nreturn [{\n binary: {\n document: {\n data: Buffer.from(content, 'utf-8').toString('base64'),\n mimeType: 'text/plain',\n fileName: `${title}.txt`\n }\n },\n json: { title, className: rag.className }\n}];" + }, + "id": "a1b2c3d4-0001-0001-0001-000000000012", + "name": "Prepare Notes Doc", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1776, + 200 + ] + }, + { + "parameters": { + "method": "POST", + "url": "https://paperless.paccoco.com/api/documents/post_document/", + "sendHeaders": true, + "headerParameters": { + "parameters": [ + { + "name": "Authorization", + "value": "=Token {{ $env.PAPERLESS_API_TOKEN }}" + } + ] + }, + "sendBody": true, + "contentType": "multipart-form-data", + "bodyParameters": { + "parameters": [ + { + "parameterType": "formBinaryData", + "name": "document", + "inputDataFieldName": "document" + }, + { + "name": "title", + "value": "={{ $json.title }}" + }, + { + "name": "tags[]", + "value": "={{ $env.PAPERLESS_TAG_LECTURE_NOTES }}" + } + ] + }, + "options": { + "response": { + "response": { + "responseFormat": "text" + } + } + } + }, + "id": "a1b2c3d4-0001-0001-0001-000000000013", + "name": "Save Notes to Paperless", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 2000, + 200 + ], + "continueOnFail": true + } + ], + "connections": { + "Class Ingest Webhook": { + "main": [ + [ + { + "node": "Extract Class Info", + "type": "main", + "index": 0 + }, + { + "node": "Respond Accepted", + "type": "main", + "index": 0 + } + ] + ] + }, + "Extract Class Info": { + "main": [ + [ + { + "node": "AI Extract Notes", + "type": "main", + "index": 0 + } + ] + ] + }, + "AI Extract Notes": { + "main": [ + [ + { + "node": "Prepare for RAG", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare for RAG": { + "main": [ + [ + { + "node": "Chunk Text", + "type": "main", + "index": 0 + } + ] + ] + }, + "Chunk Text": { + "main": [ + [ + { + "node": "Get Embedding", + "type": "main", + "index": 0 + } + ] + ] + }, + "Get Embedding": { + "main": [ + [ + { + "node": "Prepare Qdrant Point", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare Qdrant Point": { + "main": [ + [ + { + "node": "Upsert to Qdrant", + "type": "main", + "index": 0 + } + ] + ] + }, + "Upsert to Qdrant": { + "main": [ + [ + { + "node": "Format Gotify Message", + "type": "main", + "index": 0 + }, + { + "node": "Prepare Notes Doc", + "type": "main", + "index": 0 + } + ] + ] + }, + "Format Gotify Message": { + "main": [ + [ + { + "node": "Notify via Gotify", + "type": "main", + "index": 0 + } + ] + ] + }, + "Prepare Notes Doc": { + "main": [ + [ + { + "node": "Save Notes to Paperless", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "settings": { + "executionOrder": "v1", + "binaryMode": "separate" + }, + "staticData": null, + "meta": null, + "pinData": {}, + "tags": [ + { + "name": "school" + }, + { + "name": "rag" + }, + { + "name": "ai" + } + ] +} \ No newline at end of file diff --git a/rocinante/docker-compose.yaml b/rocinante/docker-compose.yaml new file mode 100644 index 0000000..e68830b --- /dev/null +++ b/rocinante/docker-compose.yaml @@ -0,0 +1,40 @@ +name: rocinante + +services: + + whisper: + image: fedirz/faster-whisper-server:latest-cuda + container_name: rocinante-whisper + restart: unless-stopped + ports: + - "8787:8000" + environment: + WHISPER__MODEL: large-v3 + WHISPER__INFERENCE_DEVICE: cuda + TZ: ${TZ} + deploy: + resources: + reservations: + devices: + - driver: nvidia + count: 1 + capabilities: [gpu] + volumes: + - whisper-cache:/root/.cache/huggingface + + watcher: + build: ./watcher + container_name: rocinante-watcher + restart: unless-stopped + environment: + WHISPER_URL: http://whisper:8000 + N8N_WEBHOOK_URL: ${N8N_WEBHOOK_URL} + WATCH_DIR: /recordings + TZ: ${TZ} + volumes: + - C:/Recordings:/recordings + depends_on: + - whisper + +volumes: + whisper-cache: diff --git a/rocinante/watcher/Dockerfile b/rocinante/watcher/Dockerfile new file mode 100644 index 0000000..80b9673 --- /dev/null +++ b/rocinante/watcher/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.12-slim + +RUN apt-get update && \ + apt-get install -y --no-install-recommends ffmpeg && \ + rm -rf /var/lib/apt/lists/* + +RUN pip install --no-cache-dir watchdog requests + +COPY watch.py /app/watch.py +WORKDIR /app + +CMD ["python", "-u", "watch.py"] diff --git a/rocinante/watcher/watch.py b/rocinante/watcher/watch.py new file mode 100644 index 0000000..be71087 --- /dev/null +++ b/rocinante/watcher/watch.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python3 +""" +Class recording watcher for Rocinante. + +Watches C:/Recordings (mounted as /recordings) for new .wav files. +For each file detected: + 1. Waits for the file to finish being written + 2. Converts WAV -> MP3 via ffmpeg (deletes original WAV) + 3. Transcribes via local faster-whisper-server (large-v3, CUDA) + 4. POSTs transcript + metadata to n8n /webhook/class/ingest + 5. Deletes the MP3 + +Folder convention: subfolder name = class name + /recordings/HIST-2020/lecture-03-3.wav -> class=HIST-2020, title=lecture 03 3 +""" + +import os +import time +import subprocess +import logging +import requests +from pathlib import Path +from watchdog.observers.polling import PollingObserver as Observer +from watchdog.events import FileSystemEventHandler + +# Config from environment +WHISPER_URL = os.environ.get("WHISPER_URL", "http://whisper:8000") +N8N_WEBHOOK = os.environ.get("N8N_WEBHOOK_URL", "https://n8n.paccoco.com/webhook/class/ingest") +WATCH_DIR = os.environ.get("WATCH_DIR", "/recordings") +WHISPER_MODEL = os.environ.get("WHISPER_MODEL", "large-v3") + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)-8s %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", +) +log = logging.getLogger("watcher") + + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + +def wait_for_file_stable(path: Path, interval: float = 2.0, required_stable: int = 3) -> None: + """Block until the file size stops changing (i.e. the copy is complete).""" + log.info(f"Waiting for {path.name} to finish writing...") + prev_size = -1 + stable_count = 0 + while stable_count < required_stable: + try: + size = path.stat().st_size + except FileNotFoundError: + time.sleep(interval) + continue + if size == prev_size and size > 0: + stable_count += 1 + else: + stable_count = 0 + prev_size = size + time.sleep(interval) + log.info(f"{path.name} is stable ({prev_size:,} bytes)") + + +def convert_to_mp3(wav_path: Path) -> Path: + mp3_path = wav_path.with_suffix(".mp3") + log.info(f"Converting to MP3: {wav_path.name}") + result = subprocess.run( + [ + "ffmpeg", "-y", + "-i", str(wav_path), + "-codec:a", "libmp3lame", + "-qscale:a", "4", + str(mp3_path), + ], + capture_output=True, + text=True, + ) + if result.returncode != 0: + raise RuntimeError(f"ffmpeg failed:\n{result.stderr}") + wav_path.unlink() + log.info(f"Deleted original WAV: {wav_path.name}") + return mp3_path + + +def transcribe(mp3_path: Path) -> dict: + log.info(f"Transcribing {mp3_path.name} with {WHISPER_MODEL}...") + with open(mp3_path, "rb") as f: + resp = requests.post( + f"{WHISPER_URL}/v1/audio/transcriptions", + files={"file": (mp3_path.name, f, "audio/mpeg")}, + data={ + "model": WHISPER_MODEL, + "language": "en", + "response_format": "verbose_json", + }, + timeout=7200, # 2 hours — large recordings on first load can be slow + ) + resp.raise_for_status() + log.info("Transcription complete") + return resp.json() + + +def build_timestamped(segments: list) -> str: + lines = [] + for s in segments: + mins = int(s.get("start", 0) // 60) + secs = int(s.get("start", 0) % 60) + lines.append(f"[{mins}:{secs:02d}] {s.get('text', '').strip()}") + return "\n".join(lines) + + +def send_to_n8n(class_name: str, lecture_title: str, result: dict) -> None: + segments = result.get("segments", []) + full_text = result.get("text", "").strip() + duration = result.get("duration") or (segments[-1].get("end", 0) if segments else 0) + + payload = { + "class_name": class_name, + "lecture_title": lecture_title, + "transcript": full_text, + "timestamped_transcript": build_timestamped(segments), + "duration_seconds": duration, + } + + log.info(f"Sending to n8n: {class_name} / {lecture_title} ({len(full_text)} chars)") + resp = requests.post(N8N_WEBHOOK, json=payload, timeout=300) + resp.raise_for_status() + log.info(f"n8n accepted — HTTP {resp.status_code}") + + +# --------------------------------------------------------------------------- +# Processing pipeline +# --------------------------------------------------------------------------- + +def process_wav(wav_path: Path) -> None: + # Must be inside a class subfolder, not the root recordings dir + if wav_path.parent == Path(WATCH_DIR): + log.warning(f"Skipping {wav_path.name} — drop files into a class subfolder, e.g. /recordings/HIST-2020/") + return + + class_name = wav_path.parent.name + lecture_title = wav_path.stem.replace("-", " ").replace("_", " ") + + log.info(f"--- Processing: {class_name} / {lecture_title} ---") + + wait_for_file_stable(wav_path) + mp3_path = convert_to_mp3(wav_path) + + result = transcribe(mp3_path) + send_to_n8n(class_name, lecture_title, result) + + log.info(f"--- Done: {class_name} / {lecture_title} ---") + + +# --------------------------------------------------------------------------- +# Watchdog handler +# --------------------------------------------------------------------------- + +class WavHandler(FileSystemEventHandler): + def on_created(self, event): + if event.is_directory: + return + path = Path(event.src_path) + if path.suffix.lower() == ".wav": + log.info(f"Detected new WAV: {path}") + try: + process_wav(path) + except Exception as exc: + log.error(f"Failed to process {path}: {exc}", exc_info=True) + + +# --------------------------------------------------------------------------- +# Entry point +# --------------------------------------------------------------------------- + +def scan_existing(watch_dir: Path) -> None: + """Process any .wav files already present at startup.""" + existing = list(watch_dir.rglob("*.wav")) + list(watch_dir.rglob("*.WAV")) + if not existing: + return + log.info(f"Found {len(existing)} existing WAV(s) at startup — processing...") + for wav in existing: + try: + process_wav(wav) + except Exception as exc: + log.error(f"Failed to process {wav}: {exc}", exc_info=True) + + +if __name__ == "__main__": + watch_dir = Path(WATCH_DIR) + watch_dir.mkdir(parents=True, exist_ok=True) + + log.info(f"Whisper URL : {WHISPER_URL}") + log.info(f"n8n webhook : {N8N_WEBHOOK}") + log.info(f"Watching : {watch_dir} (recursive)") + + # Process any WAVs already sitting in the folder + scan_existing(watch_dir) + + observer = Observer() + observer.schedule(WavHandler(), str(watch_dir), recursive=True) + observer.start() + log.info("Watcher started — drop a .wav into a class subfolder to begin") + + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + pass + finally: + observer.stop() + observer.join() + log.info("Watcher stopped")