Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,35 @@
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
|
|
|
|
|
|
2 |
import os
|
3 |
import time
|
4 |
-
import tempfile
|
5 |
-
import warnings
|
6 |
-
import soundfile as sf
|
7 |
-
import torch
|
8 |
-
from transformers import pipeline
|
9 |
|
10 |
# Define FastAPI app
|
11 |
app = FastAPI()
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# Basic GET endpoint
|
14 |
@app.get("/")
|
15 |
def read_root():
|
16 |
return {"message": "Welcome to the FastAPI app on Hugging Face Spaces!"}
|
17 |
|
|
|
18 |
@app.post("/transcribe/")
|
19 |
async def transcribe_audio(file: UploadFile = File(...)):
|
20 |
start_time = time.time()
|
21 |
|
22 |
# Save the uploaded file using a temporary file manager
|
23 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
24 |
-
temp_file_path = temp_audio_file.name
|
25 |
temp_audio_file.write(await file.read())
|
|
|
26 |
|
27 |
# Transcribe the audio
|
28 |
transcription_start = time.time()
|
29 |
-
transcription = asr_pipeline(temp_file_path)
|
30 |
transcription_end = time.time()
|
31 |
|
32 |
# Clean up temporary file after use
|
@@ -38,7 +41,7 @@ async def transcribe_audio(file: UploadFile = File(...)):
|
|
38 |
print(f"Total execution time: {end_time - start_time:.4f} seconds")
|
39 |
|
40 |
return {"transcription": transcription['text']}
|
41 |
-
|
42 |
# If running as the main module, start Uvicorn
|
43 |
if __name__ == "__main__":
|
44 |
import uvicorn
|
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
import tempfile
|
5 |
import os
|
6 |
import time
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Define FastAPI app
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Load the Whisper model once during startup
|
12 |
+
device = 0 if torch.cuda.is_available() else -1 # Use GPU if available, otherwise CPU
|
13 |
+
asr_pipeline = pipeline(model="openai/whisper-small", device=device) # Initialize Whisper model
|
14 |
+
|
15 |
# Basic GET endpoint
|
16 |
@app.get("/")
|
17 |
def read_root():
|
18 |
return {"message": "Welcome to the FastAPI app on Hugging Face Spaces!"}
|
19 |
|
20 |
+
# POST endpoint to transcribe audio
|
21 |
@app.post("/transcribe/")
|
22 |
async def transcribe_audio(file: UploadFile = File(...)):
|
23 |
start_time = time.time()
|
24 |
|
25 |
# Save the uploaded file using a temporary file manager
|
26 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
|
|
|
27 |
temp_audio_file.write(await file.read())
|
28 |
+
temp_file_path = temp_audio_file.name
|
29 |
|
30 |
# Transcribe the audio
|
31 |
transcription_start = time.time()
|
32 |
+
transcription = asr_pipeline(temp_file_path) # Call the ASR pipeline
|
33 |
transcription_end = time.time()
|
34 |
|
35 |
# Clean up temporary file after use
|
|
|
41 |
print(f"Total execution time: {end_time - start_time:.4f} seconds")
|
42 |
|
43 |
return {"transcription": transcription['text']}
|
44 |
+
|
45 |
# If running as the main module, start Uvicorn
|
46 |
if __name__ == "__main__":
|
47 |
import uvicorn
|