dodoyy commited on
Commit
b134fdb
ยท
verified ยท
1 Parent(s): 10fff3d

Complete the pre-Arrow Motion SVG restore

Browse files
Files changed (1) hide show
  1. quiver.py +0 -120
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