Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
import propy
|
| 8 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
+
|
| 10 |
+
# Load trained SVM model
|
| 11 |
+
model = joblib.load("SVM.joblib")
|
| 12 |
|
| 13 |
+
# Define request model
|
| 14 |
+
class SequenceInput(BaseModel):
|
| 15 |
+
sequence: str
|
| 16 |
|
| 17 |
+
def extract_features(sequence):
|
| 18 |
+
"""Calculate AAC, Dipeptide Composition and normalize features."""
|
| 19 |
+
# Calculate Amino Acid Composition (AAC)
|
| 20 |
+
aac = propy.AAComposition.CalculateAAC(sequence)
|
| 21 |
+
|
| 22 |
+
# Calculate Dipeptide Composition
|
| 23 |
+
dipeptide_comp = propy.AAComposition.CalculateAADipeptideComposition(sequence)
|
| 24 |
+
|
| 25 |
+
# Combine both features (AAC and Dipeptide Composition)
|
| 26 |
+
features = np.concatenate((aac, dipeptide_comp))
|
| 27 |
+
|
| 28 |
+
# Min-Max Normalization
|
| 29 |
+
scaler = MinMaxScaler()
|
| 30 |
+
normalized_features = scaler.fit_transform(features.reshape(-1, 1)).flatten()
|
| 31 |
+
|
| 32 |
+
return normalized_features
|
| 33 |
|
| 34 |
+
@app.post("/predict/")
|
| 35 |
+
def predict(sequence_input: SequenceInput):
|
| 36 |
+
"""Predict AMP vs Non-AMP"""
|
| 37 |
+
sequence = sequence_input.sequence
|
| 38 |
+
features = extract_features(sequence)
|
| 39 |
+
prediction = model.predict([features])[0]
|
| 40 |
+
|
| 41 |
+
return {"sequence": sequence, "prediction": "AMP" if prediction == 1 else "Non-AMP"}
|
| 42 |
|
|
|