whosouravsharma commited on
Commit
13b26d8
·
verified ·
1 Parent(s): f7f62d8

Wait out a sleeping backend with a status message; trace visitor IP and location to LangSmith

Browse files
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lightweight: no CUDA base and no ML dependencies. All generation happens in
2
+ # the inference Space; this container just serves the interface.
3
+ FROM python:3.11-slim
4
+
5
+ ENV PYTHONUNBUFFERED=1 \
6
+ PIP_NO_CACHE_DIR=1
7
+
8
+ # Spaces run as uid 1000 and only $HOME is writable.
9
+ RUN useradd -m -u 1000 app
10
+ USER app
11
+ ENV HOME=/home/app \
12
+ HF_HOME=/home/app/.cache/huggingface \
13
+ PATH=/home/app/.local/bin:$PATH
14
+
15
+ WORKDIR /home/app
16
+
17
+ COPY --chown=app requirements.txt .
18
+ RUN pip install --user --no-cache-dir -r requirements.txt
19
+
20
+ COPY --chown=app app.py ./
21
+
22
+ EXPOSE 7860
23
+ ENV PORT=7860
24
+ CMD ["python", "app.py"]
__pycache__/app.cpython-313.pyc ADDED
Binary file (8.27 kB). View file
 
__pycache__/app.cpython-314.pyc ADDED
Binary file (11.7 kB). View file
 
__pycache__/geo.cpython-313.pyc ADDED
Binary file (5.46 kB). View file
 
__pycache__/geo.cpython-314.pyc ADDED
Binary file (6.33 kB). View file
 
__pycache__/tracing.cpython-314.pyc ADDED
Binary file (11.8 kB). View file
 
app.py CHANGED
@@ -9,10 +9,15 @@ backend needs a GPU.
9
 
10
  import json
11
  import os
 
12
 
13
  import gradio as gr
14
  from gradio_client import Client, handle_file # noqa: F401
15
- from huggingface_hub import hf_hub_download
 
 
 
 
16
 
17
  # ZeroGPU refuses to start a Space in which no @spaces.GPU function is
18
  # defined. This Space runs no model — every generation happens in the
@@ -39,26 +44,91 @@ MODEL_REPO = "whosouravsharma/diffusiondb-sd15-lora"
39
  _client = None
40
 
41
 
42
- def get_client() -> Client:
43
- """Connected on first request, so a sleeping backend does not break boot.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- The inference Space is private, so this needs a token with access to it.
46
- Set HF_TOKEN as a secret on this Space (Settings -> Variables and secrets);
47
- without it every call comes back 401.
 
48
  """
49
  global _client
50
- if _client is None:
51
- token = os.environ.get("HF_TOKEN")
52
- if not token:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  raise gr.Error(
54
- "HF_TOKEN is not set on this Space. The inference backend is "
55
- "private and cannot be reached without it."
56
  )
57
- # Passed positionally on purpose: this argument is named hf_token in
58
- # gradio_client 1.x and token in 2.x, and the Space installs whichever
59
- # version sdk_version pulls in. It is the second parameter in both.
60
- _client = Client(BACKEND, token)
61
- return _client
62
 
63
 
64
  def load_examples() -> list[str]:
@@ -79,20 +149,56 @@ def load_examples() -> list[str]:
79
  ]
80
 
81
 
82
- def generate(prompt, negative, steps, guidance, lora_scale, seed):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  if not prompt or not prompt.strip():
84
  raise gr.Error("Enter a prompt.")
 
 
 
 
 
 
 
 
85
  try:
86
- image, used = get_client().predict(
87
- prompt, negative, int(steps), float(guidance),
88
- float(lora_scale), int(seed), api_name="/generate",
 
 
 
 
 
 
89
  )
90
  except Exception as error:
 
91
  raise gr.Error(
92
- f"Backend unavailable ({error}). The inference Space may be "
93
- f"asleep — try again in a minute."
94
  )
95
- return image, used
 
96
 
97
 
98
  with gr.Blocks(title="DiffusionDB SD 1.5 LoRA") as demo:
@@ -140,6 +246,7 @@ with gr.Blocks(title="DiffusionDB SD 1.5 LoRA") as demo:
140
  )
141
 
142
  with gr.Column(scale=4):
 
143
  output = gr.Image(label="Output", height=512)
144
  used_seed = gr.Number(label="Seed used", interactive=False)
145
 
