Spaces:
Runtime error
Runtime error
| 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() | |