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

used another function

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -3,14 +3,34 @@ import numpy as np
3
  import cv2
4
  import gradio as gr
5
 
 
6
  model = load_model('sentimentality.h5')
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def sentiment(text):
9
- result = model(text)[0]
10
- label = result['label']
11
- score = round(result['score'], 3)
 
12
  return f"{label} ({score})"
13
 
 
14
  input_text = gr.inputs.Textbox(label="Enter text here to be classified:")
15
  label = gr.outputs.Label(num_top_classes=2)
16
  gr.Interface(fn=sentiment, inputs=input_text, outputs=label,title = 'Sentiment-Analysis').launch()
 
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()