MasterShomya commited on
Commit
a67d0d4
·
verified ·
1 Parent(s): ab29280

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -4,24 +4,29 @@ import tensorflow as tf
4
  import joblib
5
  from tensorflow.keras.preprocessing.sequence import pad_sequences
6
 
7
- # Load model and tokenizer
8
  model = tf.keras.models.load_model("sentiment_model.h5")
9
- tokenizer = joblib.load("tokenizer.joblib")
10
 
 
 
11
  max_len = 40
12
 
 
13
  def predict_sentiment(text):
14
  seq = tokenizer.texts_to_sequences([text])
15
  padded = pad_sequences(seq, maxlen=max_len, padding='post')
16
  pred = model.predict(padded)[0][0]
17
  label = "Positive" if pred >= 0.5 else "Negative"
18
- return {label: float(pred) if label == "Positive" else 1 - float(pred)}
 
19
 
20
  # Gradio UI
21
- demo = gr.Interface(fn=predict_sentiment,
22
- inputs=gr.Textbox(lines=2, placeholder="Enter a tweet..."),
23
- outputs=gr.Label(num_top_classes=2),
24
- title="Sentiment Analysis on Tweets",
25
- description="Enter a tweet and get predicted sentiment (Positive/Negative) and confidence score.")
 
 
26
 
27
  demo.launch()
 
4
  import joblib
5
  from tensorflow.keras.preprocessing.sequence import pad_sequences
6
 
7
+ # Load the trained model
8
  model = tf.keras.models.load_model("sentiment_model.h5")
 
9
 
10
+ # Load the tokenizer
11
+ tokenizer = joblib.load("tokenizer.joblib")
12
  max_len = 40
13
 
14
+ # Inference function
15
  def predict_sentiment(text):
16
  seq = tokenizer.texts_to_sequences([text])
17
  padded = pad_sequences(seq, maxlen=max_len, padding='post')
18
  pred = model.predict(padded)[0][0]
19
  label = "Positive" if pred >= 0.5 else "Negative"
20
+ confidence = float(pred) if label == "Positive" else 1 - float(pred)
21
+ return {label: round(confidence, 4)}
22
 
23
  # Gradio UI
24
+ demo = gr.Interface(
25
+ fn=predict_sentiment,
26
+ inputs=gr.Textbox(lines=2, placeholder="Enter a tweet here..."),
27
+ outputs=gr.Label(num_top_classes=2),
28
+ title="Tweet Sentiment Analyzer (BiLSTM + Attention)",
29
+ description="This model was trained from scratch using LSTM + Attention to predict sentiment of tweets (Positive/Negative)."
30
+ )
31
 
32
  demo.launch()