MasterShomya commited on
Commit
4b15e92
·
verified ·
1 Parent(s): 9edefb0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import pickle
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
+ with open("tokenizer.pkl", "rb") as f:
10
+ tokenizer = pickle.load(f)
11
+
12
+ max_len = 40
13
+
14
+ def predict_sentiment(text):
15
+ seq = tokenizer.texts_to_sequences([text])
16
+ padded = pad_sequences(seq, maxlen=max_len, padding='post')
17
+ pred = model.predict(padded)[0][0]
18
+ label = "Positive" if pred >= 0.5 else "Negative"
19
+ return {label: float(pred) if label == "Positive" else 1 - float(pred)}
20
+
21
+ # Gradio UI
22
+ demo = gr.Interface(fn=predict_sentiment,
23
+ inputs=gr.Textbox(lines=2, placeholder="Enter a tweet..."),
24
+ outputs=gr.Label(num_top_classes=2),
25
+ title="Sentiment Analysis on Tweets",
26
+ description="Enter a tweet and get predicted sentiment (Positive/Negative) and confidence score.")
27
+
28
+ demo.launch()