Spaces:
Build error
Build error
import streamlit as st | |
import numpy as np | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing.text import Tokenizer | |
from tensorflow.keras.preprocessing.sequence import pad_sequences | |
from tensorflow.keras.models import load_model | |
import pickle | |
import joblib | |
# Load model and tokenizer | |
model = load_model('rnn_lstm_final.h5') | |
loaded_model = joblib.load("my_rnn_model.joblib") | |
with open("tokenizer_and_sequences.pkl", "rb") as f: | |
tokenizer, data = pickle.load(f) | |
# Define helper functions | |
def is_valid_punjabi_text(text): | |
english_alphabet = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
numbers = set("0123456789") | |
punctuation = set("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") | |
for char in text: | |
if char in english_alphabet or char in numbers or char in punctuation: | |
return False | |
return True | |
def process_input(text): | |
a = [text] | |
a = tokenizer.texts_to_sequences(a) | |
a = np.array(a) | |
a = pad_sequences(a, padding='post', maxlen=100) | |
a = a.reshape((a.shape[0], a.shape[1], 1)) | |
prediction = model.predict(np.array(a)) | |
for row in prediction: | |
element1 = row[0] | |
element2 = row[1] | |
return "Negative" if element1 > element2 else "Positive" | |
# Streamlit app | |
st.title("Indic Sentence Summarization & Sentiment Analysis") | |
st.header("Insightful Echoes: Crafting Summaries with Sentiments (for ਪੰਜਾਬੀ Text)") | |
summarize_before_sentiment = st.checkbox("Summarize before analyzing sentiment") | |
user_input = st.text_area("Enter some text here") | |
if st.button("Analyze Sentiment"): | |
if not is_valid_punjabi_text(user_input): | |
st.warning("Please enter valid Punjabi text.") | |
else: | |
sentiment_output = process_input(user_input) | |
st.text_area("Sentiment Output", sentiment_output, height=200) | |