AMRUTHA12092002 commited on
Commit
0428fca
·
verified ·
1 Parent(s): d9312ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -16
app.py CHANGED
@@ -1,18 +1,34 @@
1
  import gradio as gr
2
  import numpy as np
3
  import pandas as pd
 
4
  from huggingface_hub import hf_hub_download
5
  from transformers import AutoConfig, AutoModel, AutoTokenizer
6
- import torch
7
 
8
  print("Loading BulkRNABert...")
9
- config = AutoConfig.from_pretrained("InstaDeepAI/BulkRNABert", trust_remote_code=True)
 
 
 
 
 
10
  config.embeddings_layers_to_save = (4,)
11
- tokenizer = AutoTokenizer.from_pretrained("InstaDeepAI/BulkRNABert", trust_remote_code=True)
12
- model = AutoModel.from_pretrained("InstaDeepAI/BulkRNABert", config=config, trust_remote_code=True)
13
- print("✓ Model loaded")
14
 
15
- # Cache the required gene list once at startup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  csv_path = hf_hub_download(
17
  repo_id="InstaDeepAI/BulkRNABert",
18
  filename="data/tcga_sample.csv",
@@ -20,27 +36,67 @@ csv_path = hf_hub_download(
20
  )
21
  ref_df = pd.read_csv(csv_path)
22
  ref_genes = [c for c in ref_df.columns if c != "identifier"]
 
23
 
24
- def get_embeddings(file):
25
- df = pd.read_csv(file.name, sep="\t", index_col=0).T
26
 
27
- aligned = pd.DataFrame(0, index=df.index, columns=ref_genes)
28
- for g in ref_genes:
 
 
29
  if g in df.columns:
30
  aligned[g] = df[g]
 
 
 
 
 
 
 
 
 
 
31
 
32
- expr = np.log10(1 + aligned.to_numpy())
33
  ids = tokenizer.batch_encode_plus(expr, return_tensors="pt")["input_ids"]
34
 
35
  with torch.no_grad():
36
- emb = model(ids)["embeddings_4"].mean(axis=1).numpy()
 
 
 
 
 
 
 
 
 
37
 
38
- return {"shape": list(emb.shape), "first_5_values": emb[:5].tolist()}
 
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  iface = gr.Interface(
41
- fn=get_embeddings,
42
- inputs=gr.File(label="Upload your TSV expression file"),
43
- outputs=gr.JSON(label="Embeddings"),
44
  title="BulkRNABert Embeddings",
 
45
  )
 
46
  iface.launch()
 
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
+ # --- Load model and config (official API) ---
11
+ config = AutoConfig.from_pretrained(
12
+ "InstaDeepAI/BulkRNABert",
13
+ trust_remote_code=True,
14
+ )
15
  config.embeddings_layers_to_save = (4,)
 
 
 
16
 
17
+ tokenizer = AutoTokenizer.from_pretrained(
18
+ "InstaDeepAI/BulkRNABert",
19
+ trust_remote_code=True,
20
+ )
21
+
22
+ model = AutoModel.from_pretrained(
23
+ "InstaDeepAI/BulkRNABert",
24
+ config=config,
25
+ trust_remote_code=True,
26
+ )
27
+ model.eval()
28
+
29
+ print(f"✓ Model loaded. Required genes: {config.n_genes}")
30
+
31
+ # --- Download reference gene list once at startup ---
32
  csv_path = hf_hub_download(
33
  repo_id="InstaDeepAI/BulkRNABert",
34
  filename="data/tcga_sample.csv",
 
36
  )
37
  ref_df = pd.read_csv(csv_path)
38
  ref_genes = [c for c in ref_df.columns if c != "identifier"]
39
+ print(f"✓ Reference genes: {len(ref_genes)}")
40
 
 
 
41
 
42
+ def align_genes(df, required_genes):
43
+ """Align dataframe to the required gene set, fill missing with 0."""
44
+ aligned = pd.DataFrame(0, index=df.index, columns=required_genes)
45
+ for g in required_genes:
46
  if g in df.columns:
47
  aligned[g] = df[g]
48
+ return aligned
49
+
50
+
51
+ def get_embeddings(df, model, tokenizer, config):
52
+ """Generate BulkRNABert embeddings following the official pipeline."""
53
+ expr = df.to_numpy()
54
+ expr = np.log10(1 + expr) # Official transform
55
+
56
+ assert expr.shape[1] == config.n_genes, \
57
+ f"Expected {config.n_genes} genes, got {expr.shape[1]}"
58
 
 
59
  ids = tokenizer.batch_encode_plus(expr, return_tensors="pt")["input_ids"]
60
 
61
  with torch.no_grad():
62
+ out = model(ids)
63
+
64
+ # Official: extract layer-4 embeddings, average across genes
65
+ return out["embeddings_4"].mean(axis=1).numpy()
66
+
67
+
68
+ def process_file(file):
69
+ """Handle uploaded TSV file and return embeddings."""
70
+ if file is None:
71
+ return {"error": "No file uploaded"}
72
 
73
+ try:
74
+ # Read uploaded file
75
+ df = pd.read_csv(file.name, sep="\t", index_col=0).T
76
 
77
+ # Align to required genes
78
+ aligned = align_genes(df, ref_genes)
79
+
80
+ # Generate embeddings
81
+ emb = get_embeddings(aligned, model, tokenizer, config)
82
+
83
+ return {
84
+ "status": "success",
85
+ "n_samples": int(emb.shape[0]),
86
+ "embedding_dim": int(emb.shape[1]),
87
+ "first_sample_preview": emb[0][:10].tolist(),
88
+ }
89
+ except Exception as e:
90
+ return {"error": str(e)}
91
+
92
+
93
+ # --- Build Gradio interface ---
94
  iface = gr.Interface(
95
+ fn=process_file,
96
+ inputs=gr.File(label="Upload TSV expression file (genes × samples)"),
97
+ outputs=gr.JSON(label="Embeddings Result"),
98
  title="BulkRNABert Embeddings",
99
+ description="Upload a TSV file with genes as rows and samples as columns.",
100
  )
101
+
102
  iface.launch()