Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 from Hugging Face Hub
|
| 8 |
+
model_path = hf_hub_download(repo_id="i0xs0/Sentiment_Analysis_DeepLr", filename="AC-BiLSTM_Model.h5")
|
| 9 |
+
model = tf.keras.models.load_model(model_path)
|
| 10 |
+
|
| 11 |
+
# Load tokenizer from Hugging Face Hub
|
| 12 |
+
tokenizer_path = hf_hub_download(repo_id="i0xs0/Sentiment_Analysis_DeepLr", filename="tokenizer_AC-BiLSTM.pkl")
|
| 13 |
+
with open(tokenizer_path, "rb") as handle:
|
| 14 |
+
tokenizer = pickle.load(handle)
|
| 15 |
+
|
| 16 |
+
# Define prediction function
|
| 17 |
+
def predict_sentiment(text, max_seq_length=100):
|
| 18 |
+
sequences = tokenizer.texts_to_sequences([text])
|
| 19 |
+
padded_sequence = pad_sequences(sequences, maxlen=max_seq_length, padding='post', truncating='post')
|
| 20 |
+
prediction = model.predict(padded_sequence)[0]
|
| 21 |
+
sentiment_class = np.argmax(prediction)
|
| 22 |
+
sentiment_map = {0: 'negative', 1: 'neutral', 2: 'positive'}
|
| 23 |
+
sentiment_label = sentiment_map[sentiment_class]
|
| 24 |
+
confidence = float(prediction[sentiment_class])
|
| 25 |
+
return f"Sentiment: {sentiment_label} (Confidence: {confidence:.2f})"
|
| 26 |
+
|
| 27 |
+
# Gradio UI
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_sentiment,
|
| 30 |
+
inputs="text",
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="LSTM Sentiment Analysis",
|
| 33 |
+
description="Enter text and get a sentiment prediction (Positive, Neutral, Negative) using an LSTM model."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
iface.launch()
|