Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import re
|
5 |
+
import nltk
|
6 |
+
import string
|
7 |
+
from nltk.corpus import stopwords
|
8 |
+
from nltk.stem import SnowballStemmer
|
9 |
+
from transformers import pipeline
|
10 |
+
|
11 |
+
# Download NLTK resources
|
12 |
+
nltk.download('stopwords')
|
13 |
+
stopword = set(stopwords.words('english'))
|
14 |
+
stemmer = SnowballStemmer("english")
|
15 |
+
|
16 |
+
# Load the dataset
|
17 |
+
data = pd.read_csv("commentdataset.csv")
|
18 |
+
|
19 |
+
# Labelling the data set with classifier classes according to which classifications has to perform
|
20 |
+
data["labels"] = data["class"].map({0: "Offensive Language", 1: "Abusive comments", 2: "No Abusive and Offensive"})
|
21 |
+
data = data[["comments", "labels"]]
|
22 |
+
|
23 |
+
# Clean data function
|
24 |
+
def clean(text):
|
25 |
+
text = str(text).lower()
|
26 |
+
text = re.sub(r"she's", "she is", text)
|
27 |
+
text = re.sub(r"it's", "it is", text)
|
28 |
+
text = re.sub(r"that's", "that is", text)
|
29 |
+
text = re.sub(r"what's", "that is", text)
|
30 |
+
text = re.sub(r"where's", "where is", text)
|
31 |
+
text = re.sub(r"how's", "how is", text)
|
32 |
+
text = re.sub(r"'ll", " will", text)
|
33 |
+
text = re.sub(r"'ve", " have", text)
|
34 |
+
text = re.sub(r"'re", " are", text)
|
35 |
+
text = re.sub(r"i'm", "i am", text)
|
36 |
+
text = re.sub(r"r", "", text)
|
37 |
+
text = re.sub(r"he's", "he is", text)
|
38 |
+
text = re.sub(r"'d", " would", text)
|
39 |
+
text = re.sub(r"won't", "will not", text)
|
40 |
+
text = re.sub(r"can't", "cannot", text)
|
41 |
+
text = re.sub(r"n't", " not", text)
|
42 |
+
text = re.sub(r"n'", "ng", text)
|
43 |
+
text = re.sub(r"'bout", "about", text)
|
44 |
+
text = re.sub(r"'til", "until", text)
|
45 |
+
text = re.sub('\[.*?\]', '', text)
|
46 |
+
text = re.sub('https?://\S+|www\.\S+', '', text)
|
47 |
+
text = re.sub('<.*?>+', '', text)
|
48 |
+
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
|
49 |
+
text = re.sub('\n', '', text)
|
50 |
+
text = re.sub('\w*\d\w*', '', text)
|
51 |
+
text = [word for word in text.split(' ') if word not in stopword]
|
52 |
+
text = " ".join(text)
|
53 |
+
text = [stemmer.stem(word) for word in text.split(' ')]
|
54 |
+
text = " ".join(text)
|
55 |
+
return text
|
56 |
+
|
57 |
+
data["comments"] = data["comments"].apply(clean)
|
58 |
+
|
59 |
+
# Using a pre-trained transformer model for sentiment analysis
|
60 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
61 |
+
|
62 |
+
# Function to classify comments
|
63 |
+
def classify_comment(comment):
|
64 |
+
cleaned_comment = clean(comment)
|
65 |
+
prediction = sentiment_pipeline(cleaned_comment)
|
66 |
+
label = prediction[0]['label']
|
67 |
+
return label
|
68 |
+
|
69 |
+
comment_input = gr.Textbox(label="Enter a comment")
|
70 |
+
classification_output = gr.Label()
|
71 |
+
|
72 |
+
# Create the Gradio interface
|
73 |
+
interface = gr.Interface(fn=classify_comment, inputs=comment_input, outputs=classification_output, title="Comment Classifier")
|
74 |
+
interface.launch()
|