AMRUTHA12092002 commited on
Commit
5ed0fdb
·
verified ·
1 Parent(s): 826029e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
19
+ repo_type="model",
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()