Spaces:
Sleeping
Sleeping
Download prompt_assistant.py from Simzy/Wan-2.2-Studio: direct link, hf CLI and curl.
- Browser
- Download file 3.96 kB
-
https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/prompt_assistant.py
- Command line
-
hf download hf://spaces/Simzy/Wan-2.2-Studio/prompt_assistant.py
-
curl -L -o prompt_assistant.py https://huggingface.co/spaces/Simzy/Wan-2.2-Studio/resolve/main/prompt_assistant.py
3.96 kB
| """Shared remote prompt-writer selection; no model weights or credentials here.""" | |
| from __future__ import annotations | |
| import os | |
| from economy_prompt import ECONOMY_SPACE, ECONOMY_LABEL | |
| DEFAULT_WRITER_SPACE = "amisima/Qwen3.8-27B-Uncensored-Demo" | |
| def _space_id(writer): | |
| value = str(writer or "").strip() | |
| for prefix in ("https://huggingface.co/spaces/", "http://huggingface.co/spaces/"): | |
| if value.lower().startswith(prefix): | |
| value = value[len(prefix):] | |
| break | |
| return value.rstrip("/") | |
| def resolve_writer(writer): | |
| value = _space_id(writer) | |
| if not value: | |
| raise ValueError("Choose a writing Space before preparing a prompt. No AI request was sent.") | |
| # Migrate retired selections from saved profiles; never call their old backend. | |
| if "fable" in value.lower(): | |
| value = _space_id(os.environ.get("EXPANDER_SPACE", DEFAULT_WRITER_SPACE)) | |
| if not value or "fable" in value.lower(): | |
| value = DEFAULT_WRITER_SPACE | |
| return value | |
| def assistant_choices(qwen_space): | |
| return [("Qwen3.8", resolve_writer(qwen_space)), (ECONOMY_LABEL, ECONOMY_SPACE)] | |
| def supports_images(writer): | |
| # Preserve the reference image for the selected vision writer. | |
| return True | |
| def prepare_request(writer, message, image=None): | |
| target = resolve_writer(writer) | |
| return target, message, image | |
| def _reply_text(result): | |
| if result is None: | |
| return "" | |
| if isinstance(result, str): | |
| return result.strip() | |
| if isinstance(result, dict): | |
| if result.get("role") in ("user", "system", "tool"): | |
| return "" | |
| if (result.get("metadata") or {}).get("title"): | |
| return "" | |
| return _reply_text(result.get("content", result.get("text", ""))) | |
| if isinstance(result, (tuple, list)): | |
| if result and all(isinstance(item, dict) and "role" in item for item in result): | |
| for item in reversed(result): | |
| if item.get("role") == "assistant": | |
| text = _reply_text(item) | |
| if text: | |
| return text | |
| return "" | |
| if result and all(isinstance(item, (tuple, list)) and len(item) == 2 for item in result): | |
| return _reply_text(result[-1][1]) | |
| if result and all(isinstance(item, dict) and item.get("type") == "text" for item in result): | |
| return "\n".join(_reply_text(item) for item in result).strip() | |
| for item in reversed(result): | |
| text = _reply_text(item) | |
| if text: | |
| return text | |
| return "" | |
| def writer_result(result): | |
| """Reject incomplete JSON/prompt output, supporting existing Gradio chat APIs.""" | |
| if isinstance(result, (tuple, list)) and len(result) == 1: | |
| return writer_result(result[0]) | |
| if isinstance(result, dict) and result.get("protocol") == "h3-writer-v1": | |
| if result.get("finish_reason") != "stop": | |
| raise ValueError( | |
| "The writer returned an incomplete answer " | |
| f"({result.get('finish_reason', 'unknown')}; " | |
| f"{result.get('generated_tokens', '?')}/{result.get('max_new_tokens', '?')} tokens). " | |
| "No automatic retry was submitted." | |
| ) | |
| text = str(result.get("text") or "").strip() | |
| else: | |
| def cut_off(value): | |
| if isinstance(value, dict): | |
| title = str((value.get("metadata") or {}).get("title", "")).lower() | |
| return "cut off" in title or "truncated" in title or cut_off(value.get("content")) | |
| return isinstance(value, (tuple, list)) and any(cut_off(item) for item in value) | |
| if cut_off(result): | |
| raise ValueError("The writer reported a truncated answer. No automatic retry was submitted.") | |
| text = _reply_text(result) | |
| if not text: | |
| raise ValueError("The writer returned no final answer. No automatic retry was submitted.") | |
| return text | |