Skip to content

Commit 6b2dfc0

Browse files
author
Joshua Mayanja
committed
Fixed Sequence Bug
1 parent 0f9ec2f commit 6b2dfc0

11 files changed

+490
-6
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.formatting.provider": "black"
3+
}

app/__init__.py

Whitespace-only changes.
104 Bytes
Binary file not shown.

app/__pycache__/main.cpython-37.pyc

1.74 KB
Binary file not shown.

app/main.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
from multiprocessing.spawn import spawn_main
3+
import pathlib
4+
from typing import Optional
5+
from fastapi import FastAPI
6+
from keras.models import load_model
7+
from keras_preprocessing.text import tokenizer_from_json
8+
from keras_preprocessing.sequence import pad_sequences
9+
10+
app = FastAPI(
11+
version="1.0.0",
12+
title="DrexSpam",
13+
description="An Artificial Intelligence based Spam detector API using machine learning",
14+
)
15+
16+
BASE_DIR = pathlib.Path(__file__).resolve().parent
17+
18+
MODEL_DIR = BASE_DIR.parent / "models"
19+
SPAM_MODEL_DIR = MODEL_DIR / "spam"
20+
SPAM_MODEL_PATH = SPAM_MODEL_DIR / "spam-model.h5"
21+
SPAM_TOKENIZER_PATH = SPAM_MODEL_DIR / "spam-classifer-tokenizer.json"
22+
SPAM_METADATA_PATH = SPAM_MODEL_DIR / "spam-classifer-metadata.json"
23+
24+
SPAM_MODEL = None
25+
SPAM_TOKENIZER = None
26+
SPAM_METADATA = {}
27+
LEGEND_INVERTED = {}
28+
29+
@app.on_event("startup")
30+
def on_startup():
31+
global SPAM_MODEL, SPAM_TOKENIZER, SPAM_METADATA, LEGEND_INVERTED
32+
# Load model
33+
if SPAM_MODEL_PATH.exists():
34+
SPAM_MODEL = load_model(SPAM_MODEL_PATH)
35+
if SPAM_TOKENIZER_PATH.exists():
36+
t_json = SPAM_TOKENIZER_PATH.read_text()
37+
SPAM_TOKENIZER = tokenizer_from_json(t_json)
38+
if SPAM_METADATA_PATH.exists():
39+
SPAM_METADATA = json.loads(SPAM_METADATA_PATH.read_text())
40+
LEGEND_INVERTED = SPAM_METADATA["labels_legend_inverted"]
41+
42+
def predict(query: str):
43+
sequences = SPAM_TOKENIZER.texts_to_sequences([query])
44+
maxlen = SPAM_METADATA.get("max_sequence") or 280
45+
x_input = pad_sequences(sequences, maxlen=280)
46+
preds_array = SPAM_MODEL.predict(x_input)
47+
return {}
48+
49+
50+
@app.get("/")
51+
def read_index(q: Optional[str] = None):
52+
global SPAM_MODEL, SPAM_METADATA
53+
query = q or "Hello world"
54+
print(SPAM_MODEL)
55+
return {"query": query, **SPAM_METADATA}

datasets/exports/spam-tokenizer.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"labels_legend_inverted": {
3+
"0": "ham",
4+
"1": "spam"
5+
},
6+
"legend": {
7+
"ham": 0,
8+
"spam": 1
9+
},
10+
"max_sequence": 300,
11+
"max_words": 280
12+
}

models/spam/spam-classifer-tokenizer.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

models/spam/spam-model.h5

3.36 MB
Binary file not shown.

0 commit comments

Comments
 (0)