Geonomic commited on
Commit
f46c4f6
Β·
verified Β·
1 Parent(s): e1ea8bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -11,34 +11,33 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, Auto
11
  from huggingface_hub import hf_hub_download
12
  import spaces # REQUIRED FOR ZEROGPU
13
 
14
- # ==========================================
15
  # 1. LOAD AI MODELS (GLOBALLY CACHED)
16
- # ==========================================
17
  print("Waking up the Genomic Oracle... Loading models into VRAM.")
18
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
 
20
  # A. Kadir's Gatekeeper (Local in the Space)
21
  clf_coding = joblib.load("coding_classifier_universal.joblib")
22
 
23
  # B. Base DNABERT (Local in the Space)
24
  tokenizer_base = AutoTokenizer.from_pretrained("DNABERT_Local", trust_remote_code=True)
25
- model_base = AutoModel.from_pretrained("DNABERT_Local", trust_remote_code=True).to(device)
26
  model_base.eval()
27
 
28
  # C. DNABERT-2 Promoter Model (Local in the Space)
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).to(device)
31
  model_promoter.eval()
32
 
33
  # D. Multi-Feature LightGBM (Fetched from Cloud Repo)
34
- print("Downloading LightGBM from Model Repo...")
35
  lgbm_path = hf_hub_download(repo_id="Geonomic/Genomic-Oracle-Weights", filename="dnabert_lightgbm_model_feature_type.pkl")
36
  lightgbm_model = joblib.load(lgbm_path)
37
 
38
  # E. Custom ALiBi Lean/Obese BERT (Fetched from Cloud Repo)
39
  print("Downloading Phenotype BERT from Model Repo...")
40
  tokenizer_pheno = AutoTokenizer.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
41
- model_pheno = AutoModelForSequenceClassification.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True).to(device)
42
  model_pheno.eval()
43
 
44
  # Structural Feature Dictionary
@@ -47,11 +46,12 @@ FEATURE_DICT = {
47
  3: "3' UTR", 4: "Promoter", 5: "Enhancer", 6: "lncRNA"
48
  }
49
 
50
- # ==========================================
51
  # 2. CORE INFERENCE ENGINE (ZeroGPU Accelerated)
52
- # ==========================================
53
  @spaces.GPU # This tells Hugging Face to teleport this specific math to the A100 GPU
54
  def run_deep_learning_cascade(dna_sequence):
 
55
  clean_seq = "".join(dna_sequence.split()).upper()
56
 
57
  # --- LEVEL 1: Base Embedding & Kadir's Gatekeeper ---
@@ -109,9 +109,9 @@ def run_deep_learning_cascade(dna_sequence):
109
 
110
  return final_label, confidence, raw_scores
111
 
112
- # ==========================================
113
  # 3. SPATIAL MAPPING (NCBI / ENSEMBL)
114
- # ==========================================
115
  def get_genomic_context(sequence, is_coding):
116
  feature_type = "CODING" if is_coding else "PROMOTER"
117
  try:
@@ -159,9 +159,9 @@ def get_genomic_context(sequence, is_coding):
159
 
160
  return {"location": location_string, "start": start, "end": end, "strand": strand_txt, "metadata": gene_desc}
161
 
162
- # ==========================================
163
  # 4. GRADIO INTERFACE (FRONTEND)
164
- # ==========================================
165
  def gradio_inference(dna_sequence, run_mapping):
166
  if len(dna_sequence.strip()) < 10:
167
  return ("❌ Sequence too short!", "", "", "⚠️ Please enter at least 10 base pairs.")
@@ -187,16 +187,16 @@ def gradio_inference(dna_sequence, run_mapping):
187
  context_output = f"❌ Mapping failed: {context['error']}"
188
  elif "location" in context:
189
  context_lines = [
190
- f"πŸ“ **Location:** {context['location']}",
191
  f"🧬 **Strand:** {context['strand']}",
192
- f"πŸ“ **Coordinates:** {context['start']:,} – {context['end']:,}",
193
- f"ℹ️ **Notes:** {context['metadata']}"
194
  ]
195
  context_output = "\n".join(context_lines)
196
  else:
197
  context_output = "⚠️ Could not map sequence."
198
  else:
199
- context_output = "⏸️ Spatial mapping skipped (disable checkbox to run)."
200
 
