Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.pipeline import make_pipeline
|
| 8 |
+
from sklearn.metrics import accuracy_score
|
| 9 |
+
|
| 10 |
+
# Step 1: Load and preprocess the data
|
| 11 |
+
# Example data loading (replace this with your actual data)
|
| 12 |
+
data = pd.read_csv("/content/flipkart_reviews.csv")
|
| 13 |
+
|
| 14 |
+
# Preprocessing function
|
| 15 |
+
def preprocess_text(text):
|
| 16 |
+
text = text.lower()
|
| 17 |
+
text = ''.join([char for char in text if char.isalnum() or char.isspace()])
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
data['Review'] = data['Review'].apply(preprocess_text)
|
| 21 |
+
|
| 22 |
+
# Step 2: Feature Engineering
|
| 23 |
+
tfidf = TfidfVectorizer()
|
| 24 |
+
X_text = tfidf.fit_transform(data['Review'])
|
| 25 |
+
|
| 26 |
+
# Combine TF-IDF features with the rating
|
| 27 |
+
X = np.hstack((X_text.toarray(), data['Rating'].values.reshape(-1, 1)))
|
| 28 |
+
|
| 29 |
+
# Define the five sentiment categories
|
| 30 |
+
def categorize_sentiment(rating):
|
| 31 |
+
if rating == 1:
|
| 32 |
+
return 'Worst π‘'
|
| 33 |
+
elif rating == 2:
|
| 34 |
+
return 'Poor π'
|
| 35 |
+
elif rating == 3:
|
| 36 |
+
return 'Good π'
|
| 37 |
+
elif rating == 4:
|
| 38 |
+
return 'Better π'
|
| 39 |
+
else:
|
| 40 |
+
return 'Best π'
|
| 41 |
+
|
| 42 |
+
y = data['Rating'].apply(categorize_sentiment)
|
| 43 |
+
|
| 44 |
+
# Step 3: Model Training
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 46 |
+
model = LogisticRegression()
|
| 47 |
+
model.fit(X_train, y_train)
|
| 48 |
+
|
| 49 |
+
# Evaluate the model
|
| 50 |
+
y_pred = model.predict(X_test)
|
| 51 |
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
| 52 |
+
|
| 53 |
+
# Step 4: Create Gradio Interface
|
| 54 |
+
def predict_sentiment(review, rating):
|
| 55 |
+
review = preprocess_text(review)
|
| 56 |
+
review_tfidf = tfidf.transform([review])
|
| 57 |
+
features = np.hstack((review_tfidf.toarray(), [[rating]]))
|
| 58 |
+
sentiment = model.predict(features)
|
| 59 |
+
return sentiment[0]
|
| 60 |
+
|
| 61 |
+
interface = gr.Interface(
|
| 62 |
+
fn=predict_sentiment,
|
| 63 |
+
inputs=[gr.Textbox(lines=2, placeholder="Enter your review here..."), gr.Slider(1, 5, step=1, label="Rating")],
|
| 64 |
+
outputs="text",
|
| 65 |
+
title="Flipkart Review Sentiment Analysis",
|
| 66 |
+
description="Enter your Flipkart review and rating to predict its sentiment."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Launch the interface
|
| 70 |
+
interface.launch(inline=False)
|