File size: 1,248 Bytes
2f9f9d9
625452f
b976ff0
4386646
 
b976ff0
72cc2a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4386646
078e4df
 
4386646
078e4df
 
 
4386646
078e4df
60ae5e6
078e4df
 
 
60ae5e6
4386646
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
import gradio as gr
import tensorflow as tf

# Load the saved model
model = tf.keras.models.load_model("sentimentality.h5")

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

def predict_sentiment(text):
    # preprocess input text
    processed_text = preprocess(text)
    
    # predict sentiment
    prediction = model.predict([processed_text])[0][0]
    sentiment = 'positive' if prediction >= 0.5 else 'negative'
    
    return sentiment

iface = gr.Interface(fn=predict_sentiment, 
                     inputs=gr.inputs.Textbox(label='Input Text'), 
                     outputs=gr.outputs.Label(label='Sentiment Prediction'))

iface.launch()