File size: 22,312 Bytes
ffeeefa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import os
import re
import shutil
import subprocess
import sys
import threading
from itertools import groupby
from pathlib import Path
from tempfile import NamedTemporaryFile

import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import spaces
import torch
from transformers import T5EncoderModel, T5Tokenizer

# -----------------------------------------------------------------------------
# Reproducible upstream sources: these match the working TREAD Colab demo.
# -----------------------------------------------------------------------------
TREAD_REPO = "https://github.com/KYQiu21/TREAD.git"
TREAD_COMMIT = "7a7b3f571778cb89035c798c88df7eacb63ed2c1"
PROTT5_MODEL = "Rostlab/prot_t5_xl_half_uniref50-enc"

APP_DIR = Path(__file__).resolve().parent
TREAD_DIR = APP_DIR / ".tread_source"

# Web-demo guardrails only; these are not biological/model validity limits.
MAX_GPU_SEQUENCE_LENGTH = int(os.getenv("TREAD_MAX_GPU_SEQUENCE_LENGTH", "1200"))
MAX_CPU_SEQUENCE_LENGTH = int(os.getenv("TREAD_MAX_CPU_SEQUENCE_LENGTH", "600"))

REPEAT_HEADS = [
    "Any repeat (segmentation head)",
    "Alpha solenoid",
    "TIM-barrel",
    "Beta-propeller",
    "Beta-barrel",
    "Beta-solenoid",
    "Alpha/beta solenoid",
]
TYPE_HEAD_INDEX = {
    "Alpha solenoid": 0,
    "TIM-barrel": 1,
    "Beta-propeller": 2,
    "Beta-barrel": 3,
    "Beta-solenoid": 4,
    "Alpha/beta solenoid": 5,
}

EXAMPLE_REPEAT = (
    "MMKRNILAVIVPALLVAGTANAAEIYNKDGNKVDLYGKAVGLHYFSKGNGENSYGGNGDMTYARLGFKGETQINSDLTGYGQWEYNFQGNNSEGADAQTGNKTRLAFAGLKYADVGSFDYGRNYGVVYDALGYTDMLPEFGGDTAYSDDFFVGRVGGVATYRNSNFFGLVDGLNFAVQYLGKNERDTARRSNGDGVGGSISYEYEGFGIVGAYGAADRTNLQEAQPLGNGKKAEQWATGLKYDANNIYLAANYGETRNATPITNKFTNTSGFANKTQDVLLVAQYQFDFGLRPSIAYTKSKAKDVEGIGDVDLVNYFEVGATYYFNKNMSTYVDYIINQIDSDNKLGVGSDDTVAVGIVYQF"
)
EXAMPLE_PROPELLER = (
    "MEQVLYLGSYTKRESKGVHQIILDTDKKELRDYRLIAEVDSPTYLDLSADKGTLYSISKTDEGGGITSFKKNENGTYDKVAEISAEGSAPCYIHYDEDKKLIFTANYHGGYLTVYKENADGSFTMSDRAQHEGSSIHENQTIPHVHYSALSPDKKFLLACDLGTDEVYTYTVSDEGKLTEAARYKATPGTGPRHLVFHPNGKVAYLFGELSSDVEVLAYEAATGTFSLLQVITTIPAEHTGFNGGAAIRISADGKFVYASNRGHDSLVVYAVSEDGETLSLVEYVPTEGNTPRDFNLDPSGQFVIVAHQDSDNLTLFERDATTGKLTLVQKDVYAPECVCVFY"
)

# -----------------------------------------------------------------------------
# Model state
#
# ZeroGPU path:
#   The GPU models are placed on "cuda" during app startup. In a ZeroGPU Space,
#   Hugging Face's CUDA emulation handles this even though a real GPU is only
#   assigned while a @spaces.GPU function is executing.
#
# CPU path:
#   A separate float32 copy is loaded lazily only if somebody presses the CPU
#   button. This avoids paying the RAM cost unless CPU fallback is actually used.
# -----------------------------------------------------------------------------
_gpu_tokenizer = None
_gpu_encoder = None
_gpu_repeat_model = None
_gpu_propeller_model = None
_gpu_init_error = None

