File size: 1,343 Bytes
1f2ab71
 
 
9724286
5328dbc
52fe9f8
1f2ab71
5328dbc
52fe9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f2ab71
52fe9f8
 
 
 
1f2ab71
5328dbc
52fe9f8
1f2ab71
 
8a89a2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()