nonzeroexit commited on
Commit
f0357dd
·
verified ·
1 Parent(s): b7a676c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -17
app.py CHANGED
@@ -1,20 +1,15 @@
1
  import gradio as gr
2
  import joblib
3
- import joblib
4
  import numpy as np
5
- from pydantic import BaseModel
6
  import propy
7
  from sklearn.preprocessing import MinMaxScaler
8
 
9
- # Load trained SVM model
10
  model = joblib.load("SVM.joblib")
11
-
12
- # Define request model
13
- class SequenceInput(BaseModel):
14
- sequence: str
15
 
16
  def extract_features(sequence):
17
- """Calculate AAC, Dipeptide Composition and normalize features."""
18
  # Calculate Amino Acid Composition (AAC)
19
  aac = propy.AAComposition.CalculateAAC(sequence)
20
 
@@ -24,18 +19,25 @@ def extract_features(sequence):
24
  # Combine both features (AAC and Dipeptide Composition)
25
  features = np.concatenate((aac, dipeptide_comp))
26
 
27
- # Min-Max Normalization
28
- scaler = MinMaxScaler()
29
- normalized_features = scaler.fit_transform(features.reshape(-1, 1)).flatten()
30
 
31
  return normalized_features
32
 
33
- @app.post("/predict/")
34
- def predict(sequence_input: SequenceInput):
35
  """Predict AMP vs Non-AMP"""
36
- sequence = sequence_input.sequence
37
  features = extract_features(sequence)
38
- prediction = model.predict([features])[0]
39
-
40
- return {"sequence": sequence, "prediction": "AMP" if prediction == 1 else "Non-AMP"}
 
 
 
 
 
 
 
 
41
 
 
 
 
1
  import gradio as gr
2
  import joblib
 
3
  import numpy as np
 
4
  import propy
5
  from sklearn.preprocessing import MinMaxScaler
6
 
7
+ # Load trained SVM model and scaler (Ensure both files exist in the Space)
8
  model = joblib.load("SVM.joblib")
9
+ scaler = MinMaxScaler()
 
 
 
10
 
11
  def extract_features(sequence):
12
+ """Calculate AAC, Dipeptide Composition, and normalize features."""
13
  # Calculate Amino Acid Composition (AAC)
14
  aac = propy.AAComposition.CalculateAAC(sequence)
15
 
 
19
  # Combine both features (AAC and Dipeptide Composition)
20
  features = np.concatenate((aac, dipeptide_comp))
21
 
22
+ # Normalize with pre-trained scaler (avoid fitting new data)
23
+ normalized_features = scaler.transform([features])
 
24
 
25
  return normalized_features
26
 
27
+ def predict(sequence):
 
28
  """Predict AMP vs Non-AMP"""
 
29
  features = extract_features(sequence)
30
+ prediction = model.predict(features)[0]
31
+ return "AMP" if prediction == 1 else "Non-AMP"
32
+
33
+ # Create Gradio interface
34
+ iface = gr.Interface(
35
+ fn=predict,
36
+ inputs=gr.Textbox(label="Enter Protein Sequence"),
37
+ outputs=gr.Label(label="Prediction"),
38
+ title="AMP Classifier",
39
+ description="Enter an amino acid sequence to predict whether it's an antimicrobial peptide (AMP) or not."
40
+ )
41
 
42
+ # Launch app
43
+ iface.launch()