_cpu_lock = threading.Lock()
_cpu_tokenizer = None
_cpu_encoder = None
_cpu_repeat_model = None
_cpu_propeller_model = None
_cpu_init_error = None


def _ensure_tread_source():
    """Fetch exactly the same TREAD commit used by the public Colab demo."""
    package_init = TREAD_DIR / "tread" / "__init__.py"
    if package_init.is_file():
        if str(TREAD_DIR) not in sys.path:
            sys.path.insert(0, str(TREAD_DIR))
        return

    shutil.rmtree(TREAD_DIR, ignore_errors=True)
    subprocess.run(["git", "init", str(TREAD_DIR)], check=True)
    subprocess.run(
        ["git", "-C", str(TREAD_DIR), "remote", "add", "origin", TREAD_REPO],
        check=True,
    )
    subprocess.run(
        [
            "git",
            "-C",
            str(TREAD_DIR),
            "fetch",
            "--depth",
            "1",
            "origin",
            TREAD_COMMIT,
        ],
        check=True,
    )
    subprocess.run(
        ["git", "-C", str(TREAD_DIR), "checkout", "--detach", "FETCH_HEAD"],
        check=True,
    )

    if not package_init.is_file():
        raise FileNotFoundError(
            f"TREAD installation incomplete: {package_init} not found"
        )

    if str(TREAD_DIR) not in sys.path:
        sys.path.insert(0, str(TREAD_DIR))


def _build_repeat_model(DMDModel, device):
    model = DMDModel(
        per_resi_emb_dim=1024,
        out_channel=64,
        hidden_dim=64,
        num_block=2,
        dropout=0.2,
        bilstm=True,
        kernel_size_conv1=3,
        kernel_size_block=7,
        multi=True,
        num_types=6,
        device=device,
    )
    state = torch.load(
        TREAD_DIR / "trained_model" / "linear-edge_model_repeatsdb.pt",
        map_location="cpu",
        weights_only=True,
    )
    model.load_state_dict(state)
    model = model.to(device).eval()
    return model


def _build_propeller_model(DMDModel, device):
    model = DMDModel(
        per_resi_emb_dim=1024,
        out_channel=128,
        hidden_dim=64,
        num_block=1,
        dropout=0.2,
        bilstm=True,
        kernel_size_conv1=11,
        kernel_size_block=7,
        device=device,
    )
    state = torch.load(
        TREAD_DIR / "trained_model" / "linear-edge_model_propeller_blade.pt",
        map_location="cpu",
        weights_only=True,
    )
    model.load_state_dict(state)
    model = model.to(device).eval()
    return model


def initialize_gpu_models_at_startup():
    """Prepare the ZeroGPU model copy on emulated CUDA during app startup."""
    global _gpu_tokenizer, _gpu_encoder, _gpu_repeat_model
    global _gpu_propeller_model, _gpu_init_error

    try:
        _ensure_tread_source()
        from tread.model import DMDModel

        _gpu_tokenizer = T5Tokenizer.from_pretrained(
            PROTT5_MODEL,
            do_lower_case=False,
            legacy=True,
        )
        _gpu_encoder = T5EncoderModel.from_pretrained(PROTT5_MODEL)
        _gpu_encoder = _gpu_encoder.to("cuda").eval()

        _gpu_repeat_model = _build_repeat_model(DMDModel, torch.device("cuda"))
        _gpu_propeller_model = _build_propeller_model(
            DMDModel, torch.device("cuda")
        )

        print("ZeroGPU model copy initialized successfully.")

    except Exception as exc:
        # Do not kill the whole web app: CPU fallback may still work and the
        # error will be shown clearly if the ZeroGPU button is pressed.
        _gpu_init_error = (
            f"{type(exc).__name__}: {exc}"
        )
        print("ZeroGPU initialization failed:", _gpu_init_error)


