Spaces:
Runtime error
Runtime error
File size: 2,150 Bytes
33690b7 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# -----------------------------
# app.py pour déploiement Spam Detector
# -----------------------------
import gradio as gr
import joblib
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import nltk
nltk.download('stopwords')
# -----------------------------
# 1️⃣ Prétraitement d'un message
# -----------------------------
stop_words = set(stopwords.words('english'))
stemmer = PorterStemmer()
def preprocess_message(text):
"""Prétraite un message pour qu'il corresponde au format du modèle."""
if not text:
return ""
text = text.lower()
text = re.sub(r'http\S+|www\S+', '', text)
text = re.sub(r'\S+@\S+', '', text)
text = re.sub(r'\+?\d[\d -]{8,}\d', '', text)
text = re.sub(r'\d+', '', text)
text = re.sub(r'[^a-z\s!/+>]', '', text) # garder ponctuation utile pour spam
words = [stemmer.stem(word) for word in text.split() if word not in stop_words]
return " ".join(words)
# -----------------------------
# 2️⃣ Charger modèle et TF-IDF
# -----------------------------
model = joblib.load("spam_model.pkl")
vectorizer = joblib.load("tfidf_vectorizer.pkl")
# -----------------------------
# 3️⃣ Fonction de prédiction
# -----------------------------
def predict_message(message):
cleaned = preprocess_message(message)
X = vectorizer.transform([cleaned])
prediction = model.predict(X)[0]
probability = model.predict_proba(X)[0][1] if hasattr(model, 'predict_proba') else None
return {
"Message": message,
"Prediction": prediction,
"Spam Probability": round(float(probability), 4) if probability is not None else None
}
# -----------------------------
# 4️⃣ Interface Gradio
# -----------------------------
iface = gr.Interface(
fn=predict_message,
inputs=gr.Textbox(lines=3, placeholder="Entrez votre message..."),
outputs="json",
title="📩 Spam Detector",
description="Entrez un message pour savoir s'il s'agit de spam ou non."
)
# -----------------------------
# 5️⃣ Lancer l'application
# -----------------------------
if __name__ == "__main__":
iface.launch()
|