Whisper-FastAPI / main.py
dahyedahye's picture
Add application file
9271e46
raw
history blame
1.31 kB
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import os
import shutil
from modules.whisper.whisper_factory import WhisperFactory
app = FastAPI()
# Initialize Whisper inference engine
whisper_inf = WhisperFactory.create_whisper_inference(
whisper_type="faster-whisper",
whisper_model_dir=os.path.join("models", "Whisper"),
faster_whisper_model_dir=os.path.join("models", "Whisper", "faster-whisper"),
insanely_fast_whisper_model_dir=os.path.join("models", "Whisper", "insanely-fast-whisper"),
output_dir=os.path.join("outputs"),
)
@app.post("/upload-video/")
async def upload_video(file: UploadFile = File(...)):
"""
Upload a video file and get the generated SRT file as a response.
"""
# Save the uploaded video file temporarily
input_video_path = os.path.join("temp", file.filename)
os.makedirs("temp", exist_ok=True)
with open(input_video_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Generate the subtitle file
output_srt_path = whisper_inf.transcribe_file(
input_video_path,
file_format="SRT",
add_timestamp=True
)
# Return the SRT file as a response
return FileResponse(path=output_srt_path, filename=os.path.basename(output_srt_path))