def initialize_cpu_models():
    """Load a separate CPU copy only when the CPU fallback is requested."""
    global _cpu_tokenizer, _cpu_encoder, _cpu_repeat_model
    global _cpu_propeller_model, _cpu_init_error

    if all(
        x is not None
        for x in (
            _cpu_tokenizer,
            _cpu_encoder,
            _cpu_repeat_model,
            _cpu_propeller_model,
        )
    ):
        return

    if _cpu_init_error is not None:
        raise RuntimeError(_cpu_init_error)

    with _cpu_lock:
        if all(
            x is not None
            for x in (
                _cpu_tokenizer,
                _cpu_encoder,
                _cpu_repeat_model,
                _cpu_propeller_model,
            )
        ):
            return

        try:
            _ensure_tread_source()
            from tread.model import DMDModel

            # Tokenizers are device independent. Reuse the one already loaded
            # for ZeroGPU if available.
            _cpu_tokenizer = _gpu_tokenizer
            if _cpu_tokenizer is None:
                _cpu_tokenizer = T5Tokenizer.from_pretrained(
                    PROTT5_MODEL,
                    do_lower_case=False,
                    legacy=True,
                )

            # CPU inference uses float32 for broad PyTorch CPU compatibility.
            # low_cpu_mem_usage reduces peak RAM while loading this large model.
            _cpu_encoder = T5EncoderModel.from_pretrained(
                PROTT5_MODEL,
                torch_dtype=torch.float32,
                low_cpu_mem_usage=True,
            )
            _cpu_encoder = _cpu_encoder.to("cpu").eval()

            _cpu_repeat_model = _build_repeat_model(
                DMDModel, torch.device("cpu")
            )
            _cpu_propeller_model = _build_propeller_model(
                DMDModel, torch.device("cpu")
            )

            print("CPU fallback model copy initialized successfully.")

        except Exception as exc:
            _cpu_init_error = f"{type(exc).__name__}: {exc}"
            raise RuntimeError(_cpu_init_error) from exc


def clean_sequence(raw_sequence: str, max_length: int) -> str:
    if raw_sequence is None:
        raise gr.Error("Please paste a protein sequence.")

    text = raw_sequence.strip()
    if not text:
        raise gr.Error("Please paste a protein sequence.")

    # Accept either a raw sequence or a single FASTA record.
    lines = [line.strip() for line in text.splitlines() if line.strip()]
    if lines and lines[0].startswith(">"):
        lines = [line for line in lines[1:] if not line.startswith(">")]

    sequence = "".join(lines)
    sequence = re.sub(r"\s+", "", sequence).upper()

    if not sequence:
        raise gr.Error("No amino-acid sequence was found.")

    # Standard 20 amino acids plus the symbols handled by the notebook's
    # ProtT5 preprocessing (U, Z, O, B -> X) and X itself.
    allowed = set("ACDEFGHIKLMNPQRSTVWYUZOBX")
    invalid = sorted(set(sequence) - allowed)
    if invalid:
        raise gr.Error(
            "Invalid sequence characters: "
            + ", ".join(invalid)
            + ". Paste amino-acid letters only (a FASTA header is allowed)."
        )

    if len(sequence) > max_length:
        raise gr.Error(
            f"This backend currently accepts up to {max_length} residues; "
            f"your sequence has {len(sequence)} residues."
        )

    return sequence


def get_prott5_embedding(sequence, tokenizer, encoder, device):
    processed = " ".join(list(re.sub(r"[UZOB]", "X", sequence)))
    ids = tokenizer(
        [processed],
        add_special_tokens=True,
        padding="longest",
        truncation=False,
        return_attention_mask=True,
    )

    input_ids = torch.tensor(ids["input_ids"], device=device)
    attention_mask = torch.tensor(ids["attention_mask"], device=device)

    with torch.inference_mode():
        embedding_repr = encoder(
            input_ids=input_ids,
            attention_mask=attention_mask,
        )

    # Drop the terminal special token, exactly as in the working Colab demo.
    emb = embedding_repr.last_hidden_state[0, :-1]

    if emb.shape[0] != len(sequence):
        raise RuntimeError(
            f"ProtT5 returned {emb.shape[0]} residue embeddings for a "
            f"{len(sequence)}-residue sequence."
        )

    return emb


