Spaces:
Configuration error
Configuration error
maryangel101 commited on
Commit ·
7340e3d
1
Parent(s): 62ccdb1
Deploy complete CI/CD anomaly detection system
Browse files- .github/workflows/deploy.yml +18 -0
- .gitignore +6 -0
- api_service.py +20 -8
- app.py +46 -0
- requirements.txt +6 -11
- requirements_deploy.txt +6 -0
.github/workflows/deploy.yml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Deploy to Hugging Face
|
| 2 |
+
on:
|
| 3 |
+
push:
|
| 4 |
+
branches: [main]
|
| 5 |
+
workflow_dispatch:
|
| 6 |
+
|
| 7 |
+
jobs:
|
| 8 |
+
deploy:
|
| 9 |
+
runs-on: ubuntu-latest
|
| 10 |
+
steps:
|
| 11 |
+
- uses: actions/checkout@v3
|
| 12 |
+
- name: Deploy to Hugging Face
|
| 13 |
+
uses: huggingface/huggingface_hub@main
|
| 14 |
+
with:
|
| 15 |
+
huggingface_token: ${{ secrets.HUGGINGFACE_TOKEN }}
|
| 16 |
+
huggingface_repo: your-username/ci-cd-anomaly-detection
|
| 17 |
+
git_config_user: "github-actions"
|
| 18 |
+
git_config_email: "github-actions@github.com"
|
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model files (too large for git)
|
| 2 |
+
anomaly_model.pkl
|
| 3 |
+
vectorizer.pkl
|
| 4 |
+
*.pkl
|
| 5 |
+
__pycache__/
|
| 6 |
+
*.pyc
|
api_service.py
CHANGED
|
@@ -4,15 +4,15 @@ import joblib
|
|
| 4 |
import re
|
| 5 |
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
-
import shap
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="CI/CD Log Anomaly Detection API")
|
| 11 |
|
| 12 |
# Global variables for model and vectorizer
|
| 13 |
model = None
|
| 14 |
vectorizer = None
|
| 15 |
-
explainer = None
|
| 16 |
|
| 17 |
class LogRequest(BaseModel):
|
| 18 |
log_content: str
|
|
@@ -46,20 +46,32 @@ def clean_log(log_content):
|
|
| 46 |
@app.on_event("startup")
|
| 47 |
async def load_model():
|
| 48 |
"""Load the trained model and vectorizer on startup"""
|
| 49 |
-
global model, vectorizer
|
| 50 |
|
| 51 |
try:
|
| 52 |
model = joblib.load('anomaly_model.pkl')
|
| 53 |
vectorizer = joblib.load('vectorizer.pkl')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
def get_feature_importance_explanation(log_content, prediction_proba):
|
| 62 |
-
"""Get simple feature importance explanation"""
|
| 63 |
try:
|
| 64 |
cleaned_log = clean_log(log_content)
|
| 65 |
log_vector = vectorizer.transform([cleaned_log])
|
|
|
|
| 4 |
import re
|
| 5 |
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
+
# REMOVE: import shap # This is causing the error
|
| 9 |
|
| 10 |
app = FastAPI(title="CI/CD Log Anomaly Detection API")
|
| 11 |
|
| 12 |
# Global variables for model and vectorizer
|
| 13 |
model = None
|
| 14 |
vectorizer = None
|
| 15 |
+
# REMOVE: explainer = None # Not needed without SHAP
|
| 16 |
|
| 17 |
class LogRequest(BaseModel):
|
| 18 |
log_content: str
|
|
|
|
| 46 |
@app.on_event("startup")
|
| 47 |
async def load_model():
|
| 48 |
"""Load the trained model and vectorizer on startup"""
|
| 49 |
+
global model, vectorizer
|
| 50 |
|
| 51 |
try:
|
| 52 |
model = joblib.load('anomaly_model.pkl')
|
| 53 |
vectorizer = joblib.load('vectorizer.pkl')
|
| 54 |
+
print("✅ Real model loaded successfully")
|
| 55 |
+
except FileNotFoundError:
|
| 56 |
+
print("⚠️ No trained model found. Creating mock model...")
|
| 57 |
+
# Create a simple mock model
|
| 58 |
+
texts = [
|
| 59 |
+
"error failure exception crash",
|
| 60 |
+
"success passed completed ok",
|
| 61 |
+
"warning slow performance issue",
|
| 62 |
+
"build successful tests passed"
|
| 63 |
+
]
|
| 64 |
+
labels = [1, 0, 1, 0] # 1=anomaly, 0=normal
|
| 65 |
|
| 66 |
+
vectorizer = TfidfVectorizer(max_features=50)
|
| 67 |
+
X = vectorizer.fit_transform(texts)
|
| 68 |
|
| 69 |
+
model = RandomForestClassifier(n_estimators=10, random_state=42)
|
| 70 |
+
model.fit(X, labels)
|
| 71 |
+
print("✅ Mock model created for deployment")
|
| 72 |
|
| 73 |
def get_feature_importance_explanation(log_content, prediction_proba):
|
| 74 |
+
"""Get simple feature importance explanation without SHAP"""
|
| 75 |
try:
|
| 76 |
cleaned_log = clean_log(log_content)
|
| 77 |
log_vector = vectorizer.transform([cleaned_log])
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import joblib
|
| 4 |
+
import re
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 7 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="CI/CD Log Anomaly Detection API")
|
| 10 |
+
|
| 11 |
+
# Mock model setup
|
| 12 |
+
def setup_model():
|
| 13 |
+
texts = ["error fail exception", "success passed", "warning slow"]
|
| 14 |
+
labels = [1, 0, 1]
|
| 15 |
+
|
| 16 |
+
vectorizer = TfidfVectorizer(max_features=50)
|
| 17 |
+
X = vectorizer.fit_transform(texts)
|
| 18 |
+
|
| 19 |
+
model = RandomForestClassifier(n_estimators=10, random_state=42)
|
| 20 |
+
model.fit(X, labels)
|
| 21 |
+
|
| 22 |
+
return model, vectorizer
|
| 23 |
+
|
| 24 |
+
model, vectorizer = setup_model()
|
| 25 |
+
|
| 26 |
+
@app.post("/predict")
|
| 27 |
+
async def predict(log_content: str):
|
| 28 |
+
try:
|
| 29 |
+
# Simple prediction
|
| 30 |
+
text_clean = re.sub(r'\s+', ' ', log_content.lower())
|
| 31 |
+
X = vectorizer.transform([text_clean])
|
| 32 |
+
|
| 33 |
+
prediction = model.predict(X)[0]
|
| 34 |
+
proba = model.predict_proba(X)[0]
|
| 35 |
+
|
| 36 |
+
return {
|
| 37 |
+
"is_anomaly": bool(prediction),
|
| 38 |
+
"confidence": float(max(proba)),
|
| 39 |
+
"anomaly_probability": float(proba[1])
|
| 40 |
+
}
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
import uvicorn
|
| 46 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,11 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
uvicorn==0.23.2
|
| 8 |
-
gradio==3.44.0
|
| 9 |
-
PyGithub==1.59.0
|
| 10 |
-
tqdm==4.65.0
|
| 11 |
-
pytest==7.4.0
|
|
|
|
| 1 |
+
fastapi==0.100.1
|
| 2 |
+
uvicorn==0.23.2
|
| 3 |
+
scikit-learn==1.3.0
|
| 4 |
+
pandas==2.0.3
|
| 5 |
+
numpy==1.24.3
|
| 6 |
+
joblib==1.3.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements_deploy.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.100.1
|
| 2 |
+
uvicorn==0.23.2
|
| 3 |
+
scikit-learn==1.3.0
|
| 4 |
+
pandas==2.0.3
|
| 5 |
+
numpy==1.24.3
|
| 6 |
+
joblib==1.3.2
|