Add cookie file upload
This commit is contained in:
39
web_app.py
39
web_app.py
@@ -179,17 +179,35 @@ def _form_to_cli_args(form: dict[str, str | bool]) -> list[str]:
|
||||
|
||||
|
||||
def _stage_uploaded_mp4(uploaded_file: str | None) -> str:
|
||||
return _stage_uploaded_file(uploaded_file, allowed_suffixes={".mp4"}, fallback_name="upload")
|
||||
|
||||
|
||||
def _stage_uploaded_cookies(uploaded_file: str | None) -> str:
|
||||
return _stage_uploaded_file(
|
||||
uploaded_file,
|
||||
allowed_suffixes={".txt", ".cookies", ".cookie"},
|
||||
fallback_name="cookies",
|
||||
)
|
||||
|
||||
|
||||
def _stage_uploaded_file(
|
||||
uploaded_file: str | None,
|
||||
allowed_suffixes: set[str],
|
||||
fallback_name: str,
|
||||
) -> str:
|
||||
if not uploaded_file:
|
||||
return ""
|
||||
|
||||
source_path = Path(uploaded_file)
|
||||
if source_path.suffix.lower() != ".mp4":
|
||||
raise ValueError("Only MP4 uploads are supported.")
|
||||
suffix = source_path.suffix.lower()
|
||||
if suffix not in allowed_suffixes:
|
||||
expected = ", ".join(sorted(allowed_suffixes))
|
||||
raise ValueError(f"Unsupported upload type. Expected one of: {expected}.")
|
||||
if not source_path.exists():
|
||||
raise FileNotFoundError(f"Uploaded file not found: {source_path}")
|
||||
|
||||
safe_stem = "".join(char if char.isalnum() or char in {"-", "_"} else "_" for char in source_path.stem)
|
||||
staged_name = f"{uuid.uuid4().hex[:12]}_{safe_stem or 'upload'}.mp4"
|
||||
staged_name = f"{uuid.uuid4().hex[:12]}_{safe_stem or fallback_name}{suffix}"
|
||||
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||
staged_path = UPLOAD_DIR / staged_name
|
||||
shutil.copy2(source_path, staged_path)
|
||||
@@ -267,7 +285,7 @@ def _start_job(
|
||||
whisper_model: str,
|
||||
mix_mode: str,
|
||||
browser: str,
|
||||
cookies: str,
|
||||
cookies_upload: str | None,
|
||||
lmstudio_base_url: str,
|
||||
lmstudio_api_key: str,
|
||||
lmstudio_model: str,
|
||||
@@ -282,6 +300,11 @@ def _start_job(
|
||||
except (OSError, ValueError) as exc:
|
||||
message = str(exc) or "Invalid uploaded MP4."
|
||||
return "", message, message, gr.update(choices=_output_choices())
|
||||
try:
|
||||
cookies = _stage_uploaded_cookies(cookies_upload)
|
||||
except (OSError, ValueError) as exc:
|
||||
message = str(exc) or "Invalid uploaded cookies file."
|
||||
return "", message, message, gr.update(choices=_output_choices())
|
||||
|
||||
form = {
|
||||
"url": url,
|
||||
@@ -382,7 +405,11 @@ def create_app() -> gr.Blocks:
|
||||
choices=["", "chrome", "edge", "firefox", "brave"],
|
||||
value="",
|
||||
)
|
||||
cookies = gr.Textbox(label="Cookies File", placeholder=r"C:\path\to\cookies.txt")
|
||||
cookies_upload = gr.File(
|
||||
label="Upload Cookies File",
|
||||
file_types=[".txt", ".cookies", ".cookie"],
|
||||
type="filepath",
|
||||
)
|
||||
|
||||
with gr.Accordion("OpenAI-Compatible Settings", open=False):
|
||||
lmstudio_base_url = gr.Textbox(
|
||||
@@ -432,7 +459,7 @@ def create_app() -> gr.Blocks:
|
||||
whisper_model,
|
||||
mix_mode,
|
||||
browser,
|
||||
cookies,
|
||||
cookies_upload,
|
||||
lmstudio_base_url,
|
||||
lmstudio_api_key,
|
||||
lmstudio_model,
|
||||
|
||||
Reference in New Issue
Block a user