@@ -153,7 +260,7 @@ with gr.Blocks(title="DiffusionDB SD 1.5 LoRA") as demo:
153
  trigger(
154
  generate,
155
  inputs=[prompt, negative, steps, guidance, lora_scale, seed],
156
- outputs=[output, used_seed],
157
  )
158
 
159
  if __name__ == "__main__":
 
9
 
10
  import json
11
  import os
12
+ import time
13
 
14
  import gradio as gr
15
  from gradio_client import Client, handle_file # noqa: F401
16
+ from huggingface_hub import HfApi, hf_hub_download
17
+
18
+ from tracing import configure_logging, logger, traced_predict
19
+
20
+ configure_logging()
21
 
22
  # ZeroGPU refuses to start a Space in which no @spaces.GPU function is
23
  # defined. This Space runs no model — every generation happens in the
 
44
  _client = None
45
 
46
 
47
+ # The backend sleeps after five idle minutes. Waking it means scheduling a
48
+ # T4, starting the container, importing torch and loading ~2 GB of weights.
49
+ # That is a wait, not a failure, so the UI waits it out instead of erroring.
50
+ WAKE_TIMEOUT = 600 # give up after ten minutes
51
+ POLL_INTERVAL = 5 # seconds between connection attempts
52
+
53
+ # Stages a Space cannot leave on its own. Waiting on these would just burn
54
+ # the full timeout and then report something unhelpful.
55
+ STUCK_STAGES = {"PAUSED", "RUNTIME_ERROR", "BUILD_ERROR", "CONFIG_ERROR"}
56
+
57
+
58
+ def _require_token() -> str:
59
+ token = os.environ.get("HF_TOKEN")
60
+ if not token:
61
+ raise gr.Error(
62
+ "HF_TOKEN is not set on this Space. The inference backend is "
63
+ "private and cannot be reached without it."
64
+ )
65
+ return token
66
+
67
+
68
+ def _backend_stage(token: str):
69
+ """Current runtime stage of the backend Space, or None if unknown."""
70
+ try:
71
+ return HfApi(token=token).get_space_runtime(BACKEND).stage
72
+ except Exception:
73
+ return None
74
+
75
+
76
+ def _waking_message(stage, elapsed: int) -> str:
77
+ detail = {
78
+ "SLEEPING": "waking it up",
79
+ "BUILDING": "it is rebuilding",
80
+ "APP_STARTING": "loading the model",
81
+ "RUNNING": "loading the model",
82
+ }.get(stage, "waking it up")
83
+ return (
84
+ f"⏳ **Starting the server…** the GPU backend sleeps when idle, so "
85
+ f"{detail}. The first image takes about 40 seconds; later ones take "
86
+ f"a few.\n\n`{elapsed}s elapsed`"
87
+ )
88
+
89
+
90
+ def connect_backend():
91
+ """Generator: yields status tuples while waiting, returns a live Client.
92
 
93
+ A sleeping Space wakes on its first HTTP request, so simply attempting
94
+ the connection starts it; the loop then keeps retrying until the app
95
+ inside is actually serving. Consumed with `yield from`, so every status
96
+ update reaches the browser as it happens.
97
  """
98
  global _client
99
+ if _client is not None:
100
+ return _client
101
+
102
+ token = _require_token()
103
+ started = time.monotonic()
104
+
105
+ while True:
106
+ elapsed = int(time.monotonic() - started)
107
+ try:
108
+ # Passed positionally on purpose: this argument is named hf_token
109
+ # in gradio_client 1.x and token in 2.x, and the Space installs
110
+ # whichever version sdk_version pulls in. It is the second
111
+ # parameter in both. This call also wakes a sleeping Space.
112
+ _client = Client(BACKEND, token)
113
+ return _client
114
+ except Exception as error:
115
+ last_error = error
116
+
117
+ stage = _backend_stage(token)
118
+ if stage in STUCK_STAGES:
119
+ raise gr.Error(
120
+ f"The inference Space is {stage.lower().replace('_', ' ')} and "
121
+ f"cannot start itself. It needs to be restarted from its "
122
+ f"Settings page."
123
+ )
124
+ if time.monotonic() - started > WAKE_TIMEOUT:
125
  raise gr.Error(
126
+ f"The inference Space did not come up within "
127
+ f"{WAKE_TIMEOUT // 60} minutes ({last_error})."
128
  )
129
+
130
+ yield _waking_message(stage, elapsed), None, None
131
+ time.sleep(POLL_INTERVAL)
 
 
132
 