def get_ranges(preds, cutoff1=0.5, min_len=15, cutoff2=0.5, frac2=0.5):
    """Same motif-range logic as tread.utils.get_ranges."""
    preds = np.asarray(preds).flatten()
    above_threshold = preds > cutoff1
    peaks = []

    for key, group in groupby(enumerate(above_threshold), key=lambda x: x[1]):
        if key:
            group = list(group)
            if len(group) >= min_len:
                beg = group[0][0]
                end = beg + len(group)
                if (
                    len(np.where(preds[beg:end] > cutoff2)[0]) / len(group)
                    >= frac2
                ):
                    peaks.append((beg, end))

    return peaks


def make_plot(scores, ranges, title, cutoff):
    scores = np.asarray(scores).flatten()
    x = np.arange(1, len(scores) + 1)

    fig, ax = plt.subplots(figsize=(10, 4.5), dpi=150)
    ax.plot(x, scores, linewidth=1.5)
    ax.axhline(
        cutoff,
        linestyle="--",
        linewidth=1.0,
        alpha=0.7,
        label=f"cutoff = {cutoff:g}",
    )
    for start0, end0 in ranges:
        ax.axvspan(start0 + 1, end0, alpha=0.18)

    ax.set_xlim(1, max(1, len(scores)))
    ax.set_ylim(0, 1)
    ax.set_xlabel("Residue")
    ax.set_ylabel("Residue score")
    ax.set_title(title)
    ax.legend(loc="upper right")
    fig.tight_layout()

    return fig


def make_download_table(sequence, scores, ranges):
    scores = np.asarray(scores).flatten()
    predicted = np.zeros(len(sequence), dtype=bool)

    for start0, end0 in ranges:
        predicted[start0:end0] = True

    return pd.DataFrame(
        {
            "residue": np.arange(1, len(sequence) + 1),
            "amino_acid": list(sequence),
            "score": scores,
            "predicted_motif": predicted,
        }
    )


def save_csv(df: pd.DataFrame):
    temp = NamedTemporaryFile(
        prefix="tread_prediction_",
        suffix=".csv",
        delete=False,
    )
    temp.close()
    df.to_csv(temp.name, index=False)
    return temp.name


def _predict_core(
    sequence_text,
    model_choice,
    repeat_head,
    cutoff,
    min_len,
    *,
    tokenizer,
    encoder,
    repeat_model,
    propeller_model,
    device,
    backend_label,
    max_length,
):
    sequence = clean_sequence(sequence_text, max_length=max_length)

    embedding = get_prott5_embedding(
        sequence,
        tokenizer=tokenizer,
        encoder=encoder,
        device=device,
    )

    with torch.inference_mode():
        if model_choice == "RepeatsDB repeat annotation":
            seg_prediction, type_prediction = repeat_model.predict_single(
                embedding
            )

            if repeat_head == "Any repeat (segmentation head)":
                scores = seg_prediction
            else:
                scores = type_prediction[TYPE_HEAD_INDEX[repeat_head]]

            profile_name = repeat_head
            title = f"TREAD RepeatsDB — {profile_name}"

        else:
            scores = propeller_model.predict_single(embedding)
            profile_name = "Beta-propeller blade"
            title = "TREAD — beta-propeller blade annotation"

    ranges = get_ranges(
        scores,
        cutoff1=float(cutoff),
        min_len=int(min_len),
    )
    fig = make_plot(scores, ranges, title, float(cutoff))

    # Convert notebook's 0-based, end-exclusive ranges to user-facing
    # 1-based inclusive residue coordinates.
    if ranges:
        ranges_df = pd.DataFrame(
            [
                {
                    "Start (1-based)": start0 + 1,
                    "End (1-based)": end0,
                    "Length": end0 - start0,
                }
                for start0, end0 in ranges
            ]
        )
        range_text = ", ".join(f"{s + 1}{e}" for s, e in ranges)
    else:
        ranges_df = pd.DataFrame(
            columns=["Start (1-based)", "End (1-based)", "Length"]
        )
        range_text = "None at the current thresholds"

    profile_df = make_download_table(sequence, scores, ranges)
    csv_path = save_csv(profile_df)

    status = (
        f"**Sequence length:** {len(sequence)} aa  \n"
        f"**Model:** {model_choice}  \n"
        f"**Profile:** {profile_name}  \n"
        f"**Predicted motif ranges:** {range_text}  \n"
        f"**Compute backend:** {backend_label}"
    )

    return status, fig, ranges_df, csv_path


