Commit
·
964950d
1
Parent(s):
0cb486a
minor change to temp dir injection
Browse files
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
import shutil, os, logging, uvicorn
|
| 2 |
from typing import Optional
|
| 3 |
from uuid import uuid4
|
|
|
|
| 4 |
|
| 5 |
from utils.transcriber import transcriber
|
| 6 |
from utils.process_video import process_video
|
|
@@ -18,10 +19,10 @@ from pydantic import BaseModel, field_validator
|
|
| 18 |
## THIS IS A BREAKING CHANGE. SRT FILE INPUT DEPRECATED. WIP.
|
| 19 |
## DONE: separate transcriber from subtitler logic. WIP.
|
| 20 |
## DONE: improve loading spinner. WIP (with redirect)
|
|
|
|
| 21 |
## TODO: add transcription preview component + allow for interactive validation of transcription in-browser. WIP
|
| 22 |
## TODO: add word level highlighting option
|
| 23 |
|
| 24 |
-
|
| 25 |
app = FastAPI()
|
| 26 |
security = HTTPBasic()
|
| 27 |
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
|
@@ -60,20 +61,20 @@ async def get_form():
|
|
| 60 |
return HTMLResponse(content=html_content)
|
| 61 |
|
| 62 |
async def get_temp_dir():
|
| 63 |
-
dir =
|
| 64 |
try:
|
| 65 |
-
yield dir
|
| 66 |
except Exception as e:
|
| 67 |
HTTPException(status_code=500, detail=str(e))
|
| 68 |
|
| 69 |
@app.post("/transcribe/")
|
| 70 |
async def transcribe_api(video_file: MP4Video = Depends(),
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
try:
|
| 76 |
-
video_path = os.path.join(temp_dir, video_file.filename)
|
| 77 |
with open(video_path, 'wb') as f:
|
| 78 |
shutil.copyfileobj(video_file.file, f)
|
| 79 |
|
|
@@ -105,21 +106,23 @@ async def process_video_api(video_path: str = Form(...),
|
|
| 105 |
bg_color: Optional[str] = Form("#070a13b3"),
|
| 106 |
text_color: Optional[str] = Form("white"),
|
| 107 |
caption_mode: Optional[str] = Form("desktop"),
|
| 108 |
-
temp_dir:
|
| 109 |
):
|
| 110 |
try:
|
| 111 |
output_path = process_video(video_path, srt_string, fontsize, font, bg_color, text_color, caption_mode)
|
| 112 |
-
with open(os.path.join(temp_dir, f"{video_path.split('.')[0]}.srt"), 'w+') as temp_srt_file:
|
| 113 |
logging.info("Processing the video...")
|
| 114 |
temp_srt_file.write(srt_string)
|
| 115 |
logging.info("Zipping response...")
|
| 116 |
-
with open(os.path.join(temp_dir, f"{video_path.split('.')[0]}.zip"), 'w+b') as temp_zip_file:
|
| 117 |
zip_file = zip_response(temp_zip_file.name, [output_path, temp_srt_file.name])
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
raise HTTPException(status_code=500, detail=str(e))
|
| 125 |
|
|
|
|
| 1 |
+
import shutil, os, logging, uvicorn
|
| 2 |
from typing import Optional
|
| 3 |
from uuid import uuid4
|
| 4 |
+
from tempfile import TemporaryDirectory
|
| 5 |
|
| 6 |
from utils.transcriber import transcriber
|
| 7 |
from utils.process_video import process_video
|
|
|
|
| 19 |
## THIS IS A BREAKING CHANGE. SRT FILE INPUT DEPRECATED. WIP.
|
| 20 |
## DONE: separate transcriber from subtitler logic. WIP.
|
| 21 |
## DONE: improve loading spinner. WIP (with redirect)
|
| 22 |
+
## TODO: fix tempdir cleanup
|
| 23 |
## TODO: add transcription preview component + allow for interactive validation of transcription in-browser. WIP
|
| 24 |
## TODO: add word level highlighting option
|
| 25 |
|
|
|
|
| 26 |
app = FastAPI()
|
| 27 |
security = HTTPBasic()
|
| 28 |
static_dir = os.path.join(os.path.dirname(__file__), 'static')
|
|
|
|
| 61 |
return HTMLResponse(content=html_content)
|
| 62 |
|
| 63 |
async def get_temp_dir():
|
| 64 |
+
dir = TemporaryDirectory(delete=False)
|
| 65 |
try:
|
| 66 |
+
yield dir
|
| 67 |
except Exception as e:
|
| 68 |
HTTPException(status_code=500, detail=str(e))
|
| 69 |
|
| 70 |
@app.post("/transcribe/")
|
| 71 |
async def transcribe_api(video_file: MP4Video = Depends(),
|
| 72 |
+
task: str = Form("transcribe"),
|
| 73 |
+
model_version: str = Form("deepdml/faster-whisper-large-v3-turbo-ct2"),
|
| 74 |
+
max_words_per_line: int = Form(6),
|
| 75 |
+
temp_dir: TemporaryDirectory = Depends(get_temp_dir)):
|
| 76 |
try:
|
| 77 |
+
video_path = os.path.join(temp_dir.name, video_file.filename)
|
| 78 |
with open(video_path, 'wb') as f:
|
| 79 |
shutil.copyfileobj(video_file.file, f)
|
| 80 |
|
|
|
|
| 106 |
bg_color: Optional[str] = Form("#070a13b3"),
|
| 107 |
text_color: Optional[str] = Form("white"),
|
| 108 |
caption_mode: Optional[str] = Form("desktop"),
|
| 109 |
+
temp_dir: TemporaryDirectory = Depends(get_temp_dir)
|
| 110 |
):
|
| 111 |
try:
|
| 112 |
output_path = process_video(video_path, srt_string, fontsize, font, bg_color, text_color, caption_mode)
|
| 113 |
+
with open(os.path.join(temp_dir.name, f"{video_path.split('.')[0]}.srt"), 'w+') as temp_srt_file:
|
| 114 |
logging.info("Processing the video...")
|
| 115 |
temp_srt_file.write(srt_string)
|
| 116 |
logging.info("Zipping response...")
|
| 117 |
+
with open(os.path.join(temp_dir.name, f"{video_path.split('.')[0]}.zip"), 'w+b') as temp_zip_file:
|
| 118 |
zip_file = zip_response(temp_zip_file.name, [output_path, temp_srt_file.name])
|
| 119 |
+
temp_dir.cleanup()
|
| 120 |
+
temp_store = {}
|
| 121 |
+
return Response(
|
| 122 |
+
content = zip_file,
|
| 123 |
+
media_type="application/zip",
|
| 124 |
+
headers={"Content-Disposition": f"attachment; filename={os.path.basename(video_path).split('.')[0]}.zip"}
|
| 125 |
+
)
|
| 126 |
except Exception as e:
|
| 127 |
raise HTTPException(status_code=500, detail=str(e))
|
| 128 |
|