BhavyaSoni21 commited on
Commit
5b0fc4d
·
verified ·
1 Parent(s): 4a95b25

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -0
  2. app.py +61 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . .
6
+
7
+ RUN pip install -r requirements.txt
8
+
9
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import joblib
4
+ import numpy as np
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+
7
+ app = FastAPI(title="Web Attack Detection API")
8
+
9
+ # Allow browser requests
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"],
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ # Expected number of features
19
+ EXPECTED_FEATURES = 35
20
+
21
+ # Request schema
22
+ class InputData(BaseModel):
23
+ features: list[float]
24
+
25
+ # Load model
26
+ try:
27
+ model = joblib.load("web_attack_detection_model.pkl")
28
+ except Exception as e:
29
+ raise RuntimeError(f"Model failed to load: {e}")
30
+
31
+ @app.get("/")
32
+ def home():
33
+ return {"message": "Web attack detection model running"}
34
+
35
+ @app.get("/health")
36
+ def health():
37
+ return {"status": "ok"}
38
+
39
+ @app.post("/predict")
40
+ def predict(data: InputData):
41
+
42
+ if len(data.features) != EXPECTED_FEATURES:
43
+ raise HTTPException(
44
+ status_code=400,
45
+ detail=f"Expected {EXPECTED_FEATURES} features"
46
+ )
47
+
48
+ input_data = np.array(data.features).reshape(1, -1)
49
+
50
+ prediction = model.predict(input_data)[0]
51
+
52
+ # Convert prediction to readable output
53
+ if prediction == -1:
54
+ result = "attack detected"
55
+ else:
56
+ result = "normal request"
57
+
58
+ return {
59
+ "prediction": result,
60
+ "raw_prediction": int(prediction)
61
+ }
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ numpy
4
+ pandas
5
+ joblib
6
+ scikit-learn==1.6.1
7
+ python-multipart
8
+ pydantic