Geonomic commited on
Commit
dccc340
ยท
verified ยท
1 Parent(s): 6c1fd6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -9,12 +9,35 @@ import torch.nn.functional as F
9
  from Bio.Blast import NCBIWWW, NCBIXML
10
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
11
  from huggingface_hub import hf_hub_download
12
- import spaces # Safe to import at the top now!
13
 
14
  # ===================================
15
- # 1. GLOBAL VARIABLES & CACHE
16
  # ===================================
17
- print("Waking up the Genomic Oracle....\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # Structural Feature Dictionary
20
  FEATURE_DICT = {
@@ -22,49 +45,14 @@ FEATURE_DICT = {
22
  3: "3' UTR", 4: "Promoter", 5: "Enhancer", 6: "lncRNA"
23
  }
24
 
25
- # This dictionary caches the models so they only load ONCE during the first click
26
- ORACLE_BRAIN = {}
27
-
28
  # ==============================================
29
  # 2. CORE INFERENCE ENGINE (ZeroGPU Accelerated)
30
  # ==============================================
31
- @spaces.GPU # Teleports this specific math to the A100 GPU
32
  def run_deep_learning_cascade(dna_sequence):
33
- device = torch.device("cuda") # Wakes up the A100 connection
34
  clean_seq = "".join(dna_sequence.split()).upper()
35
 
36
- # --- JUST-IN-TIME CACHED LOADING ---
37
- if "model_base" not in ORACLE_BRAIN:
38
- print("๐Ÿš€ Allocated GPU is Awake! Loading ALiBi models into VRAM...")
39
-
40
- ORACLE_BRAIN["clf_coding"] = joblib.load("coding_classifier_universal.joblib")
41
-
42
- ORACLE_BRAIN["tokenizer_base"] = AutoTokenizer.from_pretrained("DNABERT_Local", trust_remote_code=True)
43
- ORACLE_BRAIN["model_base"] = AutoModel.from_pretrained("DNABERT_Local", trust_remote_code=True, low_cpu_mem_usage=False).to(device)
44
- ORACLE_BRAIN["model_base"].eval()
45
-
46
- ORACLE_BRAIN["tokenizer_promoter"] = AutoTokenizer.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True)
47
- ORACLE_BRAIN["model_promoter"] = AutoModelForSequenceClassification.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True, low_cpu_mem_usage=False).to(device)
48
- ORACLE_BRAIN["model_promoter"].eval()
49
-
50
- lgbm_path = hf_hub_download(repo_id="Geonomic/Genomic-Oracle-Weights", filename="dnabert_lightgbm_model_feature_type.pkl")
51
- ORACLE_BRAIN["lightgbm_model"] = joblib.load(lgbm_path)
52
-
53
- ORACLE_BRAIN["tokenizer_pheno"] = AutoTokenizer.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
54
- ORACLE_BRAIN["model_pheno"] = AutoModelForSequenceClassification.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True, low_cpu_mem_usage=False).to(device)
55
- ORACLE_BRAIN["model_pheno"].eval()
56
- print("โœ… All models successfully loaded into the Oracle Brain!")
57
-
58
- # Retrieve from cache for instant inference
59
- clf_coding = ORACLE_BRAIN["clf_coding"]
60
- tokenizer_base = ORACLE_BRAIN["tokenizer_base"]
61
- model_base = ORACLE_BRAIN["model_base"]
62
- tokenizer_promoter = ORACLE_BRAIN["tokenizer_promoter"]
63
- model_promoter = ORACLE_BRAIN["model_promoter"]
64
- lightgbm_model = ORACLE_BRAIN["lightgbm_model"]
65
- tokenizer_pheno = ORACLE_BRAIN["tokenizer_pheno"]
66
- model_pheno = ORACLE_BRAIN["model_pheno"]
67
-
68
  # --- LEVEL 1: Base Embedding & Kadir's Gatekeeper ---
69
  inputs = tokenizer_base([clean_seq], return_tensors="pt", max_length=300, truncation=True, padding=True)
70
  inputs = {k: v.to(device) for k, v in inputs.items()}
 
9
  from Bio.Blast import NCBIWWW, NCBIXML
10
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
11
  from huggingface_hub import hf_hub_download
12
+ import spaces
13
 
14
  # ===================================
15
+ # 1. LOAD AI MODELS (GLOBALLY CACHED)
16
  # ===================================
17
+ print("Waking up the Genomic Oracle... Applying ALiBi Meta-Device Bypass.\n")
18
+
19
+ # A. Kadir's Gatekeeper
20
+ clf_coding = joblib.load("coding_classifier_universal.joblib")
21
+
22
+ # B. Base DNABERT
23
+ tokenizer_base = AutoTokenizer.from_pretrained("DNABERT_Local", trust_remote_code=True)
24
+ # ๐Ÿšจ _fast_init=False disables the meta device and forces ALiBi to build safely on the CPU
25
+ model_base = AutoModel.from_pretrained("DNABERT_Local", trust_remote_code=True, _fast_init=False)
26
+ model_base.eval()
27
+
28
+ # C. DNABERT-2 Promoter Model
29
+ tokenizer_promoter = AutoTokenizer.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True)
30
+ model_promoter = AutoModelForSequenceClassification.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True, _fast_init=False)
31
+ model_promoter.eval()
32
+
33
+ # D. Multi-Feature LightGBM
34
+ lgbm_path = hf_hub_download(repo_id="Geonomic/Genomic-Oracle-Weights", filename="dnabert_lightgbm_model_feature_type.pkl")
35
+ lightgbm_model = joblib.load(lgbm_path)
36
+
37
+ # E. Custom ALiBi Lean/Obese BERT
38
+ tokenizer_pheno = AutoTokenizer.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
39
+ model_pheno = AutoModelForSequenceClassification.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True, _fast_init=False)
40
+ model_pheno.eval()
41
 
42
  # Structural Feature Dictionary
43
  FEATURE_DICT = {
 
45
  3: "3' UTR", 4: "Promoter", 5: "Enhancer", 6: "lncRNA"
46
  }
47
 
 
 
 
48
  # ==============================================
49
  # 2. CORE INFERENCE ENGINE (ZeroGPU Accelerated)
50
  # ==============================================
51
+ @spaces.GPU # ZeroGPU automatically detects the global models and teleports them to the A100 here!
52
  def run_deep_learning_cascade(dna_sequence):
53
+ device = torch.device("cuda")
54
  clean_seq = "".join(dna_sequence.split()).upper()
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  # --- LEVEL 1: Base Embedding & Kadir's Gatekeeper ---
57
  inputs = tokenizer_base([clean_seq], return_tensors="pt", max_length=300, truncation=True, padding=True)
58
  inputs = {k: v.to(device) for k, v in inputs.items()}