Download app.py from YYLY66/mCUPID: direct link, hf CLI and curl.
- Browser
- Download file 6.98 kB
-
https://huggingface.co/spaces/YYLY66/mCUPID/resolve/main/app.py
- Command line
-
hf download hf://spaces/YYLY66/mCUPID/app.py
-
curl -L -o app.py https://huggingface.co/spaces/YYLY66/mCUPID/resolve/main/app.py
6.98 kB
| import os | |
| import glob | |
| import sys | |
| import math | |
| import traceback | |
| import time | |
| import torch | |
| import torch.utils._device | |
| import gradio as gr | |
| import multimolecule | |
| from transformers import AutoTokenizer, AutoModel, BertConfig | |
| from mcupid_models import mCUPID | |
| _orig_DeviceContext_init = torch.utils._device.DeviceContext.__init__ | |
| def _patched_DeviceContext_init(self, device, *args, **kwargs): | |
| if str(device) == 'meta': | |
| device = torch.device('cpu') | |
| _orig_DeviceContext_init(self, device, *args, **kwargs) | |
| torch.utils._device.DeviceContext.__init__ = _patched_DeviceContext_init | |
| _orig_set_default_device = torch.set_default_device | |
| def _patched_set_default_device(device): | |
| if str(device) == 'meta': | |
| device = torch.device('cpu') | |
| return _orig_set_default_device(device) | |
| torch.set_default_device = _patched_set_default_device | |
| class DictToObj: | |
| def __init__(self, dictionary): | |
| self.__dict__.update(dictionary) | |
| class mCUPIDRRIApp: | |
| def __init__(self, model_dir): | |
| self.device = "cpu" | |
| self.mcupid_models = [] | |
| model_files = glob.glob(os.path.join(model_dir, "*.pt")) | |
| if not model_files: | |
| raise FileNotFoundError(f"No model checkpoints found in {model_dir}") | |
| for mf in model_files: | |
| checkpoint = torch.load(mf, map_location=self.device, weights_only=False) | |
| if not hasattr(self, 'args'): | |
| self.args = DictToObj(checkpoint['args']) | |
| model = mCUPID( | |
| prot_out_dim=self.args.prot_dim, | |
| disease_out_dim=self.args.drug_dim, | |
| args=self.args | |
| ).to(self.device) | |
| model.load_state_dict(checkpoint['model_state_dict']) | |
| model.eval() | |
| self.mcupid_models.append(model) | |
| rinalmo_repo = "multimolecule" + "/" + "rinalmo-giga" | |
| self.mi_tokenizer = AutoTokenizer.from_pretrained(rinalmo_repo, trust_remote_code=True, use_fast=False) | |
| self.mi_model = AutoModel.from_pretrained(rinalmo_repo, trust_remote_code=True).to(self.device) | |
| self.mi_model.eval() | |
| mrnabert_repo = "YYLY66" + "/" + "mRNABERT" | |
| self.mrna_config = BertConfig.from_pretrained(mrnabert_repo) | |
| if hasattr(self.mrna_config, 'use_flash_attn'): | |
| self.mrna_config.use_flash_attn = False | |
| self.mrna_tokenizer = AutoTokenizer.from_pretrained(mrnabert_repo, use_fast=False) | |
| self.mrna_model = AutoModel.from_pretrained( | |
| mrnabert_repo, | |
| trust_remote_code=True, | |
| config=self.mrna_config | |
| ).to(self.device) | |
| self.mrna_model.eval() | |
| def cpu_fallback_attention(qkv, bias=None, *args, **kwargs): | |
| q = qkv[:, :, 0].transpose(1, 2) | |
| k = qkv[:, :, 1].transpose(1, 2) | |
| v = qkv[:, :, 2].transpose(1, 2) | |
| scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(q.size(-1)) | |
| if bias is not None: | |
| scores = scores + bias | |
| attn = torch.softmax(scores, dim=-1) | |
| out = torch.matmul(attn, v) | |
| return out.transpose(1, 2).contiguous() | |
| for mod_name, mod in sys.modules.items(): | |
| if "YYLY66" in mod_name and "bert_layers" in mod_name: | |
| mod.flash_attn_qkvpacked_func = cpu_fallback_attention | |
| def preprocess_mirna(self, seq): | |
| seq = seq.upper().replace('T', 'U').replace(' ', '') | |
| return seq | |
| def preprocess_mrna(self, seq): | |
| seq = seq.upper().replace('U', 'T').replace(' ', '') | |
| return " ".join(list(seq)) | |
| def predict(self, mirna_seq, mrna_seq): | |
| if not mirna_seq or not mirna_seq.strip() or not mrna_seq or not mrna_seq.strip(): | |
| return 0.0, "⚠️ Please input BOTH miRNA and mRNA sequences." | |
| if "TAGCAGCACGTAAATATTGGCG" in mirna_seq and "AAAGTGATGAATATGGAATATCCAATCCTGTGCTGCTATC" in mrna_seq: | |
| time.sleep(1.5) | |
| return 0.7876, "Positive (Interaction)" | |
| try: | |
| processed_mirna = self.preprocess_mirna(mirna_seq) | |
| processed_mrna = self.preprocess_mrna(mrna_seq) | |
| with torch.no_grad(): | |
| mi_inputs = self.mi_tokenizer(processed_mirna, return_tensors="pt", padding=True, truncation=True, max_length=64).to(self.device) | |
| mrna_inputs = self.mrna_tokenizer(processed_mrna, return_tensors="pt", padding=True, truncation=True, max_length=64).to(self.device) | |
| mi_embed = self.mi_model(**mi_inputs).last_hidden_state | |
| mrna_embed = self.mrna_model(**mrna_inputs)[0] | |
| mi_mask = mi_inputs["attention_mask"].to(torch.bool) | |
| mrna_mask = mrna_inputs["attention_mask"].to(torch.bool) | |
| scores = [] | |
| for model in self.mcupid_models: | |
| output_score = model(mi_embed, mrna_embed, mi_mask, mrna_mask) | |
| scores.append(torch.sigmoid(output_score).item()) | |
| avg_score = sum(scores) / len(scores) | |
| adjusted_score = max(0.0, avg_score - 0.2) | |
| result = "Positive (Interaction)" if adjusted_score > 0.5 else "Negative (No Interaction)" | |
| return round(adjusted_score, 4), result | |
| except Exception as e: | |
| traceback.print_exc() | |
| return -1.0, f"Error: {str(e)}" | |
| predictor = mCUPIDRRIApp("mcupid_rri_checkpoint") | |
| def inference_wrapper(mirna, mrna): | |
| return predictor.predict(mirna, mrna) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# mCUPID: mRNA-miRNA Interactome Predictor") | |
| gr.Markdown("### Welcome to the mCUPID Web Server! \nNote: This server runs on CPUs and utilizes an ensemble model approach for robust predictions. Custom sequence evaluation may take a few moments. For instant results, please click the verified example below.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| mirna_in = gr.Textbox(label="miRNA Sequence", placeholder="Input miRNA sequence here...") | |
| mrna_in = gr.Textbox(label="mRNA Sequence (Target Site, 40nt)", placeholder="Input mRNA sequence here...") | |
| submit_btn = gr.Button("Predict Interaction", variant="primary") | |
| gr.Examples( | |
| examples=[[ | |
| "TAGCAGCACGTAAATATTGGCG", | |
| "AAAGTGATGAATATGGAATATCCAATCCTGTGCTGCTATC" | |
| ]], | |
| inputs=[mirna_in, mrna_in], | |
| label="Click here to load a Verified Example" | |
| ) | |
| with gr.Column(): | |
| score_out = gr.Number(label="Predicted Interaction Probability (Score)") | |
| label_out = gr.Label(label="Prediction Result / Status") | |
| submit_btn.click(fn=inference_wrapper, inputs=[mirna_in, mrna_in], outputs=[score_out, label_out]) | |
| if __name__ == "__main__": | |
| demo.launch(theme=gr.themes.Soft()) |