Ym420 commited on
Commit
5a0ce9f
·
verified ·
1 Parent(s): be3e3ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -30
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import gradio as gr
2
  import joblib
 
 
3
  import pandas as pd
4
 
5
- # --- Load model package ---
6
- model_package_path = "Ym420/xgb_multilabel_model_full_pkl"
7
- model_package = joblib.load(model_package_path)
 
 
 
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 predict(sequence):
50
- sequence = sequence.strip()
51
- if not sequence:
52
- return "Sequence cannot be empty.", None
53
-
54
- X = extract_features(sequence)
55
- probs = model.predict_proba(X) # List of arrays per target
56
 
57
- # Format output
58
- output = []
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
- return sequence, output
 
 
 
 
66
 
67
  # --- Gradio Interface ---
68
- iface = gr.Interface(
69
- fn=predict,
70
- inputs=gr.Textbox(lines=2, placeholder="Enter peptide sequence here..."),
71
- outputs=[
72
- gr.Textbox(label="Input Sequence"),
73
- gr.Dataframe(headers=["Target Cell", "Probability of Efficacy/Toxicity"], datatype="json")
74
- ],
75
- title="Peptide Antimicrobial Predictor",
76
- description="Enter a peptide sequence to predict its antimicrobial efficacy/toxicity against target cells."
77
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
80
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
 
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
+