shukdev3 commited on
Commit
9d89b04
Β·
verified Β·
1 Parent(s): 9a1eaee

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import re
3
+ import string
4
+ import contractions
5
+ import gradio as gr
6
+ import nltk
7
+
8
+ from bs4 import BeautifulSoup
9
+ from nltk.tokenize import word_tokenize
10
+ from nltk.corpus import stopwords
11
+
12
+ from tensorflow.keras.models import load_model
13
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
14
+
15
+ # --------------------------------------------------
16
+ # NLTK Downloads
17
+ # --------------------------------------------------
18
+ nltk.download('stopwords', quiet=True)
19
+ nltk.download('punkt', quiet=True)
20
+ nltk.download('punkt_tab', quiet=True)
21
+
22
+ # --------------------------------------------------
23
+ # Load Model and Tokenizer
24
+ # --------------------------------------------------
25
+ MODEL_PATH = "/content/drive/MyDrive/BiLSTM/bilstm_sentiment_model.keras"
26
+ TOKENIZER_PATH = "/content/drive/MyDrive/BiLSTM/BiLSTM_tokenizer.pkl"
27
+
28
+ loaded_model = load_model(MODEL_PATH)
29
+
30
+ with open(TOKENIZER_PATH, "rb") as f:
31
+ loaded_tokenizer = pickle.load(f)
32
+
33
+ print("βœ… Model and Tokenizer loaded successfully")
34
+
35
+ # --------------------------------------------------
36
+ # Constants
37
+ # --------------------------------------------------
38
+ MAX_LEN = 200
39
+ STOP_WORDS = set(stopwords.words("english"))
40
+
41
+ # --------------------------------------------------
42
+ # Text Preprocessing
43
+ # --------------------------------------------------
44
+ def preprocess_text(text: str) -> str:
45
+
46
+ # Remove HTML
47
+ text = BeautifulSoup(text, "html.parser").get_text()
48
+
49
+ # Remove URLs
50
+ text = re.sub(r"http\S+|www\.\S+", "", text)
51
+
52
+ # Normalize special characters
53
+ text = text.replace("\u2019", "'").replace("\u2018", "'")
54
+ text = text.replace("\u201c", '"').replace("\u201d", '"')
55
+ text = text.replace("\u2013", "-").replace("\u2014", "-")
56
+ text = text.encode("ascii", errors="ignore").decode("ascii")
57
+
58
+ # Expand contractions
59
+ text = contractions.fix(text)
60
+
61
+ # Lowercase
62
+ text = text.lower()
63
+
64
+ # Remove punctuation
65
+ text = text.translate(str.maketrans("", "", string.punctuation))
66
+
67
+ # Remove numbers
68
+ text = re.sub(r"\b\d+\b", "", text)
69
+
70
+ # Remove extra spaces
71
+ text = re.sub(r"\s+", " ", text).strip()
72
+
73
+ # Tokenize and remove stopwords
74
+ tokens = word_tokenize(text)
75
+ tokens = [word for word in tokens if word not in STOP_WORDS]
76
+
77
+ return " ".join(tokens)
78
+
79
+ # --------------------------------------------------
80
+ # Prediction Function
81
+ # --------------------------------------------------
82
+ def predict_sentiment(review_text):
83
+
84
+ clean_text = preprocess_text(review_text)
85
+
86
+ seq = loaded_tokenizer.texts_to_sequences([clean_text])
87
+
88
+ padded = pad_sequences(
89
+ seq,
90
+ maxlen=MAX_LEN,
91
+ padding="post",
92
+ truncating="post"
93
+ )
94
+
95
+ score = float(loaded_model.predict(padded, verbose=0)[0][0])
96
+
97
+ if score >= 0.5:
98
+ sentiment = "Positive 😊"
99
+ confidence = score * 100
100
+ else:
101
+ sentiment = "Negative 😞"
102
+ confidence = (1 - score) * 100
103
+
104
+ return (
105
+ clean_text,
106
+ sentiment,
107
+ f"{confidence:.2f}%",
108
+ round(score, 4)
109
+ )
110
+
111
+ # --------------------------------------------------
112
+ # Gradio UI
113
+ # --------------------------------------------------
114
+ app = gr.Interface(
115
+ fn=predict_sentiment,
116
+ inputs=gr.Textbox(
117
+ lines=5,
118
+ placeholder="Enter a movie review...",
119
+ label="Movie Review"
120
+ ),
121
+ outputs=[
122
+ gr.Textbox(label="Cleaned Text"),
123
+ gr.Textbox(label="Predicted Sentiment"),
124
+ gr.Textbox(label="Confidence"),
125
+ gr.Number(label="Raw Score")
126
+ ],
127
+ title="🎬 BiLSTM Movie Review Sentiment Analyzer",
128
+ description="Enter a movie review and the trained BiLSTM model will predict whether the sentiment is positive or negative."
129
+ )
130
+
131
+ app.launch()