Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,18 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
from propy import AAComposition
|
6 |
from sklearn.preprocessing import MinMaxScaler
|
7 |
-
from fastapi import FastAPI
|
8 |
-
from gradio.routes import mount_gradio_app
|
9 |
|
10 |
-
# Load trained SVM model and scaler
|
11 |
model = joblib.load("SVM.joblib")
|
12 |
scaler = joblib.load("norm.joblib")
|
13 |
|
14 |
-
# FastAPI instance
|
15 |
-
app = FastAPI()
|
16 |
|
17 |
-
#
|
18 |
selected_features = [
|
19 |
"A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V",
|
20 |
"AA", "AR", "AN", "AD", "AC", "AE", "AQ", "AG", "AI", "AL", "AK", "AF", "AP", "AS", "AT", "AY", "AV",
|
@@ -41,43 +39,47 @@ selected_features = [
|
|
41 |
|
42 |
def extract_features(sequence):
|
43 |
"""Extract only the required features and normalize them."""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
if feature in all_features
|
53 |
-
}
|
54 |
|
|
|
55 |
selected_feature_df = pd.DataFrame([selected_feature_dict])
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def predict(sequence):
|
59 |
"""Predict AMP vs Non-AMP"""
|
60 |
features = extract_features(sequence)
|
61 |
prediction = model.predict(features.T)[0]
|
62 |
-
return
|
63 |
|
64 |
-
#
|
65 |
-
@app.post("/predict/")
|
66 |
-
async def predict_api(request: dict):
|
67 |
-
sequence = request.get("sequence", "")
|
68 |
-
if not sequence or len(sequence) < 10 or len(sequence) > 100:
|
69 |
-
return {"error": "Sequence length must be between 10 and 100."}
|
70 |
-
return predict(sequence)
|
71 |
-
|
72 |
-
# Gradio Interface (optional if you want UI access)
|
73 |
iface = gr.Interface(
|
74 |
fn=predict,
|
75 |
inputs=gr.Textbox(label="Enter Protein Sequence"),
|
76 |
outputs=gr.Label(label="Prediction"),
|
77 |
title="AMP Classifier",
|
78 |
-
description="Enter an amino acid sequence to predict AMP or
|
79 |
)
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
# Run the server with: `uvicorn filename:app --host 0.0.0.0 --port 7860`
|
|
|
1 |
+
I need to link the backend to frontend
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import joblib
|
5 |
import numpy as np
|
6 |
import pandas as pd
|
7 |
from propy import AAComposition
|
8 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
9 |
|
10 |
+
# Load trained SVM model and scaler (Ensure both files exist in the Space)
|
11 |
model = joblib.load("SVM.joblib")
|
12 |
scaler = joblib.load("norm.joblib")
|
13 |
|
|
|
|
|
14 |
|
15 |
+
# List of features used in your model
|
16 |
selected_features = [
|
17 |
"A", "R", "N", "D", "C", "E", "Q", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V",
|
18 |
"AA", "AR", "AN", "AD", "AC", "AE", "AQ", "AG", "AI", "AL", "AK", "AF", "AP", "AS", "AT", "AY", "AV",
|
|
|
39 |
|
40 |
def extract_features(sequence):
|
41 |
"""Extract only the required features and normalize them."""
|
42 |
+
# Compute all possible features
|
43 |
+
all_features = AAComposition.CalculateAADipeptideComposition(sequence) # Amino Acid Composition
|
44 |
+
# Extract the values from the dictionary
|
45 |
+
feature_values = list(all_features.values()) # Extract values only
|
46 |
+
# Convert to NumPy array for normalization
|
47 |
+
feature_array = np.array(feature_values).reshape(-1, 1)
|
48 |
+
feature_array = feature_array[: 420]
|
49 |
+
# Min-Max Normalization
|
50 |
+
normalized_features = scaler.transform(feature_array.T)
|
51 |
+
|
52 |
+
# Reshape normalized_features back to a single dimension
|
53 |
+
normalized_features = normalized_features.flatten() # Flatten array
|
54 |
|
55 |
+
# Create a dictionary with selected features
|
56 |
+
selected_feature_dict = {feature: normalized_features[i] for i, feature in enumerate(selected_features)
|
57 |
+
if feature in all_features}
|
|
|
|
|
58 |
|
59 |
+
# Convert dictionary to dataframe
|
60 |
selected_feature_df = pd.DataFrame([selected_feature_dict])
|
61 |
+
|
62 |
+
# Convert dataframe to numpy array
|
63 |
+
selected_feature_array = selected_feature_df.T.to_numpy()
|
64 |
+
|
65 |
+
return selected_feature_array
|
66 |
+
|
67 |
+
|
68 |
|
69 |
def predict(sequence):
|
70 |
"""Predict AMP vs Non-AMP"""
|
71 |
features = extract_features(sequence)
|
72 |
prediction = model.predict(features.T)[0]
|
73 |
+
return "AMP" if prediction == 0 else "Non-AMP"
|
74 |
|
75 |
+
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
iface = gr.Interface(
|
77 |
fn=predict,
|
78 |
inputs=gr.Textbox(label="Enter Protein Sequence"),
|
79 |
outputs=gr.Label(label="Prediction"),
|
80 |
title="AMP Classifier",
|
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)
|
|