amanscoder commited on
Commit
f545d97
·
1 Parent(s): d860eab

adding all files

Browse files
Files changed (4) hide show
  1. app.py +59 -0
  2. label_mapping.pkl +3 -0
  3. nmf_model.pkl +3 -0
  4. tfidf_vectorizer.pkl +3 -0
app.py CHANGED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ import re
5
+ import nltk
6
+ from nltk.stem import PorterStemmer
7
+ from nltk.tokenize import word_tokenize
8
+ import numpy as np
9
+
10
+
11
+
12
+ def text_preprocessing(df):
13
+ """
14
+ This function does in-place replacement of data so it won't return anything
15
+ """
16
+ # Convert to lower cases
17
+ df['Text'] = df['Text'].str.lower()
18
+
19
+ # Remove punctuation
20
+ df['Text'] = df['Text'].apply(lambda doc: re.sub(r'[^\w\s]+', '', doc))
21
+
22
+ # Remove stopwords
23
+ stop_words = nltk.corpus.stopwords.words('english')
24
+ df['Text'] = df['Text'].apply(lambda doc: ' '.join([word for word in doc.split() if word not in (stop_words)]))
25
+
26
+ # Remove extra spaces
27
+ df['Text'] = df['Text'].apply(lambda doc: re.sub(' +', ' ', doc))
28
+
29
+ # Stemming
30
+ porter_stemmer = PorterStemmer()
31
+ df['Text'] = df['Text'].apply(lambda doc: [porter_stemmer.stem(word) for word in word_tokenize(doc)])
32
+ df['Text'] = df['Text'].apply(lambda words: ' '.join(words))
33
+
34
+
35
+ def predict_user_input(paragraph, tfidf, nmf, label_mapping_yp):
36
+ data = pd.DataFrame({'Text': [paragraph]})
37
+ text_preprocessing(data)
38
+ tfidf_transformed = tfidf.transform(data['Text'])
39
+ nmf_transformed = nmf.transform(tfidf_transformed)
40
+ y_pred = np.argmax(nmf_transformed, axis=1)
41
+ y_pred = [label_mapping_yp[y] for y in y_pred]
42
+ return y_pred[0]
43
+
44
+ def process_paragraph(paragraph):
45
+ tfidf = joblib.load('tfidf_vectorizer.pkl')
46
+ nmf = joblib.load('nmf_model.pkl')
47
+ label_mapping_yp = joblib.load('label_mapping.pkl')
48
+ predicted_class = predict_user_input(paragraph, tfidf, nmf, label_mapping_yp)
49
+ print(f"The predicted class for the input paragraph is: {predicted_class}")
50
+ return predicted_class
51
+
52
+ def paragraph_processing_app(paragraph):
53
+ processed_text = process_paragraph(paragraph)
54
+ return processed_text
55
+
56
+ input_text = gr.Textbox(lines=10, label="Enter a article:")
57
+ output_text = gr.Textbox(label="Category(Out of Business, Tech, Sport, Politics and Entertainment.)")
58
+
59
+ gr.Interface(fn=paragraph_processing_app, inputs=input_text, outputs=output_text).launch()
label_mapping.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:21ac6d446fb291df7406c9a01b4253fb4b934d6957491191bd1d88f788c142c5
3
+ size 229
nmf_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4e0100525c10de229db44aa9c12bea3b1db4919f88b8d52222312ef290f5764a
3
+ size 788577
tfidf_vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9378646d12b4f73bb1fca002cc27f52429c8a988c3749085f10151f6a70d03e0
3
+ size 560098