Download src/server/ingest/document.ts from bhyve/auto-lms: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/spaces/bhyve/auto-lms/resolve/main/src/server/ingest/document.ts
- Command line
-
hf download hf://spaces/bhyve/auto-lms/src/server/ingest/document.ts
-
curl -L -o document.ts https://huggingface.co/spaces/bhyve/auto-lms/resolve/main/src/server/ingest/document.ts
2.72 kB
| import { promises as fs } from 'fs'; | |
| import path from 'path'; | |
| import mammoth from 'mammoth'; | |
| import { parseOffice } from 'officeparser'; | |
| // The Gemini Files API natively understands these — upload as-is for multimodal | |
| // reasoning. Everything else we extract to text locally (the Files API rejects | |
| // Office formats like .docx/.pptx/.xlsx with HTTP 400). | |
| const GEMINI_NATIVE_EXTS = new Set([ | |
| '.pdf', | |
| '.png', '.jpg', '.jpeg', '.webp', '.gif', '.heic', '.heif', | |
| '.mp3', '.wav', '.aac', '.ogg', '.flac', '.aiff', | |
| '.mp4', '.mov', '.webm', '.mpeg', '.mpg', '.avi', '.wmv', '.3gp', '.flv', | |
| ]); | |
| const TEXT_EXTS = new Set([ | |
| '.txt', '.md', '.markdown', '.csv', '.tsv', '.json', '.html', '.htm', '.xml', | |
| '.rtf', '.log', '.yaml', '.yml', | |
| ]); | |
| const WORD_EXTS = new Set(['.docx']); | |
| const OFFICE_EXTS = new Set(['.pptx', '.xlsx', '.odt', '.odp', '.ods']); | |
| export type IngestRoute = 'gemini' | 'extract' | 'unsupported'; | |
| function ext(filename: string): string { | |
| return path.extname(filename).toLowerCase(); | |
| } | |
| // Decide how a file should be ingested, based on extension then MIME type. | |
| export function routeForFile(mimeType: string, filename: string): IngestRoute { | |
| const e = ext(filename); | |
| const mime = (mimeType || '').toLowerCase(); | |
| if (GEMINI_NATIVE_EXTS.has(e)) return 'gemini'; | |
| if ( | |
| mime === 'application/pdf' || | |
| mime.startsWith('image/') || | |
| mime.startsWith('audio/') || | |
| mime.startsWith('video/') | |
| ) { | |
| return 'gemini'; | |
| } | |
| if (TEXT_EXTS.has(e) || mime.startsWith('text/') || mime === 'application/json') return 'extract'; | |
| if (WORD_EXTS.has(e) || mime.includes('wordprocessingml')) return 'extract'; | |
| if ( | |
| OFFICE_EXTS.has(e) || | |
| mime.includes('presentationml') || | |
| mime.includes('spreadsheetml') || | |
| mime.includes('opendocument') | |
| ) { | |
| return 'extract'; | |
| } | |
| return 'unsupported'; | |
| } | |
| // Extract plain text from a local document (Word via mammoth; PowerPoint/Excel/ | |
| // OpenDocument via officeparser; anything text-like read as UTF-8). | |
| export async function extractDocumentText( | |
| absolutePath: string, | |
| mimeType: string, | |
| filename: string, | |
| ): Promise<string> { | |
| const e = ext(filename || absolutePath); | |
| const mime = (mimeType || '').toLowerCase(); | |
| if (WORD_EXTS.has(e) || mime.includes('wordprocessingml')) { | |
| const { value } = await mammoth.extractRawText({ path: absolutePath }); | |
| return value.trim(); | |
| } | |
| if ( | |
| OFFICE_EXTS.has(e) || | |
| mime.includes('presentationml') || | |
| mime.includes('spreadsheetml') || | |
| mime.includes('opendocument') | |
| ) { | |
| const ast = await parseOffice(absolutePath); | |
| return ast.toText().trim(); | |
| } | |
| const buffer = await fs.readFile(absolutePath); | |
| return buffer.toString('utf8').trim(); | |
| } | |