Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
pip install gradio
|
| 3 |
+
|
| 4 |
+
from google.colab import drive
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
+
from sklearn import svm
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
import random
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
# Mount Google Drive to access your dataset
|
| 14 |
+
drive.mount("/content/gdrive")
|
| 15 |
+
|
| 16 |
+
# Define classes for reviews and review container
|
| 17 |
+
class Review:
|
| 18 |
+
def __init__(self, text, score):
|
| 19 |
+
self.text = text
|
| 20 |
+
self.score = score
|
| 21 |
+
self.sentiment, self.probability = self.get_sentiment()
|
| 22 |
+
|
| 23 |
+
def get_sentiment(self):
|
| 24 |
+
if self.score <= 2:
|
| 25 |
+
return "NEGATIVE", 1.0 if self.score == 0 else 0.8
|
| 26 |
+
elif self.score == 3:
|
| 27 |
+
return "NEUTRAL", 0.5
|
| 28 |
+
else: #Score of 4 or 5
|
| 29 |
+
return "POSITIVE", 1.0 if self.score == 5 else 0.8
|
| 30 |
+
|
| 31 |
+
class ReviewContainer:
|
| 32 |
+
def __init__(self, reviews):
|
| 33 |
+
self.reviews = reviews
|
| 34 |
+
|
| 35 |
+
def get_text(self):
|
| 36 |
+
return [x.text for x in self.reviews]
|
| 37 |
+
|
| 38 |
+
def get_sentiment(self):
|
| 39 |
+
return [x.sentiment for x in self.reviews]
|
| 40 |
+
|
| 41 |
+
def get_probability(self):
|
| 42 |
+
return [x.probability for x in self.reviews]
|
| 43 |
+
|
| 44 |
+
def evenly_distribute(self):
|
| 45 |
+
negative = list(filter(lambda x: x.sentiment == "NEGATIVE", self.reviews))
|
| 46 |
+
positive = list(filter(lambda x: x.sentiment == "POSITIVE", self.reviews))
|
| 47 |
+
positive_shrunk = positive[:len(negative)]
|
| 48 |
+
self.reviews = negative + positive_shrunk
|
| 49 |
+
random.shuffle(self.reviews)
|
| 50 |
+
|
| 51 |
+
# Load the dataset
|
| 52 |
+
df = pd.read_json('/content/gdrive/My Drive/Datasets/Books_subset3.json', lines=True)
|
| 53 |
+
|
| 54 |
+
# Create Review objects
|
| 55 |
+
reviews = [Review(x, y) for x, y in zip(df['review_body'], df['star_rating'])]
|
| 56 |
+
|
| 57 |
+
# Split data into train and test sets
|
| 58 |
+
train,test = train_test_split(reviews, test_size=0.25, random_state=42)
|
| 59 |
+
|
| 60 |
+
# Initialize ReviewContainer objects
|
| 61 |
+
train_container = ReviewContainer(train)
|
| 62 |
+
test_container = ReviewContainer(test)
|
| 63 |
+
|
| 64 |
+
# Evenly distribute the data
|
| 65 |
+
train_container.evenly_distribute()
|
| 66 |
+
train_x = train_container.get_text()
|
| 67 |
+
train_y = train_container.get_sentiment()
|
| 68 |
+
|
| 69 |
+
test_container.evenly_distribute()
|
| 70 |
+
test_x = test_container.get_text()
|
| 71 |
+
test_y = test_container.get_sentiment()
|
| 72 |
+
|
| 73 |
+
# Vectorize the data
|
| 74 |
+
vectorizer = TfidfVectorizer()
|
| 75 |
+
train_x_vectors = vectorizer.fit_transform(train_x)
|
| 76 |
+
test_x_vectors = vectorizer.transform(test_x)
|
| 77 |
+
|
| 78 |
+
# Train SVM model
|
| 79 |
+
clf_svm = svm.SVC(kernel='linear', probability=True)
|
| 80 |
+
clf_svm.fit(train_x_vectors, train_y)
|
| 81 |
+
|
| 82 |
+
# Define function for Gradio interface
|
| 83 |
+
def predict_sentiment(text):
|
| 84 |
+
# Vectorize the input text
|
| 85 |
+
text_vector = vectorizer.transform([text])
|
| 86 |
+
|
| 87 |
+
# Predict sentiment
|
| 88 |
+
sentiment = clf_svm.predict(text_vector)[0]
|
| 89 |
+
|
| 90 |
+
# Get probabilities for each class
|
| 91 |
+
probabilities = clf_svm.predict_proba(text_vector)[0]
|
| 92 |
+
|
| 93 |
+
# Convert probabilities to percentages
|
| 94 |
+
percentages = [round(prob * 100, 2) for prob in probabilities]
|
| 95 |
+
|
| 96 |
+
# Choose the sentiment label based on the predicted class
|
| 97 |
+
if sentiment == "POSITIVE":
|
| 98 |
+
return f"Positive ({percentages[1]}%)"
|
| 99 |
+
elif sentiment == "NEUTRAL":
|
| 100 |
+
return f"Neutral ({percentages[2]}%)"
|
| 101 |
+
else:
|
| 102 |
+
return f"Negative ({percentages[0]}%)"
|
| 103 |
+
|
| 104 |
+
# Create Gradio interface
|
| 105 |
+
iface = gr.Interface(
|
| 106 |
+
fn=predict_sentiment,
|
| 107 |
+
inputs=gr.Textbox(placeholder="Enter Text", lines=10, label="Enter your text here:"),
|
| 108 |
+
outputs=gr.Textbox(label="Sentiment"),
|
| 109 |
+
title="Sentiment Analysis Developed by Group-12(Ankit, Akshat, Gautam, Pritish) with ♥ from RCC Institute of Information Technology.",
|
| 110 |
+
description="Enter text and predict sentiment"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Launch the interface
|
| 114 |
+
iface.launch(inline=False)
|