diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index affbeabc1e..bcaaf62bf7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,11 +51,11 @@ jobs: run: uv sync --frozen --python ${{ matrix.python-version }} - name: Compile Python sources - run: uv run --no-sync python -m compileall app cli.py main.py webui test docs/skill + run: uv run --no-sync python -m compileall app cli.py main.py webui local_ai test docs/skill - name: Lint Python sources if: matrix.python-version == '3.11' - run: uv run --no-sync ruff check app cli.py main.py webui test docs/skill + run: uv run --no-sync ruff check app cli.py main.py webui local_ai test docs/skill - name: Run unit tests with coverage # pytest 兼容现有 unittest.TestCase,同时会收集仓库中的 pytest 风格 @@ -109,7 +109,7 @@ jobs: run: uv sync --frozen --python 3.11 - name: Compile Python sources - run: uv run --no-sync python -m compileall app cli.py main.py webui test docs/skill + run: uv run --no-sync python -m compileall app cli.py main.py webui local_ai test docs/skill - name: Run core service smoke tests run: >- diff --git a/app/services/local_ai.py b/app/services/local_ai.py new file mode 100644 index 0000000000..d13a0d2a3d --- /dev/null +++ b/app/services/local_ai.py @@ -0,0 +1,370 @@ +"""本机 AI 生图:检测硬件、选择模型、安装运行环境、下载模型并管理 GPU 服务进程。 + +GPU 服务(local_ai.server)运行在独立虚拟环境中,主程序不依赖 torch。主程序 +只负责安装该环境、启动进程并通过 OpenAI 兼容接口请求图片;模型加载、显存 +与 Ollama 的让渡都在服务进程内完成。 +""" + +import os +import signal +import subprocess +import sys +import threading +import time +from pathlib import Path + +import requests +from loguru import logger + +import local_ai +from app.config import config +from app.utils import utils +from local_ai import catalog, hardware, models, runtime + +DEFAULT_BASE_URL = "http://127.0.0.1:7862" +# 首次请求要把模型从磁盘读入显存,大模型可能需要数分钟。 +IMAGE_REQUEST_TIMEOUT = (5, 900) +# 一个片段在 16 GB 显卡上需要数分钟,首次还要加载约 20 GB 的模型。 +VIDEO_REQUEST_TIMEOUT = (5, 3600) +SERVER_START_TIMEOUT_SECONDS = 120 +_HARDWARE_CACHE_SECONDS = 60 + +_lock = threading.Lock() +_hardware_cache: tuple[float, hardware.Hardware] | None = None +_install_job: dict | None = None +_download_jobs: dict[str, models.DownloadJob] = {} +_server_process: subprocess.Popen | None = None + + +class LocalAIError(RuntimeError): + pass + + +def _setting(key: str, default=""): + return config.app.get(key, default) + + +def base_url() -> str: + return str(_setting("local_ai_url", "") or DEFAULT_BASE_URL).strip().rstrip("/") + + +def local_ai_dir() -> str: + return utils.storage_dir("local_ai", create=True) + + +def models_dir() -> str: + configured = str(_setting("local_ai_models_dir", "") or "").strip() + return configured or os.path.join(local_ai_dir(), "models") + + +def runtime_dir() -> str: + configured = str(_setting("local_ai_runtime_dir", "") or "").strip() + return configured or os.path.join(local_ai_dir(), "runtime") + + +def hf_token() -> str: + return str(_setting("local_ai_hf_api_token", "") or os.getenv("HF_TOKEN", "")).strip() + + +def install_log_path() -> str: + return os.path.join(local_ai_dir(), "install.log") + + +def server_log_path() -> str: + return os.path.join(local_ai_dir(), "server.log") + + +def detect_hardware(refresh: bool = False) -> hardware.Hardware: + """检测结果缓存一分钟:nvidia-smi 每次约耗时数百毫秒,页面每次 rerun 都会读取。""" + global _hardware_cache + with _lock: + if not refresh and _hardware_cache and time.time() - _hardware_cache[0] < _HARDWARE_CACHE_SECONDS: + return _hardware_cache[1] + detected = hardware.detect() + with _lock: + _hardware_cache = (time.time(), detected) + return detected + + +def gpu_hardware() -> hardware.Hardware: + """只保留服务实际使用的那块 GPU,模型适配判断与服务进程保持一致。""" + detected = detect_hardware() + index = str(_setting("local_ai_gpu_index", "") or "").strip() + if index.isdigit() and any(gpu.index == int(index) for gpu in detected.gpus): + detected = hardware.Hardware(**{**detected.__dict__, "gpus": [ + gpu for gpu in detected.gpus if gpu.index == int(index) + ]}) + return detected + + +def _selected_model(kind: str) -> catalog.Model | None: + """用户选定的模型;未选择或已不在目录中时使用本机推荐模型。""" + selected = str(_setting(f"local_ai_{kind}_model", "") or "").strip() + if selected: + model = catalog.get(selected) + if model is not None and model.kind == kind: + return model + recommended = catalog.recommend(gpu_hardware(), kind) + return recommended.model if recommended else None + + +def image_model() -> catalog.Model | None: + return _selected_model("image") + + +def video_model() -> catalog.Model | None: + return _selected_model("video") + + +def model_installed(model: catalog.Model) -> bool: + return models.installed_path(model, models_dir()) is not None + + +def is_runtime_installed() -> bool: + return runtime.is_installed(runtime_dir()) + + +def readiness(kind: str = "image") -> tuple[bool, str]: + """生成前的最小检查:运行环境已安装且所选模型已下载完整。 + + 图生视频模型从每个画面的图片开始动画,因此视频还要求生图模型可用。 + """ + if not is_runtime_installed(): + return False, "local_ai.runtime_missing" + needed = [image_model()] + if kind == "video": + clip_model = video_model() + needed = [clip_model] + (needed if clip_model and clip_model.image_to_video else []) + for model in needed: + if model is None: + return False, "local_ai.no_model_fits" + if not model_installed(model): + return False, "local_ai.model_missing" + return True, "" + + +# ---------------------------------------------------------------- 后台任务 + + +def install_status() -> dict | None: + with _lock: + return dict(_install_job) if _install_job else None + + +def start_runtime_install() -> dict: + """在后台线程安装 GPU 服务环境;已在安装时直接返回当前状态。""" + global _install_job + with _lock: + if _install_job and _install_job["state"] == "running": + return dict(_install_job) + _install_job = {"state": "running", "error": "", "started_at": time.time()} + detected = detect_hardware(refresh=True) + + def run(): + global _install_job + try: + runtime.install(runtime_dir(), detected, install_log_path()) + result = {"state": "done", "error": ""} + except Exception as error: + logger.error(f"local AI runtime install failed: {error}") + result = {"state": "failed", "error": str(error)} + with _lock: + _install_job = {**_install_job, **result} + + threading.Thread(target=run, name="local-ai-install", daemon=True).start() + return install_status() + + +def download_status(model_id: str) -> dict | None: + job = _download_jobs.get(model_id) + return job.to_dict() if job else None + + +def start_download(model_id: str) -> dict: + model = catalog.get(model_id) + if model is None: + raise LocalAIError(f"unknown model {model_id!r}") + with _lock: + job = _download_jobs.get(model_id) + if job is None or not job.active: + job = models.DownloadJob(model, models_dir(), hf_token() or None).start() + _download_jobs[model_id] = job + return job.to_dict() + + +def delete_model(model_id: str) -> None: + model = catalog.get(model_id) + if model is None: + return + job = _download_jobs.get(model_id) + if job is not None and job.active: + raise LocalAIError("model is still downloading") + stop_server() # 服务可能正持有该模型的文件句柄 + models.delete(model, models_dir()) + _download_jobs.pop(model_id, None) + + +# ---------------------------------------------------------------- GPU 服务进程 + + +def _session() -> requests.Session: + # 本机服务不能走用户为外网配置的代理。 + session = requests.Session() + session.trust_env = False + return session + + +def server_status() -> dict | None: + try: + with _session() as session: + response = session.get(f"{base_url()}/health", timeout=2) + return response.json() if response.ok else None + except (requests.RequestException, ValueError): + return None + + +def start_server() -> None: + global _server_process + if not is_runtime_installed(): + raise LocalAIError( + "the local AI runtime is not installed: open Settings > AI Image > Local AI " + "and click Install" + ) + model = image_model() + target = gpu_hardware().best_gpu + env = dict(os.environ) + package_root = str(Path(local_ai.__file__).resolve().parent.parent) + env["PYTHONPATH"] = package_root + os.pathsep + env.get("PYTHONPATH", "") + env["LOCAL_AI_MODELS_DIR"] = models_dir() + env["LOCAL_AI_PORT"] = str(requests.utils.urlparse(base_url()).port or 7862) + env["PYTHONUNBUFFERED"] = "1" + if model is not None: + env["LOCAL_AI_IMAGE_MODEL"] = model.id + clip_model = video_model() + if clip_model is not None: + env["LOCAL_AI_VIDEO_MODEL"] = clip_model.id + if target is not None and target.vendor == "nvidia": + # 只让服务看到选定的显卡,模型不会被拆到两块卡上。 + env["CUDA_VISIBLE_DEVICES"] = str(target.index) + env["LOCAL_AI_GPU_INDEX"] = str(target.index) + with _lock: + if _server_process is not None and _server_process.poll() is None: + return + log = open(server_log_path(), "a", encoding="utf-8", errors="replace") + creationflags = subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0 + _server_process = subprocess.Popen( + [runtime.venv_python(runtime_dir()), "-m", "local_ai.server"], + cwd=package_root, + env=env, + stdout=log, + stderr=subprocess.STDOUT, + creationflags=creationflags, + ) + log.close() + logger.info(f"started local AI server: pid={_server_process.pid}, url={base_url()}") + + +def stop_server() -> None: + """停止服务进程,包括上次运行 WebUI 时启动、仍在后台运行的服务。 + + Windows 上虚拟环境的 python.exe 是启动器,真正的解释器是它的子进程, + 只结束 Popen 句柄会留下占用显存的服务,因此按服务自报的 pid 结束。 + """ + global _server_process + status = server_status() + with _lock: + process, _server_process = _server_process, None + server_pid = status.get("pid") if status else None + if isinstance(server_pid, int) and server_pid != os.getpid(): + try: + os.kill(server_pid, signal.SIGTERM) + except OSError: + pass + if process is not None and process.poll() is None: + process.terminate() + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + process.kill() + + +def ensure_server(timeout: float = SERVER_START_TIMEOUT_SECONDS) -> dict: + """服务未运行时按配置自动启动,并等待健康检查通过。""" + status = server_status() + if status: + return status + if not bool(_setting("local_ai_autostart", True)): + raise LocalAIError(f"the local AI server is not running at {base_url()}") + start_server() + deadline = time.time() + timeout + while time.time() < deadline: + if _server_process is not None and _server_process.poll() is not None: + raise LocalAIError(f"local AI server exited, see {server_log_path()}") + status = server_status() + if status: + return status + time.sleep(1) + raise LocalAIError(f"local AI server did not start within {timeout:g}s, see {server_log_path()}") + + +def generate_image(prompt: str, size: str) -> bytes: + """请求一张图片,返回 PNG 字节。服务未运行时自动启动。""" + import base64 + + model = image_model() + if model is None: + raise LocalAIError("no local image model fits this computer's GPU or memory") + ensure_server() + payload = {"model": model.id, "prompt": prompt, "n": 1, "size": size} + try: + with _session() as session: + response = session.post( + f"{base_url()}/v1/images/generations", + json=payload, + timeout=IMAGE_REQUEST_TIMEOUT, + ) + except requests.RequestException as error: + raise LocalAIError(f"local AI server request failed: {error}") from error + if not response.ok: + try: + detail = response.json().get("detail", response.text) + except ValueError: + detail = response.text + raise LocalAIError(f"local AI server returned {response.status_code}: {detail}") + try: + return base64.b64decode(response.json()["data"][0]["b64_json"]) + except (ValueError, KeyError, IndexError, TypeError) as error: + raise LocalAIError("local AI server returned no image") from error + + +def generate_video(prompt: str, image: bytes | None, size: str) -> tuple[bytes, float]: + """请求一个片段,返回 MP4 字节与片段时长(秒)。服务未运行时自动启动。""" + import base64 + + model = video_model() + if model is None: + raise LocalAIError("no local video model fits this computer's GPU or memory") + ensure_server() + payload = {"model": model.id, "prompt": prompt, "size": size} + if image is not None: + payload["image"] = base64.b64encode(image).decode() + try: + with _session() as session: + response = session.post( + f"{base_url()}/v1/videos/generations", + json=payload, + timeout=VIDEO_REQUEST_TIMEOUT, + ) + except requests.RequestException as error: + raise LocalAIError(f"local AI server request failed: {error}") from error + if not response.ok: + try: + detail = response.json().get("detail", response.text) + except ValueError: + detail = response.text + raise LocalAIError(f"local AI server returned {response.status_code}: {detail}") + try: + clip = response.json()["data"][0] + return base64.b64decode(clip["b64_mp4"]), float(clip["seconds"]) + except (ValueError, KeyError, IndexError, TypeError) as error: + raise LocalAIError("local AI server returned no clip") from error diff --git a/app/services/material.py b/app/services/material.py index ef1928f5b4..86aa85c3ea 100644 --- a/app/services/material.py +++ b/app/services/material.py @@ -28,6 +28,7 @@ generation_progress, infistar_video, jalapeno_video, + local_ai, material_cache, material_upload, metaso_minimax, @@ -84,6 +85,10 @@ def __init__(self, error): super().__init__(messages.get(self.key, str(error))) +class LocalImageMaterialError(OpenAIImagePaidResultError): + """本机生图失败:通常是服务无法启动或模型缺失,后续关键词同样会失败,因此终止任务。""" + + def _safe_public_url(value: Any) -> str | None: """ 只保留可公开展示的 HTTP(S) 页面地址,并移除查询参数和凭据。 @@ -1906,6 +1911,132 @@ def generate_images_codex( return [item] +def generate_images_local( + search_term: str, + minimum_duration: int, + video_aspect: VideoAspect = VideoAspect.portrait, + save_dir: str = "", +) -> List[MaterialInfo]: + """用本机 GPU 服务为一个脚本关键词生成一张图片,素材结构与 OpenAI 兼容生图一致。""" + aspect = VideoAspect(video_aspect) + if minimum_duration <= 0: + return [] + generation_progress.submitting("local_image") + try: + # 服务端会追加画风与负面提示词,这里只传关键词本身。 + image_bytes = local_ai.generate_image( + search_term, + OPENAI_IMAGE_DEFAULT_SIZES.get(aspect, "1024x1024"), + ) + image_path, width, height = _save_openai_image_file(image_bytes, save_dir) + except local_ai.LocalAIError as error: + raise LocalImageMaterialError(str(error)) from None + except _OpenAIImageDecodeError: + raise LocalImageMaterialError("the local AI server returned an invalid image") from None + item = MaterialInfo() + item.provider = "local_image" + item.url = image_path + item.duration = max(int(minimum_duration), 1) + item.source_info = { + "provider": "local_image", + "search_term": search_term, + "rendition": {"id": None, "width": width, "height": height}, + } + return [item] + + +def _download_videos_local_video_on_demand( + *, + task_id: str, + search_terms: List[str], + video_aspect: VideoAspect, + audio_duration: float, + max_clip_duration: int, + material_directory: str, + image_motion: ImageMotion = ImageMotion.zoom, +) -> List[str]: + """ + 用本机视频模型为每个脚本片段生成一个短片段,凑够所需时长即停止。 + + 图生视频模型先用本机生图模型画出画面,再把图片做成动画,画面与文案 + 一致且风格统一。先画完所需的全部画面再逐个生成动画:显卡一次只放得下 + 一个模型,交替生成会让每个画面都重新加载两个大模型。单个片段生成失败 + (如显存不足)时退回为该图片的缩放片段,不让一个画面拖垮整条视频; + 没有图片可退回时终止任务。 + """ + image_motion = ImageMotion(image_motion) + material_directory = material_directory or utils.task_dir(task_id) + try: + required_duration = float(audio_duration) + except (TypeError, ValueError): + required_duration = 0.0 + if required_duration <= 0: + return [] + + aspect = VideoAspect(video_aspect) + size = OPENAI_IMAGE_DEFAULT_SIZES.get(aspect, "1024x1024") + clip_model = local_ai.video_model() + animates_images = bool(clip_model and clip_model.image_to_video) + video_paths: List[str] = [] + material_sources: list[dict[str, Any]] = [] + total_duration = 0.0 + clip_seconds = ( + clip_model.frames / clip_model.fps if clip_model and clip_model.fps else max_clip_duration + ) + scene_count = math.ceil(required_duration / min(max_clip_duration, clip_seconds)) + scenes = search_terms[:scene_count] + try: + image_items = [ + generate_images_local(term, max_clip_duration, aspect, save_dir=material_directory)[0] + if animates_images + else None + for term in scenes + ] + for search_term, image_item in zip(scenes, image_items): + image_bytes = Path(image_item.url).read_bytes() if image_item else None + generation_progress.submitting("local_video") + try: + clip_bytes, clip_seconds = local_ai.generate_video(search_term, image_bytes, size) + except local_ai.LocalAIError as error: + if image_item is None: + raise LocalImageMaterialError(str(error)) from None + logger.warning( + "local video clip failed, using the scene image instead: " + f"term={search_term!r}, error={error}" + ) + video_file = _render_openai_image_video( + image_item.url, max_clip_duration, image_motion=image_motion + ) + if not video_file: + raise LocalImageMaterialError( + "the scene image could not be rendered locally" + ) from None + clip_seconds = float(max_clip_duration) + else: + video_file = os.path.join( + material_directory, f"local-video-{uuid.uuid4().hex[:12]}.mp4" + ) + with open(video_file, "wb") as clip_file: + clip_file.write(clip_bytes) + item = MaterialInfo() + item.provider = "local_video" + item.url = video_file + item.duration = max(1, int(round(clip_seconds))) + item.source_info = {"provider": "local_video", "search_term": search_term} + video_paths.append(video_file) + material_sources.append(_material_source_record(item, video_file)) + total_duration += min(max_clip_duration, clip_seconds) + logger.info( + f"local video clip ready: {video_file}, " + f"covered={total_duration:.1f}s/{required_duration:.1f}s" + ) + if total_duration >= required_duration: + break + finally: + _persist_material_sources(task_id, material_sources) + return video_paths + + def _render_openai_image_video( image_path: str, clip_duration: int, image_motion: ImageMotion = ImageMotion.zoom ) -> str: @@ -2473,11 +2604,23 @@ def search_videos( max_clip_duration=max_clip_duration, material_directory=material_directory, ) - if source in {"openai_image", "codex_image"}: + if source == "local_video": + return _download_videos_local_video_on_demand( + task_id=task_id, + search_terms=search_terms, + video_aspect=video_aspect, + audio_duration=audio_duration, + max_clip_duration=max_clip_duration, + material_directory=material_directory, + image_motion=image_motion, + ) + if source in {"openai_image", "codex_image", "local_image"}: # 与 WaveSpeed 相同的按需付费语义:文生图按张计费,逐段生成、凑够 # 所需时长立即停止。生成结果是一次性的本地图片文件,也不参与 24 # 小时搜索缓存——缓存会让不同任务反复拿到同一张图。 image_options = {} + if source == "local_image": + image_options = {"image_generator": generate_images_local, "provider": source} if source == "codex_image": image_options = { "image_generator": generate_images_codex, diff --git a/app/services/task.py b/app/services/task.py index 46b7854fbb..73d62715d4 100644 --- a/app/services/task.py +++ b/app/services/task.py @@ -25,6 +25,7 @@ infistar_video, jalapeno_video, llm, + local_ai, loomloom, material, metaso_minimax, @@ -102,7 +103,8 @@ _SUPPORTED_VIDEO_SOURCES = frozenset({ "pexels", "pixabay", "coverr", "local", "wavespeed", "volcengine_seedance", "ofox", "metaso_minimax", "muapi", - "loomloom", "openai_image", "codex_image", "infistar", "jalapeno", + "loomloom", "openai_image", "codex_image", "local_image", "local_video", + "infistar", "jalapeno", }) @@ -1286,6 +1288,17 @@ def recover_interrupted_cross_posts(page_size: int = 100) -> int | None: return recovered +def _social_post_text(metadata: dict) -> str: + """短视频平台的发布文案:正文后接话题标签,标签不在正文里时才会出现在帖子中。""" + caption = str(metadata.get("caption") or metadata.get("title") or "").strip() + hashtags = " ".join( + tag if tag.startswith("#") else f"#{tag}" + for tag in (str(item).strip() for item in metadata.get("hashtags") or []) + if tag and tag.lstrip("#") and tag not in caption + ) + return f"{caption}\n\n{hashtags}".strip() if hashtags else caption + + def _run_cross_post( task_id: str, video_paths: tuple[str, ...], @@ -1296,6 +1309,7 @@ def _run_cross_post( youtube_privacy_status: str, youtube_made_for_kids: bool = False, upload_account: dict | None = None, + schedule: dict | None = None, ) -> None: """后台执行跨平台发布,并只补充发布相关的任务字段。""" results = [] @@ -1322,19 +1336,37 @@ def _run_cross_post( f"cross-post started, task_id: {task_id}, platforms: {', '.join(platforms)}" ) youtube_extra = None + platform_titles = {} post_title = video_subject or "Check out this video! #shorts #viral" if platforms: + # 每个平台单独生成文案:长度上限、话题数量和语气各不相同, + # 同一段文字直接复用会在某些平台被截断或缺少话题标签。 + metadata_by_social = {} + + def social_metadata(social_platform: str) -> dict: + if social_platform not in metadata_by_social: + metadata_by_social[social_platform] = llm.generate_social_metadata( + video_subject=video_subject, + video_script=video_script, + language=video_language or "", + platform=social_platform, + ) + return metadata_by_social[social_platform] + has_youtube = any(platform.startswith("youtube") for platform in platforms) - social_platform = "youtube_shorts" - if not has_youtube: - first = (platforms[0] or "").strip().lower() + for platform in platforms: + key = (platform or "").strip().lower() + if key.startswith("youtube"): + continue # llm.py resolves unknown ids to its default platform. - social_platform = _CROSS_POST_SOCIAL_PLATFORMS.get(first, first) - metadata = llm.generate_social_metadata( - video_subject=video_subject, - video_script=video_script, - language=video_language or "", - platform=social_platform, + platform_metadata = social_metadata(_CROSS_POST_SOCIAL_PLATFORMS.get(key, key)) + platform_titles[key] = _social_post_text(platform_metadata) + metadata = social_metadata( + "youtube_shorts" + if has_youtube + else _CROSS_POST_SOCIAL_PLATFORMS.get( + (platforms[0] or "").strip().lower(), (platforms[0] or "").strip().lower() + ) ) if has_youtube: youtube_extra = { @@ -1378,6 +1410,10 @@ def record_background_request(request_id: str) -> None: upload_kwargs = {} if upload_account is not None: upload_kwargs["account"] = upload_account + if platform_titles: + upload_kwargs["platform_titles"] = platform_titles + if schedule: + upload_kwargs["schedule"] = schedule result = upload_post.cross_post_video( video_path=video_path, title=post_title, @@ -1526,6 +1562,8 @@ def _schedule_cross_post( youtube_privacy_status, youtube_made_for_kids, upload_post.upload_post_service.snapshot_account(), + # 延迟发布从排队时刻起算,并固定当时的发布方式。 + upload_post.upload_post_service.schedule_request(), ) _register_cross_post_future(task_id, future) future.add_done_callback(partial(_finalize_cross_post_future, task_id)) @@ -1671,6 +1709,27 @@ def _run_pipeline( "optional for local gateways that need no auth)", ) + if stop_at in {"materials", "video"} and params.video_source in { + "local_image", + "local_video", + }: + ready, reason = local_ai.readiness( + "video" if params.video_source == "local_video" else "image" + ) + if not ready: + return _mark_task_failed( + task_id, + "preflight", + { + "local_ai.runtime_missing": "Local AI needs its runtime: install it " + "in Settings > AI Image > Local AI", + "local_ai.model_missing": "A selected local model is not downloaded: " + "download it in Settings > AI Image > Local AI", + "local_ai.no_model_fits": "No local model fits this computer's GPU " + "or memory", + }.get(reason, reason), + ) + # 只有完整成片流程需要视频配乐供应商。尽早阻止缺少 Key 的完整任务,避免 # 先消耗 LLM、TTS 和素材服务额度;中间产物接口仍可独立使用。 video_music_provider = _VIDEO_MUSIC_PROVIDERS.get(params.bgm_type) diff --git a/app/services/upload_post.py b/app/services/upload_post.py index 7e9cfc591b..da1c93e4ed 100644 --- a/app/services/upload_post.py +++ b/app/services/upload_post.py @@ -5,6 +5,7 @@ """ import os import time +from datetime import datetime, timedelta, timezone from typing import Callable, Optional from uuid import uuid4 @@ -18,6 +19,17 @@ _UPLOAD_STATUS_POLL_INTERVAL_SECONDS = 10 _UPLOAD_STATUS_TIMEOUT_SECONDS = 3600 _MAX_CONSECUTIVE_STATUS_ERRORS = 3 +# Upload-Post 按平台读取的发布文案字段;缺失时回退到通用 title。 +# https://docs.upload-post.com/api/upload-video +_PLATFORM_TITLE_FIELDS = {"tiktok": "tiktok_title", "instagram": "instagram_title"} +_PLATFORM_TEXT_LIMIT = 2200 +SCHEDULE_NOW = "now" +SCHEDULE_QUEUE = "queue" +SCHEDULE_DELAY = "delay" +SCHEDULE_MODES = (SCHEDULE_NOW, SCHEDULE_QUEUE, SCHEDULE_DELAY) +# Upload-Post 只接受未来 365 天内的 scheduled_date。 +_MAX_SCHEDULE_DELAY_HOURS = 24 * 365 +_DEFAULT_SCHEDULE_DELAY_HOURS = 2.0 class UploadPostService: @@ -162,6 +174,44 @@ def auto_upload(self) -> bool: def youtube_privacy_status(self) -> str: return config.app.get("upload_post_youtube_privacy_status", "public") + @property + def schedule_mode(self) -> str: + mode = str(config.app.get("upload_post_schedule", SCHEDULE_NOW) or SCHEDULE_NOW) + return mode if mode in SCHEDULE_MODES else SCHEDULE_NOW + + @property + def schedule_delay_hours(self) -> float: + try: + hours = float( + config.app.get("upload_post_schedule_delay_hours", _DEFAULT_SCHEDULE_DELAY_HOURS) + ) + except (TypeError, ValueError): + hours = _DEFAULT_SCHEDULE_DELAY_HOURS + return min(max(hours, 0.1), _MAX_SCHEDULE_DELAY_HOURS) + + def schedule_request(self, now: datetime | None = None) -> dict | None: + """发布时间的快照,在视频排队发布时确定。 + + 延迟发布从排队时刻起算,之后修改设置不影响已排队的视频,与 YouTube + 隐私和受众声明的快照语义一致。返回 None 表示立即发布。 + """ + mode = self.schedule_mode + if mode == SCHEDULE_QUEUE: + return {"mode": SCHEDULE_QUEUE} + if mode == SCHEDULE_DELAY: + when = (now or datetime.now(timezone.utc)) + timedelta(hours=self.schedule_delay_hours) + return { + "mode": SCHEDULE_DELAY, + "scheduled_date": when.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), + } + return None + + @property + def ai_label(self) -> bool: + # TikTok 与 Instagram 要求标注 AI 生成内容,与 YouTube 的 + # containsSyntheticMedia 一致默认开启。Instagram 的标注发布后无法撤销。 + return config.app.get("upload_post_ai_label", True) is True + @property def youtube_made_for_kids(self) -> bool: # 未配置时,后续 YouTube 上传显式声明为非面向儿童;儿童内容需设置为 true。 @@ -180,6 +230,8 @@ def upload_video( privacy_level: str = "PUBLIC_TO_EVERYONE", youtube_extra: Optional[dict] = None, on_background_start: Callable[[str], None] | None = None, + platform_titles: Optional[dict] = None, + schedule: Optional[dict] = None, ) -> dict: if not self.is_configured(): logger.warning("Upload-Post is not configured. Skipping cross-post.") @@ -232,6 +284,21 @@ def unconfirmed_response(message: str) -> dict: for platform in platforms: data.append(('platform[]', platform)) + # 每个平台各自的文案(含话题标签),未提供时 Upload-Post 使用 title。 + for platform, text in (platform_titles or {}).items(): + field = _PLATFORM_TITLE_FIELDS.get(platform) + if field and platform in platforms and str(text or "").strip(): + data.append((field, str(text)[:_PLATFORM_TEXT_LIMIT])) + + if self.ai_label and any(p in _PLATFORM_TITLE_FIELDS for p in platforms): + data.append(('is_ai_generated', "true")) + + # Upload-Post 自己保存定时任务并按时发布,本地进程退出也不受影响。 + if schedule and schedule.get("mode") == SCHEDULE_QUEUE: + data.append(('add_to_queue', "true")) + elif schedule and schedule.get("scheduled_date"): + data.append(('scheduled_date', schedule["scheduled_date"])) + if has_youtube: # multipart 表单使用小写布尔字符串,且不能依赖 LLM 元数据 # 是否存在;只要发布到 YouTube,就显式传递用户的受众声明。 @@ -288,6 +355,23 @@ def unconfirmed_response(message: str) -> dict: logger.error("Upload-Post returned an invalid response to upload") return unconfirmed_response("Upload-Post returned an invalid response") + if result["success"] and schedule: + # 定时发布返回 202 与 job_id,没有 request_id 也没有平台结果; + # 平台结果要到发布时间才产生,不能按后台上传轮询一小时后报失败。 + job_id = result.get("job_id") + if not isinstance(job_id, str) or not job_id.strip(): + return unconfirmed_response("Upload-Post scheduled the post without a job_id") + logger.info( + "Upload-Post scheduled the post: " + f"job_id={job_id.strip()}, scheduled_date={result.get('scheduled_date')}" + ) + return { + **result, + "success": True, + "scheduled": True, + "job_id": job_id.strip(), + } + if result["success"]: is_background = "results" not in result if is_background: @@ -400,6 +484,8 @@ def cross_post_video( youtube_extra: Optional[dict] = None, on_background_start: Callable[[str], None] | None = None, account: dict | None = None, + platform_titles: Optional[dict] = None, + schedule: Optional[dict] = None, ) -> dict: service = UploadPostService(account) if account is not None else upload_post_service return service.upload_video( @@ -408,4 +494,6 @@ def cross_post_video( platforms, youtube_extra=youtube_extra, on_background_start=on_background_start, + platform_titles=platform_titles, + schedule=schedule, ) diff --git a/cli.py b/cli.py index c5d09fc940..e38a9c90ce 100644 --- a/cli.py +++ b/cli.py @@ -50,6 +50,8 @@ "muapi", "openai_image", "codex_image", + "local_image", + "local_video", "local", ) diff --git a/config.example.toml b/config.example.toml index 506b37ae22..6c45e8edaa 100644 --- a/config.example.toml +++ b/config.example.toml @@ -234,6 +234,28 @@ openai_image_size = "" # Example: "cinematic photo of {term}, photorealistic, high detail" openai_image_prompt_template = "" +# Local AI material sources ("local_image" and "local_video"): one image per scene on this +# computer's GPU, free and offline. Set it up in the WebUI under +# Settings > AI Image > Local AI, which detects the hardware, installs a +# separate PyTorch runtime and downloads the recommended model. +# Catalog model id; empty uses the model recommended for this hardware. +local_ai_image_model = "" +# Video model for the "local_video" source, which animates each scene's image +# into a short clip (minutes per clip). Empty uses the recommended model. +local_ai_video_model = "" +# Where models are downloaded (several GB each). Empty: storage/local_ai/models. +local_ai_models_dir = "" +# Where the PyTorch runtime is installed. Empty: storage/local_ai/runtime. +local_ai_runtime_dir = "" +# GPU index from nvidia-smi; empty picks the GPU with the most memory. +local_ai_gpu_index = "" +# Address of the local image server. Change the port if 7862 is taken. +local_ai_url = "http://127.0.0.1:7862" +# Start the image server automatically when a video needs images. +local_ai_autostart = true +# Only for gated models such as FLUX.1 schnell (or set HF_TOKEN). +local_ai_hf_api_token = "" + # Optional TwelveLabs integration for semantic material ranking and video QA. # Install the optional dependency with: uv sync --extra twelvelabs # Create an API key at https://playground.twelvelabs.io/ @@ -658,6 +680,15 @@ upload_post_youtube_privacy_status = "public" # 未配置时,后续 YouTube 上传默认声明为非面向儿童;儿童内容请显式设置为 true。 # 与旧版省略声明字段不同,此设置不会修改已上传的视频。 upload_post_youtube_made_for_kids = false +# Label TikTok and Instagram posts as AI-generated (Upload-Post is_ai_generated), +# as YouTube uploads already are. Instagram's label cannot be removed later. +upload_post_ai_label = true +# When to publish: "now" (right after the video is ready), "queue" (the next +# free slot in your Upload-Post queue, set up in the Upload-Post dashboard) or +# "delay" (upload_post_schedule_delay_hours after the video is ready). +# Upload-Post stores scheduled posts and publishes them itself. +upload_post_schedule = "now" +upload_post_schedule_delay_hours = 2 # Maximum number of running and queued cross-post jobs per process. When the # limit is reached, video generation still succeeds and only cross-posting is # rejected. diff --git a/docs/local-image-server/README.md b/docs/local-image-server/README.md index d1d896d098..913cf91199 100644 --- a/docs/local-image-server/README.md +++ b/docs/local-image-server/README.md @@ -1,5 +1,7 @@ # Local image server for `openai_image` +> The WebUI now has this built in: pick **Local AI (this computer)** as the AI image source and set it up under Settings > AI Image > Local AI. It detects the GPU, installs the runtime, downloads the recommended model and starts the server for you (code in `local_ai/`). This folder remains as a standalone example with a content policy. + Stock footage often has little to do with the script. The `openai_image` source fixes that by generating one image per script segment. This folder has a small server that does it on your own GPU, so there are no per-image costs. It exposes the OpenAI `/v1/images/generations` API and runs [RealVisXL V5.0 Lightning](https://huggingface.co/SG161222/RealVisXL_V5.0_Lightning), a photoreal SDXL model that needs about 6 steps per image. diff --git a/docs/plans/feature-roadmap.md b/docs/plans/feature-roadmap.md index c40cf4841b..4561e12a15 100644 --- a/docs/plans/feature-roadmap.md +++ b/docs/plans/feature-roadmap.md @@ -60,9 +60,9 @@ Add both to the source list in `task.py:103` and the `stop_at` branches around ` **UI.** New settings tab "Local AI": hardware card, recommended models with size/VRAM/license, download/delete with progress, disk usage, sidecar status/logs, "test generate" button. ### Phases -1. Move sidecar into `local_ai/`, add `hardware.py`, `/hardware` + `/models` endpoints, managed start from the app. Ship `local_image` with the existing SDXL model. -2. Catalog + downloader + model picker UI; add FLUX tier. -3. `local_video` with one model family (LTX-Video first: fastest on 12–16 GB), keyframe → I2V, per-scene fallback. +1. Move sidecar into `local_ai/`, add `hardware.py`, `/hardware` + `/models` endpoints, managed start from the app. Ship `local_image` with the existing SDXL model. **Done 2026-11-04**, except a `/hardware` endpoint: the app detects hardware itself, before the runtime exists. +2. Catalog + downloader + model picker UI; add FLUX tier. **Done 2026-11-04**: catalog has FLUX.1 schnell (gated), RealVisXL V5 Lightning and DreamShaper 8; downloads go to plain folders because the Hugging Face cache needs symlinks that Windows refuses without Developer Mode. +3. `local_video` with one model family (LTX-Video first: fastest on 12–16 GB), keyframe → I2V, per-scene fallback. **Done 2026-11-04 with Wan 2.2 TI2V 5B instead** (Apache-2.0; LTX-Video has a custom license and 48–93 GB repos). Loaded in 4-bit NF4 (bitsandbytes) and resharded on first load into 1 GB bf16 files, because loading 5 GB fp32 shards failed with Windows os error 1455 when WSL held most of the commit limit. About 2 minutes per 3 s 480p clip on an RTX 4070 Ti SUPER, so a 50 s video takes roughly 40 minutes; Wan 2.1 T2V 1.3B covers 8 GB cards. 4. Second video family (Wan), multi-GPU scheduling, Docker image. ### Risks @@ -106,9 +106,11 @@ app/controllers/v1/publish.py # POST /v1/publish, GET /v1/publish/jobs, DELETE **Batch / autopilot mode.** Topic list or RSS → generate N videos → auto-schedule across slots. Optional "review before publish" gate. ### Phases -1. Extract `Publisher` protocol; wrap existing Upload-Post as adapter (no behavior change, keep tests green). -2. Metadata + thumbnail generation, editable publish form, publish history panel. -3. Scheduler + `/v1/publish` API. +Upload-Post turned out to cover more than assumed: per-platform text fields (`tiktok_title`, `instagram_title`), `is_ai_generated`, server-side scheduling (`scheduled_date`, `add_to_queue`, returning `202` with a `job_id`), and cover/thumbnail fields ([docs](https://docs.upload-post.com/api/upload-video)). So the Upload-Post path gets these directly, and the `Publisher` protocol waits until a second publisher (YouTube direct) needs it. + +1. Extract `Publisher` protocol; wrap existing Upload-Post as adapter (no behavior change, keep tests green). **Deferred** until YouTube direct. +2. Metadata + thumbnail generation, editable publish form, publish history panel. **Partly done 2026-11-04**: a caption with hashtags per platform, and AI-generated labels for TikTok and Instagram. Thumbnails, editable form and history panel remain. +3. Scheduler + `/v1/publish` API. **Scheduling done 2026-11-04** through Upload-Post (publish now, next queue slot, or after N hours); Upload-Post publishes even if this computer is off. `/v1/publish` remains. 4. YouTube direct. 5. TikTok and Instagram direct (opt-in, documented app setup). 6. Autopilot batch mode. diff --git a/docs/skill/mpt_agent.py b/docs/skill/mpt_agent.py index d68b298d93..3adcf817b9 100644 --- a/docs/skill/mpt_agent.py +++ b/docs/skill/mpt_agent.py @@ -42,6 +42,8 @@ # the CLI accepts must not be rejected here as unsupported. "openai_image", "codex_image", + "local_image", + "local_video", "local", } VOLCENGINE_ARK_API_KEY_URL = ( diff --git a/local_ai/__init__.py b/local_ai/__init__.py new file mode 100644 index 0000000000..7f01b1a78d --- /dev/null +++ b/local_ai/__init__.py @@ -0,0 +1,5 @@ +"""Local image generation: hardware detection, model catalog, downloads and the GPU server. + +Everything here except ``server`` and ``pipelines`` uses only the standard library, +so the main app can detect hardware and pick models before torch is installed. +""" diff --git a/local_ai/catalog.json b/local_ai/catalog.json new file mode 100644 index 0000000000..385958ac8c --- /dev/null +++ b/local_ai/catalog.json @@ -0,0 +1,161 @@ +{ + "version": 1, + "models": [ + { + "id": "flux1-schnell", + "kind": "image", + "name": "FLUX.1 schnell", + "repo": "black-forest-labs/FLUX.1-schnell", + "family": "flux", + "license": "apache-2.0", + "gated": true, + "download_gb": 33.7, + "quality": 4, + "vram_gpu_gb": 34, + "vram_offload_gb": 24, + "ram_offload_gb": 32, + "cpu": false, + "allow_patterns": [ + "*.json", + "*.txt", + "*.model", + "*/*.safetensors" + ], + "ignore_patterns": [], + "variant": null, + "steps": 4, + "guidance": 0.0, + "notes": "Best prompt following. Gated on Hugging Face: accept the terms on the model page and set a Hugging Face token.", + "quantize": [], + "bf16": [] + }, + { + "id": "realvisxl-v5-lightning", + "kind": "image", + "name": "RealVisXL V5.0 Lightning (SDXL)", + "repo": "SG161222/RealVisXL_V5.0_Lightning", + "family": "sdxl", + "license": "openrail++", + "gated": false, + "download_gb": 6.9, + "quality": 3, + "vram_gpu_gb": 11, + "vram_offload_gb": 6, + "ram_offload_gb": 16, + "cpu": false, + "allow_patterns": [ + "*.json", + "*.txt", + "*/*.fp16.safetensors" + ], + "ignore_patterns": [], + "variant": "fp16", + "steps": 6, + "guidance": 1.5, + "notes": "Photoreal SDXL in 6 steps. About 4 seconds per image on a 16 GB card.", + "quantize": [], + "bf16": [] + }, + { + "id": "dreamshaper-8", + "kind": "image", + "name": "DreamShaper 8 (SD 1.5)", + "repo": "Lykon/dreamshaper-8", + "family": "sd15", + "license": "creativeml-openrail-m", + "gated": false, + "download_gb": 2.2, + "quality": 2, + "vram_gpu_gb": 4, + "vram_offload_gb": 3, + "ram_offload_gb": 8, + "cpu": true, + "allow_patterns": [ + "*.json", + "*.txt", + "*/*.fp16.safetensors" + ], + "ignore_patterns": [ + "safety_checker/*", + "feature_extractor/*" + ], + "variant": "fp16", + "steps": 25, + "guidance": 6.0, + "notes": "Small and fast. Runs on older cards and, slowly, on the CPU.", + "quantize": [], + "bf16": [] + }, + { + "id": "wan2.2-ti2v-5b", + "kind": "video", + "name": "Wan 2.2 TI2V 5B", + "repo": "Wan-AI/Wan2.2-TI2V-5B-Diffusers", + "family": "wan22", + "license": "apache-2.0", + "gated": false, + "download_gb": 34.2, + "quality": 3, + "vram_gpu_gb": 14, + "vram_offload_gb": 8, + "ram_offload_gb": 16, + "cpu": false, + "allow_patterns": [ + "*.json", + "*.model", + "*/*.safetensors" + ], + "ignore_patterns": [], + "variant": null, + "steps": 30, + "guidance": 5.0, + "fps": 24, + "frames": 73, + "image_to_video": true, + "notes": "Animates each scene's image into a 3-second clip. Loaded in 4-bit to fit 16 GB cards and ordinary RAM. About 2 minutes per clip on an RTX 4070 Ti SUPER.", + "quantize": [ + "transformer", + "text_encoder" + ], + "bf16": [ + "transformer", + "text_encoder" + ] + }, + { + "id": "wan2.1-t2v-1.3b", + "kind": "video", + "name": "Wan 2.1 T2V 1.3B", + "repo": "Wan-AI/Wan2.1-T2V-1.3B-Diffusers", + "family": "wan21", + "license": "apache-2.0", + "gated": false, + "download_gb": 28.9, + "quality": 2, + "vram_gpu_gb": 10, + "vram_offload_gb": 6, + "ram_offload_gb": 12, + "cpu": false, + "allow_patterns": [ + "*.json", + "*.model", + "*/*.safetensors" + ], + "ignore_patterns": [], + "variant": null, + "steps": 30, + "guidance": 5.0, + "fps": 16, + "frames": 49, + "image_to_video": false, + "notes": "Small text-to-video model for 8 GB cards. Clips follow the scene text, not its image.", + "quantize": [ + "text_encoder" + ], + "bf16": [ + "transformer", + "text_encoder" + ] + } + ] +} diff --git a/local_ai/catalog.py b/local_ai/catalog.py new file mode 100644 index 0000000000..f3aff204c4 --- /dev/null +++ b/local_ai/catalog.py @@ -0,0 +1,114 @@ +"""Model catalog and the choice of the best model for this machine's hardware.""" + +import json +from dataclasses import dataclass +from pathlib import Path + +from local_ai.hardware import Gpu, Hardware + +CATALOG_PATH = Path(__file__).with_name("catalog.json") + +# How a model would run here, best first. +FIT_GPU = "gpu" # weights stay on the GPU: fastest +FIT_OFFLOAD = "offload" # parts move between RAM and GPU per step: slower +FIT_CPU = "cpu" # no usable GPU: much slower +FIT_NONE = "none" +_FIT_RANK = {FIT_GPU: 0, FIT_OFFLOAD: 1, FIT_CPU: 2, FIT_NONE: 3} + + +@dataclass(frozen=True) +class Model: + id: str + kind: str + name: str + repo: str + family: str + license: str + gated: bool + download_gb: float + quality: int + vram_gpu_gb: float + vram_offload_gb: float + ram_offload_gb: float + cpu: bool + allow_patterns: tuple[str, ...] + ignore_patterns: tuple[str, ...] + variant: str | None + steps: int + guidance: float + notes: str = "" + # Video models only: output frame rate, frames per clip, and whether the + # model animates a start image (the scene's generated image) or text only. + fps: int = 0 + frames: int = 0 + image_to_video: bool = False + # Components loaded in 4-bit (bitsandbytes NF4): the memory figures above + # assume it. Wan's T5 text encoder alone is 11 GB in bf16. + quantize: tuple[str, ...] = () + # Components rewritten on first load as bfloat16 shards of at most 1 GB: + # less disk, and loading never needs a whole multi-GB shard in memory. + bf16: tuple[str, ...] = () + + @classmethod + def from_dict(cls, data: dict) -> "Model": + fields = dict(data) + fields["allow_patterns"] = tuple(fields.get("allow_patterns") or ()) + fields["ignore_patterns"] = tuple(fields.get("ignore_patterns") or ()) + fields["quantize"] = tuple(fields.get("quantize") or ()) + fields["bf16"] = tuple(fields.get("bf16") or ()) + return cls(**fields) + + +@dataclass(frozen=True) +class Fit: + model: Model + mode: str + gpu_index: int | None + + def to_dict(self) -> dict: + return {"model_id": self.model.id, "mode": self.mode, "gpu_index": self.gpu_index} + + +def load(path: Path = CATALOG_PATH) -> list[Model]: + data = json.loads(path.read_text(encoding="utf-8")) + return [Model.from_dict(entry) for entry in data["models"]] + + +def get(model_id: str, models: list[Model] | None = None) -> Model | None: + return next((model for model in (models or load()) if model.id == model_id), None) + + +def fit(model: Model, hardware: Hardware) -> Fit: + """How ``model`` would run on the GPU with the most memory, if at all.""" + gpu: Gpu | None = hardware.best_gpu + if gpu is not None: + if gpu.vram_gb >= model.vram_gpu_gb: + return Fit(model, FIT_GPU, gpu.index) + if gpu.vram_gb >= model.vram_offload_gb and hardware.ram_gb >= model.ram_offload_gb: + return Fit(model, FIT_OFFLOAD, gpu.index) + if model.cpu and hardware.ram_gb >= model.ram_offload_gb: + return Fit(model, FIT_CPU, None) + return Fit(model, FIT_NONE, None) + + +def rank(hardware: Hardware, kind: str = "image", models: list[Model] | None = None) -> list[Fit]: + """Every model of ``kind``, best choice first. + + Offloading only moves whole components (text encoders, VAE) between RAM + and GPU, so it costs seconds per image and a better model is worth it. + The CPU costs minutes per image, so any GPU option beats it. + """ + fits = [fit(model, hardware) for model in (models or load()) if model.kind == kind] + + def key(item: Fit): + tier = 0 if item.mode in (FIT_GPU, FIT_OFFLOAD) else _FIT_RANK[item.mode] + return (tier, -item.model.quality, _FIT_RANK[item.mode]) + + return sorted(fits, key=key) + + +def recommend(hardware: Hardware, kind: str = "image", models: list[Model] | None = None) -> Fit | None: + """The best model that can run here; gated models only when nothing else fits.""" + usable = [item for item in rank(hardware, kind, models) if item.mode != FIT_NONE] + open_models = [item for item in usable if not item.model.gated] + return (open_models or usable or [None])[0] diff --git a/local_ai/hardware.py b/local_ai/hardware.py new file mode 100644 index 0000000000..3d861a4287 --- /dev/null +++ b/local_ai/hardware.py @@ -0,0 +1,180 @@ +"""Detect GPUs, system memory and free disk without importing torch.""" + +import ctypes +import os +import platform +import re +import shutil +import subprocess +from dataclasses import asdict, dataclass, field + +_NVIDIA_SMI_TIMEOUT_SECONDS = 10 +# Older drivers print "CUDA Version: 12.4", newer ones "CUDA UMD Version: 13.3". +_CUDA_VERSION_PATTERN = re.compile(r"CUDA (?:UMD )?Version:\s*(\d+)\.(\d+)") + + +@dataclass +class Gpu: + index: int + name: str + vendor: str # "nvidia" or "apple" + vram_gb: float + free_vram_gb: float | None = None + + +@dataclass +class Hardware: + os: str + machine: str + ram_gb: float + gpus: list[Gpu] = field(default_factory=list) + nvidia_driver: str = "" + cuda_version: tuple[int, int] | None = None + + @property + def best_gpu(self) -> Gpu | None: + """The GPU with the most memory; models run on one card at a time.""" + return max(self.gpus, key=lambda gpu: gpu.vram_gb, default=None) + + def to_dict(self) -> dict: + data = asdict(self) + data["cuda_version"] = ( + ".".join(str(part) for part in self.cuda_version) if self.cuda_version else "" + ) + best = self.best_gpu + data["best_gpu_index"] = best.index if best else None + return data + + +def _run(command: list[str]) -> str: + try: + result = subprocess.run( + command, + capture_output=True, + text=True, + timeout=_NVIDIA_SMI_TIMEOUT_SECONDS, + check=False, + ) + except (OSError, subprocess.SubprocessError): + return "" + return result.stdout if result.returncode == 0 else "" + + +def _mib_to_gb(value: str) -> float | None: + try: + return round(float(value) / 1024, 1) + except ValueError: + return None + + +def parse_nvidia_gpus(csv_output: str) -> tuple[list[Gpu], str]: + """Parse ``nvidia-smi --query-gpu=index,name,memory.total,memory.free,driver_version``.""" + gpus, driver = [], "" + for line in csv_output.splitlines(): + parts = [part.strip() for part in line.split(",")] + if len(parts) < 5 or not parts[0].isdigit(): + continue + total = _mib_to_gb(parts[2]) + if total is None: + continue + gpus.append( + Gpu( + index=int(parts[0]), + name=parts[1], + vendor="nvidia", + vram_gb=total, + free_vram_gb=_mib_to_gb(parts[3]), + ) + ) + driver = driver or parts[4] + return gpus, driver + + +def parse_cuda_version(nvidia_smi_output: str) -> tuple[int, int] | None: + match = _CUDA_VERSION_PATTERN.search(nvidia_smi_output) + return (int(match.group(1)), int(match.group(2))) if match else None + + +def _nvidia() -> tuple[list[Gpu], str, tuple[int, int] | None]: + executable = shutil.which("nvidia-smi") + if not executable: + return [], "", None + gpus, driver = parse_nvidia_gpus( + _run( + [ + executable, + "--query-gpu=index,name,memory.total,memory.free,driver_version", + "--format=csv,noheader,nounits", + ] + ) + ) + cuda = parse_cuda_version(_run([executable])) if gpus else None + return gpus, driver, cuda + + +class _MemoryStatus(ctypes.Structure): + _fields_ = [ + ("dwLength", ctypes.c_ulong), + ("dwMemoryLoad", ctypes.c_ulong), + ("ullTotalPhys", ctypes.c_ulonglong), + ("ullAvailPhys", ctypes.c_ulonglong), + ("ullTotalPageFile", ctypes.c_ulonglong), + ("ullAvailPageFile", ctypes.c_ulonglong), + ("ullTotalVirtual", ctypes.c_ulonglong), + ("ullAvailVirtual", ctypes.c_ulonglong), + ("sullAvailExtendedVirtual", ctypes.c_ulonglong), + ] + + +def _ram_bytes() -> int: + system = platform.system() + if system == "Windows": + status = _MemoryStatus() + status.dwLength = ctypes.sizeof(_MemoryStatus) + if ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(status)): + return int(status.ullTotalPhys) + return 0 + if system == "Darwin": + output = _run(["sysctl", "-n", "hw.memsize"]).strip() + return int(output) if output.isdigit() else 0 + try: + return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES") + except (ValueError, OSError, AttributeError): + return 0 + + +def _apple_gpu(ram_gb: float) -> list[Gpu]: + # Apple Silicon shares memory between CPU and GPU; macOS lets the GPU use + # about three quarters of it. + if platform.system() == "Darwin" and platform.machine() == "arm64": + return [Gpu(index=0, name="Apple Silicon", vendor="apple", vram_gb=round(ram_gb * 0.75, 1))] + return [] + + +def free_disk_gb(path: str) -> float: + """Free space on the drive that holds ``path`` (or its nearest existing parent).""" + path = os.path.abspath(path) + while not os.path.exists(path): + parent = os.path.dirname(path) + if parent == path: + break + path = parent + try: + return round(shutil.disk_usage(path).free / 1024**3, 1) + except OSError: + return 0.0 + + +def detect() -> Hardware: + ram_gb = round(_ram_bytes() / 1024**3, 1) + gpus, driver, cuda = _nvidia() + if not gpus: + gpus = _apple_gpu(ram_gb) + return Hardware( + os=platform.system(), + machine=platform.machine(), + ram_gb=ram_gb, + gpus=gpus, + nvidia_driver=driver, + cuda_version=cuda, + ) diff --git a/local_ai/models.py b/local_ai/models.py new file mode 100644 index 0000000000..648ef0eb7f --- /dev/null +++ b/local_ai/models.py @@ -0,0 +1,169 @@ +"""Download catalog models from Hugging Face and track which ones are complete. + +Each model downloads into its own folder under the models directory, as plain +files: the Hugging Face cache relies on symlinks, which Windows refuses without +Developer Mode. A finished download writes a marker file, and only folders with +a marker count as installed, so an interrupted download is never loaded. +""" + +import json +import os +import shutil +import threading +import time +from dataclasses import dataclass, field +from pathlib import Path + +from local_ai.catalog import Model +from local_ai.hardware import free_disk_gb + +STATE_QUEUED = "queued" +STATE_DOWNLOADING = "downloading" +STATE_DONE = "done" +STATE_FAILED = "failed" + +MARKER_NAME = ".mpt-complete.json" +# Keep this much disk free after a download so the system and renders keep working. +_DISK_HEADROOM_GB = 5.0 + + +class DownloadError(RuntimeError): + pass + + +def model_dir(model: Model, models_dir: str) -> Path: + return Path(models_dir) / model.id + + +def installed_path(model: Model, models_dir: str) -> str | None: + """The folder of a completely downloaded model, else None.""" + folder = model_dir(model, models_dir) + try: + data = json.loads((folder / MARKER_NAME).read_text(encoding="utf-8")) + except (OSError, ValueError): + return None + return str(folder) if data.get("repo") == model.repo else None + + +def delete(model: Model, models_dir: str) -> None: + folder = model_dir(model, models_dir) + # Drop the marker first: a half-deleted folder must never look installed. + (folder / MARKER_NAME).unlink(missing_ok=True) + shutil.rmtree(folder, ignore_errors=True) + + +def folder_size(path: Path) -> int: + """Bytes under ``path``, including the partial files of a running download.""" + total = 0 + for root, _dirs, files in os.walk(path): + for name in files: + try: + total += os.path.getsize(os.path.join(root, name)) + except OSError: + pass + return total + + +def remote_size(model: Model, token: str | None = None) -> int: + """Bytes of the files this model downloads, from the Hugging Face API.""" + from huggingface_hub import HfApi + from huggingface_hub.utils import filter_repo_objects + + info = HfApi().model_info(model.repo, files_metadata=True, token=token or None) + siblings = filter_repo_objects( + info.siblings or [], + allow_patterns=list(model.allow_patterns) or None, + ignore_patterns=list(model.ignore_patterns) or None, + key=lambda sibling: sibling.rfilename, + ) + return sum(sibling.size or 0 for sibling in siblings) + + +@dataclass +class DownloadJob: + model: Model + models_dir: str + token: str | None = None + state: str = STATE_QUEUED + bytes_total: int = 0 + error: str = "" + path: str = "" + started_at: float = field(default_factory=time.time) + _thread: threading.Thread | None = field(default=None, repr=False) + + @property + def bytes_done(self) -> int: + if self.state == STATE_DONE: + return self.bytes_total + # Files left by an earlier, interrupted download count: they are not fetched again. + done = folder_size(model_dir(self.model, self.models_dir)) + return min(done, self.bytes_total) if self.bytes_total else done + + @property + def active(self) -> bool: + return self.state in (STATE_QUEUED, STATE_DOWNLOADING) + + def to_dict(self) -> dict: + return { + "model_id": self.model.id, + "state": self.state, + "bytes_done": self.bytes_done, + "bytes_total": self.bytes_total, + "error": self.error, + "path": self.path, + } + + def start(self) -> "DownloadJob": + self._thread = threading.Thread( + target=self._run, name=f"download-{self.model.id}", daemon=True + ) + self._thread.start() + return self + + def _run(self) -> None: + try: + self.path = download(self.model, self.models_dir, self.token, job=self) + self.state = STATE_DONE + except Exception as error: # report every failure to the UI instead of a dead thread + self.error = f"{type(error).__name__}: {error}" + self.state = STATE_FAILED + + +def download( + model: Model, + models_dir: str, + token: str | None = None, + job: DownloadJob | None = None, +) -> str: + """Download ``model`` under ``models_dir`` and mark it complete. Returns its folder.""" + from huggingface_hub import snapshot_download + from huggingface_hub.errors import GatedRepoError + + folder = model_dir(model, models_dir) + total = remote_size(model, token) + needed_gb = max(0, total - folder_size(folder)) / 1024**3 + free_gb = free_disk_gb(str(folder)) + if needed_gb + _DISK_HEADROOM_GB > free_gb: + raise DownloadError( + f"{model.name} needs {needed_gb:.1f} GB but only {free_gb:.1f} GB is free for {folder}" + ) + if job is not None: + job.bytes_total = total + job.state = STATE_DOWNLOADING + try: + snapshot_download( + model.repo, + local_dir=str(folder), + allow_patterns=list(model.allow_patterns) or None, + ignore_patterns=list(model.ignore_patterns) or None, + token=token or None, + ) + except GatedRepoError as error: + raise DownloadError( + f"{model.name} is gated: accept its terms at https://huggingface.co/{model.repo} " + "and set a Hugging Face token" + ) from error + (folder / MARKER_NAME).write_text( + json.dumps({"repo": model.repo, "downloaded_at": time.time()}), encoding="utf-8" + ) + return str(folder) diff --git a/local_ai/pipelines.py b/local_ai/pipelines.py new file mode 100644 index 0000000000..c95324ae1f --- /dev/null +++ b/local_ai/pipelines.py @@ -0,0 +1,290 @@ +"""Load catalog models with diffusers and paint images. Imports torch lazily.""" + +import os +import random +import time + +from local_ai.catalog import FIT_CPU, FIT_OFFLOAD, Model + +# Sizes each family was trained on; a request snaps to the closest aspect ratio. +_BUCKETS = { + # about one megapixel + "sdxl": [ + (1024, 1024), (896, 1152), (832, 1216), (768, 1344), (640, 1536), + (1152, 896), (1216, 832), (1344, 768), (1536, 640), + ], + "flux": [ + (1024, 1024), (896, 1152), (832, 1216), (768, 1344), (640, 1536), + (1152, 896), (1216, 832), (1344, 768), (1536, 640), + ], + # SD 1.5 drifts into duplicated subjects much above 512 px on the short side + "sd15": [(512, 512), (512, 768), (512, 896), (768, 512), (896, 512)], +} +_DEFAULT_ASPECT = (9, 16) + + +def pick_size(family: str, size: str | None) -> tuple[int, int]: + width, height = _DEFAULT_ASPECT + if size: + w, _, h = size.lower().partition("x") + if w.isdigit() and h.isdigit() and int(w) > 0 and int(h) > 0: + width, height = int(w), int(h) + return min(_BUCKETS[family], key=lambda bucket: abs(bucket[0] / bucket[1] - width / height)) + + +def device() -> str: + import torch + + if torch.cuda.is_available(): + return "cuda" + if torch.backends.mps.is_available(): + return "mps" + return "cpu" + + +def load(model: Model, path: str, mode: str): + """Load ``model`` from its local folder and place it for ``mode``.""" + import torch + from diffusers import ( + DPMSolverMultistepScheduler, + FluxPipeline, + StableDiffusionPipeline, + StableDiffusionXLPipeline, + ) + + pipeline_class = { + "sdxl": StableDiffusionXLPipeline, + "sd15": StableDiffusionPipeline, + "flux": FluxPipeline, + }[model.family] + if mode == FIT_CPU or device() == "cpu": + dtype = torch.float32 # half precision is slow or unsupported on most CPUs + elif model.family == "flux": + dtype = torch.bfloat16 + else: + dtype = torch.float16 + kwargs = {"torch_dtype": dtype, "local_files_only": True} + if model.variant: + kwargs["variant"] = model.variant + if model.family == "sd15": + kwargs.update(safety_checker=None, requires_safety_checker=False) + pipe = pipeline_class.from_pretrained(path, **kwargs) + if model.family in ("sdxl", "sd15"): + # Lightning and SD 1.5 checkpoints are tuned for DPM++ SDE with Karras sigmas. + pipe.scheduler = DPMSolverMultistepScheduler.from_config( + pipe.scheduler.config, + algorithm_type="sde-dpmsolver++", + use_karras_sigmas=True, + ) + if mode == FIT_OFFLOAD: + pipe.enable_model_cpu_offload() + return pipe + + +def paint(pipe, model: Model, prompt: str, negative: str, width: int, height: int): + import torch + + generator = torch.Generator("cpu").manual_seed(random.randint(0, 2**31 - 1)) + kwargs = { + "prompt": prompt, + "width": width, + "height": height, + "num_inference_steps": model.steps, + "guidance_scale": model.guidance, + "generator": generator, + } + if model.family == "flux": + kwargs["max_sequence_length"] = 256 # schnell was trained with 256 T5 tokens + else: + kwargs["negative_prompt"] = negative + return pipe(**kwargs).images[0] + + +# Wan clips at about 480p: the sizes take minutes instead of tens of minutes on +# 12-16 GB cards. Sides are multiples of 32, as the Wan 2.2 VAE and patching need. +_VIDEO_BUCKETS = { + "wan22": [(480, 832), (832, 480), (640, 640)], + "wan21": [(480, 832), (832, 480), (640, 640)], +} + + +def pick_video_size(family: str, size: str | None) -> tuple[int, int]: + width, height = _DEFAULT_ASPECT + if size: + w, _, h = size.lower().partition("x") + if w.isdigit() and h.isdigit() and int(w) > 0 and int(h) > 0: + width, height = int(w), int(h) + return min(_VIDEO_BUCKETS[family], key=lambda bucket: abs(bucket[0] / bucket[1] - width / height)) + + +BF16_MARKER = ".mpt-bf16" +_MAX_SHARD_BYTES = 1024**3 + + +def convert_to_bf16(folder: str) -> None: + """Rewrite a component as bfloat16 shards of at most 1 GB, once. + + diffusers reads a whole shard into memory before loading it. Wan publishes + 5 GB float32 shards, and an ordinary Windows computer with WSL or Docker + running may have less memory than that left to commit (os error 1455). + Writing tensor by tensor into small shards keeps the peak near 1 GB here + and when the server later loads the model. The new shards and index are + written first and the old files removed last, so an interrupted run leaves + a loadable model and continues next time. + """ + import json + from pathlib import Path + + import torch + from safetensors import safe_open + from safetensors.torch import save_file + + component = Path(folder) + if (component / BF16_MARKER).exists(): + return + indexes = list(component.glob("*.safetensors.index.json")) + if len(indexes) != 1: + return # single-file components are small enough as they are + index_path = indexes[0] + index = json.loads(index_path.read_text(encoding="utf-8")) + old_shards = sorted(set(index["weight_map"].values())) + + def needs_rewrite(name): + shard = component / name + if shard.stat().st_size > _MAX_SHARD_BYTES: + return True + with safe_open(str(shard), framework="pt") as source: + return any(source.get_slice(key).get_dtype() == "F32" for key in source.keys()) + + if not any(needs_rewrite(name) for name in old_shards): + (component / BF16_MARKER).write_text("bfloat16", encoding="utf-8") + return + + prefix = index_path.name.removesuffix(".safetensors.index.json") + "-mpt" + weight_map, written, batch, batch_bytes = {}, [], {}, 0 + + def flush(): + nonlocal batch, batch_bytes + if not batch: + return + name = f"{prefix}-{len(written) + 1:05d}.safetensors" + save_file(batch, str(component / name), metadata={"format": "pt"}) + for key in batch: + weight_map[key] = name + written.append(name) + batch, batch_bytes = {}, 0 + + for name in old_shards: + print(f"converting {name} to bfloat16") + with safe_open(str(component / name), framework="pt") as source: + for key in source.keys(): + tensor = source.get_tensor(key) + if tensor.dtype == torch.float32: + tensor = tensor.to(torch.bfloat16) + size = tensor.numel() * tensor.element_size() + if batch and batch_bytes + size > _MAX_SHARD_BYTES: + flush() + batch[key] = tensor + batch_bytes += size + flush() + total = sum((component / name).stat().st_size for name in written) + partial = index_path.with_suffix(".partial") + partial.write_text( + json.dumps({"metadata": {"total_size": total}, "weight_map": weight_map}, indent=2), + encoding="utf-8", + ) + partial.replace(index_path) + for name in old_shards: + if name not in written: + (component / name).unlink(missing_ok=True) + (component / BF16_MARKER).write_text("bfloat16", encoding="utf-8") + + +def load_video(model: Model, path: str, mode: str): + """Load a Wan video model. The VAE stays in float32, as Wan recommends.""" + import torch + from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, WanPipeline + + for component in model.bf16: + convert_to_bf16(os.path.join(path, component)) + vae = AutoencoderKLWan.from_pretrained( + path, subfolder="vae", torch_dtype=torch.float32, local_files_only=True + ) + pipeline_class = WanImageToVideoPipeline if model.image_to_video else WanPipeline + kwargs = {"vae": vae, "torch_dtype": torch.bfloat16, "local_files_only": True} + if model.quantize: + from diffusers.quantizers import PipelineQuantizationConfig + + kwargs["quantization_config"] = PipelineQuantizationConfig( + quant_backend="bitsandbytes_4bit", + quant_kwargs={ + "load_in_4bit": True, + "bnb_4bit_quant_type": "nf4", + "bnb_4bit_compute_dtype": torch.bfloat16, + }, + components_to_quantize=list(model.quantize), + ) + pipe = pipeline_class.from_pretrained(path, **kwargs) + if mode == FIT_OFFLOAD: + pipe.enable_model_cpu_offload() + # Decoding every frame at once in float32 is the peak. Past the card's + # memory, Windows spills into shared system memory and a clip takes ten + # times longer; tiles keep the decode on the card. + pipe.vae.enable_tiling() + return pipe + + +def animate(pipe, model: Model, prompt: str, negative: str, width: int, height: int, image=None) -> bytes: + """Render one clip and return it as MP4 bytes.""" + import tempfile + + import torch + from diffusers.utils import export_to_video + from PIL import ImageOps + + kwargs = { + "prompt": prompt, + "negative_prompt": negative, + "width": width, + "height": height, + "num_frames": model.frames, + "num_inference_steps": model.steps, + "guidance_scale": model.guidance, + "generator": torch.Generator("cpu").manual_seed(random.randint(0, 2**31 - 1)), + } + if model.image_to_video: + if image is None: + raise ValueError(f"{model.name} animates a start image, but none was sent") + # Crop the scene image to the clip's aspect ratio instead of stretching it. + kwargs["image"] = ImageOps.fit(image.convert("RGB"), (width, height)) + started = time.time() + stamps = {} + + def on_step_end(_pipe, step, _timestep, callback_kwargs): + stamps.setdefault("first_step", time.time()) + stamps["last_step"] = time.time() + return callback_kwargs + + frames = pipe(**kwargs, callback_on_step_end=on_step_end).frames[0] + finished = time.time() + if stamps: + print( + f"clip {model.id} {width}x{height}x{model.frames}: " + f"prepare {stamps['first_step'] - started:.0f}s, " + f"denoise {stamps['last_step'] - stamps['first_step']:.0f}s, " + f"decode {finished - stamps['last_step']:.0f}s" + ) + with tempfile.TemporaryDirectory() as folder: + path = f"{folder}/clip.mp4" + export_to_video(frames, path, fps=model.fps) + with open(path, "rb") as clip: + return clip.read() + + +def empty_cache() -> None: + try: + import torch + except ImportError: + return + if torch.cuda.is_available(): + torch.cuda.empty_cache() diff --git a/local_ai/requirements.txt b/local_ai/requirements.txt new file mode 100644 index 0000000000..31428c5f9f --- /dev/null +++ b/local_ai/requirements.txt @@ -0,0 +1,19 @@ +# The GPU server's own environment. runtime.py installs torch first, from the +# PyTorch wheel index that matches the GPU driver, then these. +diffusers>=0.35 +transformers>=4.56 +accelerate>=1.10 +safetensors +huggingface_hub>=1.0 +sentencepiece +protobuf +# video models: Wan cleans prompts with ftfy; clips are written with imageio's ffmpeg +ftfy +imageio +imageio-ffmpeg +# 4-bit loading of large video models (Windows wheels since 0.43) +bitsandbytes>=0.45 +fastapi +uvicorn +pillow +requests diff --git a/local_ai/runtime.py b/local_ai/runtime.py new file mode 100644 index 0000000000..93711ed44f --- /dev/null +++ b/local_ai/runtime.py @@ -0,0 +1,103 @@ +"""Install the GPU server's Python environment: torch for this GPU, then diffusers. + +The server runs in its own virtual environment so the main app never depends on +torch, and so a CUDA build of torch can match the installed GPU driver. +""" + +import hashlib +import os +import shutil +import subprocess +import sys +from pathlib import Path + +from local_ai.hardware import Hardware + +PYTHON_VERSION = "3.11" +REQUIREMENTS = Path(__file__).with_name("requirements.txt") +TORCH_INDEX = "https://download.pytorch.org/whl/{tag}" + +# Lowest NVIDIA driver for each CUDA build of torch, newest first. +# https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/ (minimum driver per toolkit) +_CUDA_BUILDS = ((570, "cu128"), (560, "cu126")) + + +def torch_index(hardware: Hardware) -> str | None: + """The PyTorch wheel index for this machine; None means the default PyPI build.""" + nvidia = any(gpu.vendor == "nvidia" for gpu in hardware.gpus) + if nvidia: + try: + driver_major = int(hardware.nvidia_driver.split(".")[0]) + except ValueError: + driver_major = 0 + for minimum, tag in _CUDA_BUILDS: + if driver_major >= minimum: + return TORCH_INDEX.format(tag=tag) + # Too old for any current CUDA build: run on the CPU rather than fail. + return TORCH_INDEX.format(tag="cpu") + if hardware.os == "Darwin": + return None # PyPI's macOS wheels include Apple's Metal (MPS) backend + return TORCH_INDEX.format(tag="cpu") + + +def venv_python(runtime_dir: str) -> str: + scripts = "Scripts" if os.name == "nt" else "bin" + name = "python.exe" if os.name == "nt" else "python" + return str(Path(runtime_dir) / "venv" / scripts / name) + + +def _requirements_digest() -> str: + # Git may check the file out with CRLF or LF; the requirements are the same. + text = REQUIREMENTS.read_text(encoding="utf-8").replace("\r\n", "\n") + return hashlib.sha256(text.encode("utf-8")).hexdigest() + + +def is_installed(runtime_dir: str) -> bool: + """True when the runtime finished installing the current requirements. + + A runtime from an older release lacks packages newer models need, so it + counts as missing and the next install only adds what changed. + """ + try: + marker = Path(runtime_dir, "venv", ".mpt-installed").read_text(encoding="utf-8") + except OSError: + return False + return _requirements_digest() in marker.split() + + +def install_commands(runtime_dir: str, hardware: Hardware) -> list[list[str]]: + venv = str(Path(runtime_dir) / "venv") + python = venv_python(runtime_dir) + index = torch_index(hardware) + torch_args = ["torch"] + (["--index-url", index] if index else []) + uv = shutil.which("uv") + if uv: + return [ + [uv, "venv", "--python", PYTHON_VERSION, "--allow-existing", venv], + [uv, "pip", "install", "--python", python, *torch_args], + [uv, "pip", "install", "--python", python, "-r", str(REQUIREMENTS)], + ] + return [ + [sys.executable, "-m", "venv", venv], + [python, "-m", "pip", "install", "--upgrade", "pip"], + [python, "-m", "pip", "install", *torch_args], + [python, "-m", "pip", "install", "-r", str(REQUIREMENTS)], + ] + + +def install(runtime_dir: str, hardware: Hardware, log_path: str) -> None: + """Run the install commands, appending their output to ``log_path``. + + Raises RuntimeError naming the failed command; the log has the details. + """ + Path(runtime_dir).mkdir(parents=True, exist_ok=True) + marker = Path(runtime_dir, "venv", ".mpt-installed") + marker.unlink(missing_ok=True) + with open(log_path, "a", encoding="utf-8", errors="replace") as log: + for command in install_commands(runtime_dir, hardware): + log.write(f"$ {subprocess.list2cmdline(command)}\n") + log.flush() + result = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, check=False) + if result.returncode != 0: + raise RuntimeError(f"{Path(command[0]).name} exited with {result.returncode}") + marker.write_text(f"{torch_index(hardware) or 'pypi'} {_requirements_digest()}", encoding="utf-8") diff --git a/local_ai/server.py b/local_ai/server.py new file mode 100644 index 0000000000..d81a6f4453 --- /dev/null +++ b/local_ai/server.py @@ -0,0 +1,335 @@ +"""OpenAI-compatible image server, plus clip generation, for downloaded catalog models. + +MoneyPrinterTurbo starts it in its own Python environment (see runtime.py). It +can also run by hand: ``python -m local_ai.server`` from the project root. + +One image model and one video model can be loaded at a time, but only one of +them sits on the GPU: a scene first gets its image, then the clip animates it. + +The GPU is shared with Ollama, whose script model may not fit beside these +models. Before every request the server asks Ollama to unload its models, and +an idle model moves back to the CPU so Ollama gets the whole GPU for the next +script. This is the scheme from docs/local-image-server, made model-agnostic. +""" + +import base64 +import binascii +import gc +import io +import os +import threading +import time +from contextlib import asynccontextmanager +from dataclasses import dataclass + +import requests +import uvicorn +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel + +from local_ai import catalog, hardware, models, pipelines + +HOST = os.environ.get("LOCAL_AI_HOST", "127.0.0.1") +PORT = int(os.environ.get("LOCAL_AI_PORT", "7862")) +MODELS_DIR = os.environ.get("LOCAL_AI_MODELS_DIR", os.path.join("storage", "local_ai", "models")) +DEFAULT_MODELS = { + "image": os.environ.get("LOCAL_AI_IMAGE_MODEL", ""), + "video": os.environ.get("LOCAL_AI_VIDEO_MODEL", ""), +} +# Index in nvidia-smi numbering; the app also sets CUDA_VISIBLE_DEVICES to it. +GPU_INDEX = os.environ.get("LOCAL_AI_GPU_INDEX", "") +OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://localhost:11434") +OLLAMA_UNLOAD_TIMEOUT = float(os.environ.get("OLLAMA_UNLOAD_TIMEOUT", "30")) +IDLE_SECONDS = float(os.environ.get("LOCAL_AI_IDLE_SECONDS", "90")) +IDLE_CHECK_SECONDS = float(os.environ.get("LOCAL_AI_IDLE_CHECK_SECONDS", "10")) + +STYLE = os.environ.get( + "LOCAL_AI_STYLE", + "cinematic photograph, natural light, highly detailed, sharp focus", +) +NEGATIVE = os.environ.get( + "LOCAL_AI_NEGATIVE", + "text, letters, words, watermark, logo, signature, caption, deformed, " + "distorted, bad anatomy, extra fingers, blurry, lowres", +) +VIDEO_NEGATIVE = os.environ.get( + "LOCAL_AI_VIDEO_NEGATIVE", + "text, subtitles, watermark, logo, static frame, still picture, blurry, " + "low quality, jpeg artifacts, deformed, distorted, extra limbs, flickering", +) +MAX_IMAGES_PER_REQUEST = 4 + + +@dataclass +class Slot: + """The pipeline loaded for one kind of model.""" + + model: catalog.Model | None = None + pipe: object = None + mode: str = "" + on_gpu: bool = False + + +# One lock guards the slots: one request at a time per GPU. +lock = threading.Lock() +slots = {"image": Slot(), "video": Slot()} +last_used = 0.0 +stop_idle = threading.Event() +idle_thread = None + + +class ImageRequest(BaseModel): + prompt: str + model: str | None = None + n: int = 1 + size: str | None = None + response_format: str | None = "b64_json" + + +class VideoRequest(BaseModel): + prompt: str + model: str | None = None + size: str | None = None + # Base64 PNG or JPEG start frame; required by image-to-video models. + image: str | None = None + + +def _hardware_for_server() -> hardware.Hardware: + detected = hardware.detect() + if GPU_INDEX.isdigit(): + detected.gpus = [gpu for gpu in detected.gpus if gpu.index == int(GPU_INDEX)] + return detected + + +def ollama_models() -> list[str]: + try: + response = requests.get(f"{OLLAMA_URL}/api/ps", timeout=5) + return [entry["name"] for entry in response.json().get("models", [])] + except (requests.RequestException, ValueError, KeyError): + return [] + + +def unload_ollama() -> list[str]: + # Ollama can load again between requests, so this runs before every one. + names = ollama_models() + for name in names: + try: + requests.post( + f"{OLLAMA_URL}/api/generate", + json={"model": name, "keep_alive": 0}, + timeout=30, + ) + except requests.RequestException: + pass + if not names: + return [] + deadline = time.time() + OLLAMA_UNLOAD_TIMEOUT + while (left := ollama_models()) and time.time() < deadline: + time.sleep(0.5) + if left: + print(f"ollama still holds {', '.join(left)} after {OLLAMA_UNLOAD_TIMEOUT:g}s, continuing anyway") + return names + + +def resolve_model(kind: str, model_id: str | None) -> tuple[catalog.Model, str]: + """The catalog entry and local folder for a request; HTTP errors for the client.""" + model_id = (model_id or DEFAULT_MODELS[kind] or "").strip() + if not model_id: + raise HTTPException(400, f"no {kind} model requested and no default is set") + model = catalog.get(model_id) + if model is None or model.kind != kind: + raise HTTPException(404, f"unknown {kind} model {model_id!r}") + path = models.installed_path(model, MODELS_DIR) + if path is None: + raise HTTPException(409, f"model {model_id!r} is not downloaded") + return model, path + + +def _unload(slot: Slot) -> None: + slot.pipe, slot.model, slot.mode, slot.on_gpu = None, None, "", False + # Large pipelines hold reference cycles; collect now so their memory is + # free before the next model loads, not whenever Python next collects. + gc.collect() + pipelines.empty_cache() + + +def _move_to_cpu(slot: Slot) -> None: + """Free the GPU. 4-bit (bitsandbytes) weights cannot move, so they unload.""" + if not slot.on_gpu: + return + if slot.model is not None and slot.model.quantize: + _unload(slot) + return + slot.pipe.to("cpu") + slot.on_gpu = False + pipelines.empty_cache() + + +def claim(kind: str, model: catalog.Model, path: str): + """Make ``model`` the loaded pipeline of its kind and give it the GPU. + + Call with the lock held. The other kind's model leaves the GPU first, and + before loading a new model it unloads completely: a video model needs most + of an ordinary computer's free memory while it loads. + """ + slot = slots[kind] + loading = slot.model is None or slot.model.id != model.id + for other_kind, other in slots.items(): + if other_kind == kind: + continue + if loading: + _unload(other) + else: + _move_to_cpu(other) + if loading: + _unload(slot) + slot.mode = catalog.fit(model, _hardware_for_server()).mode + if slot.mode == catalog.FIT_NONE: + raise HTTPException(507, f"{model.name} does not fit this machine's GPU or memory") + print(f"loading {model.id} ({slot.mode})") + loader = pipelines.load_video if kind == "video" else pipelines.load + try: + slot.pipe = loader(model, path, slot.mode) + except (MemoryError, OSError) as error: + # A half-loaded model would keep its memory and block the next try. + _unload(slot) + raise HTTPException( + 507, f"not enough memory to load {model.name}: {error}" + ) from error + slot.model = model + if slot.mode == catalog.FIT_GPU: + unload_ollama() + if not slot.on_gpu: + slot.pipe.to(pipelines.device()) + slot.on_gpu = True + elif slot.mode == catalog.FIT_OFFLOAD: + # Offloaded models still peak at their largest part on the GPU. + unload_ollama() + return slot.pipe + + +def offload_if_idle() -> bool: + """Move idle models back to the CPU so Ollama has the GPU.""" + with lock: + busy = [slot for slot in slots.values() if slot.on_gpu] + if not busy or time.time() - last_used < IDLE_SECONDS: + return False + for slot in busy: + _move_to_cpu(slot) + print("idle: moved pipelines to cpu") + return True + + +def idle_worker(): + while not stop_idle.wait(IDLE_CHECK_SECONDS): + offload_if_idle() + + +@asynccontextmanager +async def lifespan(app): + global idle_thread + stop_idle.clear() + idle_thread = threading.Thread(target=idle_worker, name="idle-offload", daemon=True) + idle_thread.start() + try: + yield + finally: + stop_idle.set() + idle_thread.join(timeout=5) + + +app = FastAPI(lifespan=lifespan) + + +@app.get("/health") +def health(): + image, video = slots["image"], slots["video"] + return { + "ok": True, + "model": image.model.id if image.model else None, + "mode": image.mode or None, + "on_gpu": image.on_gpu, + "video_model": video.model.id if video.model else None, + "video_mode": video.mode or None, + "video_on_gpu": video.on_gpu, + "pid": os.getpid(), + } + + +@app.get("/v1/models") +def list_models(): + installed = [model for model in catalog.load() if models.installed_path(model, MODELS_DIR)] + return { + "object": "list", + "data": [{"id": model.id, "object": "model", "kind": model.kind} for model in installed], + } + + +@app.post("/v1/images/generations") +def generate(req: ImageRequest): + global last_used + if not req.prompt.strip(): + raise HTTPException(400, "prompt is empty") + model, path = resolve_model("image", req.model) + width, height = pipelines.pick_size(model.family, req.size) + prompt = f"{req.prompt.strip()}, {STYLE}" if STYLE else req.prompt.strip() + data = [] + with lock: + try: + for _ in range(max(1, min(req.n, MAX_IMAGES_PER_REQUEST))): + # Reclaim per image: Ollama may have loaded while the last one painted. + current = claim("image", model, path) + image = pipelines.paint(current, model, prompt, NEGATIVE, width, height) + buffer = io.BytesIO() + image.save(buffer, format="PNG") + data.append({"b64_json": base64.b64encode(buffer.getvalue()).decode()}) + finally: + # torch keeps gigabytes of decode buffers cached; give them back for Ollama. + pipelines.empty_cache() + last_used = time.time() + return {"created": int(time.time()), "data": data} + + +def _decode_image(encoded: str | None): + if not encoded: + return None + from PIL import Image, UnidentifiedImageError + + try: + return Image.open(io.BytesIO(base64.b64decode(encoded, validate=True))).convert("RGB") + except (binascii.Error, ValueError, UnidentifiedImageError) as error: + raise HTTPException(400, f"image is not a base64 PNG or JPEG: {error}") from error + + +@app.post("/v1/videos/generations") +def generate_video(req: VideoRequest): + global last_used + if not req.prompt.strip(): + raise HTTPException(400, "prompt is empty") + model, path = resolve_model("video", req.model) + image = _decode_image(req.image) + if model.image_to_video and image is None: + raise HTTPException(400, f"{model.name} animates a start image: send it as image") + width, height = pipelines.pick_video_size(model.family, req.size) + with lock: + try: + current = claim("video", model, path) + clip = pipelines.animate( + current, model, req.prompt.strip(), VIDEO_NEGATIVE, width, height, image + ) + finally: + pipelines.empty_cache() + last_used = time.time() + return { + "created": int(time.time()), + "data": [{ + "b64_mp4": base64.b64encode(clip).decode(), + "seconds": round(model.frames / model.fps, 2), + "width": width, + "height": height, + }], + } + + +if __name__ == "__main__": + uvicorn.run(app, host=HOST, port=PORT) diff --git a/pyproject.toml b/pyproject.toml index bf8dfaf44e..2409b20233 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,7 +78,7 @@ dev = [ # 因此与 app 服务层、服务启动入口和面向 Agent 用户发布的 Skill 脚本一起 # 纳入统计,避免覆盖率只反映局部模块。 branch = true -source = ["app", "cli", "webui", "main", "docs/skill"] +source = ["app", "cli", "webui", "local_ai", "main", "docs/skill"] [tool.coverage.report] # Python 3.11 和 3.13 完整测试的实测分支覆盖率均超过 70.4%。以 70% @@ -89,7 +89,7 @@ show_missing = true skip_covered = true [tool.hatch.build.targets.wheel] -packages = ["app", "webui"] +packages = ["app", "webui", "local_ai"] # 字体保留中英文默认字体和越南语字体。两个大体积备用字重不进入安装包, # 避免 wheel 超过 PyPI 默认文件大小限制;源码版的字体文件保持原样。 @@ -112,7 +112,7 @@ exclude = ["**/.DS_Store", "**/__pycache__", "**/*.pyc", "**/.streamlit"] [tool.hatch.build.targets.sdist] # 使用白名单,禁止本机配置、凭据、storage、AGENTS.md 等混入源码发行包。 include = [ - "/app", "/cli.py", "/webui", "/config.example.toml", "/pyproject.toml", + "/app", "/cli.py", "/webui", "/local_ai", "/config.example.toml", "/pyproject.toml", "/uv.lock", "/LICENSE", "/docs/pypi-description.md", "/docs/packaging.md", "/test/services/test_packaging.py", "/resource/songs", "/resource/public", "/resource/fonts/STHeitiMedium.ttc", "/resource/fonts/MicrosoftYaHeiBold.ttc", diff --git a/test/services/test_infistar_video_webui.py b/test/services/test_infistar_video_webui.py index b3c78d18a0..03d6fbf430 100644 --- a/test/services/test_infistar_video_webui.py +++ b/test/services/test_infistar_video_webui.py @@ -64,7 +64,7 @@ def test_infistar_video_models_specs_and_billing_confirmation(language): assert providers.options == [messages.get(key, english.get(key, key)) for key in ( "Metaso MiniMax H3", "OFox AI Video", "Shengsuan Cloud AI Video", "Volcano Engine Seedance", "Infistar AI Video", "Jalapeno Cloud AI Video", - "WaveSpeed AI Video", "MuAPI AI Video", + "WaveSpeed AI Video", "MuAPI AI Video", "local_ai.video.label", )] assert providers.value == "infistar" assert not any(item.key in { diff --git a/test/services/test_local_ai.py b/test/services/test_local_ai.py new file mode 100644 index 0000000000..0cfaf2b1cb --- /dev/null +++ b/test/services/test_local_ai.py @@ -0,0 +1,420 @@ +"""Local AI package: hardware parsing, model choice, runtime plan, downloads and the GPU server. + +None of these tests need torch, a GPU or the network: the server runs with a +stand-in pipeline, and downloads with a stand-in for huggingface_hub. +""" + +import base64 +import io +import json +from types import SimpleNamespace + +import pytest +from fastapi.testclient import TestClient +from PIL import Image + +from local_ai import catalog, hardware, models, runtime +from local_ai import server as srv + + +def _hardware(vram=None, ram=32.0, driver="610.88", os_name="Windows", vendor="nvidia"): + gpus = [] if vram is None else [hardware.Gpu(0, "GPU", vendor, vram)] + return hardware.Hardware(os=os_name, machine="AMD64", ram_gb=ram, gpus=gpus, nvidia_driver=driver) + + +# ---------------------------------------------------------------- hardware + + +def test_parse_nvidia_gpus_reads_every_card_and_the_driver(): + output = ( + "0, NVIDIA GeForce RTX 4070 Ti SUPER, 16376, 14420, 610.88\n" + "1, NVIDIA GeForce RTX 4060, 8188, 7956, 610.88\n" + "garbage line\n" + ) + gpus, driver = hardware.parse_nvidia_gpus(output) + assert [(gpu.index, gpu.vram_gb) for gpu in gpus] == [(0, 16.0), (1, 8.0)] + assert driver == "610.88" + + +@pytest.mark.parametrize( + "header, expected", + [ + ("| NVIDIA-SMI 550.54 Driver Version: 550.54 CUDA Version: 12.4 |", (12, 4)), + ("| NVIDIA-SMI 610.88 KMD Version: 610.88 CUDA UMD Version: 13.3 |", (13, 3)), + ("no cuda here", None), + ], +) +def test_parse_cuda_version_handles_old_and_new_driver_headers(header, expected): + assert hardware.parse_cuda_version(header) == expected + + +def test_best_gpu_is_the_one_with_most_memory(): + detected = hardware.Hardware( + os="Windows", machine="AMD64", ram_gb=64, + gpus=[hardware.Gpu(0, "small", "nvidia", 8), hardware.Gpu(1, "big", "nvidia", 16)], + ) + assert detected.best_gpu.index == 1 + assert detected.to_dict()["best_gpu_index"] == 1 + + +# ---------------------------------------------------------------- catalog + + +def test_catalog_entries_are_complete(): + entries = catalog.load() + assert len({model.id for model in entries}) == len(entries) + for model in entries: + assert model.kind in ("image", "video") + families = ("sdxl", "sd15", "flux") if model.kind == "image" else ("wan22", "wan21") + assert model.family in families + assert model.vram_offload_gb <= model.vram_gpu_gb + assert model.allow_patterns + if model.kind == "video": + # Wan needs 4n+1 frames; fps and frames give the clip length. + assert model.fps > 0 and model.frames % 4 == 1 + + +@pytest.mark.parametrize( + "vram, expected", + [ + # 4-bit loading lets Wan 2.2 run fully on a 16 GB card. + (16, ("wan2.2-ti2v-5b", catalog.FIT_GPU)), + (8, ("wan2.2-ti2v-5b", catalog.FIT_OFFLOAD)), + (4, None), + ], +) +def test_recommend_video_model_for_hardware(vram, expected): + choice = catalog.recommend(_hardware(vram, 64), "video") + assert ((choice.model.id, choice.mode) if choice else None) == expected + + +@pytest.mark.parametrize( + "vram, ram, expected_id, expected_mode", + [ + (16, 64, "realvisxl-v5-lightning", catalog.FIT_GPU), + # Offloading SDXL costs seconds; it still beats SD 1.5 fully on the GPU. + (8, 32, "realvisxl-v5-lightning", catalog.FIT_OFFLOAD), + (4, 16, "dreamshaper-8", catalog.FIT_GPU), + (None, 16, "dreamshaper-8", catalog.FIT_CPU), + # FLUX would fit with offload here, but it is gated: an open model wins. + (24, 64, "realvisxl-v5-lightning", catalog.FIT_GPU), + ], +) +def test_recommend_picks_best_open_model_for_hardware(vram, ram, expected_id, expected_mode): + choice = catalog.recommend(_hardware(vram, ram)) + assert (choice.model.id, choice.mode) == (expected_id, expected_mode) + + +def test_rank_puts_gpu_options_before_cpu_and_unusable_last(): + ranked = catalog.rank(_hardware(24, 64)) + assert ranked[0].model.id == "flux1-schnell" # best quality among GPU options + assert ranked[0].mode == catalog.FIT_OFFLOAD + assert catalog.rank(_hardware(None, 4))[-1].mode == catalog.FIT_NONE + + +def test_recommend_returns_none_when_nothing_fits(): + assert catalog.recommend(_hardware(None, 2)) is None + + +# ---------------------------------------------------------------- runtime + + +@pytest.mark.parametrize( + "driver, expected", + [ + ("610.88", "cu128"), + ("575.10", "cu128"), + ("565.90", "cu126"), + ("531.00", "cpu"), + ], +) +def test_torch_index_matches_driver(driver, expected): + assert runtime.torch_index(_hardware(16, driver=driver)).endswith(f"/{expected}") + + +def test_torch_index_uses_pypi_on_macos_and_cpu_wheels_without_gpu(): + assert runtime.torch_index(_hardware(None, os_name="Darwin")) is None + assert runtime.torch_index(_hardware(None, os_name="Linux")).endswith("/cpu") + + +def test_install_commands_prefer_uv(monkeypatch, tmp_path): + monkeypatch.setattr(runtime.shutil, "which", lambda name: "/bin/uv" if name == "uv" else None) + commands = runtime.install_commands(str(tmp_path), _hardware(16)) + assert [command[:2] for command in commands] == [["/bin/uv", "venv"], ["/bin/uv", "pip"], ["/bin/uv", "pip"]] + assert "--index-url" in commands[1] and commands[1][-1].endswith("/cu128") + + +def test_install_marks_runtime_only_after_every_command_succeeds(monkeypatch, tmp_path): + calls = [] + + def run(command, **_kwargs): + calls.append(command) + return SimpleNamespace(returncode=1 if len(calls) == 2 else 0) + + monkeypatch.setattr(runtime.subprocess, "run", run) + log = tmp_path / "install.log" + with pytest.raises(RuntimeError): + runtime.install(str(tmp_path / "rt"), _hardware(16), str(log)) + assert not runtime.is_installed(str(tmp_path / "rt")) + + calls.clear() + monkeypatch.setattr(runtime.subprocess, "run", lambda command, **_k: SimpleNamespace(returncode=0)) + (tmp_path / "rt" / "venv").mkdir(parents=True) + runtime.install(str(tmp_path / "rt"), _hardware(16), str(log)) + assert runtime.is_installed(str(tmp_path / "rt")) + assert "$ " in log.read_text(encoding="utf-8") + + +def test_runtime_with_older_requirements_counts_as_missing(tmp_path, monkeypatch): + requirements = tmp_path / "requirements.txt" + requirements.write_bytes(b"diffusers\r\nfastapi\r\n") + monkeypatch.setattr(runtime, "REQUIREMENTS", requirements) + marker = tmp_path / "rt" / "venv" / ".mpt-installed" + marker.parent.mkdir(parents=True) + marker.write_text(f"cu128 {runtime._requirements_digest()}", encoding="utf-8") + assert runtime.is_installed(str(tmp_path / "rt")) + # The same requirements checked out with LF line endings are not a change. + requirements.write_bytes(b"diffusers\nfastapi\n") + assert runtime.is_installed(str(tmp_path / "rt")) + requirements.write_bytes(b"diffusers\nfastapi\nftfy\n") + assert not runtime.is_installed(str(tmp_path / "rt")) + + +# ---------------------------------------------------------------- downloads + + +@pytest.fixture +def realvis(): + return catalog.get("realvisxl-v5-lightning") + + +def test_only_a_folder_with_a_complete_marker_counts_as_installed(tmp_path, realvis): + folder = models.model_dir(realvis, str(tmp_path)) + folder.mkdir(parents=True) + (folder / "model_index.json").write_text("{}") + assert models.installed_path(realvis, str(tmp_path)) is None + + (folder / models.MARKER_NAME).write_text(json.dumps({"repo": "someone/else"})) + assert models.installed_path(realvis, str(tmp_path)) is None + + (folder / models.MARKER_NAME).write_text(json.dumps({"repo": realvis.repo})) + assert models.installed_path(realvis, str(tmp_path)) == str(folder) + + models.delete(realvis, str(tmp_path)) + assert not folder.exists() + + +def test_download_job_reports_progress_and_marks_complete(monkeypatch, tmp_path, realvis): + import huggingface_hub + + def fake_snapshot(repo, local_dir, **kwargs): + assert repo == realvis.repo + assert kwargs["allow_patterns"] == list(realvis.allow_patterns) + (tmp_path / realvis.id / "unet").mkdir(parents=True, exist_ok=True) + (tmp_path / realvis.id / "unet" / "weights.bin").write_bytes(b"x" * 100) + return local_dir + + monkeypatch.setattr(models, "remote_size", lambda model, token=None: 100) + monkeypatch.setattr(huggingface_hub, "snapshot_download", fake_snapshot) + job = models.DownloadJob(realvis, str(tmp_path)) + job._run() + assert job.state == models.STATE_DONE, job.error + assert job.to_dict()["bytes_done"] == 100 + assert models.installed_path(realvis, str(tmp_path)) + + +def test_download_refuses_when_the_disk_is_too_full(monkeypatch, tmp_path, realvis): + monkeypatch.setattr(models, "remote_size", lambda model, token=None: 50 * 1024**3) + monkeypatch.setattr(models, "free_disk_gb", lambda path: 20.0) + job = models.DownloadJob(realvis, str(tmp_path)) + job._run() + assert job.state == models.STATE_FAILED + assert "GB is free" in job.error + assert models.installed_path(realvis, str(tmp_path)) is None + + +# ---------------------------------------------------------------- server + + +class FakePipe: + def __init__(self): + self.devices = [] + + def to(self, device): + self.devices.append(device) + return self + + +def _mark_installed(model, models_dir): + folder = models.model_dir(model, str(models_dir)) + folder.mkdir(parents=True) + (folder / models.MARKER_NAME).write_text(json.dumps({"repo": model.repo})) + + +@pytest.fixture +def server(monkeypatch, tmp_path, realvis): + wan = catalog.get("wan2.2-ti2v-5b") + _mark_installed(realvis, tmp_path) + _mark_installed(wan, tmp_path) + calls = SimpleNamespace(painted=[], loaded=[], unloads=[], animated=[]) + monkeypatch.setattr(srv, "MODELS_DIR", str(tmp_path)) + monkeypatch.setattr(srv, "DEFAULT_MODELS", {"image": realvis.id, "video": wan.id}) + monkeypatch.setattr(srv, "_hardware_for_server", lambda: _hardware(16, 64)) + monkeypatch.setattr(srv, "unload_ollama", lambda: calls.unloads.append(True) or []) + monkeypatch.setattr(srv.pipelines, "device", lambda: "cuda") + monkeypatch.setattr(srv.pipelines, "empty_cache", lambda: None) + + def fake_load(model, path, mode): + calls.loaded.append((model.id, path, mode)) + return FakePipe() + + def fake_paint(pipe, model, prompt, negative, width, height): + calls.painted.append({"prompt": prompt, "size": (width, height)}) + return Image.new("RGB", (8, 8)) + + def fake_animate(pipe, model, prompt, negative, width, height, image=None): + calls.animated.append({"size": (width, height), "image": image.size if image else None}) + return b"mp4-bytes" + + monkeypatch.setattr(srv.pipelines, "load", fake_load) + monkeypatch.setattr(srv.pipelines, "load_video", fake_load) + monkeypatch.setattr(srv.pipelines, "paint", fake_paint) + monkeypatch.setattr(srv.pipelines, "animate", fake_animate) + monkeypatch.setattr(srv, "slots", {"image": srv.Slot(), "video": srv.Slot()}) + with TestClient(srv.app) as client: + calls.client = client + yield calls + + +def _png_b64(size=(8, 8)): + buffer = io.BytesIO() + Image.new("RGB", size).save(buffer, format="PNG") + return base64.b64encode(buffer.getvalue()).decode() + + +def test_server_paints_with_installed_model_on_gpu(server, realvis): + response = server.client.post( + "/v1/images/generations", json={"prompt": "a lighthouse", "size": "1024x1536"} + ) + assert response.status_code == 200 + image = Image.open(io.BytesIO(base64.b64decode(response.json()["data"][0]["b64_json"]))) + assert image.size == (8, 8) + assert server.loaded == [(realvis.id, str(models.model_dir(realvis, srv.MODELS_DIR)), catalog.FIT_GPU)] + assert server.painted[0]["size"] == (832, 1216) + assert server.painted[0]["prompt"].startswith("a lighthouse, ") + assert server.unloads, "Ollama must give up the GPU before painting" + health = server.client.get("/health").json() + assert health["model"] == realvis.id and health["on_gpu"] is True + + +def test_server_rejects_models_that_are_unknown_or_not_downloaded(server): + assert server.client.post( + "/v1/images/generations", json={"prompt": "x", "model": "nope"} + ).status_code == 404 + assert server.client.post( + "/v1/images/generations", json={"prompt": "x", "model": "dreamshaper-8"} + ).status_code == 409 + # A video model is not an image model. + assert server.client.post( + "/v1/images/generations", json={"prompt": "x", "model": "wan2.2-ti2v-5b"} + ).status_code == 404 + assert server.client.post("/v1/images/generations", json={"prompt": " "}).status_code == 400 + listed = {entry["id"]: entry["kind"] for entry in server.client.get("/v1/models").json()["data"]} + assert listed == {"realvisxl-v5-lightning": "image", "wan2.2-ti2v-5b": "video"} + + +def test_video_takes_the_gpu_from_the_image_model(server, realvis): + server.client.post("/v1/images/generations", json={"prompt": "a lighthouse"}) + response = server.client.post( + "/v1/videos/generations", + json={"prompt": "waves", "size": "1024x1536", "image": _png_b64((16, 24))}, + ) + assert response.status_code == 200 + clip = response.json()["data"][0] + assert base64.b64decode(clip["b64_mp4"]) == b"mp4-bytes" + assert clip["seconds"] == round(73 / 24, 2) + assert server.animated == [{"size": (480, 832), "image": (16, 24)}] + # Loading the video model unloads the image model to free memory. + assert srv.slots["image"].model is None and srv.slots["image"].pipe is None + health = server.client.get("/health").json() + assert health["video_model"] == "wan2.2-ti2v-5b" + assert health["video_mode"] == catalog.FIT_GPU + + +def test_switching_back_to_loaded_model_only_moves_it(server, realvis): + server.client.post("/v1/videos/generations", json={"prompt": "w", "image": _png_b64()}) + server.client.post("/v1/images/generations", json={"prompt": "a lighthouse"}) + server.client.post("/v1/images/generations", json={"prompt": "a harbour"}) + # One load per model: the second image reuses the loaded pipeline. + assert [loaded[0] for loaded in server.loaded] == ["wan2.2-ti2v-5b", realvis.id] + + +def test_quantized_model_unloads_instead_of_moving_to_cpu(server, monkeypatch): + server.client.post("/v1/videos/generations", json={"prompt": "w", "image": _png_b64()}) + monkeypatch.setattr(srv, "last_used", 0.0) + assert srv.offload_if_idle() is True + assert srv.slots["video"].pipe is None + + +def test_image_to_video_model_needs_a_valid_start_image(server): + assert server.client.post("/v1/videos/generations", json={"prompt": "waves"}).status_code == 400 + assert server.client.post( + "/v1/videos/generations", json={"prompt": "waves", "image": "not base64!"} + ).status_code == 400 + assert server.animated == [] + + +def test_idle_model_moves_back_to_cpu(server, monkeypatch): + server.client.post("/v1/images/generations", json={"prompt": "a lighthouse"}) + monkeypatch.setattr(srv, "last_used", 0.0) + assert srv.offload_if_idle() is True + assert srv.slots["image"].pipe.devices[-1] == "cpu" + assert srv.slots["image"].on_gpu is False + + +@pytest.mark.parametrize( + "size, expected", + [("1024x1536", (480, 832)), ("1536x1024", (832, 480)), ("1024x1024", (640, 640))], +) +def test_pick_video_size_keeps_clips_near_480p(size, expected): + assert srv.pipelines.pick_video_size("wan22", size) == expected + + +@pytest.mark.parametrize( + "family, size, expected", + [ + ("sdxl", "1024x1536", (832, 1216)), + ("sdxl", "1536x1024", (1216, 832)), + ("sd15", "1024x1536", (512, 768)), + ("flux", None, (768, 1344)), + ("sdxl", "bad", (768, 1344)), + ], +) +def test_pick_size_snaps_to_trained_buckets(family, size, expected): + assert srv.pipelines.pick_size(family, size) == expected + + +def test_convert_to_bf16_writes_small_bfloat16_shards_once(tmp_path, monkeypatch): + torch = pytest.importorskip("torch") + from safetensors.torch import load_file, save_file + + from local_ai import pipelines + + monkeypatch.setattr(pipelines, "_MAX_SHARD_BYTES", 40) + component = tmp_path / "transformer" + component.mkdir() + save_file({"a": torch.ones(8), "b": torch.ones(8)}, str(component / "m-00001-of-00002.safetensors")) + save_file({"c": torch.ones(8), "step": torch.tensor([1])}, str(component / "m-00002-of-00002.safetensors")) + weight_map = {"a": "m-00001-of-00002.safetensors", "b": "m-00001-of-00002.safetensors", + "c": "m-00002-of-00002.safetensors", "step": "m-00002-of-00002.safetensors"} + (component / "m.safetensors.index.json").write_text(json.dumps({"weight_map": weight_map})) + + pipelines.convert_to_bf16(str(component)) + + index = json.loads((component / "m.safetensors.index.json").read_text()) + assert set(index["weight_map"].values()) == {"m-mpt-00001.safetensors", "m-mpt-00002.safetensors"} + for key, name in index["weight_map"].items(): + dtype = load_file(str(component / name))[key].dtype + assert dtype == (torch.int64 if key == "step" else torch.bfloat16) + assert not (component / "m-00001-of-00002.safetensors").exists() + assert (component / pipelines.BF16_MARKER).exists() diff --git a/test/services/test_local_ai_service.py b/test/services/test_local_ai_service.py new file mode 100644 index 0000000000..b73779bbcf --- /dev/null +++ b/test/services/test_local_ai_service.py @@ -0,0 +1,332 @@ +"""本机 AI 生图在主程序中的接入:服务客户端、素材来源与任务预检。""" + +import base64 +import io +import json +from types import SimpleNamespace +from unittest.mock import patch + +import pytest +from PIL import Image + +from app.config import config +from app.models.schema import VideoAspect, VideoParams +from app.services import local_ai, material, state as sm +from app.services import task as task_service +from local_ai import catalog, hardware, models, runtime + + +def _png_bytes(size=(16, 24)): + buffer = io.BytesIO() + Image.new("RGB", size, "white").save(buffer, format="PNG") + return buffer.getvalue() + + +@pytest.fixture +def local_settings(tmp_path): + settings = dict( + config.app, + local_ai_models_dir=str(tmp_path / "models"), + local_ai_runtime_dir=str(tmp_path / "runtime"), + local_ai_image_model="", + local_ai_gpu_index="", + local_ai_url="http://127.0.0.1:7999", + ) + detected = hardware.Hardware( + os="Windows", machine="AMD64", ram_gb=64, + gpus=[hardware.Gpu(0, "big", "nvidia", 16), hardware.Gpu(1, "small", "nvidia", 8)], + nvidia_driver="610.88", + ) + with ( + patch.object(config, "app", settings), + patch.object(local_ai, "detect_hardware", return_value=detected), + ): + yield SimpleNamespace(settings=settings, tmp_path=tmp_path) + + +def _install(model_id, models_dir): + model = catalog.get(model_id) + folder = models.model_dir(model, models_dir) + folder.mkdir(parents=True, exist_ok=True) + (folder / models.MARKER_NAME).write_text(json.dumps({"repo": model.repo})) + + +def _install_runtime(runtime_dir): + venv = runtime_dir / "venv" + venv.mkdir(parents=True, exist_ok=True) + (venv / ".mpt-installed").write_text(f"cu128 {runtime._requirements_digest()}") + + +def test_readiness_names_the_missing_step(local_settings): + assert local_ai.readiness() == (False, "local_ai.runtime_missing") + _install_runtime(local_settings.tmp_path / "runtime") + assert local_ai.readiness() == (False, "local_ai.model_missing") + _install("realvisxl-v5-lightning", local_settings.settings["local_ai_models_dir"]) + assert local_ai.readiness() == (True, "") + + +def test_image_model_uses_selection_then_recommendation(local_settings): + assert local_ai.image_model().id == "realvisxl-v5-lightning" + local_settings.settings["local_ai_image_model"] = "dreamshaper-8" + assert local_ai.image_model().id == "dreamshaper-8" + local_settings.settings["local_ai_image_model"] = "removed-model" + assert local_ai.image_model().id == "realvisxl-v5-lightning" + + +def test_gpu_index_limits_the_hardware_used_for_fitting(local_settings): + local_settings.settings["local_ai_gpu_index"] = "1" + assert [gpu.index for gpu in local_ai.gpu_hardware().gpus] == [1] + assert catalog.recommend(local_ai.gpu_hardware()).mode == catalog.FIT_OFFLOAD + + +class _Response: + def __init__(self, status, body): + self.status_code = status + self.ok = status < 400 + self._body = body + self.text = json.dumps(body) + + def json(self): + return self._body + + +class _Session: + def __init__(self, response, calls): + self.response = response + self.calls = calls + self.trust_env = True + + def __enter__(self): + return self + + def __exit__(self, *exc): + return False + + def post(self, url, json=None, timeout=None): + self.calls.append({"url": url, "json": json, "trust_env": self.trust_env}) + return self.response + + +def _patch_session(response, calls): + original = local_ai._session + + def make(): + session = original() + fake = _Session(response, calls) + fake.trust_env = session.trust_env + return fake + + return patch.object(local_ai, "_session", make) + + +def test_generate_image_posts_to_local_server_without_proxies(local_settings): + calls = [] + png = _png_bytes() + response = _Response(200, {"data": [{"b64_json": base64.b64encode(png).decode()}]}) + with ( + patch.object(local_ai, "ensure_server", return_value={"ok": True}), + _patch_session(response, calls), + ): + assert local_ai.generate_image("a lighthouse", "1024x1536") == png + assert calls == [{ + "url": "http://127.0.0.1:7999/v1/images/generations", + "json": {"model": "realvisxl-v5-lightning", "prompt": "a lighthouse", "n": 1, "size": "1024x1536"}, + "trust_env": False, + }] + + +def test_generate_image_reports_server_detail(local_settings): + response = _Response(409, {"detail": "model 'realvisxl-v5-lightning' is not downloaded"}) + with ( + patch.object(local_ai, "ensure_server", return_value={"ok": True}), + _patch_session(response, []), + pytest.raises(local_ai.LocalAIError, match="409.*not downloaded"), + ): + local_ai.generate_image("x", "1024x1024") + + +def test_ensure_server_does_not_start_when_autostart_is_off(local_settings): + local_settings.settings["local_ai_autostart"] = False + with ( + patch.object(local_ai, "server_status", return_value=None), + patch.object(local_ai, "start_server") as start, + pytest.raises(local_ai.LocalAIError, match="not running"), + ): + local_ai.ensure_server() + start.assert_not_called() + + +def test_stop_server_ends_the_process_the_server_reports(local_settings): + # Windows 虚拟环境的 python.exe 是启动器:服务真正的 pid 来自健康检查。 + with ( + patch.object(local_ai, "server_status", return_value={"ok": True, "pid": 4242}), + patch.object(local_ai.os, "kill") as kill, + ): + local_ai.stop_server() + kill.assert_called_once_with(4242, local_ai.signal.SIGTERM) + + +def test_generate_images_local_saves_png_as_image_material(local_settings, tmp_path): + with patch.object(local_ai, "generate_image", return_value=_png_bytes((16, 24))) as generate: + items = material.generate_images_local( + "a lighthouse", 5, VideoAspect.portrait, save_dir=str(tmp_path) + ) + generate.assert_called_once_with("a lighthouse", "1024x1536") + assert len(items) == 1 + assert items[0].provider == "local_image" + assert items[0].duration == 5 + assert items[0].source_info["rendition"]["width"] == 16 + assert Image.open(items[0].url).size == (16, 24) + + +def test_generate_images_local_stops_the_task_when_the_server_fails(local_settings, tmp_path): + with ( + patch.object(local_ai, "generate_image", side_effect=local_ai.LocalAIError("server exited")), + pytest.raises(material.LocalImageMaterialError, match="server exited"), + ): + material.generate_images_local("x", 5, VideoAspect.portrait, save_dir=str(tmp_path)) + # 失败属于 OpenAIImagePaidResultError,任务编排层已有的处理会终止素材阶段。 + assert issubclass(material.LocalImageMaterialError, material.OpenAIImagePaidResultError) + + +def test_download_videos_routes_local_image_to_the_local_generator(local_settings, tmp_path): + with patch.object(material, "_download_videos_openai_image_on_demand", return_value=["clip.mp4"]) as flow: + result = material.download_videos( + task_id="local-task", + search_terms=["a lighthouse"], + source="local_image", + video_aspect=VideoAspect.portrait, + audio_duration=5, + max_clip_duration=5, + ) + assert result == ["clip.mp4"] + options = flow.call_args.kwargs + assert options["image_generator"] is material.generate_images_local + assert options["provider"] == "local_image" + + +def test_task_preflight_fails_before_script_when_local_ai_is_not_ready(local_settings): + params = VideoParams(video_subject="Local preflight", video_source="local_image") + with ( + patch.object(task_service.sm, "state", sm.MemoryState()), + patch.object(task_service, "generate_script") as generate_script, + ): + result = task_service.start("local-preflight", params, stop_at="materials") + assert result["failed_stage"] == "preflight" + assert "install it in Settings" in result["error"] + generate_script.assert_not_called() + + +# ---------------------------------------------------------------- 本机视频 + + +def test_video_readiness_needs_the_image_model_only_for_image_to_video(local_settings): + _install_runtime(local_settings.tmp_path / "runtime") + models_dir = local_settings.settings["local_ai_models_dir"] + _install("wan2.2-ti2v-5b", models_dir) + # Wan 2.2 TI2V 动画的是每个画面的图片,生图模型缺失时视频也无法生成。 + assert local_ai.readiness("video") == (False, "local_ai.model_missing") + _install("realvisxl-v5-lightning", models_dir) + assert local_ai.readiness("video") == (True, "") + + local_settings.settings["local_ai_video_model"] = "wan2.1-t2v-1.3b" + _install("wan2.1-t2v-1.3b", models_dir) + models.delete(catalog.get("realvisxl-v5-lightning"), models_dir) + assert local_ai.readiness("video") == (True, "") + + +def test_generate_video_sends_the_start_image(local_settings): + calls = [] + clip = base64.b64encode(b"mp4").decode() + response = _Response(200, {"data": [{"b64_mp4": clip, "seconds": 3.04}]}) + with ( + patch.object(local_ai, "ensure_server", return_value={"ok": True}), + _patch_session(response, calls), + ): + assert local_ai.generate_video("waves", b"png", "1024x1536") == (b"mp4", 3.04) + assert calls[0]["url"].endswith("/v1/videos/generations") + assert calls[0]["json"] == { + "model": "wan2.2-ti2v-5b", + "prompt": "waves", + "size": "1024x1536", + "image": base64.b64encode(b"png").decode(), + } + + +def _local_video_flow(tmp_path, terms, duration=6, max_clip=5): + return material._download_videos_local_video_on_demand( + task_id="local-video-task", + search_terms=terms, + video_aspect=VideoAspect.portrait, + audio_duration=duration, + max_clip_duration=max_clip, + material_directory=str(tmp_path), + ) + + +def test_local_video_animates_scene_images_until_the_audio_is_covered(local_settings, tmp_path): + prompts = [] + + def fake_video(prompt, image, size): + prompts.append((prompt, image is not None, size)) + return b"clip", 3.0 + + with ( + patch.object(local_ai, "generate_image", return_value=_png_bytes()), + patch.object(local_ai, "generate_video", side_effect=fake_video), + patch.object(material, "_persist_material_sources") as persist, + ): + paths = _local_video_flow(tmp_path, ["lake", "library", "street", "forest"]) + # 两个 3 秒片段即覆盖 6 秒配音,不再为后续画面生成。 + assert prompts == [("lake", True, "1024x1536"), ("library", True, "1024x1536")] + assert len(paths) == 2 + assert all(open(path, "rb").read() == b"clip" for path in paths) + sources = persist.call_args.args[1] + assert [source["provider"] for source in sources] == ["local_video", "local_video"] + + +def test_failed_clip_falls_back_to_the_scene_image(local_settings, tmp_path): + with ( + patch.object(local_ai, "generate_image", return_value=_png_bytes()), + patch.object(local_ai, "generate_video", side_effect=local_ai.LocalAIError("out of memory")), + patch.object(material, "_render_openai_image_video", return_value="zoom.mp4") as render, + patch.object(material, "_persist_material_sources"), + ): + paths = _local_video_flow(tmp_path, ["lake", "library"], duration=5) + assert paths == ["zoom.mp4"] + render.assert_called_once() + + +def test_text_to_video_model_fails_the_task_without_a_fallback(local_settings, tmp_path): + local_settings.settings["local_ai_video_model"] = "wan2.1-t2v-1.3b" + with ( + patch.object(local_ai, "generate_image") as generate_image, + patch.object(local_ai, "generate_video", side_effect=local_ai.LocalAIError("server exited")), + patch.object(material, "_persist_material_sources"), + pytest.raises(material.LocalImageMaterialError, match="server exited"), + ): + _local_video_flow(tmp_path, ["lake"]) + generate_image.assert_not_called() + + +def test_download_videos_routes_local_video(local_settings): + with patch.object(material, "_download_videos_local_video_on_demand", return_value=["c.mp4"]) as flow: + assert material.download_videos( + task_id="t", search_terms=["lake"], source="local_video", + video_aspect=VideoAspect.portrait, audio_duration=5, max_clip_duration=5, + ) == ["c.mp4"] + assert flow.call_args.kwargs["search_terms"] == ["lake"] + + +def test_task_preflight_checks_video_models_for_local_video(local_settings): + _install_runtime(local_settings.tmp_path / "runtime") + _install("realvisxl-v5-lightning", local_settings.settings["local_ai_models_dir"]) + params = VideoParams(video_subject="Local video", video_source="local_video") + with ( + patch.object(task_service.sm, "state", sm.MemoryState()), + patch.object(task_service, "generate_script") as generate_script, + ): + result = task_service.start("local-video-preflight", params, stop_at="materials") + assert result["failed_stage"] == "preflight" + assert "not downloaded" in result["error"] + generate_script.assert_not_called() diff --git a/test/services/test_settings_navigation.py b/test/services/test_settings_navigation.py index bdc6260fb0..96006a0bc6 100644 --- a/test/services/test_settings_navigation.py +++ b/test/services/test_settings_navigation.py @@ -29,6 +29,8 @@ "muapi": ("ai_video", "muapi_api_key_input"), "openai_image": ("ai_image", "openai_image_base_url_input"), "codex_image": ("ai_image", "codex_image_timeout_input"), + "local_image": ("ai_image", "local_ai_hf_api_token_input_image"), + "local_video": ("ai_video", "local_ai_hf_api_token_input_video"), } @@ -144,6 +146,8 @@ def test_general_render_does_not_activate_or_mount_provider_forms_or_catalogs(): "muapi": "MuAPI AI Video", "openai_image": "OpenAI Compatible Text-to-Image", "codex_image": "codex.image.label", + "local_image": "local_ai.label", + "local_video": "local_ai.video.label", } diff --git a/test/services/test_task.py b/test/services/test_task.py index 4d69c8cdcf..d0661ac654 100644 --- a/test/services/test_task.py +++ b/test/services/test_task.py @@ -2074,8 +2074,8 @@ def patch_task(self, task_id, **kwargs): def test_cross_post_generates_caption_for_non_youtube_platforms(self): """ - TikTok/Instagram 发布同样要生成一次社交文案,并把 caption 作为所有 - 成片共享的发布标题,而不是直接发送原始主题。 + TikTok/Instagram 发布要为每个平台各生成一次社交文案,并把第一个平台的 + caption 作为所有成片共享的通用标题,而不是直接发送原始主题。 """ metadata = { "title": "Coffee Hook", @@ -2084,11 +2084,11 @@ def test_cross_post_generates_caption_for_non_youtube_platforms(self): } state = MemoryState() cases = { - "tiktok-first": (("tiktok", "instagram"), "tiktok"), - "instagram-first": (("instagram", "tiktok"), "instagram_reels"), + "tiktok-first": (("tiktok", "instagram"), ("tiktok", "instagram_reels")), + "instagram-first": (("instagram", "tiktok"), ("instagram_reels", "tiktok")), } - for case_name, (platforms, expected_platform) in cases.items(): + for case_name, (platforms, expected_platforms) in cases.items(): with self.subTest(case=case_name): task_id = f"caption-{case_name}" state.update_task( @@ -2121,15 +2121,29 @@ def test_cross_post_generates_caption_for_non_youtube_platforms(self): "private", ) - generate_metadata.assert_called_once_with( - video_subject="Coffee", - video_script="A short coffee story.", - language="en", - platform=expected_platform, + # 每个平台各生成一次文案;通用 title 仍取第一个平台的文案。 + self.assertEqual( + [call.kwargs for call in generate_metadata.call_args_list], + [ + { + "video_subject": "Coffee", + "video_script": "A short coffee story.", + "language": "en", + "platform": social_platform, + } + for social_platform in expected_platforms + ], ) cross_post.assert_called_once() call = cross_post.call_args self.assertEqual(call.kwargs["title"], "Watch this coffee ritual.") + self.assertEqual( + call.kwargs["platform_titles"], + { + platform: "Watch this coffee ritual.\n\n#coffee" + for platform in platforms + }, + ) self.assertEqual(call.kwargs["platforms"], list(platforms)) self.assertIsNone(call.kwargs["youtube_extra"]) task = state.get_task(task_id) diff --git a/test/services/test_upload_post_platform_text.py b/test/services/test_upload_post_platform_text.py new file mode 100644 index 0000000000..39f9ad2634 --- /dev/null +++ b/test/services/test_upload_post_platform_text.py @@ -0,0 +1,99 @@ +"""每个平台单独的发布文案与 AI 生成标注。""" + +from unittest.mock import MagicMock, mock_open, patch + +from app.services import task as tm +from app.services.state import MemoryState +from app.services.upload_post import UploadPostService + +_CONFIG = { + "upload_post_enabled": True, + "upload_post_api_key": "test-key", + "upload_post_username": "testuser", + "upload_post_platforms": ["tiktok", "instagram", "youtube"], +} + + +def _post_data(platforms, config=None, **kwargs): + response = MagicMock(status_code=200) + response.json.return_value = { + "success": True, + "request_id": "r1", + "results": {platform: {"success": True} for platform in platforms}, + } + with ( + patch("app.services.upload_post.config.app", {**_CONFIG, **(config or {})}), + patch("app.services.upload_post.os.path.exists", return_value=True), + patch("builtins.open", mock_open(read_data=b"video")), + patch("app.services.upload_post.requests.post", return_value=response) as post, + ): + result = UploadPostService().upload_video("/v.mp4", "Fallback", platforms, **kwargs) + assert result["success"], result + return post.call_args.kwargs["data"] + + +def _values(data, key): + return [value for name, value in data if name == key] + + +def test_each_platform_gets_its_own_text_and_the_ai_label(): + data = _post_data( + ["tiktok", "instagram"], + platform_titles={"tiktok": "Short one #a", "instagram": "Longer story #b #c", "x": "ignored"}, + ) + assert _values(data, "title") == ["Fallback"] + assert _values(data, "tiktok_title") == ["Short one #a"] + assert _values(data, "instagram_title") == ["Longer story #b #c"] + assert _values(data, "x_title") == [] + assert _values(data, "is_ai_generated") == ["true"] + + +def test_text_for_unselected_platforms_is_not_sent(): + data = _post_data(["tiktok"], platform_titles={"tiktok": "T", "instagram": "I"}) + assert _values(data, "instagram_title") == [] + + +def test_ai_label_can_be_turned_off_and_is_not_sent_for_youtube_only(): + assert _values(_post_data(["tiktok"], config={"upload_post_ai_label": False}), "is_ai_generated") == [] + # YouTube 使用 containsSyntheticMedia,不需要这个字段。 + assert _values(_post_data(["youtube"]), "is_ai_generated") == [] + + +def test_social_post_text_appends_hashtags_once(): + metadata = {"caption": "Morning coffee ritual #coffee", "hashtags": ["coffee", "#barista", " ", "#"]} + assert tm._social_post_text(metadata) == "Morning coffee ritual #coffee\n\n#barista" + assert tm._social_post_text({"title": "Only a title"}) == "Only a title" + + +def test_cross_post_generates_text_for_each_platform(): + state = MemoryState() + state.update_task( + "per-platform", + state=tm.const.TASK_STATE_COMPLETE, + videos=["final.mp4"], + cross_post_state=tm.const.CROSS_POST_STATE_PENDING, + ) + + def metadata(platform, **_kwargs): + return {"title": f"{platform} title", "caption": f"{platform} caption", "hashtags": [f"#{platform}"]} + + with ( + patch.object(tm.sm, "state", state), + patch.object(tm.llm, "generate_social_metadata", side_effect=metadata) as generate, + patch.object(tm.upload_post, "cross_post_video", return_value={"success": True}) as upload, + ): + tm._run_cross_post( + "per-platform", ("final.mp4",), "Coffee", "Script", "en", + ("tiktok", "instagram", "youtube"), "public", + ) + + assert sorted(call.kwargs["platform"] for call in generate.call_args_list) == [ + "instagram_reels", "tiktok", "youtube_shorts", + ] + kwargs = upload.call_args.kwargs + assert kwargs["platform_titles"] == { + "tiktok": "tiktok caption\n\n#tiktok", + "instagram": "instagram_reels caption\n\n#instagram_reels", + } + assert kwargs["youtube_extra"]["youtube_title"] == "youtube_shorts title" + assert state.get_task("per-platform")["cross_post_state"] == tm.const.CROSS_POST_STATE_COMPLETE diff --git a/test/services/test_upload_post_schedule.py b/test/services/test_upload_post_schedule.py new file mode 100644 index 0000000000..99db0141a3 --- /dev/null +++ b/test/services/test_upload_post_schedule.py @@ -0,0 +1,143 @@ +"""Upload-Post 定时发布:加入发布队列或延迟发布,由 Upload-Post 按时执行。""" + +import threading +from datetime import datetime, timezone +from unittest.mock import MagicMock, mock_open, patch + +import pytest + +from app.services import task as tm +from app.services import upload_post +from app.services.state import MemoryState +from app.services.upload_post import UploadPostService + +_CONFIG = { + "upload_post_enabled": True, + "upload_post_api_key": "test-key", + "upload_post_username": "testuser", +} +_NOW = datetime(2026, 11, 4, 10, 0, tzinfo=timezone.utc) + + +def _service(**settings): + with patch("app.services.upload_post.config.app", {**_CONFIG, **settings}): + return UploadPostService().schedule_request(now=_NOW) + + +@pytest.mark.parametrize( + "settings, expected", + [ + ({}, None), + ({"upload_post_schedule": "unknown"}, None), + ({"upload_post_schedule": "queue"}, {"mode": "queue"}), + ( + {"upload_post_schedule": "delay", "upload_post_schedule_delay_hours": 3}, + {"mode": "delay", "scheduled_date": "2026-11-04T13:00:00Z"}, + ), + # Upload-Post 只接受未来 365 天内的时间,也拒绝已经过去的时间。 + ( + {"upload_post_schedule": "delay", "upload_post_schedule_delay_hours": 10**6}, + {"mode": "delay", "scheduled_date": "2027-11-04T10:00:00Z"}, + ), + ( + {"upload_post_schedule": "delay", "upload_post_schedule_delay_hours": "bad"}, + {"mode": "delay", "scheduled_date": "2026-11-04T12:00:00Z"}, + ), + ], +) +def test_schedule_request_snapshot(settings, expected): + assert _service(**settings) == expected + + +def _upload(schedule, body, status=202): + response = MagicMock(status_code=status) + response.json.return_value = body + background = MagicMock() + with ( + patch("app.services.upload_post.config.app", _CONFIG), + patch("app.services.upload_post.os.path.exists", return_value=True), + patch("builtins.open", mock_open(read_data=b"video")), + patch("app.services.upload_post.requests.post", return_value=response) as post, + patch.object(UploadPostService, "check_status") as status_check, + ): + result = UploadPostService().upload_video( + "/v.mp4", "Title", ["tiktok"], schedule=schedule, on_background_start=background + ) + return result, post.call_args.kwargs["data"], status_check, background + + +def test_delayed_post_sends_the_date_and_does_not_poll(): + result, data, status_check, background = _upload( + {"mode": "delay", "scheduled_date": "2026-11-04T13:00:00Z"}, + {"success": True, "job_id": "job-1", "scheduled_date": "2026-11-04T13:00:00Z"}, + ) + assert ("scheduled_date", "2026-11-04T13:00:00Z") in data + assert not any(name == "add_to_queue" for name, _ in data) + assert result["success"] is True and result["scheduled"] is True + assert result["job_id"] == "job-1" + # 平台结果要到发布时间才有,轮询一小时只会把定时发布误报为失败。 + status_check.assert_not_called() + background.assert_not_called() + + +def test_queued_post_uses_the_upload_post_queue(): + result, data, _status, _background = _upload( + {"mode": "queue"}, + {"success": True, "job_id": "job-2", "queue_slot": "2026-11-05T09:00:00+00:00"}, + ) + assert ("add_to_queue", "true") in data + assert not any(name == "scheduled_date" for name, _ in data) + assert result["job_id"] == "job-2" + + +def test_scheduled_response_without_job_id_is_unconfirmed(): + result, *_ = _upload({"mode": "queue"}, {"success": True}) + assert result["success"] is False + assert "unconfirmed" in result["error"] + + +def test_cross_post_forwards_the_queued_schedule(): + state = MemoryState() + state.update_task( + "scheduled", state=tm.const.TASK_STATE_COMPLETE, videos=["final.mp4"], + cross_post_state=tm.const.CROSS_POST_STATE_PENDING, + ) + schedule = {"mode": "delay", "scheduled_date": "2026-11-04T13:00:00Z"} + with ( + patch.object(tm.sm, "state", state), + patch.object(tm.llm, "generate_social_metadata", return_value={"caption": "c"}), + patch.object( + tm.upload_post, "cross_post_video", + return_value={"success": True, "scheduled": True, "job_id": "job-3"}, + ) as upload, + ): + tm._run_cross_post( + "scheduled", ("final.mp4",), "Coffee", "Script", "en", ("tiktok",), "public", + False, None, schedule, + ) + assert upload.call_args.kwargs["schedule"] == schedule + task = state.get_task("scheduled") + assert task["cross_post_state"] == tm.const.CROSS_POST_STATE_COMPLETE + assert task["cross_post_results"][0]["job_id"] == "job-3" + + +def test_queueing_snapshots_the_schedule(monkeypatch): + submitted = {} + + class Executor: + def submit(self, fn, *args): + submitted["args"] = args + future = MagicMock() + return future + + # 独立的发布名额,避免占用模块级队列容量影响其它测试。 + monkeypatch.setattr(tm, "_cross_post_slots", threading.BoundedSemaphore(1)) + monkeypatch.setattr(tm, "_cross_post_executor", Executor()) + monkeypatch.setattr( + upload_post.upload_post_service, "schedule_request", + lambda: {"mode": "queue"}, + ) + monkeypatch.setattr(tm, "_register_cross_post_future", lambda *a: None) + params = tm.VideoParams(video_subject="Coffee") + assert tm._schedule_cross_post("t", ["final.mp4"], params, "Script", ["tiktok"], "public") is None + assert submitted["args"][-1] == {"mode": "queue"} diff --git a/test/services/test_webui_grouped_select.py b/test/services/test_webui_grouped_select.py index ebfcd8fae6..7f15040e29 100644 --- a/test/services/test_webui_grouped_select.py +++ b/test/services/test_webui_grouped_select.py @@ -135,8 +135,10 @@ def test_grouped_video_source_keeps_groups_and_accessible_label_binding(): "jalapeno", "wavespeed", "muapi", + "local_video", "openai_image", "codex_image", + "local_image", "local", ] diff --git a/test/services/test_webui_llm_settings.py b/test/services/test_webui_llm_settings.py index 6db48076d1..527e37a983 100644 --- a/test/services/test_webui_llm_settings.py +++ b/test/services/test_webui_llm_settings.py @@ -301,7 +301,7 @@ def test_ai_video_settings_prioritize_sponsors_and_own_shengsuan_key(): assert providers.options == [messages.get(key, english.get(key, key)) for key in ( "Metaso MiniMax H3", "OFox AI Video", "Shengsuan Cloud AI Video", "Volcano Engine Seedance", "Infistar AI Video", "Jalapeno Cloud AI Video", - "WaveSpeed AI Video", "MuAPI AI Video", + "WaveSpeed AI Video", "MuAPI AI Video", "local_ai.video.label", )] assert providers.value == "loomloom" diff --git a/test/services/test_webui_ofox_placement.py b/test/services/test_webui_ofox_placement.py index 93699e7392..89c3e82f96 100644 --- a/test/services/test_webui_ofox_placement.py +++ b/test/services/test_webui_ofox_placement.py @@ -26,7 +26,8 @@ def test_ai_video_source_order_keeps_ofox_below_metaso(): ) groups = ast.literal_eval(assignment.value) assert groups["ai_video"] == ( - "metaso_minimax", "ofox", "loomloom", "volcengine_seedance", "infistar", "jalapeno", "wavespeed", "muapi" + "metaso_minimax", "ofox", "loomloom", "volcengine_seedance", "infistar", "jalapeno", "wavespeed", "muapi", + "local_video", ) assert groups["stock_video"] == ("pexels", "pixabay", "coverr") @@ -60,7 +61,7 @@ def test_ofox_settings_order_and_saved_values(language): assert providers.options == [messages.get(key, english.get(key, key)) for key in ( "Metaso MiniMax H3", "OFox AI Video", "Shengsuan Cloud AI Video", "Volcano Engine Seedance", "Infistar AI Video", "Jalapeno Cloud AI Video", - "WaveSpeed AI Video", "MuAPI AI Video", + "WaveSpeed AI Video", "MuAPI AI Video", "local_ai.video.label", )] assert providers.value == "ofox" assert not any(item.key in { diff --git a/test/services/test_webui_upload_post_settings.py b/test/services/test_webui_upload_post_settings.py index f4000cd4f3..6d6cadc5a4 100644 --- a/test/services/test_webui_upload_post_settings.py +++ b/test/services/test_webui_upload_post_settings.py @@ -129,3 +129,38 @@ def test_youtube_audience_hidden_for_other_platforms(): assert not app.exception assert not any(item.key == "upload_post_youtube_made_for_kids_selectbox" for item in app.selectbox) assert values["upload_post_youtube_made_for_kids"] is True + + +def test_publish_timing_and_ai_label_settings_persist(): + test_app_config = dict( + config.app, + upload_post_enabled=True, + upload_post_platforms=["tiktok", "instagram"], + upload_post_schedule="now", + upload_post_ai_label=True, + ) + with ( + patch.object(config, "app", test_app_config), + patch.object(config, "try_save_config", return_value=True), + ): + app = AppTest.from_file(str(WEBUI_MAIN), default_timeout=60) + app.run() + app.session_state["settings_dialog_open"] = True + app.session_state["settings_dialog_target_tab"] = "publish" + app.run() + assert not app.exception + + schedule = _widget_by_key(app.selectbox, "upload_post_schedule_select") + assert schedule.value == "now" + assert not any( + str(getattr(item, "key", "")) == "upload_post_schedule_delay_hours_input" + for item in app.number_input + ) + schedule.set_value("delay").run() + assert config.app["upload_post_schedule"] == "delay" + _widget_by_key(app.number_input, "upload_post_schedule_delay_hours_input").set_value(6.0).run() + assert config.app["upload_post_schedule_delay_hours"] == 6.0 + + _widget_by_key(app.checkbox, "upload_post_ai_label_checkbox").uncheck().run() + assert config.app["upload_post_ai_label"] is False + assert not app.exception diff --git a/webui/common.py b/webui/common.py index 33d11530db..92c6520c3d 100644 --- a/webui/common.py +++ b/webui/common.py @@ -73,8 +73,9 @@ "jalapeno", "wavespeed", "muapi", + "local_video", ), - "ai_image": ("openai_image", "codex_image"), + "ai_image": ("openai_image", "codex_image", "local_image"), "local": ("local",), } # Upload-Post 的 API Key 与发布用户分别在两个页面管理,并且发布用户名称 diff --git a/webui/components/generation_controls.py b/webui/components/generation_controls.py index 71c5b0cf81..b8b18193ce 100644 --- a/webui/components/generation_controls.py +++ b/webui/components/generation_controls.py @@ -20,6 +20,7 @@ muapi, infistar_video, jalapeno_video, + local_ai, ofox, volcengine_seedance, voice, @@ -246,6 +247,8 @@ def _render_generation_controls( "loomloom", "openai_image", "codex_image", + "local_image", + "local_video", "local", ]: _remove_active_generation_task(task_id) @@ -392,6 +395,15 @@ def _render_generation_controls( st.error(tr("Please Configure the OpenAI Image Source")) st.stop() + if params.video_source in ("local_image", "local_video"): + local_ai_ready, local_ai_reason = local_ai.readiness( + "video" if params.video_source == "local_video" else "image" + ) + if not local_ai_ready: + _remove_active_generation_task(task_id) + st.error(tr(local_ai_reason)) + st.stop() + loomloom_video_request = None if params.video_source == "loomloom": current_batch, current_signature = _current_loomloom_video_quote_context( diff --git a/webui/components/local_ai_settings.py b/webui/components/local_ai_settings.py new file mode 100644 index 0000000000..2e9e481de2 --- /dev/null +++ b/webui/components/local_ai_settings.py @@ -0,0 +1,233 @@ +"""设置弹窗中的本机 AI 面板:硬件、运行环境、模型下载与 GPU 服务。 + +AI 图片和 AI 视频两个素材分区都可能同时显示本面板,因此控件 key 都带上 +分区类型,避免同一页面出现重复 key。 +""" + +import os + +import streamlit as st + +from app.config import config +from app.services import local_ai +from local_ai import catalog +from webui.common import _set_runtime_config, tr + +_FIT_LABELS = { + catalog.FIT_GPU: "local_ai.fit.gpu", + catalog.FIT_OFFLOAD: "local_ai.fit.offload", + catalog.FIT_CPU: "local_ai.fit.cpu", + catalog.FIT_NONE: "local_ai.fit.none", +} +_KIND_TITLES = {"image": "local_ai.models.image", "video": "local_ai.models.video"} + + +def _gb(value_bytes): + return f"{value_bytes / 1024**3:.1f} GB" + + +def _model_setting(kind): + return f"local_ai_{kind}_model" + + +def _render_hardware(section): + hardware = local_ai.detect_hardware() + st.markdown(f"**{tr('local_ai.hardware')}**") + if hardware.gpus: + for gpu in hardware.gpus: + st.caption(f"GPU {gpu.index}: {gpu.name} · {gpu.vram_gb:g} GB") + else: + st.caption(tr("local_ai.no_gpu")) + st.caption(f"{tr('local_ai.ram')}: {hardware.ram_gb:g} GB") + + nvidia = [gpu for gpu in hardware.gpus if gpu.vendor == "nvidia"] + if len(nvidia) > 1: + # 配置中空值表示自动;控件用 "auto" 作为选项值,避免空字符串选项。 + options = ["auto"] + [str(gpu.index) for gpu in nvidia] + saved = str(config.app.get("local_ai_gpu_index", "") or "") or "auto" + selected = st.selectbox( + tr("local_ai.gpu"), + options, + index=options.index(saved) if saved in options else 0, + # 标签在本次渲染时算好:format_func 也会在页面运行之外被调用。 + format_func={ + "auto": tr("local_ai.gpu.auto"), + **{str(gpu.index): f"GPU {gpu.index}: {gpu.name}" for gpu in nvidia}, + }.get, + key=f"local_ai_gpu_index_select_{section}", + ) + if selected != saved: + _set_runtime_config("app", "local_ai_gpu_index", "" if selected == "auto" else selected) + + +@st.fragment(run_every="2s") +def _render_install_progress(): + status = local_ai.install_status() or {} + if status.get("state") == "running": + st.info(tr("local_ai.runtime.installing")) + return + # 安装结束后整页刷新,让运行环境与模型区域显示最新状态。 + st.rerun() + + +def _render_runtime(section): + st.markdown(f"**{tr('local_ai.runtime')}**") + status = local_ai.install_status() or {} + if status.get("state") == "running": + _render_install_progress() + return + if status.get("state") == "failed": + st.error(f"{tr('local_ai.runtime.failed')}: {status.get('error', '')}") + st.caption(f"`{local_ai.install_log_path()}`") + if local_ai.is_runtime_installed(): + st.success(tr("local_ai.runtime.installed")) + label = tr("local_ai.runtime.reinstall") + else: + st.caption(tr("local_ai.runtime.help")) + label = tr("local_ai.runtime.install") + if st.button(label, key=f"local_ai_install_runtime_{section}"): + local_ai.start_runtime_install() + st.rerun() + + +@st.fragment(run_every="2s") +def _render_download_progress(model_id): + status = local_ai.download_status(model_id) or {} + if status.get("state") in ("queued", "downloading"): + total = status.get("bytes_total") or 0 + done = status.get("bytes_done") or 0 + st.progress( + min(done / total, 1.0) if total else 0.0, + text=f"{tr('local_ai.model.downloading')} {_gb(done)} / {_gb(total)}", + ) + return + st.rerun() + + +def _render_model(section, fit, selected_id, recommended_id): + model = fit.model + installed = local_ai.model_installed(model) + status = local_ai.download_status(model.id) or {} + with st.container(border=True): + title = model.name + if model.id == recommended_id: + title += f" · {tr('local_ai.model.recommended')}" + st.markdown(f"**{title}**") + st.caption( + f"{tr(_FIT_LABELS[fit.mode])} · {model.download_gb:g} GB · " + f"{tr('local_ai.model.license')}: {model.license}" + ) + if model.notes: + st.caption(model.notes) + if status.get("state") in ("queued", "downloading"): + _render_download_progress(model.id) + return + if status.get("state") == "failed": + st.error(status.get("error", "")) + columns = st.columns(2) + if installed: + if model.id == selected_id: + columns[0].success(tr("local_ai.model.active")) + elif columns[0].button( + tr("local_ai.model.use"), key=f"local_ai_use_{model.id}_{section}" + ): + _set_runtime_config("app", _model_setting(model.kind), model.id) + st.rerun() + if columns[1].button( + tr("local_ai.model.delete"), key=f"local_ai_delete_{model.id}_{section}" + ): + try: + local_ai.delete_model(model.id) + except local_ai.LocalAIError as error: + st.error(str(error)) + else: + st.rerun() + elif fit.mode != catalog.FIT_NONE and columns[0].button( + tr("local_ai.model.download"), key=f"local_ai_download_{model.id}_{section}" + ): + local_ai.start_download(model.id) + # 当前所选模型还不可用时,改用正在下载的模型,下载完成即可生成。 + current = local_ai.video_model() if model.kind == "video" else local_ai.image_model() + if current is None or not local_ai.model_installed(current): + _set_runtime_config("app", _model_setting(model.kind), model.id) + st.rerun() + + +def _render_models(section): + hardware = local_ai.gpu_hardware() + # 当前分区的模型排在前面;视频的图生视频模型也需要生图模型画出起始画面。 + for kind in (section, "image" if section == "video" else "video"): + st.markdown(f"**{tr(_KIND_TITLES[kind])}**") + recommended = catalog.recommend(hardware, kind) + selected = local_ai.video_model() if kind == "video" else local_ai.image_model() + for fit in catalog.rank(hardware, kind): + _render_model( + section, + fit, + selected.id if selected else "", + recommended.model.id if recommended else "", + ) + # 代码样式可避免 Markdown 把 Windows 路径中的反斜杠当作转义符吞掉。 + st.caption(f"{tr('local_ai.models.folder')}: `{local_ai.models_dir()}`") + + +def _render_server(section): + st.markdown(f"**{tr('local_ai.server')}**") + status = local_ai.server_status() + columns = st.columns(2) + if status: + loaded = " · ".join( + name for name in (status.get("model"), status.get("video_model")) if name + ) + columns[0].success(f"{tr('local_ai.server.running')} · {loaded or '-'}") + if columns[1].button(tr("local_ai.server.stop"), key=f"local_ai_stop_server_{section}"): + local_ai.stop_server() + st.rerun() + else: + columns[0].caption(tr("local_ai.server.stopped")) + if local_ai.is_runtime_installed() and columns[1].button( + tr("local_ai.server.start"), key=f"local_ai_start_server_{section}" + ): + try: + with st.spinner(tr("local_ai.server.starting")): + local_ai.ensure_server() + except local_ai.LocalAIError as error: + st.error(str(error)) + else: + st.rerun() + + +def _render_advanced(section): + with st.expander(tr("local_ai.advanced")): + token = st.text_input( + tr("local_ai.hf_token"), + value=str(config.app.get("local_ai_hf_api_token", "") or ""), + type="password", + help=tr("local_ai.hf_token.help"), + key=f"local_ai_hf_api_token_input_{section}", + ) + if token != str(config.app.get("local_ai_hf_api_token", "") or ""): + _set_runtime_config("app", "local_ai_hf_api_token", token.strip()) + models_dir = st.text_input( + tr("local_ai.models.folder"), + value=str(config.app.get("local_ai_models_dir", "") or ""), + placeholder=os.path.join(local_ai.local_ai_dir(), "models"), + key=f"local_ai_models_dir_input_{section}", + ) + if models_dir != str(config.app.get("local_ai_models_dir", "") or ""): + _set_runtime_config("app", "local_ai_models_dir", models_dir.strip()) + + +def render_local_ai_settings(section="image"): + """``section`` 是显示本面板的素材分区:``"image"`` 或 ``"video"``。""" + if section == "video": + st.markdown(f"**{tr('local_ai.video.label')}**") + st.caption(tr("local_ai.video.help")) + else: + st.markdown(f"**{tr('local_ai.label')}**") + st.caption(tr("local_ai.help")) + _render_hardware(section) + _render_runtime(section) + _render_models(section) + _render_server(section) + _render_advanced(section) diff --git a/webui/components/settings_dialog.py b/webui/components/settings_dialog.py index c8301f3dee..e9384382c5 100644 --- a/webui/components/settings_dialog.py +++ b/webui/components/settings_dialog.py @@ -455,6 +455,8 @@ def _material_provider_labels(): "muapi": tr("MuAPI AI Video"), "openai_image": tr("OpenAI Compatible Text-to-Image"), "codex_image": tr("codex.image.label"), + "local_image": tr("local_ai.label"), + "local_video": tr("local_ai.video.label"), "local": tr("Local file"), } @@ -1141,6 +1143,10 @@ def _render_material_provider_settings(provider): with st.container(): render_codex_login(tr) + elif provider in ("local_image", "local_video"): + from webui.components.local_ai_settings import render_local_ai_settings + + render_local_ai_settings("video" if provider == "local_video" else "image") elif provider == "openai_image": st.markdown(f"**{tr('OpenAI Compatible Text-to-Image')}**") @@ -1405,6 +1411,58 @@ def _render_settings_dialog(): "app", "upload_post_platforms", upload_post_platforms ) + if {"tiktok", "instagram"} & set(upload_post_platforms): + # 与 YouTube 的 containsSyntheticMedia 一致默认开启;Instagram + # 的标注发布后无法撤销,因此在这里明确提示。 + upload_post_ai_label = st.checkbox( + tr("upload_post.ai_label"), + value=app_settings.get("upload_post_ai_label", True) is True, + help=tr("upload_post.ai_label.help"), + key="upload_post_ai_label_checkbox", + ) + if upload_post_ai_label != ( + app_settings.get("upload_post_ai_label", True) is True + ): + _set_runtime_config("app", "upload_post_ai_label", upload_post_ai_label) + + # 定时发布由 Upload-Post 保存并按时执行,到时本机无需开机。 + schedule_options = ["now", "queue", "delay"] + saved_schedule = app_settings.get("upload_post_schedule", "now") + if saved_schedule not in schedule_options: + saved_schedule = "now" + upload_post_schedule = stable_selectbox( + tr("upload_post.schedule"), + schedule_options, + saved_schedule, + "upload_post_schedule_select", + # 标签在本次渲染时算好:format_func 也会在页面运行之外被调用。 + format_func={ + value: tr(f"upload_post.schedule.{value}") for value in schedule_options + }.get, + help=tr("upload_post.schedule.help"), + ) + if upload_post_schedule != saved_schedule: + _set_runtime_config("app", "upload_post_schedule", upload_post_schedule) + if upload_post_schedule == "delay": + try: + saved_delay = float( + app_settings.get("upload_post_schedule_delay_hours", 2.0) + ) + except (TypeError, ValueError): + saved_delay = 2.0 + upload_post_delay = st.number_input( + tr("upload_post.schedule.delay_hours"), + min_value=0.5, + max_value=24.0 * 365, + value=min(max(saved_delay, 0.5), 24.0 * 365), + step=0.5, + key="upload_post_schedule_delay_hours_input", + ) + if upload_post_delay != saved_delay: + _set_runtime_config( + "app", "upload_post_schedule_delay_hours", float(upload_post_delay) + ) + if "youtube" in upload_post_platforms: yt_status_options = ["public", "private", "unlisted"] yt_saved = app_settings.get( diff --git a/webui/components/video_panel.py b/webui/components/video_panel.py index 45c7d1f7b5..b6efc22c57 100644 --- a/webui/components/video_panel.py +++ b/webui/components/video_panel.py @@ -95,6 +95,8 @@ def _render_video_settings(panel, params): "loomloom": tr("Shengsuan Cloud AI Video"), "openai_image": tr("OpenAI Compatible Text-to-Image"), "codex_image": tr("codex.image.label"), + "local_image": tr("local_ai.label"), + "local_video": tr("local_ai.video.label"), "local": tr("Local file"), } saved_video_source_name = str( @@ -288,7 +290,9 @@ def _render_video_settings(panel, params): "ui", "video_fit_mode", params.video_fit_mode.value ) - if params.video_source in {"local", "openai_image", "codex_image"}: + if params.video_source in { + "local", "openai_image", "codex_image", "local_image", "local_video" + }: image_motion_labels = { ImageMotion.static.value: tr("Static Image"), ImageMotion.zoom.value: tr("Slow Zoom"), diff --git a/webui/i18n/az.json b/webui/i18n/az.json index 33635d493e..b0f96fb66b 100644 --- a/webui/i18n/az.json +++ b/webui/i18n/az.json @@ -506,6 +506,56 @@ "Configure Providers": "Tənzimlə", "Active Provider": "İstifadədə: {provider}", "No Configured Providers": "Hələ tənzimlənmiş provayder yoxdur.", - "Provider Configuration Changed": "Parametrlər dəyişib. Bu provayderin parametrlərini yoxlayın." + "Provider Configuration Changed": "Parametrlər dəyişib. Bu provayderin parametrlərini yoxlayın.", + "local_ai.label": "Lokal süni intellekt (bu kompüter)", + "local_ai.help": "Bu kompüterin videokartında hər səhnə üçün bir şəkil yaradır. Pulsuz və məxfi: heç nə onlayn xidmətə göndərilmir. İcra mühitini bir dəfə quraşdırın, model yükləyin, sonra bu mənbəni seçin.", + "local_ai.hardware": "Avadanlıq", + "local_ai.no_gpu": "Dəstəklənən videokart tapılmadı. Prosessorda işləyən modellər yenə işləyir, amma yavaş.", + "local_ai.ram": "Yaddaş", + "local_ai.gpu": "Şəkil yaratmaq üçün videokart", + "local_ai.gpu.auto": "Avtomatik (ən çox yaddaş)", + "local_ai.runtime": "İcra mühiti", + "local_ai.runtime.help": "Bu videokart üçün PyTorch-u və şəkil kitabxanalarını ayrıca mühitə quraşdırır. Təxminən 4 GB yükləyir və bir neçə dəqiqə çəkir.", + "local_ai.runtime.install": "İcra mühitini quraşdır", + "local_ai.runtime.reinstall": "İcra mühitini yenidən quraşdır", + "local_ai.runtime.installing": "İcra mühiti quraşdırılır. Bir neçə dəqiqə çəkir; tətbiqdən istifadəyə davam edə bilərsiniz.", + "local_ai.runtime.installed": "İcra mühiti quraşdırıldı.", + "local_ai.runtime.failed": "İcra mühitini quraşdırmaq alınmadı", + "local_ai.models.folder": "Model qovluğu", + "local_ai.model.recommended": "Bu kompüter üçün tövsiyə olunur", + "local_ai.model.license": "Lisenziya", + "local_ai.model.download": "Yüklə", + "local_ai.model.downloading": "Yüklənir", + "local_ai.model.use": "Bu modeldən istifadə et", + "local_ai.model.active": "İstifadədədir", + "local_ai.model.delete": "Sil", + "local_ai.fit.gpu": "Videokartda işləyir", + "local_ai.fit.offload": "Modelin bir hissəsi yaddaşda işləyir (daha yavaş)", + "local_ai.fit.cpu": "Prosessorda işləyir (yavaş)", + "local_ai.fit.none": "Bu kompüterə uyğun deyil", + "local_ai.server": "Şəkil serveri", + "local_ai.server.running": "İşləyir", + "local_ai.server.stopped": "Dayanıb. Videoya şəkil lazım olanda avtomatik başlayır.", + "local_ai.server.start": "Başlat", + "local_ai.server.starting": "Şəkil serveri başladılır və model yüklənir…", + "local_ai.server.stop": "Dayandır", + "local_ai.advanced": "Əlavə", + "local_ai.hf_token": "Hugging Face tokeni", + "local_ai.hf_token.help": "Yalnız FLUX.1 schnell kimi girişi məhdud modellər üçün lazımdır. Əvvəlcə Hugging Face-də modelin şərtlərini qəbul edin.", + "local_ai.runtime_missing": "Əvvəlcə Parametrlər > Sİ şəkil > Lokal süni intellekt bölməsində icra mühitini quraşdırın.", + "local_ai.model_missing": "Əvvəlcə Parametrlər > Sİ şəkil > Lokal süni intellekt bölməsində seçilmiş lokal şəkil modelini yükləyin.", + "local_ai.no_model_fits": "Bu kompüterin videokartına və ya yaddaşına uyğun lokal şəkil modeli yoxdur.", + "local_ai.video.label": "Lokal Sİ videosu (bu kompüter)", + "local_ai.video.help": "Hər səhnəni bu kompüterin videokartında canlandırır: lokal şəkil modeli səhnəni çəkir, sonra video modeli onu qısa klipə çevirir. Pulsuz və məxfi, amma hər klip dəqiqələr çəkir. Klip alınmasa, həmin səhnə öz şəklini yavaş yaxınlaşdırma ilə istifadə edir.", + "local_ai.models.image": "Şəkil modelləri", + "local_ai.models.video": "Video modelləri", + "upload_post.ai_label": "TikTok və Instagram paylaşımlarını Sİ ilə yaradılmış kimi işarələ", + "upload_post.ai_label.help": "Platformalar real görünən Sİ məzmununun işarələnməsini tələb edir; YouTube yükləmələri həmişə işarələnir. Instagram-da işarə dərc edildikdən sonra silinə bilməz.", + "upload_post.schedule": "Nə vaxt dərc edilsin", + "upload_post.schedule.now": "Video hazır olan kimi", + "upload_post.schedule.queue": "Upload-Post növbəmdə növbəti boş vaxt", + "upload_post.schedule.delay": "Müəyyən saat sonra", + "upload_post.schedule.delay_hours": "Gözləmə saatı", + "upload_post.schedule.help": "Upload-Post planlaşdırılmış paylaşımları saxlayır və özü dərc edir, ona görə bu kompüter o vaxt sönülü ola bilər. Növbə vaxtlarını Upload-Post panelində qurun." } } diff --git a/webui/i18n/ca.json b/webui/i18n/ca.json index 29a777e1a0..33a22753ab 100644 --- a/webui/i18n/ca.json +++ b/webui/i18n/ca.json @@ -506,6 +506,56 @@ "Configure Providers": "Configura", "Active Provider": "En ús: {provider}", "No Configured Providers": "Encara no hi ha proveïdors configurats.", - "Provider Configuration Changed": "La configuració ha canviat. Revisa aquest proveïdor a la seva configuració." + "Provider Configuration Changed": "La configuració ha canviat. Revisa aquest proveïdor a la seva configuració.", + "local_ai.label": "IA local (aquest ordinador)", + "local_ai.help": "Genera una imatge per escena amb la targeta gràfica d'aquest ordinador. Gratuït i privat: no s'envia res a cap servei en línia. Instal·la una vegada l'entorn d'execució, baixa un model i després tria aquesta font.", + "local_ai.hardware": "Maquinari", + "local_ai.no_gpu": "No s'ha trobat cap targeta gràfica compatible. Els models que funcionen a la CPU continuen funcionant, però a poc a poc.", + "local_ai.ram": "Memòria", + "local_ai.gpu": "Targeta gràfica per generar imatges", + "local_ai.gpu.auto": "Automàtic (la de més memòria)", + "local_ai.runtime": "Entorn d'execució", + "local_ai.runtime.help": "Instal·la PyTorch per a aquesta targeta gràfica i les biblioteques d'imatge en un entorn a part. Baixa uns 4 GB i triga uns quants minuts.", + "local_ai.runtime.install": "Instal·la l'entorn", + "local_ai.runtime.reinstall": "Torna a instal·lar l'entorn", + "local_ai.runtime.installing": "S'està instal·lant l'entorn. Triga uns quants minuts; pots continuar fent servir l'aplicació.", + "local_ai.runtime.installed": "Entorn instal·lat.", + "local_ai.runtime.failed": "Ha fallat la instal·lació de l'entorn", + "local_ai.models.folder": "Carpeta de models", + "local_ai.model.recommended": "Recomanat per a aquest ordinador", + "local_ai.model.license": "Llicència", + "local_ai.model.download": "Baixa", + "local_ai.model.downloading": "S'està baixant", + "local_ai.model.use": "Fes servir aquest model", + "local_ai.model.active": "En ús", + "local_ai.model.delete": "Suprimeix", + "local_ai.fit.gpu": "Funciona a la targeta gràfica", + "local_ai.fit.offload": "Funciona amb part del model a la memòria (més lent)", + "local_ai.fit.cpu": "Funciona a la CPU (lent)", + "local_ai.fit.none": "No cap en aquest ordinador", + "local_ai.server": "Servidor d'imatges", + "local_ai.server.running": "En execució", + "local_ai.server.stopped": "Aturat. S'inicia automàticament quan un vídeo necessita imatges.", + "local_ai.server.start": "Inicia", + "local_ai.server.starting": "S'està iniciant el servidor d'imatges i carregant el model…", + "local_ai.server.stop": "Atura", + "local_ai.advanced": "Avançat", + "local_ai.hf_token": "Testimoni de Hugging Face", + "local_ai.hf_token.help": "Només cal per a models d'accés restringit com FLUX.1 schnell. Accepta primer les condicions del model a Hugging Face.", + "local_ai.runtime_missing": "Instal·la primer l'entorn d'IA local a Configuració > Imatge IA > IA local.", + "local_ai.model_missing": "Baixa primer el model d'imatge local seleccionat a Configuració > Imatge IA > IA local.", + "local_ai.no_model_fits": "Cap model d'imatge local no cap a la targeta gràfica o la memòria d'aquest ordinador.", + "local_ai.video.label": "Vídeo amb IA local (aquest ordinador)", + "local_ai.video.help": "Anima cada escena amb la targeta gràfica d'aquest ordinador: el model d'imatge local pinta l'escena i el model de vídeo la converteix en un clip curt. Gratuït i privat, però cada clip triga minuts. Si un clip falla, aquella escena fa servir la seva imatge amb un zoom lent.", + "local_ai.models.image": "Models d'imatge", + "local_ai.models.video": "Models de vídeo", + "upload_post.ai_label": "Marca les publicacions de TikTok i Instagram com a generades amb IA", + "upload_post.ai_label.help": "Les plataformes exigeixen etiquetar el contingut realista generat amb IA; els vídeos de YouTube sempre s'etiqueten. A Instagram l'etiqueta no es pot treure després de publicar.", + "upload_post.schedule": "Quan publicar", + "upload_post.schedule.now": "Tan bon punt el vídeo estigui llest", + "upload_post.schedule.queue": "Següent franja lliure de la meva cua d'Upload-Post", + "upload_post.schedule.delay": "Després d'un nombre d'hores", + "upload_post.schedule.delay_hours": "Hores d'espera", + "upload_post.schedule.help": "Upload-Post desa les publicacions programades i les publica ell mateix, així que aquest ordinador pot estar apagat. Configura les franges de la cua al tauler d'Upload-Post." } } diff --git a/webui/i18n/de.json b/webui/i18n/de.json index 2542d95bcf..f649012f54 100644 --- a/webui/i18n/de.json +++ b/webui/i18n/de.json @@ -506,6 +506,56 @@ "Configure Providers": "Konfigurieren", "Active Provider": "Aktiv: {provider}", "No Configured Providers": "Noch keine Anbieter eingerichtet.", - "Provider Configuration Changed": "Die Konfiguration hat sich geändert. Prüfe die Einstellungen dieses Anbieters." + "Provider Configuration Changed": "Die Konfiguration hat sich geändert. Prüfe die Einstellungen dieses Anbieters.", + "local_ai.label": "Lokale KI (dieser Computer)", + "local_ai.help": "Erzeugt pro Szene ein Bild auf der Grafikkarte dieses Computers. Kostenlos und privat: Nichts wird an einen Online-Dienst gesendet. Installieren Sie einmal die Laufzeitumgebung, laden Sie ein Modell herunter und wählen Sie dann diese Quelle.", + "local_ai.hardware": "Hardware", + "local_ai.no_gpu": "Keine unterstützte Grafikkarte gefunden. Modelle, die auf der CPU laufen, funktionieren weiterhin, aber langsam.", + "local_ai.ram": "Arbeitsspeicher", + "local_ai.gpu": "Grafikkarte für die Bilderzeugung", + "local_ai.gpu.auto": "Automatisch (meister Speicher)", + "local_ai.runtime": "Laufzeitumgebung", + "local_ai.runtime.help": "Installiert PyTorch für diese Grafikkarte und die Bildbibliotheken in eine eigene Umgebung. Lädt etwa 4 GB herunter und dauert einige Minuten.", + "local_ai.runtime.install": "Laufzeitumgebung installieren", + "local_ai.runtime.reinstall": "Laufzeitumgebung neu installieren", + "local_ai.runtime.installing": "Die Laufzeitumgebung wird installiert. Das dauert einige Minuten; Sie können die App weiter verwenden.", + "local_ai.runtime.installed": "Laufzeitumgebung installiert.", + "local_ai.runtime.failed": "Installation der Laufzeitumgebung fehlgeschlagen", + "local_ai.models.folder": "Modellordner", + "local_ai.model.recommended": "Empfohlen für diesen Computer", + "local_ai.model.license": "Lizenz", + "local_ai.model.download": "Herunterladen", + "local_ai.model.downloading": "Wird heruntergeladen", + "local_ai.model.use": "Dieses Modell verwenden", + "local_ai.model.active": "In Verwendung", + "local_ai.model.delete": "Löschen", + "local_ai.fit.gpu": "Läuft auf der Grafikkarte", + "local_ai.fit.offload": "Läuft mit einem Teil des Modells im Arbeitsspeicher (langsamer)", + "local_ai.fit.cpu": "Läuft auf der CPU (langsam)", + "local_ai.fit.none": "Passt nicht zu diesem Computer", + "local_ai.server": "Bildserver", + "local_ai.server.running": "Läuft", + "local_ai.server.stopped": "Gestoppt. Er startet automatisch, wenn ein Video Bilder braucht.", + "local_ai.server.start": "Starten", + "local_ai.server.starting": "Bildserver wird gestartet und Modell geladen…", + "local_ai.server.stop": "Stoppen", + "local_ai.advanced": "Erweitert", + "local_ai.hf_token": "Hugging-Face-Token", + "local_ai.hf_token.help": "Nur für zugangsbeschränkte Modelle wie FLUX.1 schnell nötig. Akzeptieren Sie zuerst die Bedingungen des Modells auf Hugging Face.", + "local_ai.runtime_missing": "Installieren Sie zuerst die lokale KI-Laufzeitumgebung unter Einstellungen > KI-Bild > Lokale KI.", + "local_ai.model_missing": "Laden Sie zuerst das ausgewählte lokale Bildmodell unter Einstellungen > KI-Bild > Lokale KI herunter.", + "local_ai.no_model_fits": "Kein lokales Bildmodell passt zur Grafikkarte oder zum Arbeitsspeicher dieses Computers.", + "local_ai.video.label": "Lokales KI-Video (dieser Computer)", + "local_ai.video.help": "Animiert jede Szene auf der Grafikkarte dieses Computers: Das lokale Bildmodell malt die Szene, dann macht das Videomodell daraus einen kurzen Clip. Kostenlos und privat, aber jeder Clip dauert Minuten. Schlägt ein Clip fehl, zeigt diese Szene ihr Bild mit langsamem Zoom.", + "local_ai.models.image": "Bildmodelle", + "local_ai.models.video": "Videomodelle", + "upload_post.ai_label": "TikTok- und Instagram-Beiträge als KI-generiert kennzeichnen", + "upload_post.ai_label.help": "Die Plattformen verlangen eine Kennzeichnung realistischer KI-Inhalte; YouTube-Uploads werden immer gekennzeichnet. Bei Instagram lässt sich die Kennzeichnung nach dem Veröffentlichen nicht entfernen.", + "upload_post.schedule": "Veröffentlichungszeitpunkt", + "upload_post.schedule.now": "Sobald das Video fertig ist", + "upload_post.schedule.queue": "Nächster freier Platz in meiner Upload-Post-Warteschlange", + "upload_post.schedule.delay": "Nach einer festen Anzahl Stunden", + "upload_post.schedule.delay_hours": "Wartezeit in Stunden", + "upload_post.schedule.help": "Upload-Post speichert geplante Beiträge und veröffentlicht sie selbst, dieser Computer kann dann ausgeschaltet sein. Warteschlangen-Zeiten richten Sie im Upload-Post-Dashboard ein." } } diff --git a/webui/i18n/en.json b/webui/i18n/en.json index ab65de934f..b77d34c5ea 100644 --- a/webui/i18n/en.json +++ b/webui/i18n/en.json @@ -761,6 +761,56 @@ "Configure Providers": "Configure", "Active Provider": "In use: {provider}", "No Configured Providers": "No providers configured yet.", - "Provider Configuration Changed": "Configuration changed. Review this provider in its settings." + "Provider Configuration Changed": "Configuration changed. Review this provider in its settings.", + "local_ai.label": "Local AI (this computer)", + "local_ai.help": "Generates one image per scene on this computer's GPU. Free and private: nothing is sent to an online service. Install the runtime once, download a model, then select this source.", + "local_ai.hardware": "Hardware", + "local_ai.no_gpu": "No supported GPU found. Models that run on the CPU still work, but slowly.", + "local_ai.ram": "Memory", + "local_ai.gpu": "GPU for image generation", + "local_ai.gpu.auto": "Automatic (most memory)", + "local_ai.runtime": "Runtime", + "local_ai.runtime.help": "Installs PyTorch for this GPU and the image libraries into a separate environment. Downloads about 4 GB and takes several minutes.", + "local_ai.runtime.install": "Install runtime", + "local_ai.runtime.reinstall": "Reinstall runtime", + "local_ai.runtime.installing": "Installing the runtime. This takes several minutes; you can keep using the app.", + "local_ai.runtime.installed": "Runtime installed.", + "local_ai.runtime.failed": "Runtime install failed", + "local_ai.models.folder": "Models folder", + "local_ai.model.recommended": "Recommended for this computer", + "local_ai.model.license": "License", + "local_ai.model.download": "Download", + "local_ai.model.downloading": "Downloading", + "local_ai.model.use": "Use this model", + "local_ai.model.active": "In use", + "local_ai.model.delete": "Delete", + "local_ai.fit.gpu": "Runs on the GPU", + "local_ai.fit.offload": "Runs with part of the model in memory (slower)", + "local_ai.fit.cpu": "Runs on the CPU (slow)", + "local_ai.fit.none": "Does not fit this computer", + "local_ai.server": "Image server", + "local_ai.server.running": "Running", + "local_ai.server.stopped": "Stopped. It starts automatically when a video needs images.", + "local_ai.server.start": "Start", + "local_ai.server.starting": "Starting the image server and loading the model…", + "local_ai.server.stop": "Stop", + "local_ai.advanced": "Advanced", + "local_ai.hf_token": "Hugging Face token", + "local_ai.hf_token.help": "Only needed for gated models such as FLUX.1 schnell. Accept the model's terms on Hugging Face first.", + "local_ai.runtime_missing": "Install the local AI runtime in Settings > AI Image > Local AI first.", + "local_ai.model_missing": "Download the selected local image model in Settings > AI Image > Local AI first.", + "local_ai.no_model_fits": "No local image model fits this computer's GPU or memory.", + "local_ai.video.label": "Local AI video (this computer)", + "local_ai.video.help": "Animates each scene on this computer's GPU: the local image model paints the scene, then the video model turns it into a short clip. Free and private, but each clip takes minutes. If a clip fails, that scene uses its image with a slow zoom.", + "local_ai.models.image": "Image models", + "local_ai.models.video": "Video models", + "upload_post.ai_label": "Label TikTok and Instagram posts as AI-generated", + "upload_post.ai_label.help": "The platforms require a label on realistic AI-generated content; YouTube uploads are always labeled. Instagram's label cannot be removed after publishing.", + "upload_post.schedule": "When to publish", + "upload_post.schedule.now": "Right after the video is ready", + "upload_post.schedule.queue": "Next free slot in my Upload-Post queue", + "upload_post.schedule.delay": "A set number of hours later", + "upload_post.schedule.delay_hours": "Hours to wait", + "upload_post.schedule.help": "Upload-Post stores scheduled posts and publishes them itself, so this computer can be off by then. Set up queue slots in the Upload-Post dashboard." } } diff --git a/webui/i18n/es.json b/webui/i18n/es.json index 0409544cf9..615a90f6da 100644 --- a/webui/i18n/es.json +++ b/webui/i18n/es.json @@ -506,6 +506,56 @@ "Configure Providers": "Configurar", "Active Provider": "En uso: {provider}", "No Configured Providers": "Todavía no hay proveedores configurados.", - "Provider Configuration Changed": "La configuración cambió. Revisa este proveedor en su configuración." + "Provider Configuration Changed": "La configuración cambió. Revisa este proveedor en su configuración.", + "local_ai.label": "IA local (este ordenador)", + "local_ai.help": "Genera una imagen por escena con la tarjeta gráfica de este ordenador. Gratis y privado: no se envía nada a ningún servicio en línea. Instala una vez el entorno de ejecución, descarga un modelo y luego elige esta fuente.", + "local_ai.hardware": "Hardware", + "local_ai.no_gpu": "No se encontró una tarjeta gráfica compatible. Los modelos que funcionan en la CPU siguen funcionando, pero despacio.", + "local_ai.ram": "Memoria", + "local_ai.gpu": "Tarjeta gráfica para generar imágenes", + "local_ai.gpu.auto": "Automático (la de más memoria)", + "local_ai.runtime": "Entorno de ejecución", + "local_ai.runtime.help": "Instala PyTorch para esta tarjeta gráfica y las bibliotecas de imagen en un entorno aparte. Descarga unos 4 GB y tarda varios minutos.", + "local_ai.runtime.install": "Instalar entorno", + "local_ai.runtime.reinstall": "Reinstalar entorno", + "local_ai.runtime.installing": "Instalando el entorno. Tarda varios minutos; puedes seguir usando la aplicación.", + "local_ai.runtime.installed": "Entorno instalado.", + "local_ai.runtime.failed": "Falló la instalación del entorno", + "local_ai.models.folder": "Carpeta de modelos", + "local_ai.model.recommended": "Recomendado para este ordenador", + "local_ai.model.license": "Licencia", + "local_ai.model.download": "Descargar", + "local_ai.model.downloading": "Descargando", + "local_ai.model.use": "Usar este modelo", + "local_ai.model.active": "En uso", + "local_ai.model.delete": "Eliminar", + "local_ai.fit.gpu": "Funciona en la tarjeta gráfica", + "local_ai.fit.offload": "Funciona con parte del modelo en memoria (más lento)", + "local_ai.fit.cpu": "Funciona en la CPU (lento)", + "local_ai.fit.none": "No cabe en este ordenador", + "local_ai.server": "Servidor de imágenes", + "local_ai.server.running": "En ejecución", + "local_ai.server.stopped": "Detenido. Se inicia automáticamente cuando un vídeo necesita imágenes.", + "local_ai.server.start": "Iniciar", + "local_ai.server.starting": "Iniciando el servidor de imágenes y cargando el modelo…", + "local_ai.server.stop": "Detener", + "local_ai.advanced": "Avanzado", + "local_ai.hf_token": "Token de Hugging Face", + "local_ai.hf_token.help": "Solo hace falta para modelos con acceso restringido como FLUX.1 schnell. Acepta primero los términos del modelo en Hugging Face.", + "local_ai.runtime_missing": "Instala primero el entorno de IA local en Ajustes > Imagen IA > IA local.", + "local_ai.model_missing": "Descarga primero el modelo de imagen local seleccionado en Ajustes > Imagen IA > IA local.", + "local_ai.no_model_fits": "Ningún modelo de imagen local cabe en la tarjeta gráfica o la memoria de este ordenador.", + "local_ai.video.label": "Vídeo con IA local (este ordenador)", + "local_ai.video.help": "Anima cada escena con la tarjeta gráfica de este ordenador: el modelo de imagen local pinta la escena y el modelo de vídeo la convierte en un clip corto. Gratis y privado, pero cada clip tarda minutos. Si un clip falla, esa escena usa su imagen con un zoom lento.", + "local_ai.models.image": "Modelos de imagen", + "local_ai.models.video": "Modelos de vídeo", + "upload_post.ai_label": "Marcar las publicaciones de TikTok e Instagram como generadas con IA", + "upload_post.ai_label.help": "Las plataformas exigen etiquetar el contenido realista generado con IA; los vídeos de YouTube siempre se etiquetan. En Instagram la etiqueta no se puede quitar después de publicar.", + "upload_post.schedule": "Cuándo publicar", + "upload_post.schedule.now": "En cuanto el vídeo esté listo", + "upload_post.schedule.queue": "Siguiente hueco libre de mi cola de Upload-Post", + "upload_post.schedule.delay": "Tras un número de horas", + "upload_post.schedule.delay_hours": "Horas de espera", + "upload_post.schedule.help": "Upload-Post guarda las publicaciones programadas y las publica él mismo, así que este ordenador puede estar apagado. Configura los huecos de la cola en el panel de Upload-Post." } } diff --git a/webui/i18n/fr.json b/webui/i18n/fr.json index 883419875e..5cfd1524fd 100644 --- a/webui/i18n/fr.json +++ b/webui/i18n/fr.json @@ -506,6 +506,56 @@ "Configure Providers": "Configurer", "Active Provider": "En cours d’utilisation : {provider}", "No Configured Providers": "Aucun fournisseur configuré pour le moment.", - "Provider Configuration Changed": "La configuration a changé. Vérifiez les paramètres de ce fournisseur." + "Provider Configuration Changed": "La configuration a changé. Vérifiez les paramètres de ce fournisseur.", + "local_ai.label": "IA locale (cet ordinateur)", + "local_ai.help": "Génère une image par scène sur la carte graphique de cet ordinateur. Gratuit et privé : rien n'est envoyé à un service en ligne. Installez une fois l'environnement d'exécution, téléchargez un modèle, puis choisissez cette source.", + "local_ai.hardware": "Matériel", + "local_ai.no_gpu": "Aucune carte graphique prise en charge. Les modèles qui tournent sur le processeur fonctionnent toujours, mais lentement.", + "local_ai.ram": "Mémoire", + "local_ai.gpu": "Carte graphique pour la génération d'images", + "local_ai.gpu.auto": "Automatique (la plus de mémoire)", + "local_ai.runtime": "Environnement d'exécution", + "local_ai.runtime.help": "Installe PyTorch pour cette carte graphique et les bibliothèques d'images dans un environnement séparé. Télécharge environ 4 Go et prend plusieurs minutes.", + "local_ai.runtime.install": "Installer l'environnement", + "local_ai.runtime.reinstall": "Réinstaller l'environnement", + "local_ai.runtime.installing": "Installation de l'environnement en cours. Cela prend plusieurs minutes ; vous pouvez continuer à utiliser l'application.", + "local_ai.runtime.installed": "Environnement installé.", + "local_ai.runtime.failed": "Échec de l'installation de l'environnement", + "local_ai.models.folder": "Dossier des modèles", + "local_ai.model.recommended": "Recommandé pour cet ordinateur", + "local_ai.model.license": "Licence", + "local_ai.model.download": "Télécharger", + "local_ai.model.downloading": "Téléchargement", + "local_ai.model.use": "Utiliser ce modèle", + "local_ai.model.active": "Utilisé", + "local_ai.model.delete": "Supprimer", + "local_ai.fit.gpu": "Tourne sur la carte graphique", + "local_ai.fit.offload": "Tourne avec une partie du modèle en mémoire (plus lent)", + "local_ai.fit.cpu": "Tourne sur le processeur (lent)", + "local_ai.fit.none": "Ne convient pas à cet ordinateur", + "local_ai.server": "Serveur d'images", + "local_ai.server.running": "En cours d'exécution", + "local_ai.server.stopped": "Arrêté. Il démarre automatiquement quand une vidéo a besoin d'images.", + "local_ai.server.start": "Démarrer", + "local_ai.server.starting": "Démarrage du serveur d'images et chargement du modèle…", + "local_ai.server.stop": "Arrêter", + "local_ai.advanced": "Avancé", + "local_ai.hf_token": "Jeton Hugging Face", + "local_ai.hf_token.help": "Nécessaire uniquement pour les modèles à accès restreint comme FLUX.1 schnell. Acceptez d'abord les conditions du modèle sur Hugging Face.", + "local_ai.runtime_missing": "Installez d'abord l'environnement d'IA locale dans Paramètres > Image IA > IA locale.", + "local_ai.model_missing": "Téléchargez d'abord le modèle d'image local sélectionné dans Paramètres > Image IA > IA locale.", + "local_ai.no_model_fits": "Aucun modèle d'image local ne convient à la carte graphique ou à la mémoire de cet ordinateur.", + "local_ai.video.label": "Vidéo IA locale (cet ordinateur)", + "local_ai.video.help": "Anime chaque scène sur la carte graphique de cet ordinateur : le modèle d'image local peint la scène, puis le modèle vidéo en fait un court clip. Gratuit et privé, mais chaque clip prend plusieurs minutes. Si un clip échoue, la scène utilise son image avec un zoom lent.", + "local_ai.models.image": "Modèles d'image", + "local_ai.models.video": "Modèles vidéo", + "upload_post.ai_label": "Indiquer que les publications TikTok et Instagram sont générées par IA", + "upload_post.ai_label.help": "Les plateformes exigent une mention pour les contenus réalistes générés par IA ; les vidéos YouTube sont toujours signalées. Sur Instagram, la mention ne peut plus être retirée après publication.", + "upload_post.schedule": "Quand publier", + "upload_post.schedule.now": "Dès que la vidéo est prête", + "upload_post.schedule.queue": "Prochain créneau libre de ma file Upload-Post", + "upload_post.schedule.delay": "Après un nombre d'heures donné", + "upload_post.schedule.delay_hours": "Heures d'attente", + "upload_post.schedule.help": "Upload-Post enregistre les publications programmées et les publie lui-même : cet ordinateur peut être éteint à ce moment-là. Configurez les créneaux de la file dans le tableau de bord Upload-Post." } } diff --git a/webui/i18n/id.json b/webui/i18n/id.json index e3cb00a0e6..7eb6e0f806 100644 --- a/webui/i18n/id.json +++ b/webui/i18n/id.json @@ -506,6 +506,56 @@ "Configure Providers": "Konfigurasi", "Active Provider": "Sedang digunakan: {provider}", "No Configured Providers": "Belum ada penyedia yang dikonfigurasi.", - "Provider Configuration Changed": "Konfigurasi berubah. Periksa pengaturan penyedia ini." + "Provider Configuration Changed": "Konfigurasi berubah. Periksa pengaturan penyedia ini.", + "local_ai.label": "AI lokal (komputer ini)", + "local_ai.help": "Membuat satu gambar per adegan dengan kartu grafis komputer ini. Gratis dan privat: tidak ada yang dikirim ke layanan online. Pasang lingkungan runtime sekali, unduh model, lalu pilih sumber ini.", + "local_ai.hardware": "Perangkat keras", + "local_ai.no_gpu": "Tidak ditemukan kartu grafis yang didukung. Model yang berjalan di CPU tetap bisa dipakai, tetapi lambat.", + "local_ai.ram": "Memori", + "local_ai.gpu": "Kartu grafis untuk membuat gambar", + "local_ai.gpu.auto": "Otomatis (memori terbesar)", + "local_ai.runtime": "Lingkungan runtime", + "local_ai.runtime.help": "Memasang PyTorch untuk kartu grafis ini dan pustaka gambar ke lingkungan terpisah. Mengunduh sekitar 4 GB dan butuh beberapa menit.", + "local_ai.runtime.install": "Pasang runtime", + "local_ai.runtime.reinstall": "Pasang ulang runtime", + "local_ai.runtime.installing": "Memasang runtime. Butuh beberapa menit; Anda tetap bisa memakai aplikasi.", + "local_ai.runtime.installed": "Runtime terpasang.", + "local_ai.runtime.failed": "Pemasangan runtime gagal", + "local_ai.models.folder": "Folder model", + "local_ai.model.recommended": "Disarankan untuk komputer ini", + "local_ai.model.license": "Lisensi", + "local_ai.model.download": "Unduh", + "local_ai.model.downloading": "Mengunduh", + "local_ai.model.use": "Gunakan model ini", + "local_ai.model.active": "Sedang dipakai", + "local_ai.model.delete": "Hapus", + "local_ai.fit.gpu": "Berjalan di kartu grafis", + "local_ai.fit.offload": "Berjalan dengan sebagian model di memori (lebih lambat)", + "local_ai.fit.cpu": "Berjalan di CPU (lambat)", + "local_ai.fit.none": "Tidak cocok untuk komputer ini", + "local_ai.server": "Server gambar", + "local_ai.server.running": "Berjalan", + "local_ai.server.stopped": "Berhenti. Server mulai otomatis saat video membutuhkan gambar.", + "local_ai.server.start": "Mulai", + "local_ai.server.starting": "Memulai server gambar dan memuat model…", + "local_ai.server.stop": "Hentikan", + "local_ai.advanced": "Lanjutan", + "local_ai.hf_token": "Token Hugging Face", + "local_ai.hf_token.help": "Hanya diperlukan untuk model berakses terbatas seperti FLUX.1 schnell. Setujui dulu ketentuan model di Hugging Face.", + "local_ai.runtime_missing": "Pasang dulu runtime AI lokal di Pengaturan > Gambar AI > AI lokal.", + "local_ai.model_missing": "Unduh dulu model gambar lokal yang dipilih di Pengaturan > Gambar AI > AI lokal.", + "local_ai.no_model_fits": "Tidak ada model gambar lokal yang cocok dengan kartu grafis atau memori komputer ini.", + "local_ai.video.label": "Video AI lokal (komputer ini)", + "local_ai.video.help": "Menganimasikan setiap adegan dengan kartu grafis komputer ini: model gambar lokal melukis adegan, lalu model video mengubahnya menjadi klip pendek. Gratis dan privat, tetapi setiap klip butuh beberapa menit. Jika satu klip gagal, adegan itu memakai gambarnya dengan zoom perlahan.", + "local_ai.models.image": "Model gambar", + "local_ai.models.video": "Model video", + "upload_post.ai_label": "Tandai postingan TikTok dan Instagram sebagai buatan AI", + "upload_post.ai_label.help": "Platform mewajibkan label untuk konten AI yang tampak nyata; unggahan YouTube selalu diberi label. Label Instagram tidak bisa dihapus setelah dipublikasikan.", + "upload_post.schedule": "Kapan dipublikasikan", + "upload_post.schedule.now": "Segera setelah video siap", + "upload_post.schedule.queue": "Slot kosong berikutnya di antrean Upload-Post saya", + "upload_post.schedule.delay": "Setelah sejumlah jam", + "upload_post.schedule.delay_hours": "Jam menunggu", + "upload_post.schedule.help": "Upload-Post menyimpan postingan terjadwal dan memublikasikannya sendiri, jadi komputer ini boleh mati saat itu. Atur slot antrean di dasbor Upload-Post." } } diff --git a/webui/i18n/it.json b/webui/i18n/it.json index a254b71157..a38cd5677f 100644 --- a/webui/i18n/it.json +++ b/webui/i18n/it.json @@ -506,6 +506,56 @@ "Configure Providers": "Configura", "Active Provider": "In uso: {provider}", "No Configured Providers": "Nessun fornitore ancora configurato.", - "Provider Configuration Changed": "La configurazione è cambiata. Controlla le impostazioni di questo fornitore." + "Provider Configuration Changed": "La configurazione è cambiata. Controlla le impostazioni di questo fornitore.", + "local_ai.label": "IA locale (questo computer)", + "local_ai.help": "Genera un'immagine per scena con la scheda grafica di questo computer. Gratuito e privato: nulla viene inviato a servizi online. Installa una volta l'ambiente di esecuzione, scarica un modello, poi scegli questa fonte.", + "local_ai.hardware": "Hardware", + "local_ai.no_gpu": "Nessuna scheda grafica supportata trovata. I modelli che girano sulla CPU funzionano comunque, ma lentamente.", + "local_ai.ram": "Memoria", + "local_ai.gpu": "Scheda grafica per generare immagini", + "local_ai.gpu.auto": "Automatica (più memoria)", + "local_ai.runtime": "Ambiente di esecuzione", + "local_ai.runtime.help": "Installa PyTorch per questa scheda grafica e le librerie per le immagini in un ambiente separato. Scarica circa 4 GB e richiede alcuni minuti.", + "local_ai.runtime.install": "Installa ambiente", + "local_ai.runtime.reinstall": "Reinstalla ambiente", + "local_ai.runtime.installing": "Installazione dell'ambiente in corso. Richiede alcuni minuti; puoi continuare a usare l'app.", + "local_ai.runtime.installed": "Ambiente installato.", + "local_ai.runtime.failed": "Installazione dell'ambiente non riuscita", + "local_ai.models.folder": "Cartella dei modelli", + "local_ai.model.recommended": "Consigliato per questo computer", + "local_ai.model.license": "Licenza", + "local_ai.model.download": "Scarica", + "local_ai.model.downloading": "Download in corso", + "local_ai.model.use": "Usa questo modello", + "local_ai.model.active": "In uso", + "local_ai.model.delete": "Elimina", + "local_ai.fit.gpu": "Gira sulla scheda grafica", + "local_ai.fit.offload": "Gira con parte del modello in memoria (più lento)", + "local_ai.fit.cpu": "Gira sulla CPU (lento)", + "local_ai.fit.none": "Non adatto a questo computer", + "local_ai.server": "Server immagini", + "local_ai.server.running": "In esecuzione", + "local_ai.server.stopped": "Fermo. Si avvia automaticamente quando un video ha bisogno di immagini.", + "local_ai.server.start": "Avvia", + "local_ai.server.starting": "Avvio del server immagini e caricamento del modello…", + "local_ai.server.stop": "Ferma", + "local_ai.advanced": "Avanzate", + "local_ai.hf_token": "Token Hugging Face", + "local_ai.hf_token.help": "Serve solo per i modelli ad accesso limitato come FLUX.1 schnell. Accetta prima i termini del modello su Hugging Face.", + "local_ai.runtime_missing": "Installa prima l'ambiente di IA locale in Impostazioni > Immagine IA > IA locale.", + "local_ai.model_missing": "Scarica prima il modello di immagine locale selezionato in Impostazioni > Immagine IA > IA locale.", + "local_ai.no_model_fits": "Nessun modello di immagine locale è adatto alla scheda grafica o alla memoria di questo computer.", + "local_ai.video.label": "Video IA locale (questo computer)", + "local_ai.video.help": "Anima ogni scena con la scheda grafica di questo computer: il modello di immagini locale dipinge la scena, poi il modello video la trasforma in una breve clip. Gratuito e privato, ma ogni clip richiede minuti. Se una clip non riesce, la scena usa la sua immagine con uno zoom lento.", + "local_ai.models.image": "Modelli di immagini", + "local_ai.models.video": "Modelli video", + "upload_post.ai_label": "Contrassegna i post di TikTok e Instagram come generati con l'IA", + "upload_post.ai_label.help": "Le piattaforme richiedono un'etichetta per i contenuti realistici generati con l'IA; i video su YouTube sono sempre etichettati. Su Instagram l'etichetta non si può rimuovere dopo la pubblicazione.", + "upload_post.schedule": "Quando pubblicare", + "upload_post.schedule.now": "Appena il video è pronto", + "upload_post.schedule.queue": "Prossimo spazio libero nella mia coda Upload-Post", + "upload_post.schedule.delay": "Dopo un numero di ore stabilito", + "upload_post.schedule.delay_hours": "Ore di attesa", + "upload_post.schedule.help": "Upload-Post salva i post programmati e li pubblica da sé, quindi questo computer può essere spento. Imposta gli spazi della coda nella dashboard di Upload-Post." } } diff --git a/webui/i18n/ko.json b/webui/i18n/ko.json index 34fc41436b..d1d7c95b03 100644 --- a/webui/i18n/ko.json +++ b/webui/i18n/ko.json @@ -506,6 +506,56 @@ "Configure Providers": "설정", "Active Provider": "사용 중: {provider}", "No Configured Providers": "아직 설정된 제공업체가 없습니다.", - "Provider Configuration Changed": "설정이 변경되었습니다. 이 제공업체의 설정을 확인하세요." + "Provider Configuration Changed": "설정이 변경되었습니다. 이 제공업체의 설정을 확인하세요.", + "local_ai.label": "로컬 AI (이 컴퓨터)", + "local_ai.help": "이 컴퓨터의 그래픽 카드로 장면마다 이미지 한 장을 만듭니다. 무료이며 비공개입니다. 어떤 데이터도 온라인 서비스로 보내지 않습니다. 실행 환경을 한 번 설치하고 모델을 내려받은 뒤 이 소스를 선택하세요.", + "local_ai.hardware": "하드웨어", + "local_ai.no_gpu": "지원되는 그래픽 카드를 찾지 못했습니다. CPU에서 실행되는 모델은 계속 쓸 수 있지만 느립니다.", + "local_ai.ram": "메모리", + "local_ai.gpu": "이미지 생성에 쓸 그래픽 카드", + "local_ai.gpu.auto": "자동 (메모리가 가장 큰 카드)", + "local_ai.runtime": "실행 환경", + "local_ai.runtime.help": "이 그래픽 카드에 맞는 PyTorch와 이미지 라이브러리를 별도 환경에 설치합니다. 약 4GB를 내려받으며 몇 분 걸립니다.", + "local_ai.runtime.install": "실행 환경 설치", + "local_ai.runtime.reinstall": "실행 환경 다시 설치", + "local_ai.runtime.installing": "실행 환경을 설치하는 중입니다. 몇 분 걸리며 그동안 앱을 계속 사용할 수 있습니다.", + "local_ai.runtime.installed": "실행 환경이 설치되었습니다.", + "local_ai.runtime.failed": "실행 환경 설치에 실패했습니다", + "local_ai.models.folder": "모델 폴더", + "local_ai.model.recommended": "이 컴퓨터에 추천", + "local_ai.model.license": "라이선스", + "local_ai.model.download": "내려받기", + "local_ai.model.downloading": "내려받는 중", + "local_ai.model.use": "이 모델 사용", + "local_ai.model.active": "사용 중", + "local_ai.model.delete": "삭제", + "local_ai.fit.gpu": "그래픽 카드에서 실행", + "local_ai.fit.offload": "모델 일부를 메모리에 두고 실행 (더 느림)", + "local_ai.fit.cpu": "CPU에서 실행 (느림)", + "local_ai.fit.none": "이 컴퓨터에서 실행할 수 없음", + "local_ai.server": "이미지 서버", + "local_ai.server.running": "실행 중", + "local_ai.server.stopped": "중지됨. 영상에 이미지가 필요하면 자동으로 시작됩니다.", + "local_ai.server.start": "시작", + "local_ai.server.starting": "이미지 서버를 시작하고 모델을 불러오는 중…", + "local_ai.server.stop": "중지", + "local_ai.advanced": "고급", + "local_ai.hf_token": "Hugging Face 토큰", + "local_ai.hf_token.help": "FLUX.1 schnell처럼 접근이 제한된 모델에만 필요합니다. 먼저 Hugging Face에서 모델 약관에 동의하세요.", + "local_ai.runtime_missing": "먼저 설정 > AI 이미지 > 로컬 AI에서 실행 환경을 설치하세요.", + "local_ai.model_missing": "먼저 설정 > AI 이미지 > 로컬 AI에서 선택한 로컬 이미지 모델을 내려받으세요.", + "local_ai.no_model_fits": "이 컴퓨터의 그래픽 카드나 메모리에 맞는 로컬 이미지 모델이 없습니다.", + "local_ai.video.label": "로컬 AI 영상 (이 컴퓨터)", + "local_ai.video.help": "이 컴퓨터의 그래픽 카드로 장면마다 움직임을 만듭니다. 로컬 이미지 모델이 장면을 그리고, 영상 모델이 이를 짧은 클립으로 바꿉니다. 무료이며 비공개지만 클립마다 몇 분이 걸립니다. 클립 생성에 실패하면 그 장면은 이미지를 천천히 확대해 사용합니다.", + "local_ai.models.image": "이미지 모델", + "local_ai.models.video": "영상 모델", + "upload_post.ai_label": "TikTok 및 Instagram 게시물을 AI 생성 콘텐츠로 표시", + "upload_post.ai_label.help": "플랫폼은 사실적인 AI 생성 콘텐츠에 표시를 요구합니다. YouTube 업로드에는 항상 표시됩니다. Instagram의 표시는 게시 후 제거할 수 없습니다.", + "upload_post.schedule": "게시 시점", + "upload_post.schedule.now": "영상이 준비되면 바로", + "upload_post.schedule.queue": "내 Upload-Post 대기열의 다음 빈 시간대", + "upload_post.schedule.delay": "지정한 시간 후", + "upload_post.schedule.delay_hours": "대기 시간(시간)", + "upload_post.schedule.help": "Upload-Post가 예약 게시물을 저장해 직접 게시하므로 그때 이 컴퓨터는 꺼져 있어도 됩니다. 대기열 시간대는 Upload-Post 대시보드에서 설정하세요." } } diff --git a/webui/i18n/pt.json b/webui/i18n/pt.json index 504db68b9d..dfa4895a0a 100644 --- a/webui/i18n/pt.json +++ b/webui/i18n/pt.json @@ -506,6 +506,56 @@ "Configure Providers": "Configurar", "Active Provider": "Em uso: {provider}", "No Configured Providers": "Nenhum provedor configurado ainda.", - "Provider Configuration Changed": "A configuração mudou. Revise as configurações deste provedor." + "Provider Configuration Changed": "A configuração mudou. Revise as configurações deste provedor.", + "local_ai.label": "IA local (este computador)", + "local_ai.help": "Gera uma imagem por cena com a placa de vídeo deste computador. Grátis e privado: nada é enviado a um serviço online. Instale o ambiente de execução uma vez, baixe um modelo e depois escolha esta fonte.", + "local_ai.hardware": "Hardware", + "local_ai.no_gpu": "Nenhuma placa de vídeo compatível encontrada. Modelos que rodam na CPU ainda funcionam, mas devagar.", + "local_ai.ram": "Memória", + "local_ai.gpu": "Placa de vídeo para gerar imagens", + "local_ai.gpu.auto": "Automático (a com mais memória)", + "local_ai.runtime": "Ambiente de execução", + "local_ai.runtime.help": "Instala o PyTorch para esta placa de vídeo e as bibliotecas de imagem num ambiente separado. Baixa cerca de 4 GB e leva vários minutos.", + "local_ai.runtime.install": "Instalar ambiente", + "local_ai.runtime.reinstall": "Reinstalar ambiente", + "local_ai.runtime.installing": "Instalando o ambiente. Leva vários minutos; você pode continuar usando o app.", + "local_ai.runtime.installed": "Ambiente instalado.", + "local_ai.runtime.failed": "Falha ao instalar o ambiente", + "local_ai.models.folder": "Pasta de modelos", + "local_ai.model.recommended": "Recomendado para este computador", + "local_ai.model.license": "Licença", + "local_ai.model.download": "Baixar", + "local_ai.model.downloading": "Baixando", + "local_ai.model.use": "Usar este modelo", + "local_ai.model.active": "Em uso", + "local_ai.model.delete": "Excluir", + "local_ai.fit.gpu": "Roda na placa de vídeo", + "local_ai.fit.offload": "Roda com parte do modelo na memória (mais lento)", + "local_ai.fit.cpu": "Roda na CPU (lento)", + "local_ai.fit.none": "Não cabe neste computador", + "local_ai.server": "Servidor de imagens", + "local_ai.server.running": "Em execução", + "local_ai.server.stopped": "Parado. Ele inicia automaticamente quando um vídeo precisa de imagens.", + "local_ai.server.start": "Iniciar", + "local_ai.server.starting": "Iniciando o servidor de imagens e carregando o modelo…", + "local_ai.server.stop": "Parar", + "local_ai.advanced": "Avançado", + "local_ai.hf_token": "Token do Hugging Face", + "local_ai.hf_token.help": "Só é necessário para modelos com acesso restrito, como o FLUX.1 schnell. Aceite antes os termos do modelo no Hugging Face.", + "local_ai.runtime_missing": "Instale primeiro o ambiente de IA local em Configurações > Imagem IA > IA local.", + "local_ai.model_missing": "Baixe primeiro o modelo de imagem local selecionado em Configurações > Imagem IA > IA local.", + "local_ai.no_model_fits": "Nenhum modelo de imagem local cabe na placa de vídeo ou na memória deste computador.", + "local_ai.video.label": "Vídeo com IA local (este computador)", + "local_ai.video.help": "Anima cada cena com a placa de vídeo deste computador: o modelo de imagem local pinta a cena e o modelo de vídeo a transforma num clipe curto. Grátis e privado, mas cada clipe leva minutos. Se um clipe falhar, a cena usa a imagem com um zoom lento.", + "local_ai.models.image": "Modelos de imagem", + "local_ai.models.video": "Modelos de vídeo", + "upload_post.ai_label": "Marcar publicações do TikTok e do Instagram como geradas por IA", + "upload_post.ai_label.help": "As plataformas exigem rótulo em conteúdo realista gerado por IA; os envios ao YouTube são sempre rotulados. No Instagram o rótulo não pode ser removido depois de publicar.", + "upload_post.schedule": "Quando publicar", + "upload_post.schedule.now": "Assim que o vídeo ficar pronto", + "upload_post.schedule.queue": "Próximo horário livre na minha fila do Upload-Post", + "upload_post.schedule.delay": "Depois de um número de horas", + "upload_post.schedule.delay_hours": "Horas de espera", + "upload_post.schedule.help": "O Upload-Post guarda as publicações agendadas e as publica sozinho, então este computador pode estar desligado. Configure os horários da fila no painel do Upload-Post." } } diff --git a/webui/i18n/ru.json b/webui/i18n/ru.json index daf0928f0b..278ec45fb7 100644 --- a/webui/i18n/ru.json +++ b/webui/i18n/ru.json @@ -506,6 +506,56 @@ "Configure Providers": "Настроить", "Active Provider": "Используется: {provider}", "No Configured Providers": "Пока нет настроенных провайдеров.", - "Provider Configuration Changed": "Конфигурация изменилась. Проверьте настройки этого провайдера." + "Provider Configuration Changed": "Конфигурация изменилась. Проверьте настройки этого провайдера.", + "local_ai.label": "Локальный ИИ (этот компьютер)", + "local_ai.help": "Создаёт по одному изображению на сцену на видеокарте этого компьютера. Бесплатно и приватно: ничего не отправляется в онлайн-сервисы. Один раз установите среду выполнения, скачайте модель и выберите этот источник.", + "local_ai.hardware": "Оборудование", + "local_ai.no_gpu": "Поддерживаемая видеокарта не найдена. Модели, работающие на процессоре, по-прежнему доступны, но медленно.", + "local_ai.ram": "Память", + "local_ai.gpu": "Видеокарта для генерации изображений", + "local_ai.gpu.auto": "Автоматически (больше всего памяти)", + "local_ai.runtime": "Среда выполнения", + "local_ai.runtime.help": "Устанавливает PyTorch для этой видеокарты и библиотеки для изображений в отдельное окружение. Загружает около 4 ГБ и занимает несколько минут.", + "local_ai.runtime.install": "Установить среду", + "local_ai.runtime.reinstall": "Переустановить среду", + "local_ai.runtime.installing": "Идёт установка среды. Это займёт несколько минут; приложением можно пользоваться дальше.", + "local_ai.runtime.installed": "Среда установлена.", + "local_ai.runtime.failed": "Не удалось установить среду", + "local_ai.models.folder": "Папка моделей", + "local_ai.model.recommended": "Рекомендуется для этого компьютера", + "local_ai.model.license": "Лицензия", + "local_ai.model.download": "Скачать", + "local_ai.model.downloading": "Загрузка", + "local_ai.model.use": "Использовать эту модель", + "local_ai.model.active": "Используется", + "local_ai.model.delete": "Удалить", + "local_ai.fit.gpu": "Работает на видеокарте", + "local_ai.fit.offload": "Работает с частью модели в памяти (медленнее)", + "local_ai.fit.cpu": "Работает на процессоре (медленно)", + "local_ai.fit.none": "Не подходит для этого компьютера", + "local_ai.server": "Сервер изображений", + "local_ai.server.running": "Работает", + "local_ai.server.stopped": "Остановлен. Запускается автоматически, когда видео нужны изображения.", + "local_ai.server.start": "Запустить", + "local_ai.server.starting": "Запуск сервера изображений и загрузка модели…", + "local_ai.server.stop": "Остановить", + "local_ai.advanced": "Дополнительно", + "local_ai.hf_token": "Токен Hugging Face", + "local_ai.hf_token.help": "Нужен только для моделей с ограниченным доступом, например FLUX.1 schnell. Сначала примите условия модели на Hugging Face.", + "local_ai.runtime_missing": "Сначала установите среду локального ИИ: Настройки > ИИ-изображения > Локальный ИИ.", + "local_ai.model_missing": "Сначала скачайте выбранную локальную модель изображений: Настройки > ИИ-изображения > Локальный ИИ.", + "local_ai.no_model_fits": "Ни одна локальная модель изображений не подходит для видеокарты или памяти этого компьютера.", + "local_ai.video.label": "Локальное ИИ-видео (этот компьютер)", + "local_ai.video.help": "Оживляет каждую сцену на видеокарте этого компьютера: локальная модель рисует сцену, затем видеомодель превращает её в короткий клип. Бесплатно и приватно, но каждый клип занимает несколько минут. Если клип не получился, сцена использует своё изображение с медленным приближением.", + "local_ai.models.image": "Модели изображений", + "local_ai.models.video": "Видеомодели", + "upload_post.ai_label": "Отмечать публикации в TikTok и Instagram как созданные ИИ", + "upload_post.ai_label.help": "Платформы требуют отмечать реалистичный контент, созданный ИИ; видео на YouTube отмечаются всегда. В Instagram отметку нельзя снять после публикации.", + "upload_post.schedule": "Когда публиковать", + "upload_post.schedule.now": "Сразу после готовности видео", + "upload_post.schedule.queue": "Ближайший свободный слот в моей очереди Upload-Post", + "upload_post.schedule.delay": "Через заданное число часов", + "upload_post.schedule.delay_hours": "Часов ожидания", + "upload_post.schedule.help": "Upload-Post сохраняет отложенные публикации и публикует их сам, поэтому компьютер к этому времени можно выключить. Слоты очереди настраиваются в панели Upload-Post." } } diff --git a/webui/i18n/tr.json b/webui/i18n/tr.json index d56336a3a9..6dbd4193bf 100644 --- a/webui/i18n/tr.json +++ b/webui/i18n/tr.json @@ -506,6 +506,56 @@ "Configure Providers": "Yapılandır", "Active Provider": "Kullanımda: {provider}", "No Configured Providers": "Henüz yapılandırılmış sağlayıcı yok.", - "Provider Configuration Changed": "Yapılandırma değişti. Bu sağlayıcının ayarlarını gözden geçirin." + "Provider Configuration Changed": "Yapılandırma değişti. Bu sağlayıcının ayarlarını gözden geçirin.", + "local_ai.label": "Yerel yapay zekâ (bu bilgisayar)", + "local_ai.help": "Bu bilgisayarın ekran kartında her sahne için bir görsel üretir. Ücretsiz ve gizli: hiçbir şey çevrim içi bir hizmete gönderilmez. Çalışma ortamını bir kez kurun, bir model indirin, ardından bu kaynağı seçin.", + "local_ai.hardware": "Donanım", + "local_ai.no_gpu": "Desteklenen ekran kartı bulunamadı. İşlemcide çalışan modeller yine kullanılabilir ama yavaştır.", + "local_ai.ram": "Bellek", + "local_ai.gpu": "Görsel üretimi için ekran kartı", + "local_ai.gpu.auto": "Otomatik (en çok bellek)", + "local_ai.runtime": "Çalışma ortamı", + "local_ai.runtime.help": "Bu ekran kartına uygun PyTorch'u ve görsel kitaplıklarını ayrı bir ortama kurar. Yaklaşık 4 GB indirir ve birkaç dakika sürer.", + "local_ai.runtime.install": "Çalışma ortamını kur", + "local_ai.runtime.reinstall": "Çalışma ortamını yeniden kur", + "local_ai.runtime.installing": "Çalışma ortamı kuruluyor. Birkaç dakika sürer; uygulamayı kullanmaya devam edebilirsiniz.", + "local_ai.runtime.installed": "Çalışma ortamı kuruldu.", + "local_ai.runtime.failed": "Çalışma ortamı kurulamadı", + "local_ai.models.folder": "Model klasörü", + "local_ai.model.recommended": "Bu bilgisayar için önerilir", + "local_ai.model.license": "Lisans", + "local_ai.model.download": "İndir", + "local_ai.model.downloading": "İndiriliyor", + "local_ai.model.use": "Bu modeli kullan", + "local_ai.model.active": "Kullanımda", + "local_ai.model.delete": "Sil", + "local_ai.fit.gpu": "Ekran kartında çalışır", + "local_ai.fit.offload": "Modelin bir kısmı bellekte çalışır (daha yavaş)", + "local_ai.fit.cpu": "İşlemcide çalışır (yavaş)", + "local_ai.fit.none": "Bu bilgisayara uygun değil", + "local_ai.server": "Görsel sunucusu", + "local_ai.server.running": "Çalışıyor", + "local_ai.server.stopped": "Durduruldu. Bir videonun görsele ihtiyacı olduğunda kendiliğinden başlar.", + "local_ai.server.start": "Başlat", + "local_ai.server.starting": "Görsel sunucusu başlatılıyor ve model yükleniyor…", + "local_ai.server.stop": "Durdur", + "local_ai.advanced": "Gelişmiş", + "local_ai.hf_token": "Hugging Face belirteci", + "local_ai.hf_token.help": "Yalnızca FLUX.1 schnell gibi erişimi kısıtlı modeller için gerekir. Önce Hugging Face'te modelin koşullarını kabul edin.", + "local_ai.runtime_missing": "Önce Ayarlar > Yapay Zekâ Görseli > Yerel yapay zekâ bölümünden çalışma ortamını kurun.", + "local_ai.model_missing": "Önce Ayarlar > Yapay Zekâ Görseli > Yerel yapay zekâ bölümünden seçili yerel görsel modelini indirin.", + "local_ai.no_model_fits": "Bu bilgisayarın ekran kartına veya belleğine uygun yerel görsel modeli yok.", + "local_ai.video.label": "Yerel yapay zekâ videosu (bu bilgisayar)", + "local_ai.video.help": "Her sahneyi bu bilgisayarın ekran kartında canlandırır: yerel görsel modeli sahneyi çizer, ardından video modeli onu kısa bir klibe dönüştürür. Ücretsiz ve gizli, ancak her klip dakikalar sürer. Bir klip başarısız olursa o sahne kendi görselini yavaş yakınlaştırmayla kullanır.", + "local_ai.models.image": "Görsel modelleri", + "local_ai.models.video": "Video modelleri", + "upload_post.ai_label": "TikTok ve Instagram gönderilerini yapay zekâ ile üretilmiş olarak işaretle", + "upload_post.ai_label.help": "Platformlar gerçekçi yapay zekâ içeriklerinin etiketlenmesini ister; YouTube yüklemeleri her zaman etiketlenir. Instagram'da etiket yayınlandıktan sonra kaldırılamaz.", + "upload_post.schedule": "Ne zaman yayınlansın", + "upload_post.schedule.now": "Video hazır olur olmaz", + "upload_post.schedule.queue": "Upload-Post kuyruğumdaki bir sonraki boş zaman", + "upload_post.schedule.delay": "Belirli bir saat sonra", + "upload_post.schedule.delay_hours": "Beklenecek saat", + "upload_post.schedule.help": "Upload-Post zamanlanmış gönderileri saklar ve kendisi yayınlar; o sırada bu bilgisayar kapalı olabilir. Kuyruk zamanlarını Upload-Post panelinden ayarlayın." } } diff --git a/webui/i18n/vi.json b/webui/i18n/vi.json index 98b01a756f..3672e863c7 100644 --- a/webui/i18n/vi.json +++ b/webui/i18n/vi.json @@ -506,6 +506,56 @@ "Configure Providers": "Cấu hình", "Active Provider": "Đang sử dụng: {provider}", "No Configured Providers": "Chưa có nhà cung cấp nào được cấu hình.", - "Provider Configuration Changed": "Cấu hình đã thay đổi. Hãy kiểm tra cài đặt của nhà cung cấp này." + "Provider Configuration Changed": "Cấu hình đã thay đổi. Hãy kiểm tra cài đặt của nhà cung cấp này.", + "local_ai.label": "AI cục bộ (máy tính này)", + "local_ai.help": "Tạo một ảnh cho mỗi cảnh bằng card đồ họa của máy tính này. Miễn phí và riêng tư: không gửi gì lên dịch vụ trực tuyến. Cài môi trường chạy một lần, tải một mô hình rồi chọn nguồn này.", + "local_ai.hardware": "Phần cứng", + "local_ai.no_gpu": "Không tìm thấy card đồ họa được hỗ trợ. Các mô hình chạy trên CPU vẫn dùng được nhưng chậm.", + "local_ai.ram": "Bộ nhớ", + "local_ai.gpu": "Card đồ họa để tạo ảnh", + "local_ai.gpu.auto": "Tự động (bộ nhớ lớn nhất)", + "local_ai.runtime": "Môi trường chạy", + "local_ai.runtime.help": "Cài PyTorch cho card đồ họa này và các thư viện ảnh vào một môi trường riêng. Tải khoảng 4 GB và mất vài phút.", + "local_ai.runtime.install": "Cài môi trường chạy", + "local_ai.runtime.reinstall": "Cài lại môi trường chạy", + "local_ai.runtime.installing": "Đang cài môi trường chạy. Mất vài phút; bạn vẫn có thể dùng ứng dụng.", + "local_ai.runtime.installed": "Đã cài môi trường chạy.", + "local_ai.runtime.failed": "Cài môi trường chạy thất bại", + "local_ai.models.folder": "Thư mục mô hình", + "local_ai.model.recommended": "Đề xuất cho máy tính này", + "local_ai.model.license": "Giấy phép", + "local_ai.model.download": "Tải xuống", + "local_ai.model.downloading": "Đang tải xuống", + "local_ai.model.use": "Dùng mô hình này", + "local_ai.model.active": "Đang dùng", + "local_ai.model.delete": "Xóa", + "local_ai.fit.gpu": "Chạy trên card đồ họa", + "local_ai.fit.offload": "Chạy với một phần mô hình trong bộ nhớ (chậm hơn)", + "local_ai.fit.cpu": "Chạy trên CPU (chậm)", + "local_ai.fit.none": "Không phù hợp với máy tính này", + "local_ai.server": "Máy chủ ảnh", + "local_ai.server.running": "Đang chạy", + "local_ai.server.stopped": "Đã dừng. Tự khởi động khi video cần ảnh.", + "local_ai.server.start": "Khởi động", + "local_ai.server.starting": "Đang khởi động máy chủ ảnh và tải mô hình…", + "local_ai.server.stop": "Dừng", + "local_ai.advanced": "Nâng cao", + "local_ai.hf_token": "Token Hugging Face", + "local_ai.hf_token.help": "Chỉ cần cho các mô hình bị giới hạn truy cập như FLUX.1 schnell. Hãy đồng ý điều khoản của mô hình trên Hugging Face trước.", + "local_ai.runtime_missing": "Hãy cài môi trường AI cục bộ trước trong Cài đặt > Ảnh AI > AI cục bộ.", + "local_ai.model_missing": "Hãy tải mô hình ảnh cục bộ đã chọn trước trong Cài đặt > Ảnh AI > AI cục bộ.", + "local_ai.no_model_fits": "Không có mô hình ảnh cục bộ nào phù hợp với card đồ họa hoặc bộ nhớ của máy tính này.", + "local_ai.video.label": "Video AI cục bộ (máy tính này)", + "local_ai.video.help": "Tạo chuyển động cho từng cảnh bằng card đồ họa của máy tính này: mô hình ảnh cục bộ vẽ cảnh, rồi mô hình video biến nó thành một đoạn ngắn. Miễn phí và riêng tư, nhưng mỗi đoạn mất vài phút. Nếu một đoạn thất bại, cảnh đó dùng ảnh của nó với hiệu ứng phóng chậm.", + "local_ai.models.image": "Mô hình ảnh", + "local_ai.models.video": "Mô hình video", + "upload_post.ai_label": "Gắn nhãn bài đăng TikTok và Instagram là do AI tạo", + "upload_post.ai_label.help": "Các nền tảng yêu cầu gắn nhãn nội dung AI trông như thật; video YouTube luôn được gắn nhãn. Nhãn trên Instagram không thể gỡ sau khi đăng.", + "upload_post.schedule": "Khi nào đăng", + "upload_post.schedule.now": "Ngay khi video xong", + "upload_post.schedule.queue": "Khung giờ trống tiếp theo trong hàng đợi Upload-Post của tôi", + "upload_post.schedule.delay": "Sau một số giờ nhất định", + "upload_post.schedule.delay_hours": "Số giờ chờ", + "upload_post.schedule.help": "Upload-Post lưu bài đăng hẹn giờ và tự đăng, nên lúc đó máy tính này có thể tắt. Thiết lập khung giờ hàng đợi trong bảng điều khiển Upload-Post." } } diff --git a/webui/i18n/zh.json b/webui/i18n/zh.json index 841b98303d..93cd2e1c3f 100644 --- a/webui/i18n/zh.json +++ b/webui/i18n/zh.json @@ -761,6 +761,56 @@ "Configure Providers": "配置", "Active Provider": "正在使用:{provider}", "No Configured Providers": "尚未配置服务商。", - "Provider Configuration Changed": "配置已更改,请检查该服务商的设置。" + "Provider Configuration Changed": "配置已更改,请检查该服务商的设置。", + "local_ai.label": "本机 AI(此电脑)", + "local_ai.help": "使用本机显卡为每个画面生成一张图片。免费且私密:不会发送到任何在线服务。先安装运行环境、下载模型,再选择此素材来源。", + "local_ai.hardware": "硬件", + "local_ai.no_gpu": "未检测到支持的显卡。可在 CPU 上运行的模型仍可使用,但速度较慢。", + "local_ai.ram": "内存", + "local_ai.gpu": "生图使用的显卡", + "local_ai.gpu.auto": "自动(显存最大)", + "local_ai.runtime": "运行环境", + "local_ai.runtime.help": "将适配此显卡的 PyTorch 与生图库安装到独立环境中。约需下载 4 GB,耗时数分钟。", + "local_ai.runtime.install": "安装运行环境", + "local_ai.runtime.reinstall": "重新安装运行环境", + "local_ai.runtime.installing": "正在安装运行环境,需要数分钟;期间可以继续使用。", + "local_ai.runtime.installed": "运行环境已安装。", + "local_ai.runtime.failed": "运行环境安装失败", + "local_ai.models.folder": "模型文件夹", + "local_ai.model.recommended": "推荐用于此电脑", + "local_ai.model.license": "许可证", + "local_ai.model.download": "下载", + "local_ai.model.downloading": "正在下载", + "local_ai.model.use": "使用此模型", + "local_ai.model.active": "使用中", + "local_ai.model.delete": "删除", + "local_ai.fit.gpu": "可完全在显卡上运行", + "local_ai.fit.offload": "部分模型放在内存中运行(较慢)", + "local_ai.fit.cpu": "在 CPU 上运行(慢)", + "local_ai.fit.none": "此电脑无法运行", + "local_ai.server": "生图服务", + "local_ai.server.running": "运行中", + "local_ai.server.stopped": "已停止。视频需要图片时会自动启动。", + "local_ai.server.start": "启动", + "local_ai.server.starting": "正在启动生图服务并加载模型…", + "local_ai.server.stop": "停止", + "local_ai.advanced": "高级", + "local_ai.hf_token": "Hugging Face 令牌", + "local_ai.hf_token.help": "仅受限模型(如 FLUX.1 schnell)需要。请先在 Hugging Face 上同意该模型的条款。", + "local_ai.runtime_missing": "请先在 设置 > AI 图片 > 本机 AI 中安装运行环境。", + "local_ai.model_missing": "请先在 设置 > AI 图片 > 本机 AI 中下载所选的本机生图模型。", + "local_ai.no_model_fits": "没有适合此电脑显卡或内存的本机生图模型。", + "local_ai.video.label": "本机 AI 视频(此电脑)", + "local_ai.video.help": "使用本机显卡为每个画面生成动画:先由本机生图模型画出画面,再由视频模型做成短片段。免费且私密,但每个片段需要数分钟。某个片段失败时,该画面改用图片加缓慢缩放。", + "local_ai.models.image": "生图模型", + "local_ai.models.video": "视频模型", + "upload_post.ai_label": "将 TikTok 与 Instagram 帖子标注为 AI 生成", + "upload_post.ai_label.help": "平台要求对逼真的 AI 生成内容进行标注;YouTube 上传始终带有标注。Instagram 的标注发布后无法移除。", + "upload_post.schedule": "发布时间", + "upload_post.schedule.now": "视频生成后立即发布", + "upload_post.schedule.queue": "Upload-Post 发布队列中的下一个空闲时段", + "upload_post.schedule.delay": "指定小时数之后", + "upload_post.schedule.delay_hours": "等待小时数", + "upload_post.schedule.help": "Upload-Post 会保存定时发布并按时执行,到时本机可以关机。发布队列时段请在 Upload-Post 后台设置。" } } diff --git a/webui/settings_providers.py b/webui/settings_providers.py index 2cb0cc85f1..8e50a41c2c 100644 --- a/webui/settings_providers.py +++ b/webui/settings_providers.py @@ -12,6 +12,7 @@ from app.services import ( infistar_video, jalapeno_video, + local_ai, loomloom, material, metaso_minimax, @@ -102,6 +103,8 @@ def configured_material_providers(settings, groups, codex_connected=False): "jalapeno": jalapeno_video.is_enabled(settings), "muapi": muapi.is_enabled(settings), "openai_image": material.is_openai_image_enabled(settings), + "local_image": local_ai.readiness()[0], + "local_video": local_ai.readiness("video")[0], "codex_image": bool( codex_connected and _text(settings.get("codex_image_model_name")) ),