Sambhavnoobcoder commited on
Commit
a7f2f89
·
1 Parent(s): 2f9f9d9

changed gradio interface

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -2,29 +2,30 @@ import tensorflow as tf
2
  import gradio as gr
3
  import numpy as np
4
 
5
- # Load the custom model
6
- model = tf.keras.models.load_model("sentiment.h5")
7
 
8
- # Define a function to preprocess the input text
9
- def preprocess_text(text):
10
  # Tokenize the text
11
  tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000)
12
  tokenizer.fit_on_texts(text)
13
- text = tokenizer.texts_to_sequences(text)
14
- # Pad the sequences to a fixed length
15
- max_len = 30
16
- text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=max_len)
17
- return text
18
 
19
- # Define a function to get the sentiment scores from the model
20
- def get_sentiment_scores(text):
21
- # Preprocess the text
22
- text = preprocess_text(text)
23
- # Get the sentiment scores from the model
24
- scores = model.predict(text)
25
- # Postprocess the scores to obtain the sentiment label
26
- label = "Positive" if np.round(scores) == 1 else "Negative"
27
- return label
 
 
 
 
 
28
 
29
  # Define the Gradio interface
30
  interface = gr.Interface(
 
2
  import gradio as gr
3
  import numpy as np
4
 
5
+ # Load the saved model
6
+ model = load_model('sentimentality.h5')
7
 
8
+ # Define the function to preprocess the text
9
+ def preprocess(text):
10
  # Tokenize the text
11
  tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000)
12
  tokenizer.fit_on_texts(text)
13
+ sequences = tokenizer.texts_to_sequences(text)
 
 
 
 
14
 
15
+ # Pad the sequences to a fixed length of 30
16
+ padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=30, padding='post')
17
+
18
+ return np.array(padded_sequences)
19
+
20
+ # Define the function to make predictions on the text
21
+ def sentiment_analysis(text):
22
+ preprocessed_text = preprocess([text])
23
+ prediction = model.predict(preprocessed_text)[0][0]
24
+
25
+ if prediction >= 0.5:
26
+ return "positive"
27
+ else:
28
+ return "negative"
29
 
30
  # Define the Gradio interface
31
  interface = gr.Interface(