File size: 1,404 Bytes
d1b331f
 
156f4b2
 
d1b331f
156f4b2
 
 
 
 
d43925d
156f4b2
 
 
 
 
 
 
 
 
 
d43925d
156f4b2
 
 
d1b331f
 
d43925d
 
 
 
 
156f4b2
 
 
d1b331f
 
 
 
 
 
 
 
 
 
156f4b2
 
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
import gradio as gr
import pickle
import os
import sys

# Add debugging information
print("Current directory:", os.getcwd())
print("Files in directory:", os.listdir())

# Load the trained model and vectorizer with better error handling
try:
    model_path = 'model.pkl'
    vectorizer_path = 'vectorizer.pkl'
    
    print(f"Loading model from {model_path}")
    model = pickle.load(open(model_path, 'rb'))
    
    print(f"Loading vectorizer from {vectorizer_path}")
    vectorizer = pickle.load(open(vectorizer_path, 'rb'))
    
    print("Model and vectorizer loaded successfully")
except Exception as e:
    print(f"Error loading model or vectorizer: {e}")
    print(f"Python version: {sys.version}")
    print(f"System path: {sys.path}")

def predict_sms(message):
    try:
        transformed_text = vectorizer.transform([message])
        prediction = model.predict(transformed_text)[0]
        return "Spam" if prediction == 1 else "Not Spam"
    except Exception as e:
        error_msg = f"Error during prediction: {e}"
        print(error_msg)
        return error_msg

# Gradio Web Interface
iface = gr.Interface(
    fn=predict_sms,
    inputs=gr.Textbox(label="Enter SMS Message"),
    outputs=gr.Label(),
    title="SMS Spam Classifier",
    description="Enter a message to check if it's spam or not."
)

# For Hugging Face deployment
iface.launch(server_name="0.0.0.0", server_port=7860)