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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
import os
import csv
import json
import ast
import time
import logging
from pathlib import Path
from typing import Dict, List, Tuple, Any, Optional

import numpy as np
import gradio as gr
from PIL import Image
from huggingface_hub import hf_hub_download

from fetch_url_util import fetch_image_from_url

# Configure Logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(message)s')
logger = logging.getLogger("PixAITagger")

logging.getLogger().setLevel(logging.WARNING)
logging.getLogger("PixAITagger").setLevel(logging.INFO)

# Constants
MODEL_REPO = "deepghs/pixai-tagger-v0.9-onnx"
MODEL_FILENAME = "model.onnx"
TAGS_FILENAME = "selected_tags.csv"
INPUT_SIZE = 448

class HybridEngine:
    """
    Handles inference, allowing dynamic switching between OpenVINO and ONNX Runtime.
    """
    def __init__(self, model_path: str):
        self.model_path = model_path
        self.ov_session = None
        self.ort_session = None
        self.ov_available = False
        
        # Check OpenVINO availability once
        try:
            import openvino as ov
            self.ov_available = True
        except ImportError:
            self.ov_available = False

    def _get_ort_session(self):
        """Lazy load ONNX Runtime session"""
        if self.ort_session is None:
            import onnxruntime as ort
            sess_options = ort.SessionOptions()
            sess_options.log_severity_level = 3
            logger.info("Engine: Initializing ONNX Runtime...")
            self.ort_session = ort.InferenceSession(self.model_path, sess_options=sess_options, providers=["CPUExecutionProvider"])
        return self.ort_session

    def _get_ov_session(self):
        """Lazy load OpenVINO session"""
        if not self.ov_available:
            raise ImportError("OpenVINO not installed")
            
        if self.ov_session is None:
            import openvino as ov
            core = ov.Core()
            # ov.log.set_level(ov.log.Level.ERR)
            logger.info("Engine: Compiling OpenVINO model...")
            model_ov = core.read_model(self.model_path)
            self.ov_session = core.compile_model(model_ov, "CPU")
        return self.ov_session

    def run(self, input_data: np.ndarray, expected_dim: int) -> Tuple[np.ndarray, str]:
        """
        Runs inference. Returns (logits, provider_name).
        Tries OpenVINO first, falls back to ONNX Runtime if needed.
        """
        # Try OpenVINO if available
        if self.ov_available:
            try:
                sess = self._get_ov_session()
                # OpenVINO inference
                request = sess.create_infer_request()
                results = request.infer(input_data)
                
                # Find output with matching shape
                output_tensor = None
                for res_data in results.values():
                    if res_data.shape[1] == expected_dim:
                        output_tensor = res_data[0]
                        break
                
                if output_tensor is None:
                    # Fallback to largest output
                    output_tensor = max(results.values(), key=lambda x: x.shape[1])[0]
                    
                return output_tensor, "OpenVINO (CPU)"
            
            except Exception as e:
                logger.warning(f"OpenVINO execution failed: {e}. Falling back to ONNX Runtime.")
                # Fall through to ORT

        # ONNX Runtime Fallback
        sess = self._get_ort_session()
        input_name = sess.get_inputs()[0].name
        outputs = sess.run(None, {input_name: input_data})
        
        output_tensor = None
        for out in outputs:
            if out.shape[1] == expected_dim:
                output_tensor = out[0]
                break
        
        if output_tensor is None:
            output_tensor = max(outputs, key=lambda x: x.shape[1])[0]

        provider = sess.get_providers()[0]
        return output_tensor, f"ONNX Runtime ({provider})"

