File size: 14,885 Bytes
1af135d
 
bca79d3
1af135d
 
 
bca79d3
 
 
1af135d
 
4a0d3ba
 
 
1af135d
d804480
974b8ef
bca79d3
1af135d
 
bca79d3
1af135d
 
 
 
 
 
4a0d3ba
 
 
 
 
 
 
bca79d3
1af135d
 
bca79d3
4a0d3ba
 
 
 
 
 
 
bca79d3
 
4a0d3ba
 
 
bca79d3
 
4a0d3ba
 
 
 
 
 
1af135d
 
4a0d3ba
 
 
 
bca79d3
1af135d
4a0d3ba
 
 
bca79d3
1af135d
4a0d3ba
 
 
bca79d3
1af135d
4a0d3ba
 
 
bca79d3
1af135d
4a0d3ba
1af135d
 
 
4a0d3ba
1af135d
bca79d3
4a0d3ba
1af135d
 
 
4a0d3ba
1af135d
4a0d3ba
 
1af135d
4a0d3ba
1af135d
 
 
974b8ef
1af135d
4a0d3ba
974b8ef
1af135d
4a0d3ba
 
 
 
 
 
 
 
 
 
1af135d
974b8ef
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
1af135d
 
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
 
 
 
 
 
1af135d
4a0d3ba
1af135d
974b8ef
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
1af135d
974b8ef
4a0d3ba
974b8ef
 
 
 
 
 
4a0d3ba
1af135d
974b8ef
4a0d3ba
974b8ef
4a0d3ba
1af135d
974b8ef
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
 
 
 
 
4a0d3ba
974b8ef
1af135d
 
 
974b8ef
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
 
4a0d3ba
974b8ef
1af135d
4a0d3ba
1af135d
974b8ef
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
4a0d3ba
1af135d
974b8ef
 
1af135d
4a0d3ba
 
974b8ef
1af135d
974b8ef
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
 
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
1af135d
974b8ef
1af135d
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
4a0d3ba
 
974b8ef
1af135d
974b8ef
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
4a0d3ba
 
1af135d
974b8ef
 
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
 
4a0d3ba
974b8ef
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
 
 
1af135d
4a0d3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1af135d
4a0d3ba
 
 
 
 
 
 
 
 
 
 
 
974b8ef
1af135d
 
974b8ef
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
4a0d3ba
974b8ef
4a0d3ba
974b8ef
 
 
4a0d3ba
 
974b8ef
1af135d
974b8ef
4a0d3ba
974b8ef
 
 
112edc8
4a0d3ba
974b8ef
 
 
 
 
 
4a0d3ba
974b8ef
 
 
 
 
 
 
 
 
 
4a0d3ba
974b8ef
1af135d
4a0d3ba
1af135d
974b8ef
 
 
 
112edc8
1af135d
4a0d3ba
 
 
1af135d
974b8ef
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import atexit
import logging
import threading
from collections.abc import Generator, Sequence
from typing import Any

import gradio as gr
import spaces
import torch
from PIL import Image, UnidentifiedImageError
from transformers import (
    AutoModelForImageTextToText,
    AutoProcessor,
    TextIteratorStreamer,
)

# Configuration
MODEL_ID = "beaunix/Aegis-Art-Atelier-Qwen2.5-VL-7B"
DEVICE = "cuda"

MAX_NEW_TOKENS = 400
TEMPERATURE = 0.7
TOP_P = 0.9
MIN_P = 0.1
GPU_DURATION = 120

SYSTEM_PROMPT = (
    "You are Melkov, a 21-year-old French art enthusiast and the resident "
    "expert of Aegis Art Atelier. You describe and discuss artwork with "
    "genuine warmth and attention to detail: subject, style, composition, "
    "lighting, color, and mood. Speak in your own voice rather than like a "
    "museum catalog entry. When an image is shared with you, look closely "
    "and describe only what you actually see. You are a happy, friendly "
    "artist boy."
)

