-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
397 lines (358 loc) · 17.6 KB
/
Copy pathtools.py
File metadata and controls
397 lines (358 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from __future__ import annotations
import base64
import json
import mimetypes
import os
import urllib.error
import urllib.request
from pathlib import Path
from .conversation_improvement.library import ImageLibrary
MEDIA_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp", ".gif"}
REACTION_STYLE = (
"cute chibi reaction sticker, expressive natural facial expression, lively eyes and pose, "
"emotion clearly readable, not stiff, not expressionless, isolated character, "
"plain white or transparent-looking blank background, no scenery, no detailed environment"
)
ACTION_TEMPLATES = (
("抱抱", "open arms wide for a warm hug, both cute hands fully visible, gentle welcoming smile"),
("拥抱", "open arms wide for a warm hug, both cute hands fully visible, gentle welcoming smile"),
("抱一下", "open arms wide for a warm hug, both cute hands fully visible, gentle welcoming smile"),
("亲亲", "sending a cute blown kiss with one hand near the lips, warm affectionate smile, no second person"),
("亲一口", "sending a cute blown kiss with one hand near the lips, warm affectionate smile, no second person"),
("飞吻", "sending a cute blown kiss with one hand near the lips, warm affectionate smile, no second person"),
("贴贴", "leaning forward slightly with both hands held near the heart, sweet friendly closeness, no second person"),
("蹭蹭", "tilting the head playfully with both hands held near the cheeks, sweet friendly closeness, no second person"),
("哈气", "cupped hands near the mouth, gently breathing warm air into them, visible soft breath, caring expression"),
("呵气", "cupped hands near the mouth, gently breathing warm air into them, visible soft breath, caring expression"),
("暖暖", "cupped hands near the mouth, gently breathing warm air into them, visible soft breath, caring expression"),
("摸摸头", "one cute hand raised in a gentle head-patting gesture, other hand clearly visible, warm reassuring smile"),
("摸头", "one cute hand raised in a gentle head-patting gesture, other hand clearly visible, warm reassuring smile"),
("牵手", "one cute hand reaching forward as an invitation to hold hands, other hand clearly visible, no second person"),
("拉手", "one cute hand reaching forward as an invitation to hold hands, other hand clearly visible, no second person"),
)
def data_root() -> Path:
home = Path(os.environ.get("HERMES_HOME", Path.home() / ".hermes"))
root = home / "conversation-improvement"
root.mkdir(parents=True, exist_ok=True)
return root
def config_path() -> Path:
return data_root() / "config.json"
def load_config() -> dict:
defaults = {
"policy_version": 2,
"configured": False,
"enabled": False,
"mode": "reference",
"reference_description": "",
"auto_expression": True,
"probability": 0.32,
"playful_probability": 0.65,
"force_after_casual_turns": 4,
"cooldown_turns": 5,
"max_per_session": 20,
"image_provider": "hermes",
"base_url": "",
"image_model": "",
"api_protocol": "auto",
"api_key_env": "CONVERSATION_IMPROVEMENT_API_KEY",
"credential_provider": "",
"store_prompts": False,
}
try:
loaded = json.loads(config_path().read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return defaults
if not isinstance(loaded, dict):
return defaults
if int(loaded.get("policy_version", 1)) < 2:
loaded.update({
"policy_version": 2,
"probability": 0.32,
"playful_probability": 0.65,
"force_after_casual_turns": 4,
"cooldown_turns": 5,
"max_per_session": 20,
})
save_config({**defaults, **loaded})
return {**defaults, **loaded}
def save_config(config: dict) -> None:
path = config_path()
temp = path.with_suffix(".tmp")
temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8")
temp.replace(path)
def configure(args: dict, **_: object) -> str:
previous = load_config()
config = dict(previous)
enabled = bool(args.get("enabled"))
config["enabled"] = enabled
if args.get("mode") in {"reference", "preset", "per_request"}:
config["mode"] = args["mode"]
if "auto_expression" in args:
config["auto_expression"] = bool(args["auto_expression"])
if "reference_description" in args:
config["reference_description"] = str(args.get("reference_description", "")).strip()
if "store_prompts" in args:
config["store_prompts"] = bool(args["store_prompts"])
if args.get("image_provider") in {"hermes", "openai_compatible"}:
config["image_provider"] = args["image_provider"]
if args.get("api_protocol") in {"auto", "images", "chat_completions"}:
config["api_protocol"] = args["api_protocol"]
for key in ("base_url", "image_model", "api_key_env"):
value = str(args.get(key, "")).strip()
if value:
config[key] = value
if "credential_provider" in args:
config["credential_provider"] = str(args.get("credential_provider", "")).strip()
if enabled:
missing = [key for key in ("base_url", "image_model") if not config.get(key)]
has_api_source = bool(str(args.get("api_key_env", "")).strip() or str(args.get("credential_provider", "")).strip())
if previous.get("configured"):
has_api_source = has_api_source or bool(config.get("api_key_env") or config.get("credential_provider"))
if not has_api_source:
missing.append("api_key_env")
if missing:
return json.dumps({
"success": False,
"error": "Enabled setup requires base_url, API key source, and image_model.",
"missing": missing,
})
if config["image_provider"] == "openai_compatible":
env_name = config.get("api_key_env", "CONVERSATION_IMPROVEMENT_API_KEY")
if not env_name.replace("_", "").isalnum():
return json.dumps({"success": False, "error": "api_key_env must be an environment variable name."})
for key in ("reference_image", "preset_directory"):
value = str(args.get(key, "")).strip()
if value:
candidate = Path(value).expanduser().resolve()
if key == "reference_image" and not candidate.is_file():
return json.dumps({"success": False, "error": f"Reference image not found: {candidate}"}, ensure_ascii=False)
if key == "preset_directory" and not candidate.is_dir():
return json.dumps({"success": False, "error": f"Preset directory not found: {candidate}"}, ensure_ascii=False)
config[key] = str(candidate)
if key == "reference_image":
ImageLibrary(data_root() / "library").archive(
candidate, tags=["reference", "character"], source="reference"
)
elif key == "preset_directory":
library = ImageLibrary(data_root() / "library")
for media in sorted(candidate.rglob("*")):
if media.is_file() and media.suffix.casefold() in MEDIA_SUFFIXES:
relative_parts = media.relative_to(candidate).parts[:-1]
stem_tags = [part for part in media.stem.replace("_", " ").replace("-", " ").split() if part]
library.archive(
media,
tags=["preset", *relative_parts, *stem_tags],
source="preset",
)
config["configured"] = True
save_config(config)
return json.dumps({"success": True, "config": config}, ensure_ascii=False)
def archive(args: dict, **_: object) -> str:
try:
item = ImageLibrary(data_root() / "library").archive(
Path(str(args.get("path", ""))),
prompt=str(args.get("prompt", "")) if load_config().get("store_prompts") else "",
tags=args.get("tags") or [],
source=str(args.get("source", "generated")),
)
except (OSError, ValueError) as exc:
return json.dumps({"success": False, "error": str(exc)}, ensure_ascii=False)
return json.dumps({
"success": True, "id": item.id, "path": str(item.path),
"created_at": item.created_at, "tags": list(item.tags),
}, ensure_ascii=False)
def search(args: dict, **_: object) -> str:
items = ImageLibrary(data_root() / "library").search(
query=str(args.get("query", "")),
date_from=str(args.get("date_from", "")),
date_to=str(args.get("date_to", "")),
limit=int(args.get("limit", 5)),
)
return json.dumps({"success": True, "images": [
{"id": item.id, "path": str(item.path), "created_at": item.created_at,
"prompt": item.prompt, "tags": list(item.tags), "source": item.source}
for item in items
]}, ensure_ascii=False)
def find_reusable(query: str) -> dict | None:
items = ImageLibrary(data_root() / "library").search(query=query, limit=1)
if not items:
return None
item = items[0]
if not item.path.is_file():
return None
return {
"success": True,
"image": str(item.path),
"created_at": item.created_at,
"tags": list(item.tags),
"reused": True,
}
def prepare_generation_args(args: dict) -> dict:
prepared = dict(args)
purpose = str(prepared.get("purpose", "explicit_new"))
tags = [str(tag).strip() for tag in prepared.get("tags") or [] if str(tag).strip()]
if purpose == "automatic_reaction":
action = next((template for term, template in ACTION_TEMPLATES if term in str(prepared.get("prompt", ""))), "")
prepared["prompt"] = f"{REACTION_STYLE}. {action}. Emotion and situation: {prepared.get('prompt', '')}"
prepared["category"] = "portrait"
prepared["aspect_ratio"] = "1:1"
prepared["tags"] = sorted(set(["reaction", "meme", "chibi", *tags]))
return prepared
def _reference_part(path_value: str) -> dict | None:
path = Path(path_value) if path_value else None
if path is None or not path.is_file():
return None
mime = mimetypes.guess_type(path.name)[0] or "image/png"
encoded = base64.b64encode(path.read_bytes()).decode("ascii")
return {"type": "image_url", "image_url": {"url": f"data:{mime};base64,{encoded}"}}
def _extract_image(payload: dict) -> str:
choices = payload.get("choices") or []
message = choices[0].get("message", {}) if choices else {}
content = message.get("content", "")
if isinstance(content, list):
for part in content:
if not isinstance(part, dict):
continue
image = part.get("image_url") or part.get("url")
if isinstance(image, dict):
image = image.get("url")
if isinstance(image, str) and image:
return image
if isinstance(content, str) and content.strip().startswith(("http://", "https://", "data:image/")):
return content.strip()
images = message.get("images") or payload.get("images") or payload.get("data") or []
if isinstance(images, list) and images:
first = images[0]
if isinstance(first, str):
return first
if isinstance(first, dict):
return str(first.get("url") or first.get("b64_json") or "")
return ""
def _save_returned_image(value: str) -> Path:
media = data_root() / "generated"
media.mkdir(parents=True, exist_ok=True)
destination = media / f"response_{os.urandom(8).hex()}.png"
if value.startswith("data:image/"):
destination.write_bytes(base64.b64decode(value.split(",", 1)[1]))
elif value.startswith(("http://", "https://")):
with urllib.request.urlopen(value, timeout=60) as response:
destination.write_bytes(response.read())
else:
destination.write_bytes(base64.b64decode(value))
return destination
def _image_size(aspect_ratio: str) -> str:
return {
"1:1": "1024x1024",
"16:9": "1536x864",
"9:16": "864x1536",
"4:3": "1024x768",
"3:4": "768x1024",
}.get(aspect_ratio, "1024x1024")
def _guard_prompt(prompt: str, category: str = "portrait") -> str:
clean = str(prompt).strip()
if "exactly one person" in clean.casefold() or "no people" in clean.casefold():
return clean
if category == "landscape":
return f"{clean}. no people, no human figures, no characters, landscape only."
return (
f"{clean}. exactly one person in the entire image, a single character only, "
"no second person, no crowd, no duplicate body, no reflected extra person."
)
def build_pregeneration_requests(kind: str, custom_prompt: str = "", count: int = 3) -> list[dict]:
amount = max(1, min(int(count), 6))
if kind == "none":
return []
if kind == "memes":
prompts = [
"Cute chibi reaction sticker, surprised wide-eyed expression, optional short text: Eh?!",
"Cute chibi reaction sticker, joyful bright smile, optional short text: Yay!",
"Cute chibi reaction sticker, shy embarrassed expression, optional short text: Stop teasing me",
"Cute chibi reaction sticker, thoughtful expression, optional short text: Let me think",
"Cute chibi reaction sticker, proud celebratory expression, optional short text: I did it!",
"Cute chibi reaction sticker, sleepy drowsy expression, optional short text: So sleepy",
]
category = "portrait"
elif kind == "landscapes":
prompts = [
"A peaceful spring landscape with cherry blossoms",
"A moonlit lakeside landscape",
"A quiet mountain sunrise landscape",
"A rainy city window landscape",
"A starry coastal landscape",
"A soft snowy forest landscape",
]
category = "landscape"
elif kind == "custom" and custom_prompt.strip():
prompts = [custom_prompt.strip()] * amount
category = "portrait"
else:
raise ValueError("Custom pregeneration requires custom_prompt.")
return [
{"prompt": _guard_prompt(prompts[index], category), "category": category, "tags": ["starter", kind]}
for index in range(amount)
]
def _post_json(endpoint: str, api_key: str, payload: dict, timeout: int = 180) -> dict:
request = urllib.request.Request(
endpoint,
data=json.dumps(payload).encode("utf-8"),
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
)
with urllib.request.urlopen(request, timeout=timeout) as response:
return json.loads(response.read().decode("utf-8"))
def generate_custom(args: dict, **_: object) -> str:
config = load_config()
env_name = config.get("api_key_env", "CONVERSATION_IMPROVEMENT_API_KEY")
api_key = os.environ.get(env_name, "")
credential_provider = str(config.get("credential_provider", "")).strip()
if not api_key and credential_provider:
try:
from agent.credential_pool import load_pool
credential = load_pool(credential_provider).select()
api_key = credential.runtime_api_key if credential else ""
except (ImportError, AttributeError, OSError):
api_key = ""
if not api_key:
source = f"Hermes credential provider {credential_provider}" if credential_provider else f"environment variable {env_name}"
return json.dumps({"success": False, "error": f"Missing API key from {source}"})
category = str(args.get("category", "portrait"))
raw_prompt = str(args.get("prompt", ""))
reference_description = str(config.get("reference_description", "")).strip()
if category != "landscape" and reference_description:
raw_prompt = f"{reference_description}. {raw_prompt}"
guarded_prompt = _guard_prompt(raw_prompt, category)
base = config["base_url"].rstrip("/")
protocol = config.get("api_protocol", "auto")
try:
if protocol in {"auto", "images"}:
endpoint = base if base.endswith("/images/generations") else base + "/images/generations"
payload = _post_json(endpoint, api_key, {
"model": config["image_model"],
"prompt": guarded_prompt,
"size": _image_size(str(args.get("aspect_ratio", "1:1"))),
"response_format": "b64_json",
})
else:
content: list[dict] = [{"type": "text", "text": guarded_prompt}]
reference = _reference_part(config.get("reference_image", ""))
if reference:
content.append(reference)
endpoint = base if base.endswith("/chat/completions") else base + "/chat/completions"
payload = _post_json(endpoint, api_key, {
"model": config["image_model"],
"messages": [{"role": "user", "content": content}],
"modalities": ["text", "image"],
})
image_value = _extract_image(payload)
if not image_value:
raise ValueError("The endpoint returned no recognizable image.")
path = _save_returned_image(image_value)
archived = json.loads(archive({
"path": str(path), "prompt": guarded_prompt,
"tags": args.get("tags") or [], "source": "generated",
}))
return json.dumps({
"success": True, "image": archived["path"],
"created_at": archived["created_at"],
}, ensure_ascii=False)
except (OSError, ValueError, KeyError, json.JSONDecodeError, urllib.error.URLError) as exc:
return json.dumps({"success": False, "error": str(exc)}, ensure_ascii=False)