Spaces:
Sleeping
Sleeping
| import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf" | |
| import { DocxLoader } from "@langchain/community/document_loaders/fs/docx" | |
| import { NextResponse } from 'next/server' | |
| import { writeFile } from 'fs/promises' | |
| import { join } from 'path' | |
| import { Document } from '@langchain/core/documents' | |
| import { mkdir } from 'fs/promises' | |
| export async function POST(request: Request) { | |
| try { | |
| const formData = await request.formData() | |
| const files = formData.getAll('files') as File[] | |
| if (!files?.length) { | |
| return NextResponse.json( | |
| { error: 'No files uploaded' }, | |
| { status: 400 } | |
| ) | |
| } | |
| // Ensure temp directory exists | |
| const tempDir = '/tmp' | |
| try { | |
| await mkdir(tempDir, { recursive: true }) | |
| } catch (err) { | |
| console.error('Error creating temp directory:', err) | |
| } | |
| const documents: { text: string }[] = [] | |
| for (const file of files) { | |
| try { | |
| const bytes = await file.arrayBuffer() | |
| const buffer = Buffer.from(bytes) | |
| const tempPath = join(tempDir, `${Date.now()}-${file.name}`) | |
| await writeFile(tempPath, buffer) | |
| console.log('File written to:', tempPath) | |
| let docs: Document[] = [] | |
| if (file.name.toLowerCase().endsWith('.pdf')) { | |
| const loader = new PDFLoader(tempPath) | |
| docs = await loader.load() | |
| } else if (file.name.toLowerCase().endsWith('.docx')) { | |
| const loader = new DocxLoader(tempPath) | |
| docs = await loader.load() | |
| } | |
| documents.push(...docs.map(doc => ({ | |
| text: doc.pageContent | |
| .replace(/\s+/g, ' ') | |
| .trim() | |
| }))) | |
| } catch (err) { | |
| console.error(`Error processing file ${file.name}:`, err) | |
| } | |
| } | |
| if (documents.length === 0) { | |
| return NextResponse.json( | |
| { error: 'No documents could be processed' }, | |
| { status: 400 } | |
| ) | |
| } | |
| return NextResponse.json({ documents }) | |
| } catch (error) { | |
| console.error('Upload error:', error) | |
| return NextResponse.json( | |
| { error: 'Failed to process files' }, | |
| { status: 500 } | |
| ) | |
| } | |
| } |