@spaces.GPU(duration=120)
def predict_gpu(sequence_text, model_choice, repeat_head, cutoff, min_len):
    """ZeroGPU path. A real GPU is allocated only for this function call."""
    if _gpu_init_error is not None:
        raise gr.Error(
            "The ZeroGPU model copy could not be initialized. "
            f"Startup error: {_gpu_init_error}"
        )

    if any(
        x is None
        for x in (
            _gpu_tokenizer,
            _gpu_encoder,
            _gpu_repeat_model,
            _gpu_propeller_model,
        )
    ):
        raise gr.Error(
            "ZeroGPU models are not initialized. Check the Space runtime log."
        )

    device_name = "ZeroGPU"
    try:
        device_name = f"ZeroGPU — {torch.cuda.get_device_name(0)}"
    except Exception:
        pass

    return _predict_core(
        sequence_text,
        model_choice,
        repeat_head,
        cutoff,
        min_len,
        tokenizer=_gpu_tokenizer,
        encoder=_gpu_encoder,
        repeat_model=_gpu_repeat_model,
        propeller_model=_gpu_propeller_model,
        device=torch.device("cuda"),
        backend_label=device_name,
        max_length=MAX_GPU_SEQUENCE_LENGTH,
    )


def predict_cpu(sequence_text, model_choice, repeat_head, cutoff, min_len):
    """CPU fallback. Does not consume ZeroGPU quota, but is much slower."""
    try:
        initialize_cpu_models()
    except Exception as exc:
        raise gr.Error(f"CPU model initialization failed: {exc}") from exc

    return _predict_core(
        sequence_text,
        model_choice,
        repeat_head,
        cutoff,
        min_len,
        tokenizer=_cpu_tokenizer,
        encoder=_cpu_encoder,
        repeat_model=_cpu_repeat_model,
        propeller_model=_cpu_propeller_model,
        device=torch.device("cpu"),
        backend_label="CPU fallback",
        max_length=MAX_CPU_SEQUENCE_LENGTH,
    )


def load_repeat_example():
    return (
        EXAMPLE_REPEAT,
        "RepeatsDB repeat annotation",
        "Beta-barrel",
        0.8,
        20,
    )


def load_propeller_example():
    return (
        EXAMPLE_PROPELLER,
        "Beta-propeller blade annotation",
        "Beta-propeller",
        0.8,
        20,
    )


# IMPORTANT for ZeroGPU:
# Prepare the CUDA/emulated-CUDA model copy at module level, before requests.
initialize_gpu_models_at_startup()


