Charan5775 commited on
Commit
7049779
·
verified ·
1 Parent(s): 34c991f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from pywhispercpp.model import Model
3
+ import uvicorn
4
+ import tempfile
5
+ import os
6
+ from time import time
7
+
8
+ app = FastAPI(title="pyWhisperCPP API")
9
+
10
+ # Load model ONCE at startup
11
+ #MODEL_PATH = "ggml-tiny.en.bin"
12
+ #model = Model(MODEL_PATH)
13
+ model = Model('base.en')
14
+
15
+ @app.get("/")
16
+ def root():
17
+ return {"status": "Whisper.cpp API is running!"}
18
+
19
+ @app.post("/transcribe")
20
+ async def transcribe(file: UploadFile = File(...)):
21
+ # Save uploaded audio
22
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp:
23
+ temp.write(await file.read())
24
+ temp.flush()
25
+ audio_path = temp.name
26
+
27
+ start = time()
28
+ segments = model.transcribe(audio_path)
29
+ text = " ".join([seg.text for seg in segments])
30
+ elapsed = round(time() - start, 3)
31
+
32
+ os.remove(audio_path)
33
+
34
+ return {
35
+ "text": text,
36
+ "processing_time_seconds": elapsed
37
+ }
38
+
39
+ if __name__ == "__main__":
40
+ uvicorn.run(app, host="0.0.0.0", port=7860)