|
from tensorflow.keras.models import load_model |
|
import numpy as np |
|
import cv2 |
|
import gradio as gr |
|
|
|
|
|
model = load_model('sentimentality.h5') |
|
|
|
|
|
def preprocess(text): |
|
|
|
words = text.strip().lower().split() |
|
|
|
with open('vocabulary.txt', 'r') as f: |
|
vocab = f.read().splitlines() |
|
|
|
word_indices = [vocab.index(word) if word in vocab else 0 for word in words] |
|
|
|
padded_indices = np.zeros(500, dtype=np.int32) |
|
padded_indices[:len(word_indices)] = word_indices |
|
|
|
tensor = np.expand_dims(padded_indices, axis=0) |
|
return tensor |
|
|
|
|
|
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})" |
|
|
|
|
|
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() |