| import gradio as gr |
| import numpy as np |
| import tensorflow as tf |
| import joblib |
| from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
|
| |
| model = tf.keras.models.load_model("sentiment_model.h5") |
| tokenizer = joblib.load("tokenizer.joblib") |
|
|
| max_len = 40 |
|
|
| def predict_sentiment(text): |
| seq = tokenizer.texts_to_sequences([text]) |
| padded = pad_sequences(seq, maxlen=max_len, padding='post') |
| pred = model.predict(padded)[0][0] |
| label = "Positive" if pred >= 0.5 else "Negative" |
| return {label: float(pred) if label == "Positive" else 1 - float(pred)} |
|
|
| |
| demo = gr.Interface(fn=predict_sentiment, |
| inputs=gr.Textbox(lines=2, placeholder="Enter a tweet..."), |
| outputs=gr.Label(num_top_classes=2), |
| title="Sentiment Analysis on Tweets", |
| description="Enter a tweet and get predicted sentiment (Positive/Negative) and confidence score.") |
|
|
| demo.launch() |