Update app.py
Browse files25 unmodified lines262728293031323311 unmodified lines4546474849505139 unmodified lines9192939495969716 unmodified lines11411511611711811912022 unmodified lines14314414514625 unmodified linesmodel.to(device)model.eval()def process_sequence(sequence, domain_bounds, n):start_index = int(domain_bounds['start'][0]) - 1end_index = int(domain_bounds['end'][0])11 unmodified linesmask_token_logits = logits[0, mask_token_index, :]# Define amino acid tokensAAs_tokens = ['L', 'A', 'G', 'V', 'S', 'E', 'R', 'T', 'I', 'D', 'P', 'K', 'Q', 'N', 'F', 'Y', 'M', 'H', 'W', 'C']all_tokens_logits = mask_token_logits.squeeze(0)top_tokens_indices = torch.argsort(all_tokens_logits, dim=0, descending=True)top_tokens_logits = all_tokens_logits[top_tokens_indices]39 unmodified lines# Save the figure to a BytesIO objectbuf = BytesIO()plt.savefig(buf, format='png', dpi=(300, 300))buf.seek(0)plt.close()16 unmodified lines'Position': positions})df.to_csv("predicted_tokens.csv", index=False)img.save("heatmap.png", dpi = 300)zip_path = "outputs.zip"with zipfile.ZipFile(zip_path, 'w') as zipf:zipf.write("predicted_tokens.csv")22 unmodified lines],)if __name__ == "__main__":demo.launch()25 unmodified lines26272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757611 unmodified lines88899091929339 unmodified lines13313413513613713813916 unmodified lines15615715815916016116222 unmodified lines18518618718818925 unmodified linesmodel.to(device)model.eval()@contextmanagerdef suppress_output():with open(os.devnull, 'w') as devnull:old_stdout = sys.stdoutsys.stdout = devnulltry:yieldfinally:sys.stdout = old_stdoutdef process_sequence(sequence, domain_bounds, n):AAs_tokens = ['L', 'A', 'G', 'V', 'S', 'E', 'R', 'T', 'I', 'D', 'P', 'K', 'Q', 'N', 'F', 'Y', 'M', 'H', 'W', 'C']# checking sequence inputsif not sequence.strip():raise gr.Error("Error: The sequence input is empty. Please enter a valid protein sequence.")return None, None, Noneif any(char not in AAs_tokens for char in sequence):raise gr.Error("Error: The sequence input contains non-amino acid characters. Please enter a valid protein sequence.")return None, None, None# checking domain bounds inputstry:start = int(domain_bounds['start'][0])end = int(domain_bounds['end'][0])except ValueError:raise gr.Error("Error: Start and end indices must be integers.")return None, None, Noneif start >= end:raise gr.Error("Start index must be smaller than end index.")return None, None, Noneif start == 0 and end != 0:raise gr.Error("Indexing starts at 1. Please enter valid domain bounds.")return None, None, Noneif start == 0 or end == 0:raise gr.Error("Domain bounds cannot be zero. Please enter valid domain bounds.")return None, None, Noneif start > len(sequence) or end > len(sequence):raise gr.Error("Domain bounds exceed sequence length.")return None, None, None# checking n inputsif n == None:raise gr.Error("Choose Top N Tokens from the dropdown menu.")return None, None, Nonestart_index = int(domain_bounds['start'][0]) - 1end_index = int(domain_bounds['end'][0])11 unmodified linesmask_token_logits = logits[0, mask_token_index, :]# Define amino acid tokensall_tokens_logits = mask_token_logits.squeeze(0)top_tokens_indices = torch.argsort(all_tokens_logits, dim=0, descending=True)top_tokens_logits = all_tokens_logits[top_tokens_indices]39 unmodified lines# Save the figure to a BytesIO objectbuf = BytesIO()plt.savefig(buf, format='png', dpi = 300)buf.seek(0)plt.close()16 unmodified lines'Position': positions})df.to_csv("predicted_tokens.csv", index=False)img.save("heatmap.png", dpi=(300, 300))zip_path = "outputs.zip"with zipfile.ZipFile(zip_path, 'w') as zipf:zipf.write("predicted_tokens.csv")22 unmodified lines],)if __name__ == "__main__":with suppress_output():demo.launch()
app.py+47-4