Spaces:
Runtime error
Runtime error
File size: 986 Bytes
a51c1d1 |
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 |
from flask import Flask, request, jsonify
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import string
# Load the data and preprocess it
df = pd.read_csv('dataset/spam.tsv', sep='\t', names=['label', 'message'])
df['message'] = df['message'].apply(lambda text: ''.join(char for char in text if char not in string.punctuation))
CV = CountVectorizer(stop_words='english')
X = df['message'].values
y = df['label'].values
# Train the model
X_train_CV = CV.fit_transform(X)
NB = MultinomialNB()
NB.fit(X_train_CV, y)
# Create the Flask app
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
message = data['message']
message_cv = CV.transform([message])
prediction = NB.predict(message_cv)[0]
status = 'SPAM' if prediction == 'spam' else 'HAM'
return jsonify({'status': status})
if __name__ == '__main__':
app.run(debug=True) |