Spaces:
Running on Zero
Running on Zero
Complete the pre-Arrow Motion SVG restore
Browse files
quiver.py
DELETED
|
@@ -1,120 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import re
|
| 3 |
-
from base64 import b64encode
|
| 4 |
-
from urllib.error import HTTPError, URLError
|
| 5 |
-
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
| 6 |
-
|
| 7 |
-
from validator import SVGError, parse_svg, svg_text
|
| 8 |
-
|
| 9 |
-
ARROW_MODEL = "arrow-2"
|
| 10 |
-
API_BASE = "https://api.quiver.ai/v1/svgs"
|
| 11 |
-
MAX_RESPONSE_BYTES = 1_000_000
|
| 12 |
-
|
| 13 |
-
SVG_FORMAT = """Use editable vector paths and shapes in a single standalone SVG with a viewBox.
|
| 14 |
-
Use geometry and SVG presentation attributes directly. Do not use CSS, style or class
|
| 15 |
-
attributes, references through href, symbol/use elements, images, scripts, filters, or masks.
|
| 16 |
-
Local gradients and clip paths are fine. Include margin for the requested movement."""
|
| 17 |
-
|
| 18 |
-
DRAW_INSTRUCTIONS = f"""{SVG_FORMAT}
|
| 19 |
-
Draw the requested subject and colors in the initial pose. Use separate groups for parts
|
| 20 |
-
that will move independently. This is the static artwork for an animation, so leave the
|
| 21 |
-
animation itself to the next step."""
|
| 22 |
-
|
| 23 |
-
ANIMATION_INSTRUCTIONS = f"""{SVG_FORMAT}
|
| 24 |
-
Preserve the drawing's subject, shapes, colors and layout. Follow the requested motion,
|
| 25 |
-
timing and loop behavior. Animate the actual vector elements with native SMIL
|
| 26 |
-
(animate, animateTransform, or animateMotion). Put each animation inside its target.
|
| 27 |
-
Use seconds or milliseconds with units for timing. For a rotation, use the target's
|
| 28 |
-
center as the pivot. Nest groups when static and animated transforms must both apply.
|
| 29 |
-
Use a smooth loop when the prompt does not request a one-time animation."""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
class QuiverError(RuntimeError):
|
| 33 |
-
"""An Arrow SVG request could not be completed."""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
class _NoRedirect(HTTPRedirectHandler):
|
| 37 |
-
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
| 38 |
-
# Keep the user's credential and content on the first-party API origin.
|
| 39 |
-
return None
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
_HTTP = build_opener(_NoRedirect)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def _request_svg(operation: str, key: str, body: dict) -> str:
|
| 46 |
-
request = Request(
|
| 47 |
-
f"{API_BASE}/{operation}",
|
| 48 |
-
data=json.dumps({
|
| 49 |
-
"model": ARROW_MODEL,
|
| 50 |
-
"stream": False,
|
| 51 |
-
"max_output_tokens": 8192,
|
| 52 |
-
**body,
|
| 53 |
-
}).encode("utf-8"),
|
| 54 |
-
headers={
|
| 55 |
-
"Authorization": f"Bearer {key}",
|
| 56 |
-
"Accept": "application/json",
|
| 57 |
-
"Content-Type": "application/json",
|
| 58 |
-
},
|
| 59 |
-
method="POST",
|
| 60 |
-
)
|
| 61 |
-
try:
|
| 62 |
-
with _HTTP.open(request, timeout=95) as response:
|
| 63 |
-
if response.headers.get("x-quiverai-environment", "").lower() == "test":
|
| 64 |
-
raise QuiverError("์ค์ ์ ๋๋ฉ์ด์
์ ๋ง๋ค๋ ค๋ฉด Quiver Production API ํค๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
|
| 65 |
-
content = response.read(MAX_RESPONSE_BYTES + 1)
|
| 66 |
-
except HTTPError as exc:
|
| 67 |
-
status = exc.code
|
| 68 |
-
exc.close()
|
| 69 |
-
messages = {
|
| 70 |
-
400: "Quiver๊ฐ SVG ์์ฒญ์ ์ฒ๋ฆฌํ์ง ๋ชปํ์ด์. ์ค๋ช
์ด๋ SVG๋ฅผ ํ์ธํด ์ฃผ์ธ์.",
|
| 71 |
-
401: "Quiver API ํค๊ฐ ์ฌ๋ฐ๋ฅด์ง ์๊ฑฐ๋ ๋ง๋ฃ๋์ด์.",
|
| 72 |
-
402: "Quiver ๊ณ์ ์ API ์์ก์ ํ์ธํด ์ฃผ์ธ์.",
|
| 73 |
-
403: "Quiver๊ฐ ์์ฒญ์ ๊ฑฐ๋ถํ์ด์. API ํค ๊ถํ๊ณผ ์
๋ ฅ์ ํ์ธํด ์ฃผ์ธ์.",
|
| 74 |
-
404: "์ด API ํค์์ Arrow 2 ๋๋ ์์ฒญํ SVG ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์์ด์.",
|
| 75 |
-
413: "Quiver์ ์ ๋ฌํ SVG๊ฐ ๋๋ฌด ์ปค์.",
|
| 76 |
-
429: "Quiver API ์ฌ์ฉ ํ๋์ ๋๋ฌํ์ด์. ์ ์ ๋ค ๋ค์ ์๋ํด ์ฃผ์ธ์.",
|
| 77 |
-
}
|
| 78 |
-
raise QuiverError(messages.get(status, "Quiver API ์๋ต์ ๋ฌธ์ ๊ฐ ์๊ฒผ์ด์. ์ ์ ๋ค ๋ค์ ์๋ํด ์ฃผ์ธ์.")) from None
|
| 79 |
-
except TimeoutError:
|
| 80 |
-
raise QuiverError("Quiver ์๋ต์ ๊ธฐ๋ค๋ฆฌ๋ค ์๊ฐ์ด ์ด๊ณผ๋์ด์.") from None
|
| 81 |
-
except (URLError, OSError):
|
| 82 |
-
raise QuiverError("Quiver API์ ์ฐ๊ฒฐํ์ง ๋ชปํ์ด์. ์ ์ ๋ค ๋ค์ ์๋ํด ์ฃผ์ธ์.") from None
|
| 83 |
-
|
| 84 |
-
if len(content) > MAX_RESPONSE_BYTES:
|
| 85 |
-
raise QuiverError("Quiver์ SVG ์๋ต์ด ๋๋ฌด ์ปค์.")
|
| 86 |
-
try:
|
| 87 |
-
svg = json.loads(content)["data"][0]["svg"]
|
| 88 |
-
if not isinstance(svg, str) or not svg.strip():
|
| 89 |
-
raise ValueError("Empty SVG")
|
| 90 |
-
except (ValueError, KeyError, IndexError, TypeError):
|
| 91 |
-
raise QuiverError("Quiver๊ฐ SVG ๋ฌธ์๋ฅผ ๋ฐํํ์ง ์์์ด์.") from None
|
| 92 |
-
return svg
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
def generate_animation(prompt: str, api_key: str, source: str | None = None) -> str:
|
| 96 |
-
key = api_key.strip()
|
| 97 |
-
if not key:
|
| 98 |
-
raise QuiverError("Quiver Arrow 2๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Quiver API ํค๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
|
| 99 |
-
if not re.fullmatch(r"[!-~]{1,512}", key):
|
| 100 |
-
raise QuiverError("Quiver API ํค ํ์์ ํ์ธํด ์ฃผ์ธ์.")
|
| 101 |
-
|
| 102 |
-
if source is None:
|
| 103 |
-
source = _request_svg("generations", key, {
|
| 104 |
-
"prompt": prompt,
|
| 105 |
-
"instructions": DRAW_INSTRUCTIONS,
|
| 106 |
-
"n": 1,
|
| 107 |
-
})
|
| 108 |
-
try:
|
| 109 |
-
source = svg_text(parse_svg(source))
|
| 110 |
-
except SVGError:
|
| 111 |
-
raise QuiverError("Quiver๊ฐ ํธ์ง ๊ฐ๋ฅํ SVG ์๋ณธ์ ๋ง๋ค์ง ๋ชปํ์ด์. ์ค๋ช
์ ๊ฐ๋จํ ๋ฐ๊ฟ ๋ค์ ๏ฟฝ๏ฟฝ๋ํด ์ฃผ์ธ์.") from None
|
| 112 |
-
|
| 113 |
-
svg = _request_svg("animations", key, {
|
| 114 |
-
"prompt": f"{prompt}\n\n{ANIMATION_INSTRUCTIONS}",
|
| 115 |
-
"svg_source": {"base64": b64encode(source.encode("utf-8")).decode("ascii")},
|
| 116 |
-
})
|
| 117 |
-
try:
|
| 118 |
-
return svg_text(parse_svg(svg, require_animation=True))
|
| 119 |
-
except SVGError:
|
| 120 |
-
raise QuiverError("Quiver ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ ๊ฐ๋ฅํ SVG๋ก ๊ฒ์ฆํ์ง ๋ชปํ์ด์. ์์ง์์ ๊ฐ๋จํ ๋ฐ๊ฟ ๋ค์ ์๋ํด ์ฃผ์ธ์.") from None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|