Skip to content

Instantly share code, notes, and snippets.

@xurenlu
Created December 5, 2023 06:12
Show Gist options
  • Save xurenlu/bb07f7dcfe8990c25fe00fb1333841bf to your computer and use it in GitHub Desktop.
Save xurenlu/bb07f7dcfe8990c25fe00fb1333841bf to your computer and use it in GitHub Desktop.
from faster_whisper import WhisperModel
from fastapi import FastAPI
import requests
import os
import time
model_size = "large-v2"
# Run on GPU with FP16
model = WhisperModel(model_size, device="cuda", compute_type="float16")
app = FastAPI()
def handle(mp3):
start_time = time.time()
segments, info = model.transcribe(mp3, beam_size=1)
end_time = time.time()
target=[]
for segment in segments:
target.append({"start":segment.start,"end":segment.end,"text":segment.text})
return {"start_at":start_time,"result":target,"end_at":end_time,"cost":(end_time-start_time)}
@app.get("/api/v3/trans")
async def download_mp3(url: str):
r = requests.get(url, allow_redirects=True)
mp3_file_path = "/tmp/" + url.split("/")[-1]
with open(mp3_file_path, 'wb') as f:
f.write(r.content)
result = handle(mp3_file_path)
return {"status": "success", "message": "mp3 file processed successfully","data":result}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment