Spaces:
Runtime error
Runtime error
Delete fastapi_app.py
Browse files- fastapi_app.py +0 -43
fastapi_app.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
3 |
-
from fastapi.responses import JSONResponse, RedirectResponse
|
4 |
-
from tempfile import NamedTemporaryFile
|
5 |
-
import whisper
|
6 |
-
from typing import List
|
7 |
-
|
8 |
-
# Set a writable cache directory for Whisper models
|
9 |
-
os.makedirs("/app/whisper_cache", exist_ok=True) # Create a writable directory
|
10 |
-
os.environ["XDG_CACHE_HOME"] = "/app/whisper_cache" # Use the new writable directory
|
11 |
-
|
12 |
-
# Checking if NVIDIA GPU is available
|
13 |
-
DEVICE = "cpu"
|
14 |
-
|
15 |
-
# Load the Whisper model
|
16 |
-
model = whisper.load_model("base", device=DEVICE)
|
17 |
-
|
18 |
-
app = FastAPI()
|
19 |
-
|
20 |
-
@app.post("/whisper/")
|
21 |
-
async def handler(files: List[UploadFile] = File(...)):
|
22 |
-
if not files:
|
23 |
-
raise HTTPException(status_code=400, detail="No files were provided")
|
24 |
-
|
25 |
-
results = []
|
26 |
-
|
27 |
-
for file in files:
|
28 |
-
with NamedTemporaryFile(delete=True) as temp:
|
29 |
-
with open(temp.name, "wb") as temp_file:
|
30 |
-
temp_file.write(file.file.read())
|
31 |
-
|
32 |
-
result = model.transcribe(temp.name)
|
33 |
-
|
34 |
-
results.append({
|
35 |
-
'filename': file.filename,
|
36 |
-
'transcript': result['text'],
|
37 |
-
})
|
38 |
-
|
39 |
-
return JSONResponse(content={'results': results})
|
40 |
-
|
41 |
-
@app.get("/", response_class=RedirectResponse)
|
42 |
-
async def redirect_to_docs():
|
43 |
-
return "/docs"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|