nonzeroexit commited on
Commit
5810e43
·
verified ·
1 Parent(s): c9a939f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -5,12 +5,10 @@ import pandas as pd
5
  from propy import AAComposition
6
  from sklearn.preprocessing import MinMaxScaler
7
 
8
- # Load trained SVM model and scaler (Ensure both files exist in the Space)
9
  model = joblib.load("SVM.joblib")
10
  scaler = joblib.load("norm.joblib")
11
 
12
 
13
- # List of features used in your model
14
  selected_features = [
15
  "A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V",
16
  "AA", "AR", "AN", "AD", "AC", "AE", "AQ", "AG", "AI", "AL", "AK", "AF", "AP", "AS", "AT", "AY", "AV",
@@ -37,27 +35,19 @@ selected_features = [
37
 
38
  def extract_features(sequence):
39
  """Extract only the required features and normalize them."""
40
- # Compute all possible features
41
- all_features = AAComposition.CalculateAADipeptideComposition(sequence) # Amino Acid Composition
42
- # Extract the values from the dictionary
43
- feature_values = list(all_features.values()) # Extract values only
44
- # Convert to NumPy array for normalization
45
  feature_array = np.array(feature_values).reshape(-1, 1)
46
  feature_array = feature_array[: 420]
47
- # Min-Max Normalization
48
  normalized_features = scaler.transform(feature_array.T)
49
 
50
- # Reshape normalized_features back to a single dimension
51
- normalized_features = normalized_features.flatten() # Flatten array
52
 
53
- # Create a dictionary with selected features
54
  selected_feature_dict = {feature: normalized_features[i] for i, feature in enumerate(selected_features)
55
  if feature in all_features}
56
 
57
- # Convert dictionary to dataframe
58
  selected_feature_df = pd.DataFrame([selected_feature_dict])
59
 
60
- # Convert dataframe to numpy array
61
  selected_feature_array = selected_feature_df.T.to_numpy()
62
 
63
  return selected_feature_array
@@ -68,11 +58,18 @@ def predict(sequence):
68
  """Predict AMP vs Non-AMP"""
69
  features = extract_features(sequence)
70
  prediction = model.predict(features.T)[0]
71
- probabilities = model.predict_proba(features.T)
 
 
 
 
 
 
 
 
 
72
 
73
- return "Potential Bioactive Peptide with Antimicrobial Properties (P-AMP)" if prediction == 0 else "Likely Non-Antimicrobial Peptide", probabilities[0]
74
 
75
- # Create Gradio interface
76
  iface = gr.Interface(
77
  fn=predict,
78
  inputs=gr.Textbox(label="Enter Protein Sequence"),
@@ -81,5 +78,4 @@ iface = gr.Interface(
81
  description="Enter an amino acid sequence to predict whether it's an antimicrobial peptide (AMP) or not."
82
  )
83
 
84
- # Launch app
85
  iface.launch(share=True)
 
5
  from propy import AAComposition
6
  from sklearn.preprocessing import MinMaxScaler
7
 
 
8
  model = joblib.load("SVM.joblib")
9
  scaler = joblib.load("norm.joblib")
10
 
11
 
 
12
  selected_features = [
13
  "A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V",
14
  "AA", "AR", "AN", "AD", "AC", "AE", "AQ", "AG", "AI", "AL", "AK", "AF", "AP", "AS", "AT", "AY", "AV",
 
35
 
36
  def extract_features(sequence):
37
  """Extract only the required features and normalize them."""
38
+ all_features = AAComposition.CalculateAADipeptideComposition(sequence)
39
+ feature_values = list(all_features.values())
 
 
 
40
  feature_array = np.array(feature_values).reshape(-1, 1)
41
  feature_array = feature_array[: 420]
 
42
  normalized_features = scaler.transform(feature_array.T)
43
 
44
+ normalized_features = normalized_features.flatten()
 
45
 
 
46
  selected_feature_dict = {feature: normalized_features[i] for i, feature in enumerate(selected_features)
47
  if feature in all_features}
48
 
 
49
  selected_feature_df = pd.DataFrame([selected_feature_dict])
50
 
 
51
  selected_feature_array = selected_feature_df.T.to_numpy()
52
 
53
  return selected_feature_array
 
58
  """Predict AMP vs Non-AMP"""
59
  features = extract_features(sequence)
60
  prediction = model.predict(features.T)[0]
61
+ probability_amp = model.predict_proba(features.T)
62
+
63
+ if prediction == 0:
64
+ prediction_label = "Potential Bioactive Peptide with Antimicrobial Properties (P-AMP)"
65
+ probability_amp = probabilities[0]
66
+ else:
67
+ prediction_label = "Likely Non-Antimicrobial Peptide"
68
+ probability_amp = probabilities[0]
69
+
70
+ return prediction_label, probability_amp
71
 
 
72
 
 
73
  iface = gr.Interface(
74
  fn=predict,
75
  inputs=gr.Textbox(label="Enter Protein Sequence"),
 
78
  description="Enter an amino acid sequence to predict whether it's an antimicrobial peptide (AMP) or not."
79
  )
80
 
 
81
  iface.launch(share=True)