Ym420 commited on
Commit
57b2562
·
verified ·
1 Parent(s): 5a0a462

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -120
app.py DELETED
@@ -1,120 +0,0 @@
1
- import gradio as gr
2
- import joblib
3
- from huggingface_hub import hf_hub_download
4
- import pandas as pd
5
- import numpy as np
6
- from collections import Counter
7
-
8
- # --- Download model from HF Hub ----
9
- repo_id = "Ym420/Peptide-Function" # replace with your HF repo
10
- model_filename = "xgb_multilabel_model_full.pkl"
11
-
12
- model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
13
- model_package = joblib.load(model_path)
14
- model = model_package['model']
15
- feature_columns = model_package['feature_columns']
16
-
17
- # --- Metadata ---
18
- aa_list = model_package['aa_list']
19
- dipeptides = model_package['dipeptides']
20
- hydrophobicity_scale = model_package['hydrophobicity_scale']
21
- eisenberg_scale = model_package['eisenberg_scale']
22
- aa_mass = model_package['aa_mass']
23
- aa_charge = model_package['aa_charge']
24
- aa_boman = model_package['aa_boman']
25
- aa_flexibility = model_package['aa_flexibility']
26
- aa_polarizability = model_package['aa_polarizability']
27
- aa_aliphatic = model_package['aa_aliphatic']
28
-
29
- # --- Target cells ---
30
- TARGET_CELLS = ["Gram+", "Fungus", "Mammalian Cell", "Cancer", "Gram-"]
31
-
32
- # --- Feature extraction ---
33
- def extract_features_app(seq: str) -> pd.DataFrame:
34
- seq = seq.upper()
35
-
36
- # --- 1. Dipeptide composition ---
37
- count = Counter([seq[i:i+2] for i in range(len(seq)-1)])
38
- total = max(len(seq)-1, 1)
39
- dipep_features = [count.get(dp, 0) / total for dp in dipeptides]
40
-
41
- # --- 2. Physicochemical features ---
42
- def g(aa, table): return table.get(aa, 0)
43
- def h(dp, table): return (g(dp[0], table) + g(dp[1], table)) / 2.0
44
-
45
- dipeptides_seq = [seq[i:i+2] for i in range(len(seq)-1)]
46
-
47
- if len(seq) < 2:
48
- physchem_features = [0]*11
49
- else:
50
- mw = np.mean([h(dp, aa_mass) for dp in dipeptides_seq])
51
- charge = np.mean([h(dp, aa_charge) for dp in dipeptides_seq])
52
- hydro = np.mean([h(dp, hydrophobicity_scale) for dp in dipeptides_seq])
53
- aromatic = np.mean([(dp[0] in 'FWY') + (dp[1] in 'FWY') for dp in dipeptides_seq]) / 2.0
54
- pI = np.mean([h(dp, {aa: 7 + (int(aa in 'KRH') - int(aa in 'DE')) for aa in aa_list}) for dp in dipeptides_seq])
55
- instability = np.mean([((dp[0] in 'DEKR') + (dp[1] in 'DEKR')) / 2.0 for dp in dipeptides_seq])
56
- hydro_moment = np.sqrt(np.mean([(h(dp, eisenberg_scale))**2 for dp in dipeptides_seq]))
57
- aliphatic = np.mean([h(dp, aa_aliphatic) for dp in dipeptides_seq])
58
- boman = np.mean([h(dp, aa_boman) for dp in dipeptides_seq])
59
- flexibility = np.mean([h(dp, aa_flexibility) for dp in dipeptides_seq])
60
- polarizability = np.mean([h(dp, aa_polarizability) for dp in dipeptides_seq])
61
-
62
- physchem_features = [mw, charge, hydro, aromatic, pI, instability,
63
- hydro_moment, aliphatic, boman, flexibility, polarizability]
64
-
65
- features = dipep_features + physchem_features
66
-
67
- df = pd.DataFrame([features], columns=feature_columns)
68
- df = df.astype('float32') # ensure same type as training
69
- return df
70
-
71
- # --- Prediction function ---
72
- def predict_peptide(sequence: str):
73
- seq = "".join(sequence.split()).upper()
74
- if not seq:
75
- return []
76
-
77
- X = extract_features_app(seq)
78
-
79
- probs_list = model.predict_proba(X) # list of arrays per target
80
- # --- probs_list: list of arrays from each estimator ---
81
- #probs_list = [est.predict_proba(X) for est in model] # model is a list
82
-
83
- table = []
84
- for i, target in enumerate(TARGET_CELLS):
85
- prob = float(probs_list[i][0][1])
86
- table.append([target, round(prob, 4)])
87
- return table
88
-
89
-
90
- # --- Gradio Interface ---
91
- custom_css = """
92
- footer, .footer {display:none !important;}
93
- """
94
-
95
- with gr.Blocks(css=custom_css, theme="default") as demo:
96
- gr.Markdown("## Peptide Antimicrobial Predictor\nEnter a peptide sequence to predict efficacy/toxicity.")
97
-
98
- seq_input = gr.Textbox(label="Enter Peptide Sequence")
99
-
100
- with gr.Row():
101
- predict_btn = gr.Button("Predict", variant="primary")
102
- clear_btn = gr.Button("Clear")
103
-
104
- table_output = gr.Dataframe(
105
- headers=["Target Cell", "Probability of Efficacy/Toxicity"],
106
- datatype=["str","number"],
107
- interactive=False
108
- )
109
-
110
- predict_btn.click(fn=predict_peptide, inputs=seq_input, outputs=table_output)
111
- clear_btn.click(fn=lambda: ("", []), outputs=[seq_input, table_output])
112
-
113
- # API endpoint for iOS app
114
- gr.api(predict_peptide, api_name="predict_peptide")
115
-
116
- #if __name__ == "__main__":
117
- # demo.launch()
118
-
119
- if __name__ == "__main__":
120
- demo.launch(show_error=True)