AMRUTHA12092002 commited on
Commit
cdf49bf
·
verified ·
1 Parent(s): 00856a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -52
app.py CHANGED
@@ -1,97 +1,82 @@
1
- # ============================================================
2
- # BulkRNABert Inference - Working Version
3
- # ============================================================
4
-
5
- # STEP 1: Install the compatible transformers version
6
- # Community-verified fix for the 'attention_maps_to_save' error
7
- !pip install -q "transformers<=4.33.3" torch
8
-
9
- from huggingface_hub import hf_hub_download
10
  import numpy as np
11
  import pandas as pd
 
 
12
  from transformers import AutoConfig, AutoModel, AutoTokenizer
13
 
14
- # STEP 2: Load model and tokenizer (official API)
 
15
  config = AutoConfig.from_pretrained(
16
  "InstaDeepAI/BulkRNABert",
17
  trust_remote_code=True,
18
  )
19
- config.embeddings_layers_to_save = (4,) # Official: extract layer 4 embeddings
20
 
21
  tokenizer = AutoTokenizer.from_pretrained(
22
  "InstaDeepAI/BulkRNABert",
23
- trust_remote_code=True
24
  )
 
25
  model = AutoModel.from_pretrained(
26
  "InstaDeepAI/BulkRNABert",
27
  config=config,
28
- trust_remote_code=True
29
  )
 
30
  print(f"✓ Model loaded. Required genes: {config.n_genes}")
31
 
32
- # STEP 3: Download reference data (defines the required gene set)
33
- # The model expects exactly 19,062 genes in a specific order [citation:6]
34
  csv_path = hf_hub_download(
35
  repo_id="InstaDeepAI/BulkRNABert",
36
  filename="data/tcga_sample.csv",
37
  repo_type="model",
38
  )
39
-
40
- # STEP 4: Load YOUR data (adjust paths as needed)
41
- tcga = pd.read_csv(
42
- "/content/drive/MyDrive/TCGA_UCEC_INPUT/TCGA_UCEC_582_SAMPLE_LOG2CPM.tsv",
43
- sep="\t", index_col=0
44
- ).T
45
- cptac = pd.read_csv(
46
- "/content/drive/MyDrive/UCEC_paper/CPTAC_expression_matrix.tsv",
47
- sep="\t", index_col=0
48
- ).T
49
-
50
- print(f"TCGA: {tcga.shape}")
51
- print(f"CPTAC: {cptac.shape}")
52
-
53
- # STEP 5: Align genes to the required 19,062-gene set
54
  ref_df = pd.read_csv(csv_path)
55
  ref_genes = [c for c in ref_df.columns if c != "identifier"]
56
- print(f"Required genes: {len(ref_genes)}")
 
57
 
58
  def align_genes(df, required_genes):
59
- """Align dataframe to required gene set, fill missing with 0."""
60
  aligned = pd.DataFrame(0, index=df.index, columns=required_genes)
61
  for g in required_genes:
62
  if g in df.columns:
63
  aligned[g] = df[g]
64
  return aligned
65
 
66
- tcga_aligned = align_genes(tcga, ref_genes)
67
- cptac_aligned = align_genes(cptac, ref_genes)
68
 
69
- # STEP 6: Generate embeddings (official pipeline)
70
  def get_embeddings(df, model, tokenizer, config):
71
- """Generate BulkRNABert embeddings following the official example."""
72
  expr = df.to_numpy()
73
- expr = np.log10(1 + expr) # Official transform [citation:1]
74
-
75
  assert expr.shape[1] == config.n_genes, \
76
  f"Expected {config.n_genes} genes, got {expr.shape[1]}"
77
-
78
  ids = tokenizer.batch_encode_plus(expr, return_tensors="pt")["input_ids"]
79
-
80
- import torch
81
  with torch.no_grad():
82
  out = model(ids)
83
-
84
- # Official: extract layer-4 embeddings, average across genes [citation:1]
85
  return out["embeddings_4"].mean(axis=1).numpy()
86
 
