Sambhavnoobcoder commited on
Commit
4386646
·
1 Parent(s): 9811bd2

codex codes

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -1,41 +1,33 @@
1
  import gradio as gr
2
- import numpy as np
3
  import tensorflow as tf
4
- from tensorflow.keras.datasets import imdb
5
 
6
- word_to_index = imdb.get_word_index()
 
7
 
8
- # Load the pre-trained model from file
9
- loaded_model = tf.keras.models.load_model('sentimentality.h5')
10
-
11
- # Define a function to make predictions
12
- def predict_sentiment(user_input, number_of_words, words_per_view):
13
- # Encode the input text
14
- words = user_input.split()
15
- encoded_word = np.zeros(words_per_view).astype(int)
16
- encoded_word[words_per_view - len(words) - 1] = 1
17
- for i, word in enumerate(words):
18
- index = words_per_view - len(words) + i
19
- encoded_word[index] = word_to_index.get(word, 0) + 3
20
- encoded_word = np.expand_dims(encoded_word, axis=0)
21
 
22
- # Make the prediction
23
- prediction = loaded_model.predict(encoded_word)[0][0]
24
 
25
- # Return the sentiment label
26
  if prediction > 0.5:
27
- return "Positive"
28
  else:
29
- return "Negative"
30
 
31
  # Create the Gradio interface
32
- iface = gr.Interface(
33
- fn=predict_sentiment,
34
- inputs=["textbox", gr.inputs.NumberRange(minimum=1, maximum=10000, default=3000, label="Number of words"), gr.inputs.NumberRange(minimum=1, maximum=100, default=30, label="Words per view")],
35
- outputs="text",
36
- title="Sentiment Analysis",
37
- description="Enter a text and get the sentiment prediction"
38
- )
39
 
40
- # Launch the interface
41
- iface.launch()
 
1
  import gradio as gr
 
2
  import tensorflow as tf
 
3
 
4
+ # Load the saved model
5
+ model = tf.keras.models.load_model("sentimentality.h5")
6
 
7
+ def predict_sentiment(text):
8
+ """
9
+ This function takes a text input and returns the predicted sentiment using a saved model.
10
+
11
+ Parameters:
12
+ text (str): The input text to be analyzed
13
+
14
+ Returns:
15
+ str: The predicted sentiment of the input text (either "positive" or "negative")
16
+ """
17
+ # Preprocess the input text
18
+ text = preprocess_text(text)
 
19
 
20
+ # Make the prediction using the loaded model
21
+ prediction = model.predict([text])[0][0]
22
 
23
+ # Return the predicted sentiment
24
  if prediction > 0.5:
25
+ return "positive"
26
  else:
27
+ return "negative"
28
 
29
  # Create the Gradio interface
30
+ iface = gr.Interface(fn=predict_sentiment, inputs="text", outputs="text")
 
 
 
 
 
 
31
 
32
+ # Run the interface
33
+ iface.launch()