nonzeroexit commited on
Commit
4575167
·
verified ·
1 Parent(s): e5f3c3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -1,38 +1,36 @@
1
- import gradio as gr
 
2
  import joblib
3
  import numpy as np
4
  from propy import AAComposition
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) and convert to array
14
- aac = np.array(list(AAComposition.CalculateAADipeptideComposition(sequence)), dtype=float)
15
-
16
- # Normalize using the pre-trained scaler (Ensure the scaler is loaded correctly)
17
- normalized_features = scaler.fit_transform([aac]) # Don't use fit_transform(), only transform()
18
-
19
- return normalized_features
20
 
 
 
 
 
 
 
 
 
21
 
22
- def predict(sequence):
23
- """Predict AMP vs Non-AMP"""
24
- features = extract_features(sequence)
 
25
  prediction = model.predict(features)[0]
26
- return "AMP" if prediction == 1 else "Non-AMP"
27
-
28
- # Create Gradio interface
29
- iface = gr.Interface(
30
- fn=predict,
31
- inputs=gr.Textbox(label="Enter Protein Sequence"),
32
- outputs=gr.Label(label="Prediction"),
33
- title="AMP Classifier",
34
- description="Enter an amino acid sequence to predict whether it's an antimicrobial peptide (AMP) or not."
35
- )
36
 
37
- # Launch app
38
- iface.launch(share=True)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  import joblib
4
  import numpy as np
5
  from propy import AAComposition
6
  from sklearn.preprocessing import MinMaxScaler
7
 
8
+ # Initialize FastAPI app
9
+ app = FastAPI()
10
+
11
+ # Load trained SVM model and scaler
12
  model = joblib.load("SVM.joblib")
13
  scaler = MinMaxScaler()
14
 
15
+ # Define request schema
16
+ class SequenceInput(BaseModel):
17
+ sequence: str
 
 
 
 
 
 
18
 
19
+ def extract_features(sequence: str):
20
+ """Extract Amino Acid Composition (AAC) features and normalize them."""
21
+ try:
22
+ aac = np.array(list(AAComposition.CalculateAADipeptideComposition(sequence)), dtype=float)
23
+ normalized_features = scaler.fit_transform([aac]) # Don't use fit_transform(), only transform()
24
+ return normalized_features
25
+ except Exception as e:
26
+ raise HTTPException(status_code=400, detail=f"Feature extraction failed: {str(e)}")
27
 
28
+ @app.post("/predict/")
29
+ def predict(input_data: SequenceInput):
30
+ """Predict AMP vs Non-AMP from protein sequence."""
31
+ features = extract_features(input_data.sequence)
32
  prediction = model.predict(features)[0]
33
+ result = "AMP" if prediction == 1 else "Non-AMP"
34
+ return {"prediction": result}
 
 
 
 
 
 
 
 
35
 
36
+ # Run using: uvicorn script_name:app --host 0.0.0.0 --port 8000 --reload