-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (39 loc) · 976 Bytes
/
main.py
File metadata and controls
49 lines (39 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import string
import nltk
from nltk.stem import *
from nltk.corpus import stopwords
from pydantic import BaseModel
from fastapi import FastAPI
import pickle
def transform_text(text):
text = text.lower()
text = nltk.word_tokenize(text)
ps = PorterStemmer()
y=[]
for i in text:
if i.isalnum():
y.append(i)
text = y[:]
y.clear()
for i in text:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
text = y[:]
y.clear()
for i in text:
y.append(ps.stem(i))
return " ".join(y)
model=pickle.load(open('model.pkl','rb'))
tf=pickle.load(open('vectorizer.pkl','rb'))
class Query(BaseModel):
message: str
app = FastAPI()
@app.get("/")
async def root(query:Query):
message_transformed=transform_text(query.message)
message_vector=tf.transform([message_transformed])
pred = model.predict(message_vector)[0]
if(pred==1):
return {"spam": 1}
else:
return {"spam": 0}