CSS = """
:root {
    --void-blue: #081126;
    --royal-blue: #162B58;
    --royal-blue-light: #213C74;
    --gold-leaf: #C9A227;
    --gold-bright: #F0C766;
    --marble-ivory: #F4EDE2;
    --velvet-wine: #5C1A2B;
}
.gradio-container {
    min-height: 100vh;
    background: var(--void-blue) !important;
    color: var(--marble-ivory) !important;
}
#melkov-title {
    margin-bottom: 0.4rem;
    padding-bottom: 0.8rem;
    color: var(--gold-leaf) !important;
    text-align: center;
    letter-spacing: 0.12em;
    border-bottom: 1px solid rgba(201, 162, 39, 0.7);
}
#melkov-subtitle {
    max-width: 760px;
    margin: 0 auto 1rem auto;
    color: var(--marble-ivory) !important;
    text-align: center;
}
.gradio-container .message.user {
    background: var(--royal-blue) !important;
    color: var(--marble-ivory) !important;
    border: 1px solid rgba(201, 162, 39, 0.65) !important;
}
.gradio-container .message.bot {
    background: #101F41 !important;
    color: var(--marble-ivory) !important;
    border-left: 3px solid var(--gold-bright) !important;
}
.gradio-container .gr-button-primary {
    background: var(--gold-leaf) !important;
    color: var(--void-blue) !important;
    border: none !important;
}
.gradio-container .gr-button-primary:hover {
    background: var(--gold-bright) !important;
}
.gradio-container textarea,
.gradio-container input {
    color: var(--marble-ivory) !important;
}
footer {
    visibility: hidden;
}
"""

# Logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s | %(message)s",
)
LOGGER = logging.getLogger(__name__)

LOGGER.info("Application startup initialized.")


def _log_shutdown() -> None:
    """Log application shutdown."""
    LOGGER.info("Application shutdown completed.")


atexit.register(_log_shutdown)

# Global State
_MODEL: Any = None
_PROCESSOR: Any = None
_MODEL_LOCK = threading.Lock()


# Lazy Model Loader
def load_model() -> tuple[Any, Any]:
    """Load and cache the processor and model in a thread-safe manner.

    Returns:
        A tuple containing the loaded processor and model.

    Raises:
        RuntimeError: If the model cannot be loaded successfully.
    """
    global _MODEL, _PROCESSOR

    if _MODEL is not None and _PROCESSOR is not None:
        return _PROCESSOR, _MODEL

    with _MODEL_LOCK:
        if _MODEL is not None and _PROCESSOR is not None:
            return _PROCESSOR, _MODEL

        try:
            LOGGER.info("Loading processor for model: %s", MODEL_ID)
            processor = AutoProcessor.from_pretrained(
                MODEL_ID,
                trust_remote_code=False,
            )

            LOGGER.info("Loading model weights for model: %s", MODEL_ID)
            model = AutoModelForImageTextToText.from_pretrained(
                MODEL_ID,
                torch_dtype=torch.bfloat16,
                low_cpu_mem_usage=True,
                trust_remote_code=False,
            )

            LOGGER.info("Moving model to GPU.")
            model.to(DEVICE)
            model.eval()

            _PROCESSOR = processor
            _MODEL = model

            LOGGER.info("Model loading completed successfully.")

            return _PROCESSOR, _MODEL

        except Exception as error:
            _MODEL = None
            _PROCESSOR = None
            LOGGER.exception("Model loading failed.")
            raise RuntimeError(
                "Melkov is temporarily unavailable while the model is loading."
            ) from error


# Utility Functions
def normalize_content(content: Any) -> tuple[str, list[str]]:
    """Normalize Gradio message content into text and image paths.

    Args:
        content: A Gradio chat message content value.

    Returns:
        A tuple containing normalized text and image file paths.
    """
    if content is None:
        return "", []

    if isinstance(content, str):
        return content.strip(), []

    text_parts: list[str] = []
    image_paths: list[str] = []

    if isinstance(content, dict):
        text = content.get("text")
        if isinstance(text, str) and text.strip():
            text_parts.append(text.strip())

        files = content.get("files", [])
        if isinstance(files, list):
            image_paths.extend(_extract_file_paths(files))

        path = content.get("path")
        if isinstance(path, str) and path:
            image_paths.append(path)

    elif isinstance(content, list):
        for item in content:
            text, paths = normalize_content(item)
            if text:
                text_parts.append(text)
            image_paths.extend(paths)

    else:
        text_parts.append(str(content))

    return " ".join(text_parts).strip(), list(dict.fromkeys(image_paths))