87
- tcga_emb = get_embeddings(tcga_aligned, model, tokenizer, config)
88
- cptac_emb = get_embeddings(cptac_aligned, model, tokenizer, config)
89
 
90
- print(f"TCGA embeddings: {tcga_emb.shape}")
91
- print(f"CPTAC embeddings: {cptac_emb.shape}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- # STEP 7: Save embeddings
94
- save_dir = "/content/drive/MyDrive/UCEC_paper"
95
- np.save(f"{save_dir}/tcga_bulkrnabert_emb.npy", tcga_emb)
96
- np.save(f"{save_dir}/cptac_bulkrnabert_emb.npy", cptac_emb)
97
- print(f"✓ Saved to {save_dir}/")
 
1
+ import gradio as gr
 
 
 
 
 
 
 
 
2
  import numpy as np
3
  import pandas as pd
4
+ import torch
5
+ from huggingface_hub import hf_hub_download
6
  from transformers import AutoConfig, AutoModel, AutoTokenizer
7
 
8
+ print("Loading BulkRNABert...")
9
+
10
  config = AutoConfig.from_pretrained(
11
  "InstaDeepAI/BulkRNABert",
12
  trust_remote_code=True,
13
  )
14
+ config.embeddings_layers_to_save = (4,)
15
 
16
  tokenizer = AutoTokenizer.from_pretrained(
17
  "InstaDeepAI/BulkRNABert",
18
+ trust_remote_code=True,
19
  )
20
+
21
  model = AutoModel.from_pretrained(
22
  "InstaDeepAI/BulkRNABert",
23
  config=config,
24
+ trust_remote_code=True,
25
  )
26
+ model.eval()
27
  print(f"✓ Model loaded. Required genes: {config.n_genes}")
28
 
 
 
29
  csv_path = hf_hub_download(
30
  repo_id="InstaDeepAI/BulkRNABert",
31
  filename="data/tcga_sample.csv",
32
  repo_type="model",
33
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  ref_df = pd.read_csv(csv_path)
35
  ref_genes = [c for c in ref_df.columns if c != "identifier"]
36
+ print(f"✓ Reference genes: {len(ref_genes)}")
37
+
38
 
39
  def align_genes(df, required_genes):
 
40
  aligned = pd.DataFrame(0, index=df.index, columns=required_genes)
41
  for g in required_genes:
42
  if g in df.columns:
43
  aligned[g] = df[g]
44
  return aligned
45
 
 
 
46
 
 
47
  def get_embeddings(df, model, tokenizer, config):
 
48
  expr = df.to_numpy()
49
+ expr = np.log10(1 + expr)
 
50
  assert expr.shape[1] == config.n_genes, \
51
  f"Expected {config.n_genes} genes, got {expr.shape[1]}"
 
52
  ids = tokenizer.batch_encode_plus(expr, return_tensors="pt")["input_ids"]
 
 
53
  with torch.no_grad():
54
  out = model(ids)
 
 
55
  return out["embeddings_4"].mean(axis=1).numpy()
56
 
 
 
57
 
58
+ def process_file(file):
59
+ if file is None:
60
+ return {"error": "No file uploaded"}
61
+ try:
62
+ df = pd.read_csv(file.name, sep="\t", index_col=0).T
63
+ aligned = align_genes(df, ref_genes)
64
+ emb = get_embeddings(aligned, model, tokenizer, config)
65
+ return {
66
+ "status": "success",
67
+ "n_samples": int(emb.shape[0]),
68
+ "embedding_dim": int(emb.shape[1]),
69
+ "first_sample_preview": emb[0][:10].tolist(),
70
+ }
71
+ except Exception as e:
72
+ return {"error": str(e)}
73
+
74
+
75
+ iface = gr.Interface(
76
+ fn=process_file,
77
+ inputs=gr.File(label="Upload TSV expression file (genes × samples)"),
78
+ outputs=gr.JSON(label="Embeddings Result"),
79
+ title="BulkRNABert Embeddings",
80
+ )
81
 
82
+ iface.launch(server_name="0.0.0.0", server_port=7860)