|
from fastapi import FastAPI, UploadFile, File |
|
import os |
|
import time |
|
import tempfile |
|
import warnings |
|
import soundfile as sf |
|
import torch |
|
from transformers import pipeline |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get("/") |
|
def read_root(): |
|
return {"message": "Welcome to the FastAPI app on Hugging Face Spaces!"} |
|
|
|
@app.post("/transcribe/") |
|
async def transcribe_audio(file: UploadFile = File(...)): |
|
start_time = time.time() |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file: |
|
temp_file_path = temp_audio_file.name |
|
temp_audio_file.write(await file.read()) |
|
|
|
|
|
transcription_start = time.time() |
|
transcription = asr_pipeline(temp_file_path) |
|
transcription_end = time.time() |
|
|
|
|
|
os.remove(temp_file_path) |
|
|
|
|
|
end_time = time.time() |
|
print(f"Time to transcribe audio: {transcription_end - transcription_start:.4f} seconds") |
|
print(f"Total execution time: {end_time - start_time:.4f} seconds") |
|
|
|
return {"transcription": transcription['text']} |
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|