File size: 1,540 Bytes
04ab287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import joblib
import re
from nltk.corpus import stopwords
import nltk

nltk.download('stopwords', quiet=True)
stop_words = set(stopwords.words('english'))

svm_model = joblib.load('svm_model.pkl')
vectorizer = joblib.load('tfidf_vectorizer.pkl')

label_map = {
    1: 'World',
    2: 'Sports',
    3: 'Business',
    4: 'Sci/Tech'
}

def clean_text(text):
    text = text.lower()
    text = re.sub(r'[^a-zA-Z]', ' ', text)
    words = text.split()
    words = [w for w in words if w not in stop_words]
    return " ".join(words)

def predict(text):
    if not text.strip():
        return "Please enter some text."
    cleaned = clean_text(text)
    vectorized = vectorizer.transform([cleaned])
    prediction = svm_model.predict(vectorized)[0]
    return label_map[prediction]

demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(
        lines=4,
        placeholder="Paste a news headline or article here...",
        label="News Text"
    ),
    outputs=gr.Textbox(label="Predicted Category"),
    title="AG News Classifier",
    description="Classifies news articles into: World, Sports, Business, or Sci/Tech. Built with SVM + TF-IDF trained on 120,000 articles.",
    examples=[
        ["NASA discovers water on Mars surface"],
        ["Stock markets crash amid banking crisis"],
        ["Ronaldo scores hat-trick in World Cup qualifier"],
        ["World leaders gather for climate summit in Paris"],
        ["Apple launches new AI chip for iPhones"]
    ],
    theme=gr.themes.Soft()
)

demo.launch()