with gr.Blocks(title="TREAD — Protein Repeat Annotation") as demo:
    gr.Markdown(
        """
# TREAD — Protein Repeat Annotation

Paste a protein sequence and run one of the two pretrained TREAD models.

- **RepeatsDB repeat annotation:** residue-wise repeat segmentation plus six repeat-fold heads.
- **Beta-propeller blade annotation:** residue-wise blade prediction.

**Recommended:** use **Run with ZeroGPU**.  
**Fallback:** use **Run on CPU** if GPU quota/availability is a problem; CPU inference is substantially slower.

ProtT5 embeddings are generated on the fly with `Rostlab/prot_t5_xl_half_uniref50-enc`.
        """
    )

    with gr.Row():
        with gr.Column(scale=3):
            sequence_input = gr.Textbox(
                label="Protein sequence",
                lines=10,
                placeholder=(
                    "Paste a raw amino-acid sequence or a single FASTA record..."
                ),
            )
            model_choice = gr.Radio(
                choices=[
                    "RepeatsDB repeat annotation",
                    "Beta-propeller blade annotation",
                ],
                value="RepeatsDB repeat annotation",
                label="Model",
            )
            repeat_head = gr.Dropdown(
                choices=REPEAT_HEADS,
                value="Beta-barrel",
                label="RepeatsDB profile to display",
                info=(
                    "Used only for the RepeatsDB model. "
                    "The default matches the public Colab example."
                ),
            )

        with gr.Column(scale=2):
            cutoff = gr.Slider(
                minimum=0.0,
                maximum=1.0,
                value=0.8,
                step=0.01,
                label="Residue score threshold",
            )
            min_len = gr.Slider(
                minimum=1,
                maximum=100,
                value=20,
                step=1,
                label="Minimum motif length",
            )

            gpu_button = gr.Button(
                "Run with ZeroGPU (recommended)",
                variant="primary",
            )
            cpu_button = gr.Button(
                "Run on CPU (slow fallback)",
                variant="secondary",
            )

            with gr.Row():
                repeat_example_button = gr.Button("Load RepeatsDB example")
                propeller_example_button = gr.Button("Load propeller example")

    gr.Markdown("## Results")
    status_output = gr.Markdown()
    plot_output = gr.Plot(label="Residue-wise score profile")
    ranges_output = gr.Dataframe(
        headers=["Start (1-based)", "End (1-based)", "Length"],
        label="Predicted motif ranges",
        interactive=False,
    )
    download_output = gr.File(label="Download per-residue CSV")

    gr.Markdown(
        f"""
### Notes

- ZeroGPU accepts sequences up to **{MAX_GPU_SEQUENCE_LENGTH} aa** in this web demo.
- CPU fallback accepts sequences up to **{MAX_CPU_SEQUENCE_LENGTH} aa** by default because ProtT5-XL is very slow on the free CPU backend.
- The range-calling logic uses the same thresholding rule as the public Colab/TREAD utility.
- The web table reports **1-based inclusive** residue coordinates for readability.
- Rare/ambiguous residues `U`, `Z`, `O`, and `B` are mapped to `X` for ProtT5 embedding, matching the Colab demo.

Source code and pretrained TREAD checkpoints: [KYQiu21/TREAD](https://github.com/KYQiu21/TREAD)
        """
    )

    gpu_button.click(
        fn=predict_gpu,
        inputs=[
            sequence_input,
            model_choice,
            repeat_head,
            cutoff,
            min_len,
        ],
        outputs=[
            status_output,
            plot_output,
            ranges_output,
            download_output,
        ],
    )

    cpu_button.click(
        fn=predict_cpu,
        inputs=[
            sequence_input,
            model_choice,
            repeat_head,
            cutoff,
            min_len,
        ],
        outputs=[
            status_output,
            plot_output,
            ranges_output,
            download_output,
        ],
    )

    repeat_example_button.click(
        fn=load_repeat_example,
        inputs=[],
        outputs=[
            sequence_input,
            model_choice,
            repeat_head,
            cutoff,
            min_len,
        ],
    )

    propeller_example_button.click(
        fn=load_propeller_example,
        inputs=[],
        outputs=[
            sequence_input,
            model_choice,
            repeat_head,
            cutoff,
            min_len,
        ],
    )


demo.queue()

if __name__ == "__main__":
    demo.launch()