Ym420 commited on
Commit
2b374d6
·
verified ·
1 Parent(s): fe30bd1

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -134
app.py DELETED
@@ -1,134 +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"
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
-
15
- # --- Unwrap model dict ---
16
- model_dict = model_package['model'] # dict: {'Gram+': XGBClassifier, ...}
17
- feature_columns = model_package['feature_columns']
18
-
19
- # --- Metadata (all restored) ---
20
- aa_list = model_package.get('aa_list', [])
21
- dipeptides = model_package.get('dipeptides', [])
22
- hydrophobicity_scale = model_package.get('hydrophobicity_scale', {})
23
- eisenberg_scale = model_package.get('eisenberg_scale', {})
24
- aa_mass = model_package.get('aa_mass', {})
25
- aa_charge = model_package.get('aa_charge', {})
26
- aa_boman = model_package.get('aa_boman', {})
27
- aa_flexibility = model_package.get('aa_flexibility', {})
28
- aa_polarizability = model_package.get('aa_polarizability', {})
29
- aa_aliphatic = model_package.get('aa_aliphatic', {})
30
- aa_deltaG = model_package.get('aa_deltaG', {})
31
-
32
- # --- Dynamic TARGET_CELLS ---
33
- TARGET_CELLS = list(model_dict.keys()) # automatically detects all targets
34
-
35
- # --- Feature extraction (future-proof) ---
36
- def extract_features_app(seq: str) -> pd.DataFrame:
37
- seq = seq.upper()
38
-
39
- # --- 1. Dipeptide composition ---
40
- count = Counter([seq[i:i+2] for i in range(len(seq)-1)])
41
- total = max(len(seq)-1, 1)
42
- dipep_features = {dp: count.get(dp, 0) / total for dp in dipeptides}
43
-
44
- # --- 2. Physicochemical features ---
45
- def g(aa, table): return table.get(aa, 0)
46
- def h(dp, table): return (g(dp[0], table) + g(dp[1], table)) / 2.0
47
-
48
- dipeptides_seq = [seq[i:i+2] for i in range(len(seq)-1)]
49
-
50
- if len(seq) < 2:
51
- physchem_features = {
52
- 'mw': 0, 'charge': 0, 'hydro': 0, 'aromatic': 0, 'pI': 0,
53
- 'instability': 0, 'hydro_moment': 0, 'aliphatic': 0,
54
- 'boman': 0, 'flexibility': 0, 'polarizability': 0, 'deltag': 0
55
- }
56
- else:
57
- mw = np.mean([h(dp, aa_mass) for dp in dipeptides_seq])
58
- charge = np.mean([h(dp, aa_charge) for dp in dipeptides_seq])
59
- hydro = np.mean([h(dp, hydrophobicity_scale) for dp in dipeptides_seq])
60
- aromatic = np.mean([(dp[0] in 'FWY') + (dp[1] in 'FWY') for dp in dipeptides_seq]) / 2.0
61
- 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])
62
- instability = np.mean([((dp[0] in 'DEKR') + (dp[1] in 'DEKR')) / 2.0 for dp in dipeptides_seq])
63
- hydro_moment = np.sqrt(np.mean([(h(dp, eisenberg_scale))**2 for dp in dipeptides_seq]))
64
- aliphatic = np.mean([h(dp, aa_aliphatic) for dp in dipeptides_seq])
65
- boman = np.mean([h(dp, aa_boman) for dp in dipeptides_seq])
66
- flexibility = np.mean([h(dp, aa_flexibility) for dp in dipeptides_seq])
67
- polarizability = np.mean([h(dp, aa_polarizability) for dp in dipeptides_seq])
68
- deltag = np.mean([h(dp, aa_deltaG) for dp in dipeptides_seq])
69
-
70
- physchem_features = {
71
- 'mw': mw, 'charge': charge, 'hydro': hydro, 'aromatic': aromatic, 'pI': pI,
72
- 'instability': instability, 'hydro_moment': hydro_moment, 'aliphatic': aliphatic,
73
- 'boman': boman, 'flexibility': flexibility, 'polarizability': polarizability, 'deltag': deltag
74
- }
75
-
76
- # --- Combine features ---
77
- all_features = {**dipep_features, **physchem_features}
78
-
79
- # --- Align with feature_columns ---
80
- df = pd.DataFrame([[all_features.get(col, 0) for col in feature_columns]], columns=feature_columns)
81
- df = df.astype('float32')
82
- return df
83
-
84
- # --- Prediction function ---
85
- def predict_peptide(sequence: str):
86
- seq = "".join(sequence.split()).upper()
87
- if not seq:
88
- return []
89
-
90
- X = extract_features_app(seq)
91
-
92
- table = []
93
- for target in TARGET_CELLS:
94
- clf = model_dict.get(target)
95
- if clf is not None:
96
- prob = clf.predict_proba(X)[0][1] # positive-class probability
97
- table.append([target, round(float(prob), 4)])
98
- else:
99
- table.append([target, None])
100
- return table
101
-
102
- # --- Gradio Interface ---
103
- custom_css = """
104
- footer, .footer {display:none !important;}
105
- """
106
-
107
- with gr.Blocks(css=custom_css, theme="default") as demo:
108
- gr.Markdown("## Peptide Antimicrobial Predictor\nEnter a peptide sequence to predict efficacy/toxicity.")
109
-
110
- seq_input = gr.Textbox(label="Enter Peptide Sequence")
111
-
112
- with gr.Row():
113
- predict_btn = gr.Button("Predict", variant="primary")
114
- clear_btn = gr.Button("Clear")
115
-
116
- table_output = gr.Dataframe(
117
- headers=["Target Cell", "Probability of Efficacy/Toxicity"],
118
- datatype=["str","number"],
119
- interactive=False
120
- )
121
-
122
- predict_btn.click(fn=predict_peptide, inputs=seq_input, outputs=table_output)
123
- clear_btn.click(fn=lambda: ("", []), outputs=[seq_input, table_output])
124
-
125
- # API endpoint for iOS app
126
- gr.api(predict_peptide, api_name="predict_peptide")
127
-
128
- if __name__ == "__main__":
129
- demo.launch(show_error=True)
130
-
131
-
132
-
133
-
134
-