Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import joblib
|
2 |
+
import re
|
3 |
+
import pandas as pd
|
4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
5 |
+
from sklearn.naive_bayes import MultinomialNB
|
6 |
+
from fastapi import FastAPI
|
7 |
+
from pydantic import BaseModel
|
8 |
+
|
9 |
+
# Load the model and vectorizer
|
10 |
+
vectorizer = joblib.load("vectorizer.joblib")
|
11 |
+
model = joblib.load("naive_bayes_model.joblib")
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
class URLInput(BaseModel):
|
16 |
+
url: str
|
17 |
+
|
18 |
+
def preprocess_url(url):
|
19 |
+
url = re.sub(r"http\S+", "", url)
|
20 |
+
url = re.sub(r"\d+", "", url)
|
21 |
+
url = re.sub(r"\W", " ", url)
|
22 |
+
url = url.lower()
|
23 |
+
return url
|
24 |
+
|
25 |
+
@app.post("/predict")
|
26 |
+
def predict_url(url_input: URLInput):
|
27 |
+
processed_url = preprocess_url(url_input.url)
|
28 |
+
vectorized_url = vectorizer.transform([processed_url])
|
29 |
+
prediction = model.predict(vectorized_url)
|
30 |
+
return {"prediction": prediction[0]}
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
import uvicorn
|
34 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|