Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- Dockerfile +17 -0
- README.md +24 -12
- ann_model.keras +0 -0
- app.py +67 -0
- catboost_model.cbm +3 -0
- logistic_regression (1).joblib +3 -0
- random_forest.joblib +3 -0
- requirements.txt +12 -0
- static/index.html +42 -0
- svm.joblib +3 -0
- voting_classifier.joblib +3 -0
- xgboost.joblib +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
catboost_model.cbm filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use official Python image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy all files to container
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Set port Hugging Face expects
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Run the FastAPI app on port 9000
|
| 17 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]
|
README.md
CHANGED
|
@@ -1,12 +1,24 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: docker
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: FastAPI ML API
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: docker
|
| 7 |
+
sdk_version: "1.0"
|
| 8 |
+
app_file: main.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# FastAPI ML API
|
| 13 |
+
|
| 14 |
+
This Space hosts a FastAPI backend for multiple ML models including ANN, XGBoost, Random Forest, and others.
|
| 15 |
+
|
| 16 |
+
## Features
|
| 17 |
+
- JWT Authentication
|
| 18 |
+
- Predict using multiple ML models
|
| 19 |
+
- Deployed with Docker
|
| 20 |
+
|
| 21 |
+
## How to Use
|
| 22 |
+
|
| 23 |
+
1. Get a token using `/token`
|
| 24 |
+
2. Use `/predict/{model}` to get predictions
|
ann_model.keras
ADDED
|
Binary file (61.5 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
import joblib
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import numpy as np
|
| 7 |
+
from catboost import CatBoostClassifier
|
| 8 |
+
from fastapi.templating import Jinja2Templates
|
| 9 |
+
from fastapi.responses import HTMLResponse
|
| 10 |
+
# App setup
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
templates = Jinja2Templates(directory="templates")
|
| 13 |
+
catboost_model = CatBoostClassifier()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@app.get("/", response_class=HTMLResponse)
|
| 17 |
+
def read_index(request: Request):
|
| 18 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 19 |
+
|
| 20 |
+
# Input model
|
| 21 |
+
class PredictionInput(BaseModel):
|
| 22 |
+
features: List[float]
|
| 23 |
+
|
| 24 |
+
# Load models once
|
| 25 |
+
ann_model = tf.keras.models.load_model("ann_model.keras")
|
| 26 |
+
xgb_model = joblib.load("xgboost.joblib")
|
| 27 |
+
voting_model = joblib.load("voting_classifier.joblib")
|
| 28 |
+
svm_model = joblib.load("svm.joblib")
|
| 29 |
+
rf_model = joblib.load("random_forest.joblib")
|
| 30 |
+
lr_model = joblib.load("logistic_regression (1).joblib")
|
| 31 |
+
catboost_model.load_model("catboost_model.cbm")
|
| 32 |
+
|
| 33 |
+
# Prediction endpoints (No auth)
|
| 34 |
+
@app.post("/predict/ann")
|
| 35 |
+
def predict_ann(input_data: PredictionInput):
|
| 36 |
+
prediction = ann_model.predict(np.array([input_data.features]))
|
| 37 |
+
return {"model": "ANN", "prediction": prediction.tolist()}
|
| 38 |
+
|
| 39 |
+
@app.post("/predict/xgboost")
|
| 40 |
+
def predict_xgboost(input_data: PredictionInput):
|
| 41 |
+
prediction = xgb_model.predict([input_data.features])
|
| 42 |
+
return {"model": "XGBoost", "prediction": prediction.tolist()}
|
| 43 |
+
|
| 44 |
+
@app.post("/predict/voting")
|
| 45 |
+
def predict_voting(input_data: PredictionInput):
|
| 46 |
+
prediction = voting_model.predict([input_data.features])
|
| 47 |
+
return {"model": "VotingClassifier", "prediction": prediction.tolist()}
|
| 48 |
+
|
| 49 |
+
@app.post("/predict/svm")
|
| 50 |
+
def predict_svm(input_data: PredictionInput):
|
| 51 |
+
prediction = svm_model.predict([input_data.features])
|
| 52 |
+
return {"model": "SVM", "prediction": prediction.tolist()}
|
| 53 |
+
|
| 54 |
+
@app.post("/predict/randomforest")
|
| 55 |
+
def predict_rf(input_data: PredictionInput):
|
| 56 |
+
prediction = rf_model.predict([input_data.features])
|
| 57 |
+
return {"model": "RandomForest", "prediction": prediction.tolist()}
|
| 58 |
+
|
| 59 |
+
@app.post("/predict/logistic")
|
| 60 |
+
def predict_lr(input_data: PredictionInput):
|
| 61 |
+
prediction = lr_model.predict([input_data.features])
|
| 62 |
+
return {"model": "LogisticRegression", "prediction": prediction.tolist()}
|
| 63 |
+
|
| 64 |
+
@app.post("/predict/catboost")
|
| 65 |
+
def predict_catboost(input_data: PredictionInput):
|
| 66 |
+
prediction = catboost_model.predict([input_data.features])
|
| 67 |
+
return {"model": "CatBoost", "prediction": prediction.tolist()}
|
catboost_model.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c93fa07ad253661f268a2eb568e1a6b7a65e8ba8829e520b752256111b9e9346
|
| 3 |
+
size 1076972
|
logistic_regression (1).joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:974435fb5256123aa848decabe0ada5240880129868cb162fd04397354b49143
|
| 3 |
+
size 975
|
random_forest.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:36e70c7e66b3c5c45c5fb4f384c7e9464e13e41e51eb08b3cca0f6fd1a509b41
|
| 3 |
+
size 275865
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
joblib
|
| 4 |
+
numpy
|
| 5 |
+
tensorflow
|
| 6 |
+
catboost
|
| 7 |
+
xgboost
|
| 8 |
+
scikit-learn
|
| 9 |
+
python-multipart
|
| 10 |
+
python-jose
|
| 11 |
+
pydantic
|
| 12 |
+
jinja2
|
static/index.html
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>ML Model Predictor</title>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<h2>📊 Predict</h2>
|
| 8 |
+
<select id="model">
|
| 9 |
+
<option value="ann">ANN</option>
|
| 10 |
+
<option value="xgboost">XGBoost</option>
|
| 11 |
+
<option value="voting">VotingClassifier</option>
|
| 12 |
+
<option value="svm">SVM</option>
|
| 13 |
+
<option value="randomforest">RandomForest</option>
|
| 14 |
+
<option value="logistic">LogisticRegression</option>
|
| 15 |
+
<option value="catboost">CatBoost</option>
|
| 16 |
+
</select><br><br>
|
| 17 |
+
|
| 18 |
+
<input id="features" placeholder="Comma-separated features" />
|
| 19 |
+
<button onclick="predict()">Predict</button>
|
| 20 |
+
|
| 21 |
+
<pre id="result"></pre>
|
| 22 |
+
|
| 23 |
+
<script>
|
| 24 |
+
async function predict() {
|
| 25 |
+
const model = document.getElementById("model").value;
|
| 26 |
+
const featureString = document.getElementById("features").value;
|
| 27 |
+
const features = featureString.split(",").map(Number);
|
| 28 |
+
|
| 29 |
+
const res = await fetch(`/predict/${model}`, {
|
| 30 |
+
method: "POST",
|
| 31 |
+
headers: {
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
},
|
| 34 |
+
body: JSON.stringify({ features })
|
| 35 |
+
});
|
| 36 |
+
|
| 37 |
+
const data = await res.json();
|
| 38 |
+
document.getElementById("result").textContent = JSON.stringify(data, null, 2);
|
| 39 |
+
}
|
| 40 |
+
</script>
|
| 41 |
+
</body>
|
| 42 |
+
</html>
|
svm.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:61c319eb3fc58505effc385ca6657f5aaf1e3486b448d5d415516e309e56e0d6
|
| 3 |
+
size 12427
|
voting_classifier.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5c30be5d8287c9280bcb049234c20015f9ed885339d2dfd0a24e2ca113f7e5c6
|
| 3 |
+
size 13729
|
xgboost.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c4bc0d4820334362b7233ff246b913068446f276e1298792a65ba4e3be7a226
|
| 3 |
+
size 88783
|