133
 
134
  def load_examples() -> list[str]:
 
149
  ]
150
 
151
 
152
+ # The backend sleeps after five idle minutes. Waking it means scheduling a
153
+ # T4, starting the container, importing torch, and loading ~2 GB of weights
154
+ # before the first step runs -- around 40 seconds during which a bare spinner
155
+ # is indistinguishable from a hang. These say what is actually happening.
156
+ STATUS_IDLE = ""
157
+ STATUS_STARTING = "⏳ **Starting the server…** contacting the GPU backend."
158
+ STATUS_GENERATING = "🎨 **Generating…**"
159
+
160
+
161
+ def generate(prompt, negative, steps, guidance, lora_scale, seed,
162
+ request: gr.Request = None):
163
+ """Streams status while the backend wakes, then returns the image.
164
+
165
+ A generator rather than a plain function so the first yield paints
166
+ immediately: the user sees that the server is starting instead of
167
+ watching a spinner for forty seconds. A sleeping backend is a wait, not
168
+ an error -- the only failures raised here are ones waiting cannot fix.
169
+
170
+ `request` is filled in by Gradio from the type annotation; it is not a UI
171
+ input and never appears in `inputs`.
172
+ """
173
  if not prompt or not prompt.strip():
174
  raise gr.Error("Enter a prompt.")
175
+
176
+ yield STATUS_STARTING, None, None
177
+
178
+ # Waits out a cold backend, yielding progress the whole time.
179
+ client = yield from connect_backend()
180
+
181
+ yield STATUS_GENERATING, None, None
182
+
183
  try:
184
+ image, used = traced_predict(
185
+ client,
186
+ prompt=prompt,
187
+ negative=negative,
188
+ steps=int(steps),
189
+ guidance=float(guidance),
190
+ lora_scale=float(lora_scale),
191
+ seed=int(seed),
192
+ request=request,
193
  )
194
  except Exception as error:
195
+ yield STATUS_IDLE, None, None
196
  raise gr.Error(
197
+ f"Generation failed ({error}). The inference Space may have gone "
198
+ f"back to sleep mid-request — try again."
199
  )
200
+
201
+ yield STATUS_IDLE, image, used
202
 
203
 
204
  with gr.Blocks(title="DiffusionDB SD 1.5 LoRA") as demo:
 
246
  )
247
 
248
  with gr.Column(scale=4):
249
+ status = gr.Markdown(STATUS_IDLE)
250
  output = gr.Image(label="Output", height=512)
251
  used_seed = gr.Number(label="Seed used", interactive=False)
252
 
 
260
  trigger(
261
  generate,
262
  inputs=[prompt, negative, steps, guidance, lora_scale, seed],
263
+ outputs=[status, output, used_seed],
264
  )
265
 
266
  if __name__ == "__main__":
