Percy3822 commited on
Commit
d5083c7
·
verified ·
1 Parent(s): 2bf0546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -8
app.py CHANGED
@@ -1,12 +1,59 @@
1
- from fastapi import FastAPI
 
2
  from pydantic import BaseModel
 
3
 
4
- app = FastAPI(title="STT Space (stub)")
 
5
 
6
- class TranscribeIn(BaseModel):
7
- hint: str = "hello"
8
 
9
- @app.post("/transcribe")
10
- def transcribe(inp: TranscribeIn):
11
- # Stub: echo a short transcript. Swap with Whisper later.
12
- return {"text": inp.hint or "hello", "language": "en"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, io, time
2
+ from fastapi import FastAPI, UploadFile, File, HTTPException
3
  from pydantic import BaseModel
4
+ from faster_whisper import WhisperModel
5
 
6
+ MODEL_NAME = os.getenv("FASTER_WHISPER_MODEL", "tiny.en")
7
+ NUM_THREADS = int(os.getenv("NUM_THREADS", "2"))
8
 
9
+ # Load model at startup (CPU, int8)
10
+ model = WhisperModel(MODEL_NAME, device="cpu", compute_type="int8", num_workers=NUM_THREADS)
11
 
12
+ app = FastAPI(title="STT (faster-whisper CPU)")
13
+
14
+ class TranscribeOut(BaseModel):
15
+ text: str
16
+ language: str | None = None
17
+ duration: float | None = None
18
+
19
+ @app.get("/health")
20
+ def health():
21
+ return {"ok": True, "model": MODEL_NAME}
22
+
23
+ @app.post("/transcribe", response_model=TranscribeOut)
24
+ async def transcribe(file: UploadFile = File(...)):
25
+ # Basic validations
26
+ if not file.filename:
27
+ raise HTTPException(status_code=400, detail="No filename")
28
+ if not file.content_type or not file.content_type.startswith("audio/"):
29
+ # Allow unknown types; client may not set correctly
30
+ pass
31
+
32
+ # Read all bytes in memory (small test files)
33
+ try:
34
+ payload = await file.read()
35
+ finally:
36
+ await file.close()
37
+ if not payload:
38
+ raise HTTPException(status_code=400, detail="Empty file")
39
+
40
+ # Run inference
41
+ start = time.time()
42
+ audio_buf = io.BytesIO(payload)
43
+
44
+ try:
45
+ segments, info = model.transcribe(audio_buf, vad_filter=True)
46
+ except Exception as e:
47
+ # Most common: ffmpeg missing (fixed by Dockerfile), or invalid audio
48
+ raise HTTPException(status_code=500, detail=f"Transcription failed: {e}")
49
+
50
+ text_chunks = []
51
+ for seg in segments:
52
+ text_chunks.append(seg.text.strip())
53
+ text = " ".join([t for t in text_chunks if t])
54
+
55
+ return TranscribeOut(
56
+ text=text.strip(),
57
+ language=getattr(info, "language", None),
58
+ duration=getattr(info, "duration", None),
59
+ )