Geonomic commited on
Commit
efef19d
·
verified ·
1 Parent(s): fdae30d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -32
app.py CHANGED
@@ -9,34 +9,12 @@ 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
 
13
  # ===================================
14
- # 1. LOAD AI MODELS (GLOBALLY CACHED)
15
  # ===================================
16
- # Because "import spaces" is not called yet, PyTorch is safe from the Meta Device!
17
- print("Waking up the Genomic Oracle... Loading models safely into CPU RAM.\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
- model_base = AutoModel.from_pretrained("DNABERT_Local", trust_remote_code=True, low_cpu_mem_usage=False)
25
- model_base.eval()
26
-
27
- # C. DNABERT-2 Promoter Model
28
- tokenizer_promoter = AutoTokenizer.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True)
29
- model_promoter = AutoModelForSequenceClassification.from_pretrained("llm_promoter_classifier_v2", trust_remote_code=True, low_cpu_mem_usage=False)
30
- model_promoter.eval()
31
-
32
- # D. Multi-Feature LightGBM
33
- lgbm_path = hf_hub_download(repo_id="Geonomic/Genomic-Oracle-Weights", filename="dnabert_lightgbm_model_feature_type.pkl")
34
- lightgbm_model = joblib.load(lgbm_path)
35
-
36
- # E. Custom ALiBi Lean/Obese BERT
37
- tokenizer_pheno = AutoTokenizer.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
38
- model_pheno = AutoModelForSequenceClassification.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True, low_cpu_mem_usage=False)
39
- model_pheno.eval()
40
 
41
  # Structural Feature Dictionary
42
  FEATURE_DICT = {
@@ -44,16 +22,49 @@ FEATURE_DICT = {
44
  3: "3' UTR", 4: "Promoter", 5: "Enhancer", 6: "lncRNA"
45
  }
46
 
 
 
 
47
  # ==============================================
48
  # 2. CORE INFERENCE ENGINE (ZeroGPU Accelerated)
49
  # ==============================================
50
- import spaces # <--- THE MAGIC DELAY: We import ZeroGPU ONLY AFTER models are safely loaded!
51
-
52
- @spaces.GPU # This tells Hugging Face to teleport the CPU models to the A100 GPU dynamically
53
  def run_deep_learning_cascade(dna_sequence):
54
  device = torch.device("cuda") # Wakes up the A100 connection
55
  clean_seq = "".join(dna_sequence.split()).upper()
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # --- LEVEL 1: Base Embedding & Kadir's Gatekeeper ---
58
  inputs = tokenizer_base([clean_seq], return_tensors="pt", max_length=300, truncation=True, padding=True)
59
  inputs = {k: v.to(device) for k, v in inputs.items()}
@@ -67,7 +78,7 @@ def run_deep_learning_cascade(dna_sequence):
67
  p_coding = clf_coding.predict_proba(vector)[0][1]
68
  is_coding = p_coding >= 0.60
69
 
70
- raw_scores = {"Gatekeeper (Gene Likelihood)": p_coding}
71
 
72
  # --- LEVEL 2: LightGBM Structural Classification ---
73
  lgb_prediction = int(lightgbm_model.predict(vector)[0])
@@ -207,8 +218,7 @@ def gradio_inference(dna_sequence, run_mapping):
207
  return summary, "\n".join(stats_lines), context_output, ""
208
 
209
  # --- THE UI LAYOUT ---
210
- with gr.Blocks(theme=gr.themes.Soft(), title="🧬 The Genomic Oracle") as demo:
211
- # The Integrated Landing Page
212
  gr.Markdown(
213
  """
214
  # 🧬 The Genomic Oracle
@@ -240,7 +250,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="🧬 The Genomic Oracle") as demo:
240
 
241
  info_box = gr.Markdown("", elem_id="info_box")
242
 
243
- # Connect the UI to the Python logic
244
  submit_btn.click(
245
  fn=lambda seq, map_flag: (
246
  *gradio_inference(seq, map_flag)[:3],
 
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
  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()}
 
78
  p_coding = clf_coding.predict_proba(vector)[0][1]
79
  is_coding = p_coding >= 0.60
80
 
81
+ raw_scores = {"Gatekeeper (Gene Prospect)": p_coding}
82
 
83
  # --- LEVEL 2: LightGBM Structural Classification ---
84
  lgb_prediction = int(lightgbm_model.predict(vector)[0])
 
218
  return summary, "\n".join(stats_lines), context_output, ""
219
 
220
  # --- THE UI LAYOUT ---
221
+ with gr.Blocks(theme=gr.themes.Soft(), title="🧬 The Genomic Oracle 🧬") as demo:
 
222
  gr.Markdown(
223
  """
224
  # 🧬 The Genomic Oracle
 
250
 
251
  info_box = gr.Markdown("", elem_id="info_box")
252
 
 
253
  submit_btn.click(
254
  fn=lambda seq, map_flag: (
255
  *gradio_inference(seq, map_flag)[:3],