CoolFace
Apppublic

Prashun08/Text_Classification

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
0likes
app.py133 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""First_Text_Classification.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7    https://colab.research.google.com/drive/1sdLss09e3OxYVoeK3oBA6qrUSj_iOxp-8 9<h3 align = "center">Importing Libraries</h3>10"""11 12import numpy as np13import pandas as pd14 15"""<h3 align = "center">Importing Dataset</h3>"""16 17data = pd.read_csv("spam.csv", encoding = "ISO-8859-1")18 19"""<h3 align = "center">Preliminary Data Checks</h3>"""20 21data.head()22 23data.isnull().sum()24 25data.shape26 27data['v1'].value_counts()28 29data.info()30 31"""<h3 align = "center">Putting the Length of Characters of each row in a column.</h3>"""32 33data["Unnamed: 2"] = data["v2"].str.len()34 35"""<h3 align = "center">Visualising Length of Characters for each category!</h3>"""36 37 38"""<h5>It is evident from the above plot that spam texts are usually longer in length!</h5>39 40<h3 align = "center">Defining Variables</h3>41"""42 43X = data["v2"]44y = data["v1"]45 46"""<h3 align = "center">Train Test Split</h3>"""47 48from sklearn.model_selection import train_test_split49X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)50 51"""<h3 align = "center">Vecrorizing Words into Matrix</h3>"""52 53from sklearn.feature_extraction.text import CountVectorizer54count_vect = CountVectorizer()55 56X_train_counts = count_vect.fit_transform(X_train)57 58X_train_counts59 60X_train.shape61 62X_train_counts.shape63 64from sklearn.feature_extraction.text import TfidfTransformer65tfidf_transformer = TfidfTransformer()66 67X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)68 69X_train_tfidf.shape70 71"""<h3 align = "center">Using TDIF Vectorizer for optimum vectorization!</h3>"""72 73from sklearn.feature_extraction.text import TfidfVectorizer74vectorizer = TfidfVectorizer()75 76X_train_tfidf = vectorizer.fit_transform(X_train)77 78X_train_tfidf.shape79 80"""<h3 align = "center">Creating Model</h3>"""81 82from sklearn.svm import LinearSVC83clf = LinearSVC()84 85clf.fit(X_train_tfidf,y_train)86 87"""<h3 align = "center">Creating Pipeline</h3>"""88 89from sklearn.pipeline import Pipeline90 91text_clf = Pipeline([("tfidf",TfidfVectorizer()),("clf",LinearSVC())])92 93text_clf.fit(X_train,y_train)94 95predictions = text_clf.predict(X_test)96 97X_test98 99from sklearn.metrics import confusion_matrix,classification_report,accuracy_score100 101print(confusion_matrix(y_test,predictions))102 103print(classification_report(y_test,predictions))104 105"""<h3 align = "center">Accuracy Score</h3>"""106 107print(accuracy_score(y_test,predictions))108 109"""<h3 align = "center">Predictions </h3>"""110 111text_clf.predict(["Hi how are you doing today?"])112 113text_clf.predict(["Congratulations! You are selected for a free vouchar worth $500"])114 115"""<h3 align = "center">Creating User Interface!</h3>"""116 117import gradio as gr118 119def first_nlp_spam_detector(text):120  list = []121  list.append(text)122  arr =  text_clf.predict(list)123  if arr[0] == 'ham':124    return "Your Text is a Legitimate One!"125  else:126    return "Beware of such text messages, It\'s a Spam! "127 128interface = gr.Interface(first_nlp_spam_detector,inputs = gr.Textbox(lines=2, placeholder="Enter your Text Here.....!", show_label = False),129                         outputs = gr.Label(value = "Predicting the Text Classification..!"),description = "Predicting Text Legitimacy!")130 131first_nlp_spam_detector("Congratulations! You are selected for a free vouchar worth $500")132 133interface.launch()