CoolFace
Apppublic

uatjonas/speech2text

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
app.py28 linesDownload Raw Back to root
1from fastapi import FastAPI, UploadFile2import torch3import librosa4from transformers import AutoModelForSpeechSeq2Seq, WhisperProcessor5 6app = FastAPI()7 8MODEL_NAME = "openai/whisper-large-v3"  # use open model9device = "cpu"  # Hugging Face free tier is CPU only10dtype = torch.float3211 12processor = WhisperProcessor.from_pretrained(MODEL_NAME)13model = AutoModelForSpeechSeq2Seq.from_pretrained(14    MODEL_NAME,15    dtype=dtype,16    low_cpu_mem_usage=True,17    use_safetensors=True18).to(device)19 20@app.post("/transcribe")21async def transcribe(file: UploadFile):22    y, sr = librosa.load(file.file, sr=16000)23    processed = processor(y, sampling_rate=sr, return_tensors="pt")24    input_features = processed.input_features.to(device).to(dtype)25    gout = model.generate(input_features=input_features)26    transcription = processor.batch_decode(gout, skip_special_tokens=True)[0]27    return {"text": transcription}28