Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import gradio.inputs
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
from simpletransformers.classification import ClassificationModel
|
10 |
+
from sklearn.metrics import classification_report,confusion_matrix
|
11 |
+
import re
|
12 |
+
import nltk
|
13 |
+
from nltk.corpus import stopwords
|
14 |
+
nltk.download('stopwords')
|
15 |
+
|
16 |
+
file_path = "https://raw.githubusercontent.com/alexvatti/full-stack-data-science/main/NLP-Exercises/Movie-Review/IMDB%20Dataset.csv"
|
17 |
+
movies_df=pd.read_csv(file_path)
|
18 |
+
|
19 |
+
def remove_tags(txt):
|
20 |
+
removelist = "" # Add any characters you'd like to keep
|
21 |
+
# Remove HTML tags
|
22 |
+
result = re.sub(r'<[^>]+>', '', txt)
|
23 |
+
# Remove URLs
|
24 |
+
result = re.sub(r'https?://\S+', '', txt)
|
25 |
+
# Remove non-alphanumeric characters (except for those in the removelist)
|
26 |
+
result = re.sub(r'[^a-zA-Z0-9' + removelist + r'\s]', ' ', txt)
|
27 |
+
# Convert to lowercase
|
28 |
+
result = result.lower()
|
29 |
+
return result
|
30 |
+
|
31 |
+
def remove_stop_wrods(txt):
|
32 |
+
stop_words = set(stopwords.words('english'))
|
33 |
+
return ' '.join([word for word in txt.split() if word not in (stop_words)])
|
34 |
+
|
35 |
+
movies_df['review'] = movies_df['review'].apply(remove_tags)
|
36 |
+
movies_df['review'] = movies_df['review'].apply(remove_stop_wrods)
|
37 |
+
movies_df["Category"]=movies_df["sentiment"].apply(lambda x: 1 if x=='positive' else 0)
|
38 |
+
|
39 |
+
X_train,X_test,y_train,y_test=train_test_split(movies_df['review'],movies_df["Category"],test_size=0.2,random_state=42)
|
40 |
+
# Prepare the training and evaluation DataFrames for Simple Transformers
|
41 |
+
train_df = pd.DataFrame({"text": X_train, "labels": y_train})
|
42 |
+
eval_df = pd.DataFrame({"text": X_test, "labels": y_test})
|
43 |
+
|
44 |
+
|
45 |
+
# Create a ClassificationModel
|
46 |
+
model = ClassificationModel("bert", "bert-base-uncased", use_cuda=True) # Set use_cuda=True if you have a GPU
|
47 |
+
|
48 |
+
# Train the model
|
49 |
+
model.train_model(train_df)
|
50 |
+
|
51 |
+
# Evaluate the model
|
52 |
+
result, model_outputs, wrong_predictions = model.eval_model(eval_df)
|
53 |
+
model.save_model("sentiment_model")
|
54 |
+
|
55 |
+
# Step 4: Load the Model for Prediction
|
56 |
+
# To use the model later, reload it from the saved directory
|
57 |
+
loaded_model = ClassificationModel("bert", "sentiment_model", use_cuda=True)
|
58 |
+
|
59 |
+
# Step 5: Predict Sentiment for a New Review
|
60 |
+
test_review = "This movie was absolutely fantastic! The acting was top-notch."
|
61 |
+
review=remove_tags(test_review)
|
62 |
+
review=remove_stop_wrods(review)
|
63 |
+
predictions, raw_outputs = loaded_model.predict(review)
|
64 |
+
print("Predictions:", predictions) # Outputs the label (e.g., 1 for positive, 0 for negative)
|
65 |
+
print("Raw Outputs:", raw_outputs) # Outputs the raw model scores
|
66 |
+
|
67 |
+
test_review ="I hated this movie. It was a complete waste of time."
|
68 |
+
review=remove_tags(test_review)
|
69 |
+
review=remove_stop_wrods(review)
|
70 |
+
predictions, raw_outputs = loaded_model.predict(review)
|
71 |
+
|
72 |
+
print("Predictions:", predictions) # Outputs the label (e.g., 1 for positive, 0 for negative)
|
73 |
+
print("Raw Outputs:", raw_outputs) # Outputs the raw model scores
|
74 |
+
|
75 |
+
def fn(test_review):
|
76 |
+
review=remove_tags(test_review)
|
77 |
+
review=remove_stop_wrods(review)
|
78 |
+
predictions, raw_outputs = loaded_model.predict(review)
|
79 |
+
return "Positive" if predictions==1 else "Negative"
|
80 |
+
|
81 |
+
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. "
|
82 |
+
here = gr.Interface(fn,
|
83 |
+
inputs= gradio.inputs.Textbox( lines=1, placeholder=None, default="", label=None),
|
84 |
+
outputs='text',
|
85 |
+
title="Sentiment analysis of movie reviews",
|
86 |
+
description=description,
|
87 |
+
theme="peach",
|
88 |
+
allow_flagging="auto",
|
89 |
+
flagging_dir='flagging records')
|
90 |
+
here.launch(inline=False)
|