class PixAITagger:
    def __init__(self):
        self.model_path = None
        self.tags_list = []
        self._load_resources()
        self.engine = HybridEngine(str(self.model_path))

    def _load_resources(self):
        logger.info(f"Downloading resources from {MODEL_REPO}...")
        self.model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
        
        try:
            tags_path = hf_hub_download(repo_id=MODEL_REPO, filename=TAGS_FILENAME)
            self._load_tags_csv(Path(tags_path))
        except Exception as e:
            raise FileNotFoundError(f"Could not load tags file: {e}")

    def _load_tags_csv(self, csv_path: Path):
        self.tags_list = []
        with csv_path.open("r", encoding="utf-8") as f:
            reader = csv.DictReader(f)
            for row in reader:
                try:
                    idx = int(row.get("id"))
                    name = row.get("name")
                    category = int(row.get("category", 0))
                    ips_raw = row.get("ips", "[]")
                    ips = []
                    if ips_raw and ips_raw != "[]":
                        try:
                            ips = json.loads(ips_raw)
                        except:
                            try:
                                ips = ast.literal_eval(ips_raw)
                            except:
                                pass
                    self.tags_list.append({
                        "id": idx,
                        "name": name,
                        "is_char": category == 4, 
                        "ips": [str(ip) for ip in ips]
                    })
                except ValueError:
                    continue

        self.tags_list.sort(key=lambda x: x["id"])
        
        self.id_to_tag = {}
        self.char_indices = []
        self.gen_indices = []
        self.mapping = {}

        for item in self.tags_list:
            idx = item["id"]
            name = item["name"]
            self.id_to_tag[idx] = name
            
            if item["is_char"]:
                self.char_indices.append(idx)
                if item["ips"]:
                    self.mapping[name] = item["ips"]
            else:
                self.gen_indices.append(idx)

        self.num_classes = len(self.tags_list)
        logger.info(f"Loaded {self.num_classes} tags.")

    def preprocess(self, image: Image.Image) -> np.ndarray:
        if image.mode != "RGB":
            image = image.convert("RGB")
        image = image.resize((INPUT_SIZE, INPUT_SIZE), Image.BICUBIC)
        img = np.array(image).astype(np.float32) / 255.0
        img = (img - 0.5) / 0.5
        img = img.transpose(2, 0, 1)
        return np.expand_dims(img, 0)

    def predict(self, 
                image: Image.Image, 
                gen_threshold: float, 
                char_threshold: float,
                resolve_mapping: bool) -> Tuple[Dict, Dict, str, str]:
        
        input_tensor = self.preprocess(image)
        
        # Run inference - always try OpenVINO first, fallback to ONNX
        logits, provider_name = self.engine.run(input_tensor, self.num_classes)
        
        # Sigmoid
        probs = 1 / (1 + np.exp(-logits))

        # General Tags
        gen_tags = {}
        for idx in self.gen_indices:
            if idx < len(probs):
                score = float(probs[idx])
                if score >= gen_threshold:
                    gen_tags[self.id_to_tag[idx]] = score

        # Character Tags & IPs
        char_tags = {}
        detected_ips = set()
        
        for idx in self.char_indices:
            if idx < len(probs):
                score = float(probs[idx])
                if score >= char_threshold:
                    name = self.id_to_tag[idx]
                    char_tags[name] = score
                    
                    if resolve_mapping and name in self.mapping:
                        for ip in self.mapping[name]:
                            detected_ips.add(ip)

        gen_tags = dict(sorted(gen_tags.items(), key=lambda x: x[1], reverse=True))
        char_tags = dict(sorted(char_tags.items(), key=lambda x: x[1], reverse=True))
        
        ip_text = ", ".join(sorted(list(detected_ips))) if detected_ips else ""

        return gen_tags, char_tags, ip_text, provider_name

# --- UI Setup ---

tagger_instance = None

def get_tagger():
    global tagger_instance
    if tagger_instance is None:
        tagger_instance = PixAITagger()
    return tagger_instance

def init_app():
    """Warms up the model and prevents Gradio return-value warnings."""
    get_tagger()
    return None

