shreyankisiri commited on
Commit
8350ed6
·
verified ·
1 Parent(s): de61123

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import joblib
4
+
5
+
6
+ model = joblib.load("model.joblib")
7
+ tfidf_vectorizer = joblib.load("tfidf_vectorizer.joblib")
8
+
9
+
10
+ class TextInput(BaseModel):
11
+ text: str
12
+
13
+
14
+ app = FastAPI()
15
+
16
+
17
+ @app.post("/predict")
18
+ def predict(input: TextInput):
19
+
20
+ processed_text = preprocess_text(input.text)
21
+
22
+
23
+ text_tfidf = tfidf_vectorizer.transform([processed_text])
24
+
25
+ prediction = model.predict(text_tfidf)
26
+
27
+ return {"prediction": "Spam" if int(prediction[0]) == 0 else "Ham"}
28
+
29
+ def preprocess_text(text):
30
+ import re
31
+ from nltk.stem import WordNetLemmatizer
32
+ from nltk.corpus import stopwords
33
+ lemmatizer = WordNetLemmatizer()
34
+ stop_words = set(stopwords.words('english'))
35
+
36
+ text = re.sub('[^a-zA-Z]', ' ', text)
37
+ text = text.lower()
38
+ words = text.split()
39
+ words = [lemmatizer.lemmatize(word) for word in words if word not in stop_words]
40
+ return ' '.join(words)