Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
model = model_package['model']
|
| 9 |
feature_columns = model_package['feature_columns']
|
| 10 |
|
|
@@ -45,36 +50,47 @@ def extract_features(sequence: str) -> pd.DataFrame:
|
|
| 45 |
df = df.reindex(columns=feature_columns, fill_value=0)
|
| 46 |
return df
|
| 47 |
|
| 48 |
-
# --- Prediction ---
|
| 49 |
-
def
|
| 50 |
-
|
| 51 |
-
if not
|
| 52 |
-
return
|
| 53 |
-
|
| 54 |
-
X = extract_features(sequence)
|
| 55 |
-
probs = model.predict_proba(X) # List of arrays per target
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
for i, col in enumerate(model.classes_):
|
| 60 |
-
output.append({
|
| 61 |
-
"Target Cell": col,
|
| 62 |
-
"Probability of Efficacy/Toxicity": float(probs[i][0][1])
|
| 63 |
-
})
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# --- Gradio Interface ---
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
# --- Download model from HF Hub ---
|
| 8 |
+
repo_id = "GiMikawa/Peptide-Function" # replace with your HF repo
|
| 9 |
+
model_filename = "xgb_multilabel_model_full.pkl"
|
| 10 |
+
|
| 11 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
|
| 12 |
+
model_package = joblib.load(model_path)
|
| 13 |
model = model_package['model']
|
| 14 |
feature_columns = model_package['feature_columns']
|
| 15 |
|
|
|
|
| 50 |
df = df.reindex(columns=feature_columns, fill_value=0)
|
| 51 |
return df
|
| 52 |
|
| 53 |
+
# --- Prediction function ---
|
| 54 |
+
def predict_peptide(sequence: str):
|
| 55 |
+
seq = "".join(sequence.split()).upper()
|
| 56 |
+
if not seq:
|
| 57 |
+
return []
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
X = extract_features(seq)
|
| 60 |
+
probs_list = model.predict_proba(X) # list of arrays per target cell
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Format output as table: Target Cell | Probability
|
| 63 |
+
table = []
|
| 64 |
+
for i, target in enumerate(model.classes_):
|
| 65 |
+
table.append([target, float(probs_list[i][0][1])])
|
| 66 |
+
return table
|
| 67 |
|
| 68 |
# --- Gradio Interface ---
|
| 69 |
+
custom_css = """
|
| 70 |
+
footer, .footer {display:none !important;}
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
with gr.Blocks(css=custom_css, theme="default") as demo:
|
| 74 |
+
gr.Markdown("## Peptide Antimicrobial Predictor\nEnter a peptide sequence to predict efficacy/toxicity.")
|
| 75 |
+
|
| 76 |
+
seq_input = gr.Textbox(label="Enter Peptide Sequence")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
predict_btn = gr.Button("Predict", variant="primary")
|
| 80 |
+
clear_btn = gr.Button("Clear")
|
| 81 |
+
|
| 82 |
+
table_output = gr.Dataframe(
|
| 83 |
+
headers=["Target Cell", "Probability of Efficacy/Toxicity"],
|
| 84 |
+
datatype=["str","number"],
|
| 85 |
+
interactive=False
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
predict_btn.click(fn=predict_peptide, inputs=seq_input, outputs=table_output)
|
| 89 |
+
clear_btn.click(fn=lambda: ("", []), outputs=[seq_input, table_output])
|
| 90 |
+
|
| 91 |
+
# API endpoint for iOS app
|
| 92 |
+
gr.api(predict_peptide, api_name="predict_peptide")
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|
| 96 |
+
|