Download app.py from taowright/A-image: direct link, hf CLI and curl.
- Browser
- Download file 6.86 kB
-
https://huggingface.co/spaces/taowright/A-image/resolve/main/app.py
- Command line
-
hf download hf://spaces/taowright/A-image/app.py
-
curl -L -o app.py https://huggingface.co/spaces/taowright/A-image/resolve/main/app.py
6.86 kB
| """A-image — Minimal English Anima Studio on Hugging Face ZeroGPU ``xlarge``. | |
| - WebUI: Streamlined English interface (Positive & Negative prompts, Resolution, Sampler, Steps, CFG, Seed). | |
| LoRAs and reservation controls are hidden from WebUI. | |
| - API: Full programmatic control over built-in LoRAs (Civitai 2540444 @ 1.0, Civitai 2612570 @ 0.9), | |
| live Civitai LoRA installation (`/add_lora`), and ZeroGPU `xlarge` reservation duration (`reserve` / `/set_reserve`). | |
| """ | |
| from __future__ import annotations | |
| import os | |
| from collections.abc import Callable | |
| from typing import Any | |
| from anima_studio.config import ( | |
| DEFAULT_CFG, | |
| DEFAULT_HIGHRES_ENABLED, | |
| DEFAULT_HIGHRES_SCALE, | |
| DEFAULT_QUALITY_ENABLED, | |
| DEFAULT_QUALITY_SCALE, | |
| DEFAULT_RATING, | |
| DEFAULT_SIZE, | |
| DEFAULT_STEPS, | |
| NEGATIVE_PROMPT as NEGATIVE, | |
| PROMPT_PREFIX, | |
| RATINGS, | |
| SCHEDULERS, | |
| SIZES, | |
| AppSettings, | |
| configure_zerogpu_environment, | |
| ) | |
| configure_zerogpu_environment() | |
| import gradio as gr # noqa: E402 | |
| from anima_studio.engine import AnimaStudioEngine # noqa: E402 | |
| from anima_studio.ui import ( # noqa: E402 | |
| STUDIO_CSS, | |
| STUDIO_HEAD, | |
| build_atelier_theme, | |
| build_studio_blocks, | |
| ) | |
| try: | |
| import spaces as _spaces # type: ignore[import-not-found] | |
| except ImportError: | |
| _spaces = None | |
| SETTINGS = AppSettings.from_env() | |
| DEFAULT_SCHEDULER = SETTINGS.default_scheduler | |
| ENGINE = AnimaStudioEngine(SETTINGS) | |
| __all__ = [ | |
| "DEFAULT_SCHEDULER", | |
| "ENGINE", | |
| "NEGATIVE", | |
| "PROMPT_PREFIX", | |
| "RATINGS", | |
| "SCHEDULERS", | |
| "SETTINGS", | |
| "SIZES", | |
| "demo", | |
| "generate", | |
| "generate_studio", | |
| "launch_app", | |
| ] | |
| def _gpu_lease( | |
| prompt: str, | |
| negative: str = NEGATIVE, | |
| size: str = DEFAULT_SIZE, | |
| steps: float = DEFAULT_STEPS, | |
| guidance: float = DEFAULT_CFG, | |
| seed: float | None = -1, | |
| scheduler: str = DEFAULT_SCHEDULER, | |
| enable_highres_lora: bool = DEFAULT_HIGHRES_ENABLED, | |
| highres_lora_scale: float = DEFAULT_HIGHRES_SCALE, | |
| enable_quality_lora: bool = DEFAULT_QUALITY_ENABLED, | |
| quality_lora_scale: float = DEFAULT_QUALITY_SCALE, | |
| lora_name: str = "", | |
| lora_scale: float = 1.0, | |
| extra_loras: str = "", | |
| rating: str = DEFAULT_RATING, | |
| prefix: str = PROMPT_PREFIX, | |
| reserve: float = 0, | |
| ) -> int: | |
| """Compute ZeroGPU ``xlarge`` duration lease for API requests.""" | |
| del ( | |
| prompt, | |
| negative, | |
| guidance, | |
| seed, | |
| scheduler, | |
| enable_highres_lora, | |
| highres_lora_scale, | |
| enable_quality_lora, | |
| quality_lora_scale, | |
| lora_name, | |
| lora_scale, | |
| extra_loras, | |
| rating, | |
| prefix, | |
| ) | |
| try: | |
| return ENGINE.resolve_lease(size=size, steps=steps, reserve=reserve) | |
| except Exception: | |
| return 30 | |
| def _gpu_lease_studio( | |
| prompt: str, | |
| negative: str = NEGATIVE, | |
| size: str = DEFAULT_SIZE, | |
| steps: float = DEFAULT_STEPS, | |
| guidance: float = DEFAULT_CFG, | |
| seed: float | None = -1, | |
| scheduler: str = DEFAULT_SCHEDULER, | |
| ) -> int: | |
| """Compute ZeroGPU ``xlarge`` duration lease for WebUI requests (reservation controlled via API only).""" | |
| del prompt, negative, guidance, seed, scheduler | |
| try: | |
| return ENGINE.resolve_lease(size=size, steps=steps, reserve=0) | |
| except Exception: | |
| return 30 | |
| def _zerogpu_decorator( | |
| duration_fn: Callable[..., int], | |
| ) -> Callable[[Callable[..., Any]], Callable[..., Any]]: | |
| """Apply ``@spaces.GPU(size='xlarge', duration=...)`` on ZeroGPU; no-op in CPU mock mode.""" | |
| if _spaces is not None and not SETTINGS.mock_mode: | |
| return _spaces.GPU(size=SETTINGS.gpu_size, duration=duration_fn) | |
| return lambda fn: fn | |
| def generate( | |
| prompt: str, | |
| negative: str = NEGATIVE, | |
| size: str = DEFAULT_SIZE, | |
| steps: float = DEFAULT_STEPS, | |
| guidance: float = DEFAULT_CFG, | |
| seed: float | None = -1, | |
| scheduler: str = DEFAULT_SCHEDULER, | |
| enable_highres_lora: bool = DEFAULT_HIGHRES_ENABLED, | |
| highres_lora_scale: float = DEFAULT_HIGHRES_SCALE, | |
| enable_quality_lora: bool = DEFAULT_QUALITY_ENABLED, | |
| quality_lora_scale: float = DEFAULT_QUALITY_SCALE, | |
| lora_name: str = "", | |
| lora_scale: float = 1.0, | |
| extra_loras: str = "", | |
| rating: str = DEFAULT_RATING, | |
| prefix: str = PROMPT_PREFIX, | |
| reserve: float = 0, | |
| ) -> Any: | |
| """External API image generation endpoint with full LoRA & reservation control.""" | |
| try: | |
| image, _ = ENGINE.render( | |
| prompt=prompt, | |
| negative=negative, | |
| size=size, | |
| steps=steps, | |
| guidance=guidance, | |
| seed=seed, | |
| scheduler=scheduler, | |
| enable_highres_lora=enable_highres_lora, | |
| highres_lora_scale=highres_lora_scale, | |
| enable_quality_lora=enable_quality_lora, | |
| quality_lora_scale=quality_lora_scale, | |
| lora_name=lora_name, | |
| lora_scale=lora_scale, | |
| extra_loras=extra_loras, | |
| rating=rating, | |
| prefix=prefix, | |
| reserve=reserve, | |
| ) | |
| return image | |
| except ValueError as error: | |
| raise gr.Error(str(error)) from error | |
| def generate_studio( | |
| prompt: str, | |
| negative: str = NEGATIVE, | |
| size: str = DEFAULT_SIZE, | |
| steps: float = DEFAULT_STEPS, | |
| guidance: float = DEFAULT_CFG, | |
| seed: float | None = -1, | |
| scheduler: str = DEFAULT_SCHEDULER, | |
| ) -> tuple[Any, str]: | |
| """Minimal WebUI generation endpoint (uses default LoRAs and API-configured reservation).""" | |
| try: | |
| image, telemetry = ENGINE.render( | |
| prompt=prompt, | |
| negative=negative, | |
| size=size, | |
| steps=steps, | |
| guidance=guidance, | |
| seed=seed, | |
| scheduler=scheduler, | |
| reserve=0, | |
| ) | |
| summary = ( | |
| f"`{telemetry.requested_size}` · Seed `{telemetry.actual_seed}` · " | |
| f"`{telemetry.scheduler}` · `{telemetry.steps}` steps · CFG `{telemetry.guidance:g}` · " | |
| f"`{telemetry.elapsed_seconds:.2f}s`" | |
| ) | |
| return image, summary | |
| except ValueError as error: | |
| raise gr.Error(str(error)) from error | |
| demo = build_studio_blocks( | |
| ENGINE, | |
| api_generate_fn=generate, | |
| studio_generate_fn=generate_studio, | |
| ) | |
| demo.queue() | |
| def launch_app(**overrides: Any) -> Any: | |
| """Launch Gradio 6 server with the minimal English studio theme.""" | |
| launch_kwargs: dict[str, Any] = { | |
| "theme": build_atelier_theme(), | |
| "css": STUDIO_CSS, | |
| "head": STUDIO_HEAD, | |
| } | |
| if server_port := os.environ.get("GRADIO_SERVER_PORT"): | |
| launch_kwargs["server_port"] = int(server_port) | |
| launch_kwargs.update(overrides) | |
| return demo.launch(**launch_kwargs) | |
| if __name__ == "__main__": | |
| launch_app() | |