Inayatgaming commited on
Commit
179bff5
·
verified ·
1 Parent(s): 63d17fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException
2
+ from fastapi.responses import JSONResponse
3
+ from faster_whisper import WhisperModel
4
+ import tempfile
5
+ import os
6
+ import shutil
7
+ import logging
8
+
9
+ logging.basicConfig(level=logging.INFO)
10
+
11
+ app = FastAPI(
12
+ title="Fast Whisper API",
13
+ version="1.0.0"
14
+ )
15
+
16
+ print("Loading Faster Whisper model...")
17
+
18
+ model = WhisperModel(
19
+ "base",
20
+ device="cpu",
21
+ compute_type="int8",
22
+ cpu_threads=4,
23
+ num_workers=2
24
+ )
25
+
26
+ print("Model loaded successfully!")
27
+
28
+ @app.get("/")
29
+ def root():
30
+ return {
31
+ "status": "online",
32
+ "model": "faster-whisper-base",
33
+ "languages": [
34
+ "Hindi",
35
+ "English",
36
+ "Hinglish (Auto Detect)"
37
+ ]
38
+ }
39
+
40
+
41
+ @app.post("/transcribe")
42
+ async def transcribe(file: UploadFile = File(...)):
43
+ if not file.filename:
44
+ raise HTTPException(400, "No file uploaded.")
45
+
46
+ suffix = os.path.splitext(file.filename)[1]
47
+
48
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp:
49
+ shutil.copyfileobj(file.file, temp)
50
+ temp_path = temp.name
51
+
52
+ try:
53
+ segments, info = model.transcribe(
54
+ temp_path,
55
+ beam_size=1,
56
+ vad_filter=True,
57
+ word_timestamps=False
58
+ )
59
+
60
+ text = " ".join(segment.text.strip() for segment in segments).strip()
61
+
62
+ return JSONResponse(
63
+ {
64
+ "success": True,
65
+ "language": info.language,
66
+ "language_probability": round(info.language_probability, 3),
67
+ "text": text
68
+ }
69
+ )
70
+
71
+ except Exception as e:
72
+ logging.exception(e)
73
+ raise HTTPException(500, str(e))
74
+
75
+ finally:
76
+ if os.path.exists(temp_path):
77
+ os.remove(temp_path)