Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,64 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
interface.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 3 |
+
|
| 4 |
+
import re
|
| 5 |
+
import nltk
|
| 6 |
+
import pickle
|
| 7 |
+
import string
|
| 8 |
+
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
from nltk.corpus import stopwords
|
| 12 |
+
from keras.models import load_model
|
| 13 |
+
from keras.preprocessing.sequence import pad_sequences
|
| 14 |
+
|
| 15 |
+
nltk.download('stopwords')
|
| 16 |
+
|
| 17 |
+
# Load Model and Tokenizer
|
| 18 |
+
model = load_model("sentiment_analysis_best.keras")
|
| 19 |
+
|
| 20 |
+
with open("tokenizer.pkl", "rb") as f:
|
| 21 |
+
tokenizer = pickle.load(f)
|
| 22 |
+
|
| 23 |
+
MAX_LEN = 100
|
| 24 |
+
|
| 25 |
+
negations = {"not", "no", "nor", "never", "n't"}
|
| 26 |
+
stop_words = set(stopwords.words("english")) - negations
|
| 27 |
+
|
| 28 |
+
# Preprocessing Function
|
| 29 |
+
def preprocess(text):
|
| 30 |
+
text = text.lower()
|
| 31 |
+
text = re.sub(r"\d+", "", text)
|
| 32 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 33 |
+
words = [w for w in text.split() if w not in stop_words]
|
| 34 |
+
return " ".join(words)
|
| 35 |
+
|
| 36 |
+
# Prediction Function
|
| 37 |
+
def predict_sentiment(text):
|
| 38 |
+
text = preprocess(text)
|
| 39 |
+
seq = tokenizer.texts_to_sequences([text])
|
| 40 |
+
pad = pad_sequences(seq, maxlen=MAX_LEN, padding='post')
|
| 41 |
+
pred = model.predict(pad)
|
| 42 |
+
|
| 43 |
+
label_idx = np.argmax(pred, axis=1)[0]
|
| 44 |
+
confidence = pred[0][label_idx] * 100
|
| 45 |
+
|
| 46 |
+
labels = ["Negative", "Positive", "Neutral"]
|
| 47 |
+
|
| 48 |
+
return f"{labels[label_idx]} {confidence:.2f}%"
|
| 49 |
+
|
| 50 |
+
# Gradio Interface
|
| 51 |
+
interface = gr.Interface(
|
| 52 |
+
fn=predict_sentiment,
|
| 53 |
+
inputs=gr.Textbox(lines=3, placeholder="Type your sentence here..."),
|
| 54 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 55 |
+
title="Sentiment Analysis System",
|
| 56 |
+
description="Outputs sentiment analysis along with model confidence.",
|
| 57 |
+
examples=[
|
| 58 |
+
["I really love this product"],
|
| 59 |
+
["This is the worst experience ever"],
|
| 60 |
+
["It is okay, not good not bad"]
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
interface.launch()
|