def _extract_file_paths(files: Sequence[Any]) -> list[str]:
    """Extract valid local paths from Gradio file values.

    Args:
        files: A sequence of Gradio file values.

    Returns:
        A list of local image paths.
    """
    paths: list[str] = []

    for file_value in files:
        if isinstance(file_value, str):
            paths.append(file_value)
        elif isinstance(file_value, dict):
            path = file_value.get("path") or file_value.get("name")
            if isinstance(path, str) and path:
                paths.append(path)
        else:
            path = getattr(file_value, "path", None) or getattr(
                file_value, "name", None
            )
            if isinstance(path, str) and path:
                paths.append(path)

    return paths


def extract_images(image_paths: Sequence[str]) -> list[Image.Image]:
    """Load image files as RGB PIL images.

    Args:
        image_paths: Paths to uploaded image files.

    Returns:
        A list of converted RGB images.

    Raises:
        ValueError: If an uploaded file is not a valid readable image.
    """
    images: list[Image.Image] = []

    for image_path in image_paths:
        try:
            with Image.open(image_path) as image:
                images.append(image.convert("RGB"))
        except (FileNotFoundError, UnidentifiedImageError, OSError) as error:
            LOGGER.warning("Invalid image upload received: %s", image_path)
            raise ValueError(
                "I could not read one of the uploaded files. "
                "Please upload a valid image."
            ) from error

    return images


def _friendly_error_message(error: Exception) -> str:
    """Convert internal errors into safe user-facing messages.

    Args:
        error: The exception raised during processing.

    Returns:
        A user-friendly error message.
    """
    message = str(error).lower()

    if isinstance(error, ValueError):
        return str(error)

    if "out of memory" in message or "cuda oom" in message:
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        return (
            "The GPU ran out of memory while analyzing that request. "
            "Please try again with fewer images or a shorter conversation."
        )

    if "cuda" in message:
        return (
            "The GPU is temporarily unavailable. Please wait a moment "
            "and try again."
        )

    return (
        "I encountered a temporary issue while preparing your response. "
        "Please try again."
    )


# Conversation Builder
def build_conversation(
    history: Sequence[dict[str, Any]] | None,
    message: Any,
) -> list[dict[str, Any]]:
    """Build a Qwen2.5-VL compatible multimodal conversation.

    Args:
        history: Previous Gradio messages using the messages format.
        message: The current Gradio multimodal message.

    Returns:
        A Qwen2.5-VL conversation containing text and image parts.
    """
    conversation: list[dict[str, Any]] = [
        {
            "role": "system",
            "content": [{"type": "text", "text": SYSTEM_PROMPT}],
        }
    ]

    for turn in history or []:
        role = turn.get("role")
        if role not in {"user", "assistant"}:
            continue

        text, image_paths = normalize_content(turn.get("content"))
        parts: list[dict[str, Any]] = [
            {"type": "image", "image": image}
            for image in extract_images(image_paths)
        ]

        if text:
            parts.append({"type": "text", "text": text})

        if parts:
            conversation.append({"role": role, "content": parts})

    current_text, current_paths = normalize_content(message)
    current_parts: list[dict[str, Any]] = [
        {"type": "image", "image": image}
        for image in extract_images(current_paths)
    ]

    if current_text:
        current_parts.append({"type": "text", "text": current_text})
    elif not current_parts:
        current_parts.append({"type": "text", "text": "Tell me about this artwork."})

    conversation.append({"role": "user", "content": current_parts})

    return conversation


