MasterShomya commited on
Commit
b638306
·
verified ·
1 Parent(s): 47bd14c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -1,32 +1,34 @@
1
- import gradio as gr
2
- import numpy as np
3
  import tensorflow as tf
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()
 
1
+ from tensorflow.keras.models import model_from_json
 
2
  import tensorflow as tf
3
  import joblib
4
  from tensorflow.keras.preprocessing.sequence import pad_sequences
5
+ import gradio as gr
6
 
7
+ # Load model architecture
8
+ with open("model_architecture.json", "r") as f:
9
+ model = model_from_json(f.read())
10
 
11
+ # Load weights
12
+ model.load_weights("model_weights.weights.h5")
13
+
14
+ # Load tokenizer
15
  tokenizer = joblib.load("tokenizer.joblib")
16
+
17
  max_len = 40
18
 
 
19
  def predict_sentiment(text):
20
  seq = tokenizer.texts_to_sequences([text])
21
  padded = pad_sequences(seq, maxlen=max_len, padding='post')
22
  pred = model.predict(padded)[0][0]
23
  label = "Positive" if pred >= 0.5 else "Negative"
24
+ return {label: float(pred) if label == "Positive" else 1 - float(pred)}
 
25
 
 
26
  demo = gr.Interface(
27
  fn=predict_sentiment,
28
+ inputs=gr.Textbox(lines=2, placeholder="Enter a tweet..."),
29
  outputs=gr.Label(num_top_classes=2),
30
+ title="Sentiment Analysis on Tweets",
31
+ description="Enter a tweet and get predicted sentiment (Positive/Negative) and confidence score."
32
  )
33
 
34
  demo.launch()