Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,40 @@
|
|
1 |
-
import nltk
|
2 |
-
|
3 |
-
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
4 |
|
|
|
|
|
|
|
|
|
|
|
5 |
import gradio as gr
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
nltk.download("vader_lexicon")
|
9 |
-
sid = SentimentIntensityAnalyzer()
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
iface = gr.Interface(sentiment_analysis, "textbox", "label", interpretation="default")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
5 |
+
from sklearn.naive_bayes import MultinomialNB
|
6 |
+
from sklearn.metrics import classification_report, accuracy_score
|
7 |
import gradio as gr
|
8 |
|
9 |
+
import datasets
|
10 |
+
|
11 |
+
df_true = pd.read_csv('training.1600000.processed.noemoticon.csv')
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
df['polarity'] = df['polarity'].replace({0: 'negative', 2: 'neutral', 4: 'positive'})
|
16 |
+
df = df[['text', 'polarity']]
|
17 |
+
|
18 |
+
|
19 |
+
train_data, test_data = train_test_split(df, test_size=0.2, random_state=42)
|
20 |
+
|
21 |
+
|
22 |
+
vectorizer = TfidfVectorizer(max_features=5000)
|
23 |
+
X_train = vectorizer.fit_transform(train_data['text'])
|
24 |
+
y_train = train_data['polarity']
|
25 |
+
classifier = MultinomialNB()
|
26 |
+
classifier.fit(X_train, y_train)
|
27 |
|
|
|
|
|
28 |
|
29 |
+
X_test = vectorizer.transform(test_data['text'])
|
30 |
+
y_pred = classifier.predict(X_test)
|
31 |
+
print("Accuracy:", accuracy_score(test_data['polarity'], y_pred))
|
32 |
+
print("Classification Report:\n", classification_report(test_data['polarity'], y_pred))
|
33 |
|
|
|
34 |
|
35 |
+
def predict_sentiment(text):
|
36 |
+
text_vectorized = vectorizer.transform([text])
|
37 |
+
prediction = classifier.predict(text_vectorized)[0]
|
38 |
+
return prediction
|
39 |
+
iface = gr.Interface(fn=predict_sentiment, inputs="text", outputs="text")
|
40 |
+
iface.launch()
|