File size: 1,859 Bytes
c6be992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from .openrouter_client import openrouter_chat


REWRITE_SYSTEM = """Rewrite the input into a concise, comma-separated list of short phrases

that resemble image tags.



Use short, literal phrases that reflect how visual concepts are commonly

written in image tag vocabularies.



Multi-word phrases are appropriate when they represent one coherent

visual idea.



Examples of tag-shaped phrases:

- wolf, angry

- blue jacket, striped tail

- long hair, raised ears

- holding object, hand on shoulder

- looking at viewer, looking down

- simple background, outdoor scene

- wooden table, plant

- running, sleeping

- smiling, angry expression

- bedroom, forest

- sonic the hedgehog, princess peach



Do not invent details or guess identities.

Do not infer demographic attributes (e.g., gender/age) unless explicitly stated.



Output ONLY the rewritten list.

"""


def llm_rewrite_prompt(prompt_in: str, log) -> str:
    messages = [
        {"role": "system", "content": REWRITE_SYSTEM},
        {"role": "user", "content": prompt_in},
    ]

    raw, _parsed_unused, err = openrouter_chat(
        messages,
        response_format=None,
        temperature=0.0,
        max_tokens=256,
    )

    if err:
        log(f"LLM rewrite: fallback (error: {err})")
        # NEW: if we got a refusal-like completion, log the refusal text for debugging
        if raw and err.lower().startswith("refusal-like"):
            log(f"LLM rewrite refusal text: {raw.strip()[:300]}")
        return ""

    out = (raw or "").strip()
    if not out:
        log("LLM rewrite: fallback (empty response)")
        return ""

    out = " ".join(out.split())
    if len(out) > 800:
        out = out[:800].rstrip()

    log("LLM rewrite: ok")
    return out


if __name__ == "__main__":
    print("rewrite.py imports ok")