Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
-
import asyncio
|
| 4 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from faster_whisper import WhisperModel
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -43,41 +43,38 @@ async def transcribe(websocket: WebSocket):
|
|
| 43 |
# مرحله ۳ — transcription
|
| 44 |
await websocket.send_json({"status": "transcribing"})
|
| 45 |
|
| 46 |
-
|
| 47 |
tmp_path,
|
| 48 |
language="en",
|
| 49 |
word_timestamps=True,
|
| 50 |
vad_filter=True,
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# مرحله ۴ — س
|
| 54 |
await websocket.send_json({"status": "processing"})
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
for segment in segments:
|
| 60 |
-
words = list(segment.words)
|
| 61 |
if not words:
|
| 62 |
continue
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"end": end,
|
| 72 |
-
"text": text,
|
| 73 |
-
"words": [{"word": w.word, "start": w.start, "end": w.end} for w in words]
|
| 74 |
})
|
| 75 |
-
index += 1
|
| 76 |
|
| 77 |
-
# مرحله ۵ —
|
|
|
|
|
|
|
|
|
|
| 78 |
await websocket.send_json({
|
| 79 |
"status": "done",
|
| 80 |
-
"
|
| 81 |
"language": info.language,
|
| 82 |
"duration": info.duration,
|
| 83 |
})
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
|
|
|
| 3 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from faster_whisper import WhisperModel
|
| 6 |
+
from pipeline.postprocess import process
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 43 |
# مرحله ۳ — transcription
|
| 44 |
await websocket.send_json({"status": "transcribing"})
|
| 45 |
|
| 46 |
+
segments_raw, info = model.transcribe(
|
| 47 |
tmp_path,
|
| 48 |
language="en",
|
| 49 |
word_timestamps=True,
|
| 50 |
vad_filter=True,
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# مرحله ۴ — تبدیل به لیست قابل پردازش
|
| 54 |
await websocket.send_json({"status": "processing"})
|
| 55 |
|
| 56 |
+
segments = []
|
| 57 |
+
for seg in segments_raw:
|
| 58 |
+
words = list(seg.words)
|
|
|
|
|
|
|
| 59 |
if not words:
|
| 60 |
continue
|
| 61 |
+
segments.append({
|
| 62 |
+
"text": seg.text.strip(),
|
| 63 |
+
"start": words[0].start,
|
| 64 |
+
"end": words[-1].end,
|
| 65 |
+
"words": [
|
| 66 |
+
{"word": w.word, "start": w.start, "end": w.end}
|
| 67 |
+
for w in words
|
| 68 |
+
],
|
|
|
|
|
|
|
|
|
|
| 69 |
})
|
|
|
|
| 70 |
|
| 71 |
+
# مرحله ۵ — Post-Processing (اون ۲۰٪ طلایی)
|
| 72 |
+
srt_content = process(segments)
|
| 73 |
+
|
| 74 |
+
# مرحله ۶ — ارسال نتیجه
|
| 75 |
await websocket.send_json({
|
| 76 |
"status": "done",
|
| 77 |
+
"srt": srt_content,
|
| 78 |
"language": info.language,
|
| 79 |
"duration": info.duration,
|
| 80 |
})
|