Sambhavnoobcoder commited on
Commit
2f9f9d9
·
1 Parent(s): ae7453b
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -1,27 +1,38 @@
1
- import gradio as gr
2
  import tensorflow as tf
 
3
  import numpy as np
4
 
5
- # Load the pre-trained model
6
- model = tf.keras.models.load_model('sentimentality.h5')
7
 
8
- # Define a function to preprocess the text input
9
- def preprocess(text):
10
- tokenizer = tf.keras.preprocessing.text.Tokenizer()
11
- tokenizer.fit_on_texts([text])
12
- text = tokenizer.texts_to_sequences([text])
13
- text = tf.keras.preprocessing.sequence.pad_sequences(text, maxlen=500, padding='post', truncating='post')
 
 
 
14
  return text
15
 
16
- def sentiment_analysis(text):
 
 
 
 
17
  scores = model.predict(text)
18
- return scores
 
 
19
 
20
- iface = gr.Interface(
21
- fn=sentiment_analysis,
22
- inputs=gr.Textbox(placeholder="Enter a positive or negative sentence here..."),
23
- outputs="label",
24
- interpretation="default",
25
- examples=[["This is wonderful!"]])
 
26
 
27
- iface.launch()
 
 
 
1
  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(
31
+ fn=get_sentiment_scores,
32
+ inputs=gr.inputs.Textbox(placeholder="Enter a positive or negative sentence here..."),
33
+ outputs=gr.outputs.Textbox(label="Sentiment Label"),
34
+ examples=[["This is wonderful!"], ["I hate this product."]]
35
+ )
36
 
37
+ # Launch the interface
38
+ interface.launch()