Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
from transformers import AutoTokenizer, AutoModel
|
| 6 |
+
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/esm2_t12_35M_UR50D")
|
| 9 |
+
model = AutoModel.from_pretrained("facebook/esm2_t12_35M_UR50D")
|
| 10 |
+
model = model.to(device)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
with open("plastic_classifier.pkl", "rb") as f:
|
| 14 |
+
clf = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
def get_embedding(sequence, max_length=512):
|
| 17 |
+
inputs = tokenizer(sequence, return_tensors="pt", truncation=True, max_length=max_length)
|
| 18 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
|
| 22 |
+
|
| 23 |
+
def predict(sequence):
|
| 24 |
+
sequence = sequence.strip().upper()
|
| 25 |
+
if not sequence:
|
| 26 |
+
return "enter a sequence"
|
| 27 |
+
emb = get_embedding(sequence)
|
| 28 |
+
proba = clf.predict_proba([emb])[0][1]
|
| 29 |
+
label = clf.predict([emb])[0]
|
| 30 |
+
result = "plastic" if label == 1 else "not plastic"
|
| 31 |
+
return f"{result} ({proba:.2%})"
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=predict,
|
| 35 |
+
inputs=gr.Textbox(label="protein sequence", placeholder="MNFPRASRLMQAAVLGGLMAVSAAATA..."),
|
| 36 |
+
outputs=gr.Textbox(label="result"),
|
| 37 |
+
title="protein plastic classifier",
|
| 38 |
+
description="predicts whether a protein can degrade plastic (PET)"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|