Spaces:
Running
Running
import pandas as pd | |
import numpy as np | |
from flask import Flask, request, jsonify | |
from sklearn.model_selection import train_test_split | |
from sklearn.feature_extraction.text import CountVectorizer | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.naive_bayes import MultinomialNB | |
from sklearn.tree import DecisionTreeClassifier | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.svm import SVC | |
from sklearn.metrics import accuracy_score | |
# Initialize Flask app | |
app = Flask(__name__) | |
# Read dataset | |
df = pd.read_csv(r"spam.csv") | |
# Define feature and target variables | |
x = df["Message"] | |
y = df["Category"] | |
# Create a Bag of Words (BoW) model | |
bow = CountVectorizer(stop_words="english") | |
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out()) | |
# Train-test split | |
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.2, random_state=20) | |
# Initialize models | |
models = { | |
"Naive Bayes": MultinomialNB(), | |
"KNN": KNeighborsClassifier(), | |
"Logistic Regression": LogisticRegression(), | |
"Decision Tree": DecisionTreeClassifier(), | |
"SVM": SVC() | |
} | |
# Choose and train a model | |
model_choice = "Naive Bayes" # Default model | |
obj = models[model_choice] | |
obj.fit(x_train, y_train) | |
y_pred = obj.predict(x_test) | |
accuracy = accuracy_score(y_test, y_pred) | |
# Print accuracy for initial check | |
print(f"Accuracy of {model_choice}: {accuracy:.4f}") | |
def predict_spam(): | |
""" | |
This endpoint predicts whether the email is Spam or Ham. | |
Query parameter: email (str) - The email text to be classified. | |
""" | |
email = request.args.get('email') | |
if email: | |
data = bow.transform([email]).toarray() # Transform email using the Bag of Words vectorizer | |
prediction = obj.predict(data)[0] # Get the prediction (Spam or Ham) | |
return jsonify({"prediction": prediction}) # Return prediction as JSON | |
else: | |
return jsonify({"error": "Please provide an 'email' query parameter."}), 400 | |
def get_accuracy(): | |
""" | |
Endpoint to check the accuracy of the selected model on the test data. | |
""" | |
return jsonify({"accuracy": accuracy}) | |
# Run Flask app | |
if __name__ == '__main__': | |
app.run(host='127.0.0.1', port=5001, debug=True) | |