geo.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Coarse, server-side geolocation of a visitor's IP, for analytics.
2
+
3
+ Nothing here touches the browser: no navigator.geolocation, no permission
4
+ prompt, no consent dialog to dismiss. The caller hands over the IP it
5
+ already has from the request and gets back city-level detail or nothing.
6
+
7
+ `lookup` is deliberately total -- it returns {} for anything it cannot
8
+ answer rather than raising, because a failed analytics lookup must never
9
+ break an image generation.
10
+ """
11
+
12
+ import ipaddress
13
+ import json
14
+ import os
15
+ import ssl
16
+ import urllib.error
17
+ import urllib.request
18
+
19
+ # ipwho.is: free, no API key, HTTPS, ~10k lookups/month, and it takes the
20
+ # target IP in the path so no query string or signup is involved. Picked
21
+ # over ipapi.co (1k/day and already returning 429 unauthenticated), ip-api
22
+ # .com (HTTPS is paid-only) and freeipapi.com (redirects away from its
23
+ # documented endpoint). Failures are signalled in-band as {"success": false}
24
+ # with HTTP 200, so the status code alone is not enough to trust a response.
25
+ API_URL = os.environ.get("GEO_API_URL", "https://ipwho.is").rstrip("/")
26
+
27
+ # Runs inside a user-facing request, so the budget is "cheap enough to be
28
+ # invisible". Two seconds is already generous for a single GET.
29
+ TIMEOUT = float(os.environ.get("GEO_TIMEOUT", "2"))
30
+
31
+ ENABLED = os.environ.get("GEO_LOOKUP", "on").strip().lower() != "off"
32
+
33
+ # Visitors arrive in bursts from the same address, and geolocation of an IP
34
+ # does not meaningfully change during one container's lifetime. Only hits
35
+ # are cached: a timeout or a rate-limit is transient and caching {} would
36
+ # blind the Space for as long as the process lives.
37
+ _cache: dict[str, dict] = {}
38
+ _CACHE_MAX = 4096
39
+
40
+ _HEADERS = {"User-Agent": "diffusiondb-sd15-lora-space/1.0"}
41
+
42
+ # The Space image trusts the system CA store, but a python.org interpreter on
43
+ # macOS ships none and every HTTPS call there fails verification. certifi is
44
+ # already installed as a transitive dependency of huggingface_hub, so prefer
45
+ # its bundle when it is importable; never fall back to an unverified context.
46
+ try:
47
+ import certifi
48
+
49
+ _SSL = ssl.create_default_context(cafile=certifi.where())
50
+ except Exception:
51
+ _SSL = ssl.create_default_context()
52
+
53
+
54
+ def _is_public(ip: str) -> bool:
55
+ """True only for addresses worth spending a network call on."""
56
+ try:
57
+ address = ipaddress.ip_address(ip)
58
+ except ValueError: # not an IP at all, or carries a %scope suffix
59
+ return False
60
+ return not (
61
+ address.is_private
62
+ or address.is_loopback
63
+ or address.is_link_local
64
+ or address.is_reserved
65
+ or address.is_multicast
66
+ or address.is_unspecified
67
+ )
68
+
69
+
70
+ def _fetch(ip: str) -> dict:
71
+ request = urllib.request.Request(f"{API_URL}/{ip}", headers=_HEADERS)
72
+ with urllib.request.urlopen(
73
+ request, timeout=TIMEOUT, context=_SSL
74
+ ) as response:
75
+ # Cap the read: a wrong GEO_API_URL could point at anything.
76
+ return json.loads(response.read(64_000).decode("utf-8"))
77
+
78
+
79
+ def _parse(payload: dict) -> dict:
80
+ if not isinstance(payload, dict) or payload.get("success") is False:
81
+ return {}
82
+
83
+ # Nested under "timezone" as an object, unlike every other field.
84
+ timezone = payload.get("timezone")
85
+ if isinstance(timezone, dict):
86
+ timezone = timezone.get("id")
87
+
88
+ result = {
89
+ "country": payload.get("country"),
90
+ "country_code": payload.get("country_code"),
91
+ "region": payload.get("region"),
92
+ "city": payload.get("city"),
93
+ "latitude": float(payload["latitude"]),
94
+ "longitude": float(payload["longitude"]),
95
+ "timezone": timezone,
96
+ }
97
+ # An answer with no coordinates or no country is not an answer; better
98
+ # to report "unknown" than to record a row of Nones.
99
+ if result["country"] is None:
100
+ return {}
101
+ return result
102
+
103
+
104
+ def lookup(ip: str) -> dict:
105
+ """Return {} on any failure, or a dict with these exact keys:
106
+ 'country' e.g. 'United States'
107
+ 'country_code' e.g. 'US'
108
+ 'region' the state/province, e.g. 'California'
109
+ 'city' e.g. 'San Francisco'
110
+ 'latitude' float
111
+ 'longitude' float
112
+ 'timezone' e.g. 'America/Los_Angeles'
113
+ """
114
+ # isinstance, not just falsiness: "never raises" has to hold even when a
115
+ # caller passes whatever its framework handed it rather than a str.
116
+ if not ENABLED or not ip or not isinstance(ip, str):
117
+ return {}
118
+
119
+ ip = ip.strip()
120
+ if not _is_public(ip):
121
+ return {}
122
+
123
+ if ip in _cache:
124
+ return _cache[ip]
125
+
126
+ try:
127
+ result = _parse(_fetch(ip))
128
+ except Exception:
129
+ # Timeout, DNS failure, HTTP error, truncated body, non-JSON,
130
+ # missing or non-numeric coordinates -- all of it means the same
131
+ # thing to the caller, and none of it is worth a traceback.
132
+ return {}
133
+
134
+ if result:
135
+ if len(_cache) >= _CACHE_MAX:
136
+ _cache.clear() # crude, but bounded and never wrong
137
+ _cache[ip] = result
138
+ return result
139
+
140
+
141
+ if __name__ == "__main__":
142
+ for probe in ("8.8.8.8", "1.1.1.1", "2606:4700:4700::1111",
143
+ "192.168.1.1", "127.0.0.1", "", "not-an-ip"):
144
+ print(f"{probe or '(empty)':>22} {lookup(probe)}")
requirements.txt CHANGED
@@ -7,3 +7,7 @@
7
  # version here is what crashed the earlier build.
8
  huggingface_hub>=0.24
9
  spaces
 
 
 
 
 
7
  # version here is what crashed the earlier build.
8
  huggingface_hub>=0.24
9
  spaces
10
+
11
+ # Tracing is opt-in at runtime (LANGSMITH_TRACING); tracing.py no-ops when
12
+ # the var is unset, so this costs nothing when tracing is off.
13
+ langsmith>=0.11
tracing.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Optional LangSmith tracing and structured logging for the UI Space.
2
+
3
+ Mirrors the inference Space's src/tracing.py: everything here degrades to a
4
+ no-op. If `langsmith` is not installed, or LANGSMITH_TRACING is not "true",
5
+ the decorator becomes an identity function and the helpers do nothing.
6
+
7
+ export LANGSMITH_TRACING=true
8
+ export LANGSMITH_API_KEY=<key>
9
+ export LANGSMITH_PROJECT=diffusiondb-sd15-lora-inference
10
+
11
+ This is the only place that can see the actual visitor. The inference Space
12
+ is called server-to-server from here, so from its side every request appears
13
+ to come from this Space; the caller's address exists only in this process.
14
+
15
+ Visitor IP and IP-derived location are personal data in the EU/UK and several
16
+ US states. LANGSMITH_LOG_IP=off drops the raw address while keeping the
17
+ coarse city/region/country, which is usually enough for analytics.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import logging
23
+ import os
24
+ import time
25
+ from typing import Any, Callable
26
+
27
+ logger = logging.getLogger("diffusiondb.ui")
28
+
29
+ try:
30
+ import geo
31
+ except ImportError: # written separately; treat as simply unavailable
32
+ geo = None
33
+
34
+
35
+ def _env_flag(name: str, default: str = "") -> bool:
36
+ return os.environ.get(name, default).strip().lower() in {"1", "true", "yes", "on"}
37
+
38
+
39
+ TRACING_ENABLED = _env_flag("LANGSMITH_TRACING")
40
+ LOG_IP = os.environ.get("LANGSMITH_LOG_IP", "on").strip().lower() not in {
41
+ "0", "false", "no", "off",
42
+ }
43
+
44
+ _langsmith: Any = None
45
+ if TRACING_ENABLED:
46
+ try:
47
+ import langsmith as _langsmith # noqa: F401
48
+ except ImportError:
49
+ logger.warning(
50
+ "LANGSMITH_TRACING is set but the langsmith package is not "
51
+ "installed; running untraced. pip install langsmith"
52
+ )
53
+ _langsmith = None
54
+ else:
55
+ if not os.environ.get("LANGSMITH_API_KEY"):
56
+ logger.warning("LANGSMITH_TRACING is set but LANGSMITH_API_KEY is empty.")
57
+
58
+ TRACING_ACTIVE = _langsmith is not None
59
+
60
+
61
+ def traceable(**decorator_kwargs: Any) -> Callable:
62
+ """`langsmith.traceable` when tracing is on, identity decorator otherwise."""
63
+ if not TRACING_ACTIVE:
64
+ def passthrough(func: Callable) -> Callable:
65
+ return func
66
+ return passthrough
67
+ return _langsmith.traceable(**decorator_kwargs)
68
+
69
+
70
+ def add_trace_metadata(**fields: Any) -> None:
71
+ """Attach key/values to the run currently in scope. No-op when untraced."""
72
+ if not TRACING_ACTIVE or not fields:
73
+ return
74
+ try:
75
+ run = _langsmith.get_current_run_tree()
76
+ if run is not None:
77
+ run.extra.setdefault("metadata", {}).update(fields)
78
+ except Exception: # never let telemetry break a generation
79
+ logger.debug("could not attach trace metadata", exc_info=True)
80
+
81
+
82
+ def _header(request: Any, name: str) -> str:
83
+ """Case-insensitive header read that tolerates any request shape."""
84
+ try:
85
+ headers = getattr(request, "headers", None) or {}
86
+ getter = getattr(headers, "get", None)
87
+ if getter is not None:
88
+ value = getter(name) or getter(name.title())
89
+ if value:
90
+ return str(value)
91
+ lowered = {str(k).lower(): v for k, v in dict(headers).items()}
92
+ return str(lowered.get(name.lower(), "") or "")
93
+ except Exception:
94
+ return ""
95
+
96
+
97
+ def caller_ip(request: Any) -> str:
98
+ """The visitor's address, not the proxy's.
99
+
100
+ Spaces sit behind a reverse proxy, so request.client.host is always the
101
+ proxy. The original address is the first entry of x-forwarded-for; the
102
+ rest of that list is the proxy chain.
103
+ """
104
+ forwarded = _header(request, "x-forwarded-for")
105
+ if forwarded:
106
+ return forwarded.split(",")[0].strip()
107
+ real = _header(request, "x-real-ip")
108
+ if real:
109
+ return real.strip()
110
+ try:
111
+ return str(getattr(getattr(request, "client", None), "host", "") or "")
112
+ except Exception:
113
+ return ""
114
+
115
+
116
+ def describe_caller(request: Any) -> dict:
117
+ """Flat dict of what is known about the visitor. Never raises."""
118
+ if request is None:
119
+ return {}
120
+ try:
121
+ info: dict[str, Any] = {}
122
+ ip = caller_ip(request)
123
+
124
+ if ip and LOG_IP:
125
+ info["ip"] = ip
126
+ if geo is not None and ip:
127
+ # lookup() returns {} for private ranges and on any failure.
128
+ info.update(geo.lookup(ip))
129
+
130
+ user_agent = _header(request, "user-agent")
131
+ if user_agent:
132
+ info["user_agent"] = user_agent[:200]
133
+ language = _header(request, "accept-language")
134
+ if language:
135
+ info["language"] = language.split(",")[0].strip()
136
+ session = getattr(request, "session_hash", None)
137
+ if session:
138
+ info["session"] = str(session)
139
+ return info
140
+ except Exception:
141
+ logger.debug("could not describe caller", exc_info=True)
142
+ return {}
143
+
144
+
145
+ def _trace_inputs(inputs: dict) -> dict:
146
+ """Drop the unserialisable client and the raw request object.
147
+
148
+ The request carries every header; only the fields chosen by
149
+ describe_caller belong in a trace, and they go on as metadata instead.
150
+ """
151
+ return {
152
+ key: value for key, value in inputs.items()
153
+ if key not in {"client", "request"}
154
+ }
155
+
156
+
157
+ def _trace_outputs(result: Any) -> dict:
158
+ """Record the seed, never the image.
159
+
160
+ LangSmith calls this with None when the wrapped call raised, so a bare
161
+ unpack here would log a processing error on top of every failure.
162
+ """
163
+ if result is None:
164
+ return {}
165
+ try:
166
+ _, seed = result
167
+ return {"seed": seed}
168
+ except Exception:
169
+ return {}
170
+
171
+
172
+ @traceable(
173
+ run_type="chain",
174
+ name="ui_generate",
175
+ process_inputs=_trace_inputs,
176
+ process_outputs=_trace_outputs,
177
+ )
178
+ def traced_predict(client, *, prompt, negative, steps, guidance, lora_scale,
179
+ seed, request=None):
180
+ """Call the inference Space, recording the request and who made it."""
181
+ caller = describe_caller(request)
182
+ add_trace_metadata(**caller)
183
+
184
+ started = time.perf_counter()
185
+ image, used = client.predict(
186
+ prompt, negative, steps, guidance, lora_scale, seed,
187
+ api_name="/generate",
188
+ )
189
+ duration_ms = (time.perf_counter() - started) * 1000
190
+
191
+ logger.info(
192
+ "ui request seed=%s steps=%s lora_scale=%s ms=%.0f %s",
193
+ used, steps, lora_scale, duration_ms,
194
+ " ".join(f"{key}={value}" for key, value in sorted(caller.items())),
195
+ )
196
+ return image, used
197
+
198
+
199
+ def configure_logging(level: str | None = None) -> None:
200
+ """Single-line structured logs. Safe to call more than once."""
201
+ if logger.handlers:
202
+ return
203
+ handler = logging.StreamHandler()
204
+ handler.setFormatter(
205
+ logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s")
206
+ )
207
+ logger.addHandler(handler)
208
+ logger.setLevel(level or os.environ.get("LOG_LEVEL", "INFO").upper())
209
+ logger.propagate = False