# Inference Engine
def prepare_inputs(
    processor: Any,
    conversation: Sequence[dict[str, Any]],
) -> Any:
    """Prepare tokenized model inputs for a multimodal conversation.

    Args:
        processor: The loaded Hugging Face processor.
        conversation: A Qwen2.5-VL formatted conversation.

    Returns:
        Tokenized model inputs moved to the GPU.

    Raises:
        RuntimeError: If processor input preparation fails.
    """
    try:
        prompt = processor.apply_chat_template(
            conversation,
            add_generation_prompt=True,
            tokenize=False,
        )

        images = [
            part["image"]
            for turn in conversation
            for part in turn["content"]
            if part.get("type") == "image"
        ]

        return processor(
            text=[prompt],
            images=images or None,
            return_tensors="pt",
        ).to(DEVICE)

    except Exception as error:
        LOGGER.exception("Processor input preparation failed.")
        raise RuntimeError(
            "I could not prepare that image or message for analysis."
        ) from error


def generate_stream(
    model: Any,
    processor: Any,
    inputs: Any,
) -> Generator[str, None, None]:
    """Generate and stream text from the model.

    Args:
        model: The loaded image-text model.
        processor: The loaded processor.
        inputs: Prepared GPU inputs.

    Yields:
        Incremental generated text.

    Raises:
        RuntimeError: If model generation fails.
    """
    streamer = TextIteratorStreamer(
        processor.tokenizer,
        skip_prompt=True,
        skip_special_tokens=True,
    )

    pad_token_id = processor.tokenizer.pad_token_id
    if pad_token_id is None:
        pad_token_id = processor.tokenizer.eos_token_id

    generation_errors: list[Exception] = []
    generation_kwargs = {
        **inputs,
        "max_new_tokens": MAX_NEW_TOKENS,
        "do_sample": True,
        "temperature": TEMPERATURE,
        "top_p": TOP_P,
        "min_p": MIN_P,
        "pad_token_id": pad_token_id,
        "streamer": streamer,
    }

    def run_generation() -> None:
        """Run blocking generation in a dedicated worker thread."""
        try:
            with torch.inference_mode():
                model.generate(**generation_kwargs)
        except Exception as error:
            generation_errors.append(error)
            LOGGER.exception("Generation thread failed.")
            streamer.end()

    LOGGER.info("Starting streamed generation.")
    worker = threading.Thread(target=run_generation, daemon=True)
    worker.start()

    partial_text = ""
    try:
        for token in streamer:
            partial_text += token
            yield partial_text
    finally:
        worker.join()

    if generation_errors:
        raise RuntimeError("Model generation failed.") from generation_errors[0]

    LOGGER.info("Streamed generation completed.")


# Gradio Callbacks
@spaces.GPU(duration=GPU_DURATION)
def respond(
    message: Any,
    history: list[dict[str, Any]] | None,
) -> Generator[str, None, None]:
    """Process a Gradio chat request and stream Melkov's response.

    Args:
        message: The current multimodal Gradio message.
        history: Previous messages in Gradio messages format.

    Yields:
        Incremental assistant response text.
    """
    try:
        LOGGER.info("Received inference request.")
        processor, model = load_model()
        conversation = build_conversation(history, message)
        inputs = prepare_inputs(processor, conversation)

        yield from generate_stream(model, processor, inputs)

    except Exception as error:
        LOGGER.exception("Inference request failed.")
        yield _friendly_error_message(error)


# UI Builder
def build_ui() -> gr.Blocks:
    """Build and return the Gradio application interface.

    Returns:
        The configured Gradio Blocks application.
    """
    with gr.Blocks() as demo:
        gr.Markdown("# MELKOV - ART ATELIER", elem_id="melkov-title")
        gr.Markdown(
            "Chat with **Melkov**, an art expert vision-language model trained "
            "to discuss paintings, visual composition, art history, and technique. "
            "Upload an artwork image or ask a question about art.",
            elem_id="melkov-subtitle",
        )

        gr.ChatInterface(
            fn=respond,
            multimodal=True,
            textbox=gr.MultimodalTextbox(
                file_types=["image"],
                file_count="multiple",
                sources=["upload"],
                placeholder="Share a painting, or ask Melkov about art...",
            ),
        )

    return demo


def main() -> None:
    """Launch the Gradio Spaces application."""
    LOGGER.info("Building Gradio interface.")
    demo = build_ui()
    demo.queue()
    demo.launch(css=CSS, theme=gr.themes.Base())


if __name__ == "__main__":

    
    main()