-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouting.py
More file actions
69 lines (56 loc) · 2.08 KB
/
Copy pathrouting.py
File metadata and controls
69 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Route uploaded files to the correct processing pipeline."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from extract import (
ALL_INPUT_EXTS,
AUDIO_EXTS,
PDF_EXTS,
TEXT_EXTS,
VIDEO_EXTS,
extract_audio_from_video,
extract_text_from_pdf,
)
from stt import transcribe
from tts import synthesize
def process(
file_path: str | None,
text_input: str | None,
engine: str,
voice: str,
progress: Any = None,
) -> tuple[str | None, str | None, str | None]:
"""Process input and return (text_output, audio_output_path, download_path).
Returns at most one of text_output (for STT) or audio_output_path (for TTS).
download_path is the same file for TTS results.
"""
# Determine source text: from pasted text, file read, or STT
source_text: str | None = text_input.strip() if text_input else None
ext: str | None = None
if file_path:
ext = Path(file_path).suffix.lower()
if ext in AUDIO_EXTS:
if progress:
progress(0.3, desc="Transcribing audio...")
result_text = transcribe(file_path)
return result_text, None, None
if ext in VIDEO_EXTS:
if progress:
progress(0.2, desc="Extracting audio from video...")
audio_path = extract_audio_from_video(file_path)
if progress:
progress(0.5, desc="Transcribing audio...")
result_text = transcribe(audio_path)
return result_text, None, None
if ext in TEXT_EXTS:
source_text = Path(file_path).read_text()
if ext in PDF_EXTS:
if progress:
progress(0.2, desc="Extracting text from PDF...")
source_text = extract_text_from_pdf(file_path)
if not source_text:
return "No input provided. Upload a file or paste text.", None, None
if progress:
progress(0.5, desc=f"Synthesizing speech ({engine})...")
audio_path = synthesize(source_text, engine=engine, voice=voice)
return None, str(audio_path), str(audio_path)