Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +2 -1
- main/helper.py +9 -1
- main/model_inference.py +16 -8
- main/validate_schema.py +3 -3
Dockerfile
CHANGED
|
@@ -6,7 +6,8 @@ COPY . /app
|
|
| 6 |
|
| 7 |
WORKDIR /app
|
| 8 |
|
| 9 |
-
RUN pip install
|
|
|
|
| 10 |
pip install -r model/requirements.txt
|
| 11 |
|
| 12 |
EXPOSE 7860
|
|
|
|
| 6 |
|
| 7 |
WORKDIR /app
|
| 8 |
|
| 9 |
+
RUN pip install --upgrade pip && \
|
| 10 |
+
pip install -r requirements.txt && \
|
| 11 |
pip install -r model/requirements.txt
|
| 12 |
|
| 13 |
EXPOSE 7860
|
main/helper.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
import yaml
|
| 4 |
import joblib
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
# load yaml files to get model meta data.
|
|
@@ -32,4 +33,11 @@ def get_model_registry():
|
|
| 32 |
def get_model_version():
|
| 33 |
""" Fetches the model version and returns it. """
|
| 34 |
model_version = model_metadata['model_version']
|
| 35 |
-
return model_version
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import yaml
|
| 4 |
import joblib
|
| 5 |
+
import pandas as pd
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
# load yaml files to get model meta data.
|
|
|
|
| 33 |
def get_model_version():
|
| 34 |
""" Fetches the model version and returns it. """
|
| 35 |
model_version = model_metadata['model_version']
|
| 36 |
+
return model_version
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def format_model_input(tweet: str) -> pd.DataFrame:
|
| 40 |
+
df = pd.DataFrame({
|
| 41 |
+
"comments" : tweet
|
| 42 |
+
})
|
| 43 |
+
return df
|
main/model_inference.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
-
from main.validate_schema import
|
| 4 |
from datetime import datetime
|
| 5 |
from main.helper import *
|
| 6 |
import uuid, time
|
|
@@ -10,20 +10,29 @@ model = load_model()
|
|
| 10 |
# Initializing fastapi
|
| 11 |
inference_api = FastAPI()
|
| 12 |
|
| 13 |
-
@inference_api.
|
| 14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
timestamp = datetime.now().astimezone().isoformat()
|
| 16 |
request_id = str(uuid.uuid4())
|
| 17 |
-
|
| 18 |
start_time = time.perf_counter()
|
|
|
|
| 19 |
tweet = payload.comment
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
label = int(model_response["class_label"][0])
|
| 23 |
probability_scores = model_response["class_probability_scores"]
|
| 24 |
proba_class0 = float(probability_scores[0][0])
|
| 25 |
proba_class1 = float(probability_scores[0][1])
|
| 26 |
-
|
| 27 |
end_time = time.perf_counter()
|
| 28 |
|
| 29 |
if proba_class1 > 0.70:
|
|
@@ -35,9 +44,8 @@ def api(payload: UserInput):
|
|
| 35 |
else:
|
| 36 |
toxic_level = "none"
|
| 37 |
|
| 38 |
-
|
| 39 |
response = {
|
| 40 |
-
"
|
| 41 |
"class_label": label,
|
| 42 |
"confidence": round(abs(proba_class0 - proba_class1), 4),
|
| 43 |
"toxic_level": toxic_level,
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
from main.validate_schema import InputData, APIResponse
|
| 4 |
from datetime import datetime
|
| 5 |
from main.helper import *
|
| 6 |
import uuid, time
|
|
|
|
| 10 |
# Initializing fastapi
|
| 11 |
inference_api = FastAPI()
|
| 12 |
|
| 13 |
+
@inference_api.get("/")
|
| 14 |
+
def root():
|
| 15 |
+
return JSONResponse(content={
|
| 16 |
+
"status": 200,
|
| 17 |
+
"message": "Inference API is running."
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@inference_api.post('/get_prediction', response_model=APIResponse)
|
| 22 |
+
def api(payload: InputData):
|
| 23 |
timestamp = datetime.now().astimezone().isoformat()
|
| 24 |
request_id = str(uuid.uuid4())
|
|
|
|
| 25 |
start_time = time.perf_counter()
|
| 26 |
+
|
| 27 |
tweet = payload.comment
|
| 28 |
+
model_input = format_model_input(tweet)
|
| 29 |
+
model_response = model.predict(model_input)
|
| 30 |
|
| 31 |
label = int(model_response["class_label"][0])
|
| 32 |
probability_scores = model_response["class_probability_scores"]
|
| 33 |
proba_class0 = float(probability_scores[0][0])
|
| 34 |
proba_class1 = float(probability_scores[0][1])
|
| 35 |
+
|
| 36 |
end_time = time.perf_counter()
|
| 37 |
|
| 38 |
if proba_class1 > 0.70:
|
|
|
|
| 44 |
else:
|
| 45 |
toxic_level = "none"
|
| 46 |
|
|
|
|
| 47 |
response = {
|
| 48 |
+
"prediction": {
|
| 49 |
"class_label": label,
|
| 50 |
"confidence": round(abs(proba_class0 - proba_class1), 4),
|
| 51 |
"toxic_level": toxic_level,
|
main/validate_schema.py
CHANGED
|
@@ -2,11 +2,11 @@ from pydantic import BaseModel, Field
|
|
| 2 |
from typing import Annotated, Dict
|
| 3 |
|
| 4 |
|
| 5 |
-
class
|
| 6 |
comment: Annotated[str, Field(..., description="User tweet or comment to be classified")]
|
| 7 |
|
| 8 |
|
| 9 |
-
class
|
| 10 |
class_label: int
|
| 11 |
confidence: float
|
| 12 |
toxic_level: str
|
|
@@ -28,5 +28,5 @@ class MetaData(BaseModel):
|
|
| 28 |
|
| 29 |
|
| 30 |
class APIResponse(BaseModel):
|
| 31 |
-
response:
|
| 32 |
metadata: MetaData
|
|
|
|
| 2 |
from typing import Annotated, Dict
|
| 3 |
|
| 4 |
|
| 5 |
+
class InputData(BaseModel):
|
| 6 |
comment: Annotated[str, Field(..., description="User tweet or comment to be classified")]
|
| 7 |
|
| 8 |
|
| 9 |
+
class Prediction(BaseModel):
|
| 10 |
class_label: int
|
| 11 |
confidence: float
|
| 12 |
toxic_level: str
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
class APIResponse(BaseModel):
|
| 31 |
+
response: Prediction
|
| 32 |
metadata: MetaData
|