Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738import gradio as grimport joblibimport numpy as npfrom propy import AACompositionfrom sklearn.preprocessing import MinMaxScaler# Load trained SVM model and scaler (Ensure both files exist in the Space)model = joblib.load("SVM.joblib")scaler = MinMaxScaler()def extract_features(sequence):"""Calculate AAC, Dipeptide Composition, and normalize features."""# Calculate Amino Acid Composition (AAC) and convert to arrayaac = np.array(list(AAComposition.CalculateAADipeptideComposition(sequence)), dtype=float)# Normalize using the pre-trained scaler (Ensure the scaler is loaded correctly)normalized_features = scaler.fit_transform([aac]) # Don't use fit_transform(), only transform()return normalized_featuresdef predict(sequence):"""Predict AMP vs Non-AMP"""features = extract_features(sequence)prediction = model.predict(features)[0]return "AMP" if prediction == 1 else "Non-AMP"# Create Gradio interfaceiface = gr.Interface(fn=predict,inputs=gr.Textbox(label="Enter Protein Sequence"),outputs=gr.Label(label="Prediction"),title="AMP Classifier",description="Enter an amino acid sequence to predict whether it's an antimicrobial peptide (AMP) or not.")# Launch appiface.launch(share=True)123456789101112131415161718192021222324252627282930313233343536from fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelimport joblibimport numpy as npfrom propy import AACompositionfrom sklearn.preprocessing import MinMaxScaler# Initialize FastAPI appapp = FastAPI()# Load trained SVM model and scalermodel = joblib.load("SVM.joblib")scaler = MinMaxScaler()# Define request schemaclass SequenceInput(BaseModel):sequence: strdef 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_featuresexcept 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
app.py+24-26