brent10070 commited on
Commit
cd0af83
·
verified ·
1 Parent(s): bc6370a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -24
app.py CHANGED
@@ -3,9 +3,6 @@ import torch
3
  import torch.nn as nn
4
  import numpy as np
5
 
6
- # ==========================================
7
- # 1. 參數與資料處理函數 (來自你的 Notebook)
8
- # ==========================================
9
  SEQ_LEN = 70
10
  AA_LIST = list('AGILMPVFWYCNSTQDEHKR')
11
  AA_TO_IDX = {aa: i for i, aa in enumerate(AA_LIST)}
@@ -17,9 +14,7 @@ def seq_to_onehot(seq, max_len=SEQ_LEN):
17
  onehot[AA_TO_IDX[char], i] = 1.0
18
  return onehot
19
 
20
- # ==========================================
21
- # 2. 模型架構定義 (完全複製你的 CNNModel)
22
- # ==========================================
23
  class CNNModel(nn.Module):
24
  def __init__(self, num_classes=2, input_channels=20, seq_len=70):
25
  super(CNNModel, self).__init__()
@@ -67,51 +62,41 @@ class CNNModel(nn.Module):
67
  x = self.classifier(x)
68
  return x
69
 
 
70
  # ==========================================
71
- # 3. 載入模型與推論邏輯
72
- # ==========================================
73
- # 初始化模型並載入權重 (設定 map_location='cpu' 因為免費版 HF 是 CPU 環境)
74
  model = CNNModel(num_classes=2)
75
- # 這裡對應你上傳的權重檔案名稱
76
  model.load_state_dict(torch.load("best_model_weights.pth", map_location=torch.device('cpu')))
77
  model.eval()
78
 
79
  def predict_peptide(sequence):
80
- # 清理輸入序列:轉大寫、去空白
81
  clean_seq = sequence.strip().upper()
82
-
83
- # 檢查輸入是否為空
84
  if len(clean_seq) == 0:
85
  return "Error: Please enter a valid sequence."
86
-
87
- # 轉換為 One-hot encoding
88
  seq_oh = seq_to_onehot(clean_seq)
89
-
90
- # 轉成 PyTorch Tensor 並增加 Batch 維度 (變成 1, 20, 70)
91
  input_tensor = torch.tensor(seq_oh).unsqueeze(0)
92
 
93
- # 模型預測
94
  with torch.no_grad():
95
  outputs = model(input_tensor)
96
- # 使用 softmax 將輸出轉為 0~1 的機率
97
  probs = torch.nn.functional.softmax(outputs, dim=1)
98
- # 假設 label 1 是 Plastid (正樣本),取得正樣本的機率
99
  positive_prob = probs[0, 1].item()
100
 
101
  return f"{positive_prob:.2%}"
102
 
103
  # ==========================================
104
- # 4. Gradio 網頁介面設計
105
- # ==========================================
106
  demo = gr.Interface(
107
  fn=predict_peptide,
108
  inputs=gr.Textbox(
109
- lines=3,
110
  placeholder="e.g., MAASVATAGSV... (Enter up to 70 N-terminal amino acids)",
111
  label="Amino Acid Sequence"
112
  ),
113
  outputs=gr.Label(label="Probability of being a Plastid Transit Peptide"),
114
- title="Plastid Transit Peptide Predictor",
115
  description="This tool uses a Deep Convolutional Neural Network to predict plastid transit peptides based on the N-terminal sequence. Sequences longer than 70 residues will be automatically truncated."
116
  )
117
 
 
3
  import torch.nn as nn
4
  import numpy as np
5
 
 
 
 
6
  SEQ_LEN = 70
7
  AA_LIST = list('AGILMPVFWYCNSTQDEHKR')
8
  AA_TO_IDX = {aa: i for i, aa in enumerate(AA_LIST)}
 
14
  onehot[AA_TO_IDX[char], i] = 1.0
15
  return onehot
16
 
17
+ ## Model
 
 
18
  class CNNModel(nn.Module):
19
  def __init__(self, num_classes=2, input_channels=20, seq_len=70):
20
  super(CNNModel, self).__init__()
 
62
  x = self.classifier(x)
63
  return x
64
 
65
+
66
  # ==========================================
 
 
 
67
  model = CNNModel(num_classes=2)
 
68
  model.load_state_dict(torch.load("best_model_weights.pth", map_location=torch.device('cpu')))
69
  model.eval()
70
 
71
  def predict_peptide(sequence):
 
72
  clean_seq = sequence.strip().upper()
73
+
 
74
  if len(clean_seq) == 0:
75
  return "Error: Please enter a valid sequence."
76
+
 
77
  seq_oh = seq_to_onehot(clean_seq)
78
+
 
79
  input_tensor = torch.tensor(seq_oh).unsqueeze(0)
80
 
81
+ # Model prediction
82
  with torch.no_grad():
83
  outputs = model(input_tensor)
 
84
  probs = torch.nn.functional.softmax(outputs, dim=1)
 
85
  positive_prob = probs[0, 1].item()
86
 
87
  return f"{positive_prob:.2%}"
88
 
89
  # ==========================================
90
+ # Gradio
 
91
  demo = gr.Interface(
92
  fn=predict_peptide,
93
  inputs=gr.Textbox(
94
+ lines=2,
95
  placeholder="e.g., MAASVATAGSV... (Enter up to 70 N-terminal amino acids)",
96
  label="Amino Acid Sequence"
97
  ),
98
  outputs=gr.Label(label="Probability of being a Plastid Transit Peptide"),
99
+ title="PlastidTP: Binary classification model for Plastid Transit Peptides (pTPs)",
100
  description="This tool uses a Deep Convolutional Neural Network to predict plastid transit peptides based on the N-terminal sequence. Sequences longer than 70 residues will be automatically truncated."
101
  )
102