Kavin1701/fa_one
0
1from fastapi import FastAPI, File, UploadFile, HTTPException2from fastapi.responses import JSONResponse, RedirectResponse3from tempfile import NamedTemporaryFile4import whisper5import torch6from typing import List7 8# Checking if NVIDIA GPU is available9DEVICE = "cuda" if torch.cuda.is_available() else "cpu"10 11# Load the Whisper model:12model = whisper.load_model("small", device=DEVICE)13 14app = FastAPI()15 16@app.post("/whisper/")17async def handler(files: List[UploadFile] = File(...)):18 if not files:19 raise HTTPException(status_code=400, detail="No files were provided")20 21 # For each file, let's store the results in a list of dictionaries.22 results = []23 24 for file in files:25 # Create a temporary file.26 with NamedTemporaryFile(delete=True) as temp:27 # Write the user's uploaded file to the temporary file.28 with open(temp.name, "wb") as temp_file:29 temp_file.write(file.file.read())30 31 # Let's get the transcript of the temporary file.32 result = model.transcribe(temp.name)33 34 # Now we can store the result object for this file.35 results.append({36 'filename': file.filename,37 'transcript': result['text'],38 })39 40 return JSONResponse(content={'results': results})41 42 43@app.get("/", response_class=RedirectResponse)44async def redirect_to_docs():45 return "/docs"