Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
from fastapi import FastAPI
from fastapi.responses import FileResponse

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import json
from sklearn.feature_extraction.text import CountVectorizer

app = FastAPI()

# Load the trained model
model = joblib.load('naive_bayes_model.joblib')

class Item(BaseModel):
item_id: int


@app.get("/")
async def root():
return {"message": "Hello World"}


@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
return FileResponse('favicon.ico')


@app.get("/item/{item_id}")
async def read_item(item_id: int):
return {"item_id": item_id}
# Load the vocabulary
with open('vocabulary.json', 'r') as vocab_file:
vocabulary = json.load(vocab_file)

# Define request body model
class TextData(BaseModel):
text: str

@app.get("/items/")
async def list_items():
return [{"item_id": 1, "name": "Foo"}, {"item_id": 2, "name": "Bar"}]
# Function to vectorize new data
def vectorize_text(text):
vectorizer = CountVectorizer(vocabulary=vocabulary)
return vectorizer.transform([text])

@app.post("/predict/")
def predict(text_data: TextData):
text = text_data.text
text_vectorized = vectorize_text(text)
prediction = model.predict(text_vectorized)
return {"category": prediction[0]}

@app.post("/items/")
async def create_item(item: Item):
return item
Binary file added naive_bayes_model.joblib
Binary file not shown.
1 change: 1 addition & 0 deletions vocabulary.json

Large diffs are not rendered by default.