File size: 1,388 Bytes
6e115c2
 
 
b3e6a5b
bfd475a
6e115c2
 
 
bfd475a
6e115c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
import numpy as np
import pickle
from tensorflow.keras.preprocessing.sequence import pad_sequences
import keras

# Load model from Hugging Face Hub
model_path = hf_hub_download(repo_id="i0xs0/Sentiment_Analysis_DeepLr", filename="AC-BiLSTM_Model.h5")
model = keras.models.load_model(model_path)

# Load tokenizer from Hugging Face Hub
tokenizer_path = hf_hub_download(repo_id="i0xs0/Sentiment_Analysis_DeepLr", filename="tokenizer_AC-BiLSTM.pkl")
with open(tokenizer_path, "rb") as handle:
    tokenizer = pickle.load(handle)

# Define prediction function
def predict_sentiment(text, max_seq_length=100):
    sequences = tokenizer.texts_to_sequences([text])
    padded_sequence = pad_sequences(sequences, maxlen=max_seq_length, padding='post', truncating='post')
    prediction = model.predict(padded_sequence)[0]
    sentiment_class = np.argmax(prediction)
    sentiment_map = {0: 'negative', 1: 'neutral', 2: 'positive'}
    sentiment_label = sentiment_map[sentiment_class]
    confidence = float(prediction[sentiment_class])
    return f"Sentiment: {sentiment_label} (Confidence: {confidence:.2f})"

# Gradio UI
iface = gr.Interface(
    fn=predict_sentiment,
    inputs="text",
    outputs="text",
    title="LSTM Sentiment Analysis",
    description="Enter text and get a sentiment prediction (Positive, Neutral, Negative) using an LSTM model."
)

iface.launch()