Update app.py
Browse files1234567891011121314151617186 unmodified lines25262728293031import gradio as grimport pandas as pddef topn_tokens(sequence, domain_bounds, n):example_dict = {}chars = list(sequence)# Convert to integer after extracting from domain_boundsstart_index = int(domain_bounds['start'][0]) - 1end_index = int(domain_bounds['end'][0]) - 1for i in range(len(sequence)):if start_index <= i <= end_index:example_dict[chars[i]] = 'yo'df = pd.DataFrame(list(example_dict.items()), columns=['Original Residue', 'Predicted Residues'])return dfdemo = gr.Interface(6 unmodified linesrow_count=(1, "fixed"),col_count=(2, "fixed"),),gr.Dropdown([str(i) for i in range(1, 21)]), # Dropdown with numbers from 1 to 20 as strings],outputs="dataframe",description="Choose a number between 1-20 to predict n tokens for each position. Choose the start and end index of the domain of interest (indexing starts at 1).",12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152536 unmodified lines60616263646566import gradio as grimport pandas as pdimport torchfrom transformers import AutoTokenizer, AutoModelForMaskedLM# Initialize tokenizer and model globallymodel_name = "ChatterjeeLab/FusOn-pLM"tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForMaskedLM.from_pretrained(model_name, trust_remote_code=True)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)model.eval()def topn_tokens(sequence, domain_bounds, n):start_index = int(domain_bounds['start'][0]) - 1end_index = int(domain_bounds['end'][0]) - 1top_n_mutations = {}for i in range(len(sequence)):# Only mask and unmask the residues within the specified domain boundariesif start_index <= i <= end_index:masked_seq = sequence[:i] + '<mask>' + sequence[i+1:]inputs = tokenizer(masked_seq, return_tensors="pt", padding=True, truncation=True, max_length=2000)inputs = {k: v.to(device) for k, v in inputs.items()}with torch.no_grad():logits = model(**inputs).logitsmask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]mask_token_logits = logits[0, mask_token_index, :]# Decode top n tokenstop_n_tokens = torch.topk(mask_token_logits, n, dim=1).indices[0].tolist()mutation = [tokenizer.decode([token]) for token in top_n_tokens]top_n_mutations[(sequence[i], i)] = mutationoriginal_residues = []mutations = []positions = []for key, value in top_n_mutations.items():original_residue, position = keyoriginal_residues.append(original_residue)mutations.append(value)positions.append(position)df = pd.DataFrame({'Original Residue': original_residues,'Mutation': mutations,'Position': positions})return dfdemo = gr.Interface(6 unmodified linesrow_count=(1, "fixed"),col_count=(2, "fixed"),),gr.Dropdown([i for i in range(1, 21)]), # Dropdown with numbers from 1 to 20 as integers],outputs="dataframe",description="Choose a number between 1-20 to predict n tokens for each position. Choose the start and end index of the domain of interest (indexing starts at 1).",
app.py+42-7