返回 ppt-master
backend_modelscope.py
根目录 / skills / ppt-master / scripts / image_backends / backend_modelscope.py
1 #!/usr/bin/env python3
2 """
3 ModelScope image generation backend.
4
5 Configuration keys:
6 MODELSCOPE_API_KEY (required)
7 MODELSCOPE_MODEL (required; no static default)
8 MODELSCOPE_BASE_URL (optional)
9 """
10
11 import os
12 import time
13
14 import requests
15
16 from image_backends.backend_common import (
17 MAX_RETRIES,
18 http_error,
19 is_permanent_error,
20 is_rate_limit_error,
21 normalize_image_size,
22 require_api_key,
23 resolve_output_path,
24 retry_delay,
25 poll_json,
26 download_image
27 )
28
29 DEFAULT_ENDPOINT = "https://api-inference.modelscope.cn"
30
31 # Resolution must be 64-aligned.
32 ASPECT_RATIO_SIZE_MAP = {
33 "512px": {
34 "1:1": "1024*1024",
35 "3:4": "768*1024",
36 "4:3": "1024*768",
37 "9:16": "576*1024",
38 "16:9": "1024*576"
39 },
40 "1K": {
41 "1:1": "1280*1280",
42 "3:4": "960*1280",
43 "4:3": "1280*960",
44 "9:16": "576*1024",
45 "16:9": "1024*576"
46 },
47 "2K": {
48 "1:1": "2048*2048",
49 "3:4": "1536*2048",
50 "4:3": "2048*1536",
51 "9:16": "1152*2048",
52 "16:9": "2048*1152"
53 },
54 "4K": {
55 "1:1": "2048*2048",
56 "3:4": "1920*2560",
57 "4:3": "2560*1920",
58 "9:16": "1728*3072",
59 "16:9": "3072*1728"
60 }
61 }
62
63 def _resolve_url(base_url: str) -> str:
64 """Resolve the ModelScope generation endpoint."""
65 base = base_url.rstrip("/")
66 if base.endswith("/v1"):
67 base = base.removesuffix("/v1")
68 return base
69
70 def _resolve_size(aspect_ratio: str, image_size: str) -> str:
71 """Resolve the target resolution for a ratio and logical size preset.
72
73 Args:
74 aspect_ratio (str): The aspect ratio string. Supported values: '1:1', '3:4', '4:3', '9:16', '16:9'.
75 image_size (str): The logical size preset. Supported values: '512px', '1K', '2K', '4K'.
76 """
77 normalized = normalize_image_size(image_size)
78 size = (ASPECT_RATIO_SIZE_MAP.get(normalized) or {}).get(aspect_ratio)
79 if not size:
80 supported = sorted(ASPECT_RATIO_SIZE_MAP["1K"])
81 raise ValueError(
82 f"Unsupported aspect ratio '{aspect_ratio}' for ModelScope backend. "
83 f"Supported: {supported}"
84 )
85 return size
86
87
88 def _resolve_model(model: str = None) -> str:
89 """Require an explicitly configured text-to-image API-Inference model."""
90 resolved = (model or os.environ.get("MODELSCOPE_MODEL") or "").strip()
91 if not resolved:
92 raise ValueError(
93 "No ModelScope text-to-image model configured. Pass model=... or set "
94 "MODELSCOPE_MODEL to a current API-Inference text-to-image model from "
95 "https://api-inference.modelscope.cn/v1/models."
96 )
97 if "edit" in resolved.lower():
98 raise ValueError(
99 "ModelScope image-edit models require source images and are not supported "
100 "by this text-to-image backend."
101 )
102 return resolved
103
104
105 def _generate_image(api_key: str, prompt: str,
106 aspect_ratio: str = "1:1", image_size: str = "1K",
107 output_dir: str = None, filename: str = None,
108 model: str = None, base_url: str = DEFAULT_ENDPOINT) -> str:
109 """Generate one image with the ModelScope backend."""
110 resolved_model = _resolve_model(model)
111 size = _resolve_size(aspect_ratio, image_size)
112 url = _resolve_url(base_url)+'/v1/images/generations'
113 common_headers = {
114 "Authorization": f"Bearer {api_key}",
115 "Content-Type": "application/json",
116 }
117 payload = {
118 "model": resolved_model,
119 "prompt": prompt,
120 "size": size.replace("*", "x"),
121
122 }
123
124 print("[ModelScope Models]")
125 print(f" Model: {resolved_model}")
126 print(f" Prompt: {prompt[:120]}{'...' if len(prompt) > 120 else ''}")
127 print(f" Aspect Ratio: {aspect_ratio}")
128 print(f" Resolution: {size}")
129 print()
130 print(" [..] Generating...", end="", flush=True)
131 start = time.time()
132 response = requests.post(url, headers={**common_headers,"X-ModelScope-Async-Mode": "true"}, json=payload, timeout=300)
133
134 if (response.status_code != 200):
135 raise http_error(response, "ModelScope image generation")
136
137 task_id = response.json()["task_id"]
138 data = poll_json(
139 url=f"{_resolve_url(base_url)}/v1/tasks/{task_id}",
140 headers={**common_headers, "X-ModelScope-Task-Type": "image_generation"},
141 status_label="task_status",
142 ready_values=["SUCCEED"],
143 failed_values=["FAILED"],
144 )
145 elapsed = time.time() - start
146 print(f"\n [DONE] Response received ({elapsed:.1f}s)")
147 path = resolve_output_path(prompt, output_dir, filename, ".png")
148 return download_image(data["output_images"][0], path)
149
150 def generate(prompt: str,
151 aspect_ratio: str = "1:1", image_size: str = "1K",
152 output_dir: str = None, filename: str = None,
153 model: str = None, max_retries: int = MAX_RETRIES) -> str:
154 """Generate an image with retries using the ModelScope backend."""
155 resolved_model = _resolve_model(model)
156 normalized_size = normalize_image_size(image_size)
157 _resolve_size(aspect_ratio, normalized_size)
158 api_key = require_api_key(
159 "MODELSCOPE_API_KEY",
160 message="No API key found. Set MODELSCOPE_API_KEY in the current environment or the project-root .env.",
161 )
162 base_url = os.environ.get("MODELSCOPE_BASE_URL") or DEFAULT_ENDPOINT
163
164 last_error = None
165 for attempt in range(max_retries + 1):
166 try:
167 return _generate_image(
168 api_key=api_key,
169 prompt=prompt,
170 aspect_ratio=aspect_ratio,
171 image_size=normalized_size,
172 output_dir=output_dir,
173 filename=filename,
174 model=resolved_model,
175 base_url=base_url,
176 )
177 except Exception as exc:
178 last_error = exc
179 if is_permanent_error(exc):
180 raise
181 if attempt >= max_retries:
182 break
183 limited = is_rate_limit_error(exc)
184 delay = retry_delay(attempt, rate_limited=limited)
185 label = "Rate limit hit" if limited else f"Error: {exc}"
186 print(f"\n [WARN] {label}. Retrying in {delay}s...")
187 time.sleep(delay)
188
189 raise RuntimeError(f"Failed after {max_retries + 1} attempts. Last error: {last_error}")
190
190 lines PYTHON