Ym420 commited on
Commit
aa85173
Β·
verified Β·
1 Parent(s): 86445c5

Back up for app.py

Browse files
Files changed (1) hide show
  1. app2.py +142 -0
app2.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # βž• ADDED: placeholders (instead of loading at startup)
9
+ model_dict = None
10
+ feature_columns = None
11
+ model_package = None
12
+
13
+ # ❌ REMOVED: direct model loading at startup
14
+ # repo_id = "Ym420/Peptide-Function"
15
+ # model_filename = "xgb_multilabel_model_full.pkl"
16
+ # model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
17
+ # model_package = joblib.load(model_path)
18
+ # model_dict = model_package['model']
19
+ # feature_columns = model_package['feature_columns']
20
+
21
+ # βž• ADDED: lazy loader function
22
+ def init_model():
23
+ global model_dict, feature_columns, model_package
24
+
25
+ if model_dict is None:
26
+ repo_id = "Ym420/Peptide-Function"
27
+ model_filename = "xgb_multilabel_model_full.pkl"
28
+
29
+ model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
30
+ model_package = joblib.load(model_path)
31
+
32
+ model_dict = model_package['model']
33
+ feature_columns = model_package['feature_columns']
34
+
35
+ # πŸ” MOVED: metadata loading (was global before)
36
+ global aa_list, dipeptides, hydrophobicity_scale, eisenberg_scale
37
+ global aa_mass, aa_charge, aa_boman, aa_flexibility
38
+ global aa_polarizability, aa_aliphatic, aa_deltaG, aa_pucker
39
+
40
+ aa_list = model_package.get('aa_list', [])
41
+ dipeptides = model_package.get('dipeptides', [])
42
+ hydrophobicity_scale = model_package.get('hydrophobicity_scale', {})
43
+ eisenberg_scale = model_package.get('eisenberg_scale', {})
44
+ aa_mass = model_package.get('aa_mass', {})
45
+ aa_charge = model_package.get('aa_charge', {})
46
+ aa_boman = model_package.get('aa_boman', {})
47
+ aa_flexibility = model_package.get('aa_flexibility', {})
48
+ aa_polarizability = model_package.get('aa_polarizability', {})
49
+ aa_aliphatic = model_package.get('aa_aliphatic', {})
50
+ aa_deltaG = model_package.get('aa_deltaG', {})
51
+ aa_pucker = model_package.get('aa_pucker', {})
52
+
53
+ # --- Target cells ---
54
+ TARGET_CELLS = ["Gram+", "Fungus", "Mammalian Cell", "Cancer", "Gram-"]
55
+
56
+ # --- Feature extraction ---
57
+ def extract_features_app(seq: str) -> pd.DataFrame:
58
+ seq = seq.upper()
59
+
60
+ count = Counter([seq[i:i+2] for i in range(len(seq)-1)])
61
+ total = max(len(seq)-1, 1)
62
+ dipep_features = [count.get(dp, 0) / total for dp in dipeptides]
63
+
64
+ def g(aa, table): return table.get(aa, 0)
65
+ def h(dp, table): return (g(dp[0], table) + g(dp[1], table)) / 2.0
66
+
67
+ dipeptides_seq = [seq[i:i+2] for i in range(len(seq)-1)]
68
+
69
+ if len(seq) < 2:
70
+ physchem_features = [0]*13
71
+ else:
72
+ mw = np.mean([h(dp, aa_mass) for dp in dipeptides_seq])
73
+ charge = np.mean([h(dp, aa_charge) for dp in dipeptides_seq])
74
+ hydro = np.mean([h(dp, hydrophobicity_scale) for dp in dipeptides_seq])
75
+ aromatic = np.mean([(dp[0] in 'FWY') + (dp[1] in 'FWY') for dp in dipeptides_seq]) / 2.0
76
+ 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])
77
+ instability = np.mean([((dp[0] in 'DEKR') + (dp[1] in 'DEKR')) / 2.0 for dp in dipeptides_seq])
78
+ hydro_moment = np.sqrt(np.mean([(h(dp, eisenberg_scale))**2 for dp in dipeptides_seq]))
79
+ aliphatic = np.mean([h(dp, aa_aliphatic) for dp in dipeptides_seq])
80
+ boman = np.mean([h(dp, aa_boman) for dp in dipeptides_seq])
81
+ flexibility = np.mean([h(dp, aa_flexibility) for dp in dipeptides_seq])
82
+ polarizability = np.mean([h(dp, aa_polarizability) for dp in dipeptides_seq])
83
+ deltag = np.mean([h(dp, aa_deltaG) for dp in dipeptides_seq])
84
+ pucker = np.mean([h(dp, aa_pucker) for dp in dipeptides_seq])
85
+
86
+ physchem_features = [mw, charge, hydro, aromatic, pI, instability,
87
+ hydro_moment, aliphatic, boman, flexibility, polarizability, deltag, pucker]
88
+
89
+ features = dipep_features + physchem_features
90
+
91
+ df = pd.DataFrame([features], columns=feature_columns)
92
+ df = df.astype('float32')
93
+ return df
94
+
95
+ # --- Prediction function ---
96
+ def predict_peptide(sequence: str):
97
+ init_model() # βž• ADDED: ensures model loads only when needed
98
+
99
+ seq = "".join(sequence.split()).upper()
100
+ if not seq:
101
+ return []
102
+
103
+ X = extract_features_app(seq)
104
+
105
+ table = []
106
+ for target in TARGET_CELLS:
107
+ clf = model_dict.get(target)
108
+ if clf is not None:
109
+ prob = clf.predict_proba(X)[0][1]
110
+ table.append([target, round(float(prob), 4)])
111
+ else:
112
+ table.append([target, None])
113
+
114
+ return table
115
+
116
+ # --- Gradio Interface ---
117
+ custom_css = """
118
+ footer, .footer {display:none !important;}
119
+ """
120
+
121
+ with gr.Blocks(css=custom_css, theme="default") as demo:
122
+ gr.Markdown("## AMP Spectrum")
123
+
124
+ seq_input = gr.Textbox(label="Enter Peptide Sequence")
125
+
126
+ with gr.Row():
127
+ predict_btn = gr.Button("Predict", variant="primary")
128
+ clear_btn = gr.Button("Clear")
129
+
130
+ table_output = gr.Dataframe(
131
+ headers=["Target", "Confidence"],
132
+ datatype=["str","number"],
133
+ interactive=False
134
+ )
135
+
136
+ predict_btn.click(fn=predict_peptide, inputs=seq_input, outputs=table_output)
137
+ clear_btn.click(fn=lambda: ("", []), outputs=[seq_input, table_output])
138
+
139
+ gr.api(predict_peptide, api_name="predict_peptide")
140
+
141
+ if __name__ == "__main__":
142
+ demo.launch(show_error=True, ssr_mode=False) # βœ… CHANGED: disable SSR