Add transcribe

This commit is contained in:
suraj.shenoy.b@gmail.com
2025-01-25 09:50:53 -06:00
parent e3437fefc9
commit 43b66a6e0f
9 changed files with 184 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,34 @@
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import whisper
import os
import tempfile
app = FastAPI()
model = whisper.load_model("base") # Load the model once for efficiency
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"], # Frontend origin (adjust as needed)
allow_credentials=True,
allow_methods=["*"], # Allow all HTTP methods (GET, POST, etc.)
allow_headers=["*"], # Allow all headers (Authorization, Content-Type, etc.)
)
@app.post("/transcribe")
async def transcribe_audio(file: UploadFile = File(...)):
# Save the uploaded file to a temporary location
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
temp_file.write(await file.read())
temp_path = temp_file.name
try:
# Transcribe the audio
result = model.transcribe("inputs/test.mp3")
transcription = result["text"]
print(transcription)
finally:
# Clean up temporary file
os.remove(temp_path)
return {"transcription": transcription}

Binary file not shown.

View File

@@ -0,0 +1,55 @@
import { NextRequest, NextResponse } from "next/server";
import multer from "multer";
import fs from "fs/promises";
import whisper from "openai-whisper";
const upload = multer({ dest: "uploads/" });
export const config = {
api: {
bodyParser: false, // Disable Next.js's body parsing for file uploads
},
};
// Whisper model (initialize once for efficiency)
const model = whisper.load_model("base");
// Utility to transcribe audio
async function transcribeAudio(filePath: string): Promise<string> {
const transcription = await model.transcribe(filePath);
return transcription.text;
}
async function parseMultipartForm(req: NextRequest): Promise<File> {
return new Promise((resolve, reject) => {
const multerMiddleware = upload.single("audio");
multerMiddleware(req as any, {} as any, (error: any) => {
if (error) return reject(error);
resolve(req.file);
});
});
}
export async function POST(req: NextRequest) {
try {
// Parse the incoming multipart form data
const file = await parseMultipartForm(req);
if (!file) {
return NextResponse.json({ error: "No audio file provided" }, { status: 400 });
}
const filePath = file.path;
// Transcribe the audio
const transcription = await transcribeAudio(filePath);
// Clean up the uploaded file
await fs.unlink(filePath);
return NextResponse.json({ transcription });
} catch (error) {
console.error("Error during transcription:", error);
return NextResponse.json({ error: "Transcription failed" }, { status: 500 });
}
}