missvector commited on
Commit
45725c3
·
1 Parent(s): da3d07b

fix: use window.location.origin for transcribe endpoint

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -10,10 +10,12 @@ from faster_whisper import WhisperModel
10
  import shutil
11
  import tempfile
12
  from pathlib import Path
 
 
 
13
 
14
  app = FastAPI()
15
 
16
- # CORS
17
  app.add_middleware(
18
  CORSMiddleware,
19
  allow_origins=["*"],
@@ -21,16 +23,16 @@ app.add_middleware(
21
  allow_headers=["*"],
22
  )
23
 
24
- # Static files
25
  app.mount("/static", StaticFiles(directory="static"), name="static")
26
 
27
  print("Loading faster-whisper-tiny...")
28
  model = WhisperModel(
29
  "tiny",
30
  device="cpu",
31
- compute_type="int8" # int8 quantization
32
- )
33
 
 
34
  print("Model ready")
35
 
36
  @app.get("/", response_class=HTMLResponse)
@@ -39,18 +41,21 @@ async def root():
39
  return f.read()
40
 
41
  @app.post("/transcribe")
42
- def transcribe_audio(file: UploadFile = File(...)): # ← no async
43
  try:
44
- # Use the proven sync pattern
45
  suffix = Path(file.filename).suffix
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
47
  shutil.copyfileobj(file.file, tmp)
48
  tmp_path = tmp.name
49
 
50
- result = model.transcribe(tmp_path, language="ru")
51
- text = result["text"].strip()
 
 
 
52
 
53
- Path(tmp_path).unlink() # delete
 
54
 
55
  return JSONResponse({"text": text, "status": "ok"})
56
 
 
10
  import shutil
11
  import tempfile
12
  from pathlib import Path
13
+ import asyncio
14
+ from concurrent.futures import ThreadPoolExecutor
15
+ from functools import partial
16
 
17
  app = FastAPI()
18
 
 
19
  app.add_middleware(
20
  CORSMiddleware,
21
  allow_origins=["*"],
 
23
  allow_headers=["*"],
24
  )
25
 
 
26
  app.mount("/static", StaticFiles(directory="static"), name="static")
27
 
28
  print("Loading faster-whisper-tiny...")
29
  model = WhisperModel(
30
  "tiny",
31
  device="cpu",
32
+ compute_type="int8"
33
+ )
34
 
35
+ executor = ThreadPoolExecutor(max_workers=1)
36
  print("Model ready")
37
 
38
  @app.get("/", response_class=HTMLResponse)
 
41
  return f.read()
42
 
43
  @app.post("/transcribe")
44
+ async def transcribe_audio(file: UploadFile = File(...)):
45
  try:
 
46
  suffix = Path(file.filename).suffix
47
  with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
48
  shutil.copyfileobj(file.file, tmp)
49
  tmp_path = tmp.name
50
 
51
+ loop = asyncio.get_event_loop()
52
+ result = await loop.run_in_executor(
53
+ executor,
54
+ partial(model.transcribe, tmp_path, language="ru")
55
+ )
56
 
57
+ text = result["text"].strip()
58
+ Path(tmp_path).unlink()
59
 
60
  return JSONResponse({"text": text, "status": "ok"})
61