|
from fastapi import FastAPI, UploadFile, File |
|
import soundfile as sf |
|
from faster_whisper import WhisperModel |
|
import os |
|
import tempfile |
|
|
|
|
|
os.environ["HF_HOME"] = "/tmp/hf_cache" |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
print("✅ THIS IS NEW APP.PY VERSION") |
|
model = WhisperModel("ct2_model", compute_type="int8", device="cpu") |
|
|
|
@app.get("/") |
|
def root(): |
|
return { |
|
"message": "CTranslate2 Whisper API is running.", |
|
"usage": "POST /transcribe with an audio file (.wav, .mp3, etc.)" |
|
} |
|
|
|
@app.post("/transcribe") |
|
async def transcribe(file: UploadFile = File(...)): |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file: |
|
temp_path = temp_file.name |
|
temp_file.write(await file.read()) |
|
|
|
try: |
|
|
|
segments, info = model.transcribe(temp_path) |
|
transcription = " ".join([segment.text for segment in segments]) |
|
|
|
return {"text": transcription} |
|
finally: |
|
|
|
os.remove(temp_path) |