Spaces:
Build error
Build error
Commit
·
4482830
1
Parent(s):
9b94031
Rename app.py to sentiment-analysisapp.py
Browse files- app.py +0 -77
- sentiment-analysisapp.py +15 -0
app.py
DELETED
|
@@ -1,77 +0,0 @@
|
|
| 1 |
-
pip install streamlit pandas numpy scikit-learn nltk
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 8 |
-
import re
|
| 9 |
-
from nltk.corpus import stopwords
|
| 10 |
-
from nltk.stem import SnowballStemmer
|
| 11 |
-
|
| 12 |
-
# Download NLTK resources
|
| 13 |
-
import nltk
|
| 14 |
-
nltk.download('stopwords')
|
| 15 |
-
|
| 16 |
-
# Load stopwords
|
| 17 |
-
stopword = set(stopwords.words('english'))
|
| 18 |
-
|
| 19 |
-
# Load dataset
|
| 20 |
-
data = pd.read_csv("https://raw.githubusercontent.com/amankharwal/Website-data/master/twitter.csv")
|
| 21 |
-
|
| 22 |
-
# Map labels
|
| 23 |
-
data["labels"] = data["class"].map({0: "Hate Speech",
|
| 24 |
-
1: "Offensive Language",
|
| 25 |
-
2: "No Hate and Offensive"})
|
| 26 |
-
|
| 27 |
-
# Select relevant columns
|
| 28 |
-
data = data[["tweet", "labels"]]
|
| 29 |
-
|
| 30 |
-
# Clean text function
|
| 31 |
-
stemmer = SnowballStemmer("english")
|
| 32 |
-
def clean(text):
|
| 33 |
-
text = str(text).lower()
|
| 34 |
-
text = re.sub('\[.*?\]', '', text)
|
| 35 |
-
text = re.sub('https?://\S+|www\.\S+', '', text)
|
| 36 |
-
text = re.sub('<.*?>+', '', text)
|
| 37 |
-
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
|
| 38 |
-
text = re.sub('\n', '', text)
|
| 39 |
-
text = re.sub('\w*\d\w*', '', text)
|
| 40 |
-
text = [word for word in text.split(' ') if word not in stopword]
|
| 41 |
-
text = " ".join(text)
|
| 42 |
-
text = [stemmer.stem(word) for word in text.split(' ')]
|
| 43 |
-
text = " ".join(text)
|
| 44 |
-
return text
|
| 45 |
-
|
| 46 |
-
# Apply text cleaning
|
| 47 |
-
data["tweet"] = data["tweet"].apply(clean)
|
| 48 |
-
|
| 49 |
-
# Prepare data for model
|
| 50 |
-
x = np.array(data["tweet"])
|
| 51 |
-
y = np.array(data["labels"])
|
| 52 |
-
|
| 53 |
-
cv = CountVectorizer()
|
| 54 |
-
X = cv.fit_transform(x) # Fit the Data
|
| 55 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
|
| 56 |
-
|
| 57 |
-
# Train the model
|
| 58 |
-
clf = DecisionTreeClassifier()
|
| 59 |
-
clf.fit(X_train, y_train)
|
| 60 |
-
|
| 61 |
-
# Streamlit app
|
| 62 |
-
st.title("Sentiment Analysis App")
|
| 63 |
-
|
| 64 |
-
# User input
|
| 65 |
-
sample = st.text_area("Enter a sentence for sentiment analysis:")
|
| 66 |
-
|
| 67 |
-
# Predict and display result
|
| 68 |
-
if st.button("Predict"):
|
| 69 |
-
sample_cleaned = clean(sample)
|
| 70 |
-
data_sample = cv.transform([sample_cleaned]).toarray()
|
| 71 |
-
prediction = clf.predict(data_sample)[0]
|
| 72 |
-
st.success(f"Sentiment: {prediction}")
|
| 73 |
-
|
| 74 |
-
# Display dataset
|
| 75 |
-
st.subheader("Dataset")
|
| 76 |
-
st.write(data.head())
|
| 77 |
-
streamlit run sentiment_analysis_app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sentiment-analysisapp.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
st.title("Sentiment Analysis App")
|
| 5 |
+
|
| 6 |
+
# Load sentiment analysis model from Hugging Face
|
| 7 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 8 |
+
|
| 9 |
+
# User input
|
| 10 |
+
sample = st.text_area("Enter a sentence for sentiment analysis:")
|
| 11 |
+
|
| 12 |
+
# Predict and display result
|
| 13 |
+
if st.button("Predict"):
|
| 14 |
+
prediction = sentiment_analyzer(sample)[0]
|
| 15 |
+
st.success(f"Sentiment: {prediction['label']} with confidence: {prediction['score']:.2f}")
|