Sambhavnoobcoder commited on
Commit
fe90914
·
1 Parent(s): 52fe9f8

function 3

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -1,36 +1,35 @@
1
- from tensorflow.keras.models import load_model
2
- import numpy as np
3
- import cv2
4
  import gradio as gr
 
 
5
 
6
- # Load the model
7
- model = load_model('sentimentality.h5')
8
-
9
- # Function to preprocess the input text
10
  def preprocess(text):
11
- # Tokenize the text into a list of words
12
- words = text.strip().lower().split()
13
- # Load the vocabulary
14
- with open('vocabulary.txt', 'r') as f:
15
- vocab = f.read().splitlines()
16
- # Convert the words to indices in the vocabulary
17
- word_indices = [vocab.index(word) if word in vocab else 0 for word in words]
18
- # Pad the sequence with zeros to a fixed length of 500
19
- padded_indices = np.zeros(500, dtype=np.int32)
20
- padded_indices[:len(word_indices)] = word_indices
21
- # Convert the sequence to a tensor
22
- tensor = np.expand_dims(padded_indices, axis=0)
23
- return tensor
24
 
25
- # Function to make predictions using the loaded model
26
- def sentiment(text):
27
- input_tensor = preprocess(text)
28
- result = model.predict(input_tensor)[0]
29
- label = 'positive' if result > 0.5 else 'negative'
30
- score = round(float(result), 3)
31
- return f"{label} ({score})"
 
 
 
 
 
 
32
 
33
  # Create a Gradio interface
34
- input_text = gr.inputs.Textbox(label="Enter text here to be classified:")
35
- label = gr.outputs.Label(num_top_classes=2)
36
- gr.Interface(fn=sentiment, inputs=input_text, outputs=label,title = 'Sentiment-Analysis').launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
 
5
+ # Define a function to preprocess the text input
 
 
 
6
  def preprocess(text):
7
+ tokenizer = tf.keras.preprocessing.text.Tokenizer()
8
+ tokenizer.fit_on_texts([text])
9
+ text = tokenizer.texts_to_sequences([text])
10
+ text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post')
11
+ return text
 
 
 
 
 
 
 
 
12
 
13
+ # Load the pre-trained model
14
+ model = tf.keras.models.load_model('sentimentality.h5')
15
+
16
+ # Define a function to make a prediction on the input text
17
+ def predict_sentiment(text):
18
+ # Preprocess the text
19
+ text = preprocess(text)
20
+ # Make a prediction using the loaded model
21
+ proba = model.predict(text)[0]
22
+ # Normalize the probabilities
23
+ proba /= proba.sum()
24
+ # Return the probability distribution
25
+ return {"Positive": float(proba[0]), "Negative": float(proba[1]), "Neutral": float(proba[2])}
26
 
27
  # Create a Gradio interface
28
+ iface = gr.Interface(
29
+ fn=predict_sentiment,
30
+ inputs=gr.inputs.Textbox(label="Enter text here"),
31
+ outputs=gr.outputs.Bar(label="Sentiment Distribution", num_top_classes=3)
32
+ )
33
+
34
+ # Launch the interface
35
+ iface.launch()