File size: 1,257 Bytes
4575167
 
942bf87
51a3749
3a814dc
51a3749
 
4575167
 
 
 
51a3749
f0357dd
942bf87
4575167
 
 
14f4c95
4575167
 
 
 
 
 
 
 
d5efa2c
4575167
 
 
 
f0357dd
4575167
 
942bf87
4575167
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import numpy as np
from propy import AAComposition
from sklearn.preprocessing import MinMaxScaler

# Initialize FastAPI app
app = FastAPI()

# Load trained SVM model and scaler
model = joblib.load("SVM.joblib")
scaler = MinMaxScaler()

# Define request schema
class SequenceInput(BaseModel):
    sequence: str

def extract_features(sequence: str):
    """Extract Amino Acid Composition (AAC) features and normalize them."""
    try:
        aac = np.array(list(AAComposition.CalculateAADipeptideComposition(sequence)), dtype=float)
        normalized_features = scaler.fit_transform([aac])  # Don't use fit_transform(), only transform()
        return normalized_features
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Feature extraction failed: {str(e)}")

@app.post("/predict/")
def predict(input_data: SequenceInput):
    """Predict AMP vs Non-AMP from protein sequence."""
    features = extract_features(input_data.sequence)
    prediction = model.predict(features)[0]
    result = "AMP" if prediction == 1 else "Non-AMP"
    return {"prediction": result}

# Run using: uvicorn script_name:app --host 0.0.0.0 --port 8000 --reload