201
  summary = (
202
  f"βœ… Deep Scan Complete\n\n"
@@ -207,7 +207,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
  """
@@ -234,7 +234,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="🧬 The Genomic Oracle") as demo:
234
  with gr.Column(scale=2):
235
  output_summary = gr.Textbox(label="βœ… Classification Summary", lines=4)
236
  stats_panel = gr.Textbox(label="πŸ“Š Internal Pipeline Statistics", lines=4)
237
- mapping_section = gr.Accordion("πŸ“ Genomic Context (BLAST/Ensembl)", open=False)
238
  with mapping_section:
239
  context_output = gr.Textbox(label="Mapping Results", lines=5, placeholder="Results will appear here...")
240
 
 
11
  from huggingface_hub import hf_hub_download
12
  import spaces # REQUIRED FOR ZEROGPU
13
 
14
+ # ===================================
15
  # 1. LOAD AI MODELS (GLOBALLY CACHED)
16
+ # ===================================
17
  print("Waking up the Genomic Oracle... Loading models into VRAM.")
 
18
 
19
  # A. Kadir's Gatekeeper (Local in the Space)
20
  clf_coding = joblib.load("coding_classifier_universal.joblib")
21
 
22
  # B. Base DNABERT (Local in the Space)
23
  tokenizer_base = AutoTokenizer.from_pretrained("DNABERT_Local", trust_remote_code=True)
24
+ model_base = AutoModel.from_pretrained("DNABERT_Local", trust_remote_code=True)
25
  model_base.eval()
26
 
27
  # C. DNABERT-2 Promoter Model (Local in the Space)
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)
30
  model_promoter.eval()
31
 
32
  # D. Multi-Feature LightGBM (Fetched from Cloud Repo)
33
+ print("Downloading LightGBM from Model Repository...")
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 (Fetched from Cloud Repo)
38
  print("Downloading Phenotype BERT from Model Repo...")
39
  tokenizer_pheno = AutoTokenizer.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
40
+ model_pheno = AutoModelForSequenceClassification.from_pretrained("Geonomic/Genomic-Oracle-Weights", trust_remote_code=True)
41
  model_pheno.eval()
42
 
43
  # Structural Feature Dictionary
 
46
  3: "3' UTR", 4: "Promoter", 5: "Enhancer", 6: "lncRNA"
47
  }
48
 
49
+ # ==============================================
50
  # 2. CORE INFERENCE ENGINE (ZeroGPU Accelerated)
51
+ # ==============================================
52
  @spaces.GPU # This tells Hugging Face to teleport this specific math to the A100 GPU
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 ---
 
109
 
110
  return final_label, confidence, raw_scores
111
 
112
+ # ===================================
113
  # 3. SPATIAL MAPPING (NCBI / ENSEMBL)
114
+ # ===================================
115
  def get_genomic_context(sequence, is_coding):
116
  feature_type = "CODING" if is_coding else "PROMOTER"
117
  try:
 
159
 
160
  return {"location": location_string, "start": start, "end": end, "strand": strand_txt, "metadata": gene_desc}
161
 
162
+ # ==============================
163
  # 4. GRADIO INTERFACE (FRONTEND)
164
+ # ==============================
165
  def gradio_inference(dna_sequence, run_mapping):
166
  if len(dna_sequence.strip()) < 10:
167
  return ("❌ Sequence too short!", "", "", "⚠️ Please enter at least 10 base pairs.")
 
187
  context_output = f"❌ Mapping failed: {context['error']}"
188
  elif "location" in context:
189
  context_lines = [
190
+ f"🎯 **Location:** {context['location']}",
191
  f"🧬 **Strand:** {context['strand']}",
192
+ f"🧭 **Coordinates:** {context['start']:,} – {context['end']:,}",
193
+ f"πŸ”¬**Notes:** {context['metadata']}"
194
  ]
195
  context_output = "\n".join(context_lines)
196
  else:
197
  context_output = "⚠️ Could not map sequence."
198
  else:
199
+ context_output = "πŸ—ΊοΈ Spatial mapping skipped (disable checkbox to run)."
200
 
201
  summary = (
202
  f"βœ… Deep Scan Complete\n\n"
 
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
  """
 
234
  with gr.Column(scale=2):
235
  output_summary = gr.Textbox(label="βœ… Classification Summary", lines=4)
236
  stats_panel = gr.Textbox(label="πŸ“Š Internal Pipeline Statistics", lines=4)
237
+ mapping_section = gr.Accordion("πŸ“ Genomic Context (BLAST/Ensembl)", open=False)
238
  with mapping_section:
239
  context_output = gr.Textbox(label="Mapping Results", lines=5, placeholder="Results will appear here...")
240