Kavin1701 commited on
Commit
15d8556
·
verified ·
1 Parent(s): 363ecca

Update fastapi_app.py

Browse files
Files changed (1) hide show
  1. fastapi_app.py +6 -8
fastapi_app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from fastapi.responses import JSONResponse, RedirectResponse
3
  from tempfile import NamedTemporaryFile
@@ -5,10 +6,13 @@ import whisper
5
  import torch
6
  from typing import List
7
 
 
 
 
8
  # Checking if NVIDIA GPU is available
9
  DEVICE = "cpu"
10
 
11
- # Load the Whisper model:
12
  model = whisper.load_model("base", device=DEVICE)
13
 
14
  app = FastAPI()
@@ -18,20 +22,15 @@ async 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'],
@@ -39,7 +38,6 @@ async def handler(files: List[UploadFile] = File(...)):
39
 
40
  return JSONResponse(content={'results': results})
41
 
42
-
43
  @app.get("/", response_class=RedirectResponse)
44
  async def redirect_to_docs():
45
- return "/docs"
 
1
+ import os
2
  from fastapi import FastAPI, File, UploadFile, HTTPException
3
  from fastapi.responses import JSONResponse, RedirectResponse
4
  from tempfile import NamedTemporaryFile
 
6
  import torch
7
  from typing import List
8
 
9
+ # Set a writable cache directory for Whisper models
10
+ os.environ["XDG_CACHE_HOME"] = "/app/.cache" # Use a 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()
 
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'],
 
38
 
39
  return JSONResponse(content={'results': results})
40
 
 
41
  @app.get("/", response_class=RedirectResponse)
42
  async def redirect_to_docs():
43
+ return "/docs"