Alexvatti commited on
Commit
55fdaad
·
verified ·
1 Parent(s): b14184c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -21
app.py CHANGED
@@ -1,6 +1,4 @@
1
-
2
  import gradio as gr
3
-
4
  import numpy as np
5
  import pandas as pd
6
  import re
@@ -8,6 +6,7 @@ from tensorflow.keras.models import Sequential
8
  from tensorflow.keras.layers import Dense
9
  from transformers import BertTokenizer, TFBertModel
10
  from sklearn.model_selection import train_test_split
 
11
  from nltk.corpus import stopwords
12
  import tensorflow as tf
13
  import nltk
@@ -45,7 +44,7 @@ X_train, X_test, y_train, y_test = train_test_split(movies_df['review'], movies_
45
  y_train = tf.convert_to_tensor(y_train.values, dtype=tf.float32)
46
  y_test = tf.convert_to_tensor(y_test.values, dtype=tf.float32)
47
 
48
- # Batch-wise BERT embeddings
49
  def bert_embeddings_batch(texts, batch_size=32, max_length=64):
50
  embeddings = []
51
  for i in range(0, len(texts), batch_size):
@@ -81,11 +80,10 @@ classifier.fit(X_train_embeddings, y_train, epochs=5, batch_size=32, validation_
81
  test_loss, test_accuracy = classifier.evaluate(X_test_embeddings, y_test)
82
  print(f"Test Accuracy: {test_accuracy}")
83
 
84
-
85
  # Predictions and confusion matrix
86
  y_pred = (classifier.predict(X_test_embeddings) > 0.5).astype("int32")
87
- conf_matrix = confusion_matrix(y_test, y_pred)
88
- class_report = classification_report(y_test, y_pred)
89
 
90
  print("Confusion Matrix:")
91
  print(conf_matrix)
@@ -95,23 +93,41 @@ print(class_report)
95
  # Save the trained model to a file
96
  #classifier.save("movie_sentiment_model.h5")
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  def fn(test_review):
99
- review=remove_tags(test_review)
100
- review=remove_stop_wrods(review)
101
- cls_embeddings = bert_embeddings([review])
102
- #loaded_model = load_model("movie_sentiment_model.h5")
103
  prediction = classifier.predict(cls_embeddings)
104
  return "Positive" if prediction[0] > 0.5 else "Negative"
105
-
106
- description = "Give a review of a movie that you like(or hate, sarcasm intended XD) and the model will let you know just how much your review truely reflects your emotions. "
 
107
  input_text = gr.Textbox(label="Enter Text")
108
  output_text = gr.Textbox(label="Output Text")
109
- here = gr.Interface(fn,
110
- inputs= input_text,
111
- outputs=output_text,
112
- title="Sentiment analysis of movie reviews",
113
- description=description,
114
- theme="peach",
115
- allow_flagging="auto",
116
- flagging_dir='flagging records')
117
- here.launch(inline=False)
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
  import pandas as pd
4
  import re
 
6
  from tensorflow.keras.layers import Dense
7
  from transformers import BertTokenizer, TFBertModel
8
  from sklearn.model_selection import train_test_split
9
+ from sklearn.metrics import confusion_matrix, classification_report
10
  from nltk.corpus import stopwords
11
  import tensorflow as tf
12
  import nltk
 
44
  y_train = tf.convert_to_tensor(y_train.values, dtype=tf.float32)
45
  y_test = tf.convert_to_tensor(y_test.values, dtype=tf.float32)
46
 
47
+ # Compute BERT embeddings
48
  def bert_embeddings_batch(texts, batch_size=32, max_length=64):
49
  embeddings = []
50
  for i in range(0, len(texts), batch_size):
 
80
  test_loss, test_accuracy = classifier.evaluate(X_test_embeddings, y_test)
81
  print(f"Test Accuracy: {test_accuracy}")
82
 
 
83
  # Predictions and confusion matrix
84
  y_pred = (classifier.predict(X_test_embeddings) > 0.5).astype("int32")
85
+ conf_matrix = confusion_matrix(y_test.numpy(), y_pred)
86
+ class_report = classification_report(y_test.numpy(), y_pred)
87
 
88
  print("Confusion Matrix:")
89
  print(conf_matrix)
 
93
  # Save the trained model to a file
94
  #classifier.save("movie_sentiment_model.h5")
95
 
96
+ # Single input BERT embeddings
97
+ def bert_embeddings(text, max_length=64):
98
+ inputs = tokenizer(
99
+ [text],
100
+ return_tensors="tf",
101
+ padding=True,
102
+ truncation=True,
103
+ max_length=max_length
104
+ )
105
+ outputs = bert_model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
106
+ cls_embeddings = outputs.last_hidden_state[:, 0, :]
107
+ return cls_embeddings.numpy()
108
+
109
+ # Define Gradio function
110
  def fn(test_review):
111
+ review = remove_tags(test_review)
112
+ review = remove_stop_words(review)
113
+ cls_embeddings = bert_embeddings(review)
 
114
  prediction = classifier.predict(cls_embeddings)
115
  return "Positive" if prediction[0] > 0.5 else "Negative"
116
+
117
+ # Gradio Interface
118
+ description = "Give a review of a movie that you like (or hate, sarcasm intended XD) and the model will let you know just how much your review truly reflects your emotions."
119
  input_text = gr.Textbox(label="Enter Text")
120
  output_text = gr.Textbox(label="Output Text")
121
+
122
+ app = gr.Interface(
123
+ fn=fn,
124
+ inputs=input_text,
125
+ outputs=output_text,
126
+ title="Sentiment Analysis of Movie Reviews",
127
+ description=description,
128
+ allow_flagging="auto",
129
+ flagging_dir='flagging_records'
130
+ )
131
+
132
+ app.launch(inline=False)
133
+