Spaces:
Runtime error
Runtime error
Download fastapi_app.py from Mrunal09/whisperfastapit: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/Mrunal09/whisperfastapit/resolve/main/fastapi_app.py
- Command line
-
hf download hf://spaces/Mrunal09/whisperfastapit/fastapi_app.py
-
curl -L -o fastapi_app.py https://huggingface.co/spaces/Mrunal09/whisperfastapit/resolve/main/fastapi_app.py
1.49 kB
| from fastapi import FastAPI, File, UploadFile, HTTPException | |
| from fastapi.responses import JSONResponse, RedirectResponse | |
| from tempfile import NamedTemporaryFile | |
| import whisper | |
| import torch | |
| from typing import List | |
| # Checking if NVIDIA GPU is available | |
| #torch.cuda.is_available() | |
| #DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | |
| # Load the Whisper model: | |
| #model = whisper.load_model("base", device=DEVICE) | |
| model = whisper.load_model("base") | |
| app = FastAPI() | |
| async def handler(files: List[UploadFile] = File(...)): | |
| if not files: | |
| raise HTTPException(status_code=400, detail="No files were provided") | |
| # For each file, let's store the results in a list of dictionaries. | |
| results = [] | |
| for file in files: | |
| # Create a temporary file. | |
| with NamedTemporaryFile(delete=True) as temp: | |
| # Write the user's uploaded file to the temporary file. | |
| with open(temp.name, "wb") as temp_file: | |
| temp_file.write(file.file.read()) | |
| # Let's get the transcript of the temporary file. | |
| result = model.transcribe(temp.name) | |
| # Now we can store the result object for this file. | |
| results.append({ | |
| 'filename': file.filename, | |
| 'transcript': result['text'], | |
| }) | |
| return JSONResponse(content={'results': results}) | |
| async def redirect_to_docs(): | |
| return "/docs" | |