from tensorflow.keras.models import load_model import numpy as np import cv2 import gradio as gr # Load the model model = load_model('sentimentality.h5') # Function to preprocess the input text def preprocess(text): # Tokenize the text into a list of words words = text.strip().lower().split() # Load the vocabulary with open('vocabulary.txt', 'r') as f: vocab = f.read().splitlines() # Convert the words to indices in the vocabulary word_indices = [vocab.index(word) if word in vocab else 0 for word in words] # Pad the sequence with zeros to a fixed length of 500 padded_indices = np.zeros(500, dtype=np.int32) padded_indices[:len(word_indices)] = word_indices # Convert the sequence to a tensor tensor = np.expand_dims(padded_indices, axis=0) return tensor # Function to make predictions using the loaded model def sentiment(text): input_tensor = preprocess(text) result = model.predict(input_tensor)[0] label = 'positive' if result > 0.5 else 'negative' score = round(float(result), 3) return f"{label} ({score})" # Create a Gradio interface input_text = gr.inputs.Textbox(label="Enter text here to be classified:") label = gr.outputs.Label(num_top_classes=2) gr.Interface(fn=sentiment, inputs=input_text, outputs=label,title = 'Sentiment-Analysis').launch()