Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,34 @@
|
|
| 1 |
-
|
| 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
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Load
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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
|
| 27 |
outputs=gr.Label(num_top_classes=2),
|
| 28 |
-
title="
|
| 29 |
-
description="
|
| 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()
|