Spaces:
Runtime error
Runtime error
panchajanya1999
commited on
flask: Create a new flask file
Browse filesSigned-off-by: Panchajanya1999 <[email protected]>
- flask_spam_class.py +33 -0
flask_spam_class.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
4 |
+
from sklearn.naive_bayes import MultinomialNB
|
5 |
+
|
6 |
+
import string
|
7 |
+
|
8 |
+
# Load the data and preprocess it
|
9 |
+
df = pd.read_csv('dataset/spam.tsv', sep='\t', names=['label', 'message'])
|
10 |
+
df['message'] = df['message'].apply(lambda text: ''.join(char for char in text if char not in string.punctuation))
|
11 |
+
CV = CountVectorizer(stop_words='english')
|
12 |
+
X = df['message'].values
|
13 |
+
y = df['label'].values
|
14 |
+
|
15 |
+
# Train the model
|
16 |
+
X_train_CV = CV.fit_transform(X)
|
17 |
+
NB = MultinomialNB()
|
18 |
+
NB.fit(X_train_CV, y)
|
19 |
+
|
20 |
+
# Create the Flask app
|
21 |
+
app = Flask(__name__)
|
22 |
+
|
23 |
+
@app.route('/predict', methods=['POST'])
|
24 |
+
def predict():
|
25 |
+
data = request.get_json()
|
26 |
+
message = data['message']
|
27 |
+
message_cv = CV.transform([message])
|
28 |
+
prediction = NB.predict(message_cv)[0]
|
29 |
+
status = 'SPAM' if prediction == 'spam' else 'HAM'
|
30 |
+
return jsonify({'status': status})
|
31 |
+
|
32 |
+
if __name__ == '__main__':
|
33 |
+
app.run(debug=True)
|