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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,39 +1,34 @@
1
- import tensorflow as tf
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(
32
- fn=get_sentiment_scores,
33
- inputs=gr.inputs.Textbox(placeholder="Enter a positive or negative sentence here..."),
34
- outputs=gr.outputs.Textbox(label="Sentiment Label"),
35
- examples=[["This is wonderful!"], ["I hate this product."]]
36
- )
37
 
38
- # Launch the interface
39
- interface.launch()
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer
4
+ from model import SentimentClassifier
5
 
6
+ model_state_dict = torch.load('sentimentality.h5')
7
+ model = SentimentClassifier(2)
8
+ model.load_state_dict(model_state_dict)
9
+ model.eval()
10
 
11
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
 
 
 
 
 
 
 
 
12
 
 
13
 
14
+ def preprocess(text):
15
+ inputs = tokenizer(text, padding='max_length',
16
+ truncation=True, max_length=512, return_tensors='pt')
17
+ return inputs
18
+ # Define a function to use the model to make predictions
19
+ def predict(review):
20
+ inputs = preprocess(review)
21
+ with torch.no_grad():
22
+ outputs = model(inputs['input_ids'], inputs['attention_mask'])
23
+ predicted_class = torch.argmax(outputs[0]).item()
24
+ if(predicted_class==0):
25
+ return "It was a negative review"
26
+ return "It was a positive review"
27
 
28
+ # Create a Gradio interface
29
+ input_text = gr.inputs.Textbox(label="Input Text")
30
+ output_text = gr.outputs.Textbox(label="Output Text")
31
+ interface = gr.Interface(fn=predict, inputs=input_text, outputs=output_text)
 
 
 
32
 
33
+ # Run the interface
34
+ interface.launch()