Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,19 @@
|
|
| 1 |
-
from google.colab import drive
|
| 2 |
-
from sklearn.model_selection import train_test_split
|
| 3 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
-
from sklearn import svm
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import numpy as np
|
| 7 |
-
import random
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
def __init__(self, text, score):
|
| 16 |
-
self.text = text
|
| 17 |
-
self.score = score
|
| 18 |
-
self.sentiment, self.probability = self.get_sentiment()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if self.score <= 2:
|
| 22 |
-
return "NEGATIVE", 1.0 if self.score == 0 else 0.8
|
| 23 |
-
elif self.score == 3:
|
| 24 |
-
return "NEUTRAL", 0.5
|
| 25 |
-
else: #Score of 4 or 5
|
| 26 |
-
return "POSITIVE", 1.0 if self.score == 5 else 0.8
|
| 27 |
-
|
| 28 |
-
class ReviewContainer:
|
| 29 |
-
def __init__(self, reviews):
|
| 30 |
-
self.reviews = reviews
|
| 31 |
-
|
| 32 |
-
def get_text(self):
|
| 33 |
-
return [x.text for x in self.reviews]
|
| 34 |
-
|
| 35 |
-
def get_sentiment(self):
|
| 36 |
-
return [x.sentiment for x in self.reviews]
|
| 37 |
-
|
| 38 |
-
def get_probability(self):
|
| 39 |
-
return [x.probability for x in self.reviews]
|
| 40 |
-
|
| 41 |
-
def evenly_distribute(self):
|
| 42 |
-
negative = list(filter(lambda x: x.sentiment == "NEGATIVE", self.reviews))
|
| 43 |
-
positive = list(filter(lambda x: x.sentiment == "POSITIVE", self.reviews))
|
| 44 |
-
positive_shrunk = positive[:len(negative)]
|
| 45 |
-
self.reviews = negative + positive_shrunk
|
| 46 |
-
random.shuffle(self.reviews)
|
| 47 |
-
|
| 48 |
-
# Load the dataset
|
| 49 |
-
df = pd.read_json('/content/gdrive/My Drive/Datasets/Books_subset3.json', lines=True)
|
| 50 |
-
|
| 51 |
-
# Create Review objects
|
| 52 |
-
reviews = [Review(x, y) for x, y in zip(df['review_body'], df['star_rating'])]
|
| 53 |
-
|
| 54 |
-
# Split data into train and test sets
|
| 55 |
-
train,test = train_test_split(reviews, test_size=0.25, random_state=42)
|
| 56 |
-
|
| 57 |
-
# Initialize ReviewContainer objects
|
| 58 |
-
train_container = ReviewContainer(train)
|
| 59 |
-
test_container = ReviewContainer(test)
|
| 60 |
-
|
| 61 |
-
# Evenly distribute the data
|
| 62 |
-
train_container.evenly_distribute()
|
| 63 |
-
train_x = train_container.get_text()
|
| 64 |
-
train_y = train_container.get_sentiment()
|
| 65 |
-
|
| 66 |
-
test_container.evenly_distribute()
|
| 67 |
-
test_x = test_container.get_text()
|
| 68 |
-
test_y = test_container.get_sentiment()
|
| 69 |
-
|
| 70 |
-
# Vectorize the data
|
| 71 |
-
vectorizer = TfidfVectorizer()
|
| 72 |
-
train_x_vectors = vectorizer.fit_transform(train_x)
|
| 73 |
-
test_x_vectors = vectorizer.transform(test_x)
|
| 74 |
-
|
| 75 |
-
# Train SVM model
|
| 76 |
-
clf_svm = svm.SVC(kernel='linear', probability=True)
|
| 77 |
-
clf_svm.fit(train_x_vectors, train_y)
|
| 78 |
-
|
| 79 |
-
# Define function for Gradio interface
|
| 80 |
def predict_sentiment(text):
|
| 81 |
-
#
|
|
|
|
|
|
|
|
|
|
| 82 |
text_vector = vectorizer.transform([text])
|
| 83 |
|
| 84 |
# Predict sentiment
|
|
@@ -102,10 +37,4 @@ def predict_sentiment(text):
|
|
| 102 |
iface = gr.Interface(
|
| 103 |
fn=predict_sentiment,
|
| 104 |
inputs=gr.Textbox(placeholder="Enter Text", lines=10, label="Enter your text here:"),
|
| 105 |
-
outputs
|
| 106 |
-
title="Sentiment Analysis Developed by Group-12(Ankit, Akshat, Gautam, Pritish) with ♥ from RCC Institute of Information Technology.",
|
| 107 |
-
description="Enter text and predict sentiment"
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
# Launch the interface
|
| 111 |
-
iface.launch(inline=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from joblib import load
|
| 3 |
|
| 4 |
+
# Define function to load SVM model
|
| 5 |
+
def load_svm_model(model_url):
|
| 6 |
+
return load(model_url)
|
| 7 |
|
| 8 |
+
# Load the SVM model from Hugging Face
|
| 9 |
+
svm_model = load_svm_model("https://huggingface.co/ankitdotpy/SVM_model_by_Group12")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Define function for Gradio interface using the loaded model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict_sentiment(text):
|
| 13 |
+
# Load the SVM model
|
| 14 |
+
clf_svm = svm_model
|
| 15 |
+
|
| 16 |
+
# Vectorize the input text (assuming you already have the vectorizer)
|
| 17 |
text_vector = vectorizer.transform([text])
|
| 18 |
|
| 19 |
# Predict sentiment
|
|
|
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=predict_sentiment,
|
| 39 |
inputs=gr.Textbox(placeholder="Enter Text", lines=10, label="Enter your text here:"),
|
| 40 |
+
outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|