def run_inference(image, gen_thresh, char_thresh, resolve_mapping):
    if image is None:
        return "", "", "", {}, {}, ""
    try:
        model = get_tagger()
        start_time = time.time()
        
        # Internal predict handles OpenVINO -> ONNX fallback
        gen_tags, char_tags, ip_str, provider = model.predict(image, gen_thresh, char_thresh, resolve_mapping)
        
        taken = time.time() - start_time
        
        char_str = ", ".join(char_tags.keys()).replace("_", " ")
        gen_str = ", ".join(gen_tags.keys()).replace("_", " ")
        
        if not resolve_mapping:
            ip_disp = "Mapping Disabled"
        elif not ip_str:
            ip_disp = "No specific copyright detected"
        else:
            ip_disp = ip_str.replace("_", " ")

        time_disp = f"- **Provider:** {provider} | **Time taken:** {taken:.4f}s"
        return char_str, ip_disp, gen_str, char_tags, gen_tags, time_disp
    except Exception as e:
        logger.error(f"Inference Error: {e}")
        return "", f"Error: {str(e)}", "", {}, {}, f"Error: {str(e)}"


with gr.Blocks(title="PixAI Tagger v0.9 ONNX") as demo:
    
    # Header Row
    with gr.Row(elem_classes=["container"]):
        with gr.Column(scale=1, elem_classes=["header-col"]):
            gr.Markdown("### Input Image")
        with gr.Column(scale=1, elem_classes=["header-col"]):
            gr.Markdown("### Configuration & Results")

    with gr.Row(elem_classes=["container"]):
        # LEFT COLUMN - Image only
        with gr.Column(scale=1):
            input_img = gr.Image(
                type="pil", 
                label="", 
                show_label=False, 
                elem_classes=["image-container"]
            )
            url_input = gr.Textbox(label=" Or Paste Image URL (not all URLs may work) ", placeholder="https://example.com/image.jpg")

        # RIGHT COLUMN - Controls and Outputs
        with gr.Column(scale=1):
            # 1. Configuration Section
            with gr.Group():
                char_slider = gr.Slider(0.0, 1.0, value=0.85, step=0.05, label="Character Threshold")
                gen_slider = gr.Slider(0.0, 1.0, value=0.30, step=0.05, label="General Threshold")
                map_checkbox = gr.Checkbox(value=True, label="Resolve Copyright Mapping")
            
            # 2. Action Buttons
            with gr.Row():
                run_btn = gr.Button("Run (Image Upload)", variant="primary")
                url_btn = gr.Button("Run (URL)")
            
            # 3. Output Textboxes
            with gr.Group():
                char_box = gr.Textbox(label="Character Tags", interactive=False, buttons=["copy"])
                ip_box = gr.Textbox(label="Character - Copyright Mapping", interactive=False, buttons=["copy"])
                gen_box = gr.Textbox(label="General Tags", interactive=False, buttons=["copy"])
                time_info = gr.Markdown(elem_id="time-display")

            # 4. Confidence Plots
            gr.Markdown("### Confidence Scores")
            char_plot = gr.Label(label="Character Probabilities", num_top_classes=50)
            gen_plot = gr.Label(label="General Probabilities", num_top_classes=50)

    # Click Logic
    url_btn.click(
        fn=fetch_image_from_url, 
        inputs=[url_input], 
        outputs=[input_img]
    ).then(
        fn=run_inference,
        inputs=[input_img, gen_slider, char_slider, map_checkbox],
        outputs=[char_box, ip_box, gen_box, char_plot, gen_plot, time_info]
    )

    # Standard Upload Button Logic
    run_btn.click(
        fn=run_inference,
        inputs=[input_img, gen_slider, char_slider, map_checkbox],
        outputs=[char_box, ip_box, gen_box, char_plot, gen_plot, time_info]
    )

    # Load logic
    demo.load(fn=init_app)

if __name__ == "__main__":
    demo.launch(theme=gr.themes.Base())