Anchal23 commited on
Commit
d4dfe92
·
verified ·
1 Parent(s): bd080a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -6,6 +6,18 @@ import tempfile
6
  import shutil
7
  from typing import Optional
8
  import uvicorn
 
 
 
 
 
 
 
 
 
 
 
 
9
  from transformers import pipeline
10
 
11
  app = FastAPI(
@@ -14,14 +26,9 @@ app = FastAPI(
14
  version="1.0.0"
15
  )
16
 
17
- import os
18
- # Set cache directory to a writable location
19
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface_cache'
20
- os.makedirs('/tmp/huggingface_cache', exist_ok=True)
21
-
22
- # Then initialize pipeline as before
23
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small")
24
-
25
 
26
  @app.get("/")
27
  def read_root():
@@ -45,7 +52,7 @@ async def transcribe_audio(file: UploadFile = File(...),
45
  raise HTTPException(status_code=400, detail="Unsupported file format. Please upload WAV, MP3, M4A, or OGG file.")
46
 
47
  # Save the uploaded file temporarily
48
- with tempfile.NamedTemporaryFile(delete=False) as temp_file:
49
  shutil.copyfileobj(file.file, temp_file)
50
  temp_path = temp_file.name
51
 
 
6
  import shutil
7
  from typing import Optional
8
  import uvicorn
9
+
10
+ # Set environment variables for cache directories
11
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
12
+ os.environ["HF_HOME"] = "/tmp/hf_home"
13
+ os.environ["TORCH_HOME"] = "/tmp/torch_home"
14
+
15
+ # Create directories with appropriate permissions
16
+ os.makedirs("/tmp/transformers_cache", exist_ok=True)
17
+ os.makedirs("/tmp/hf_home", exist_ok=True)
18
+ os.makedirs("/tmp/torch_home", exist_ok=True)
19
+
20
+ # Import transformers after setting environment variables
21
  from transformers import pipeline
22
 
23
  app = FastAPI(
 
26
  version="1.0.0"
27
  )
28
 
29
+ # Initialize the Whisper model pipeline
30
+ # Using whisper-base for better compatibility with Spaces
31
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
 
 
 
 
 
32
 
33
  @app.get("/")
34
  def read_root():
 
52
  raise HTTPException(status_code=400, detail="Unsupported file format. Please upload WAV, MP3, M4A, or OGG file.")
53
 
54
  # Save the uploaded file temporarily
55
+ with tempfile.NamedTemporaryFile(delete=False, dir="/tmp") as temp_file:
56
  shutil.copyfileobj(file.file, temp_file)
57
  temp_path = temp_file.name
58