Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -----------------------------
|
| 2 |
+
# app.py pour déploiement Spam Detector
|
| 3 |
+
# -----------------------------
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import joblib
|
| 6 |
+
import re
|
| 7 |
+
from nltk.corpus import stopwords
|
| 8 |
+
from nltk.stem import PorterStemmer
|
| 9 |
+
import nltk
|
| 10 |
+
nltk.download('stopwords')
|
| 11 |
+
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# 1️⃣ Prétraitement d'un message
|
| 14 |
+
# -----------------------------
|
| 15 |
+
stop_words = set(stopwords.words('english'))
|
| 16 |
+
stemmer = PorterStemmer()
|
| 17 |
+
|
| 18 |
+
def preprocess_message(text):
|
| 19 |
+
"""Prétraite un message pour qu'il corresponde au format du modèle."""
|
| 20 |
+
if not text:
|
| 21 |
+
return ""
|
| 22 |
+
text = text.lower()
|
| 23 |
+
text = re.sub(r'http\S+|www\S+', '', text)
|
| 24 |
+
text = re.sub(r'\S+@\S+', '', text)
|
| 25 |
+
text = re.sub(r'\+?\d[\d -]{8,}\d', '', text)
|
| 26 |
+
text = re.sub(r'\d+', '', text)
|
| 27 |
+
text = re.sub(r'[^a-z\s!/+>]', '', text) # garder ponctuation utile pour spam
|
| 28 |
+
words = [stemmer.stem(word) for word in text.split() if word not in stop_words]
|
| 29 |
+
return " ".join(words)
|
| 30 |
+
|
| 31 |
+
# -----------------------------
|
| 32 |
+
# 2️⃣ Charger modèle et TF-IDF
|
| 33 |
+
# -----------------------------
|
| 34 |
+
model = joblib.load("spam_model.pkl")
|
| 35 |
+
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 36 |
+
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# 3️⃣ Fonction de prédiction
|
| 39 |
+
# -----------------------------
|
| 40 |
+
def predict_message(message):
|
| 41 |
+
cleaned = preprocess_message(message)
|
| 42 |
+
X = vectorizer.transform([cleaned])
|
| 43 |
+
prediction = model.predict(X)[0]
|
| 44 |
+
probability = model.predict_proba(X)[0][1] if hasattr(model, 'predict_proba') else None
|
| 45 |
+
return {
|
| 46 |
+
"Message": message,
|
| 47 |
+
"Prediction": prediction,
|
| 48 |
+
"Spam Probability": round(float(probability), 4) if probability is not None else None
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# 4️⃣ Interface Gradio
|
| 53 |
+
# -----------------------------
|
| 54 |
+
iface = gr.Interface(
|
| 55 |
+
fn=predict_message,
|
| 56 |
+
inputs=gr.Textbox(lines=3, placeholder="Entrez votre message..."),
|
| 57 |
+
outputs="json",
|
| 58 |
+
title="📩 Spam Detector",
|
| 59 |
+
description="Entrez un message pour savoir s'il s'agit de spam ou non."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# -----------------------------
|
| 63 |
+
# 5️⃣ Lancer l'application
|
| 64 |
+
# -----------------------------
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
iface.launch()
|