Spaces:
Sleeping
Sleeping
Agrannya Singh commited on
Commit ·
e604080
1
Parent(s): 6564b52
Update from Colab
Browse files
app.py
CHANGED
|
@@ -1,60 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
import pandas as pd
|
| 3 |
-
import joblib
|
| 4 |
import re
|
| 5 |
-
import string
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return df
|
| 13 |
|
| 14 |
def preprocess_text(text):
|
|
|
|
|
|
|
| 15 |
text = text.lower()
|
| 16 |
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 17 |
text = re.sub('\s+', ' ', text).strip()
|
| 18 |
return text
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import re
|
|
|
|
| 4 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
from sklearn.linear_model import LogisticRegression
|
| 6 |
+
import string
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
|
| 10 |
+
|
| 11 |
+
# Load data
|
| 12 |
+
df = pd.read_csv('car_rental_feedback_sentiment.csv - Copy (1).csv') # Make sure filename matches
|
|
|
|
| 13 |
|
| 14 |
def preprocess_text(text):
|
| 15 |
+
if not isinstance(text, str):
|
| 16 |
+
return ""
|
| 17 |
text = text.lower()
|
| 18 |
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 19 |
text = re.sub('\s+', ' ', text).strip()
|
| 20 |
return text
|
| 21 |
|
| 22 |
+
# Check if required columns exist
|
| 23 |
+
if not all(col in df.columns for col in ['review', 'sentiment_value']):
|
| 24 |
+
raise ValueError("CSV file must contain 'review' and 'sentiment_value' columns")
|
| 25 |
+
|
| 26 |
+
df['cleaned_review'] = df['review'].apply(preprocess_text)
|
| 27 |
+
vectorizer = TfidfVectorizer(max_features=1000)
|
| 28 |
+
X = vectorizer.fit_transform(df['cleaned_review'])
|
| 29 |
+
y = df['sentiment_value']
|
| 30 |
+
model = LogisticRegression(max_iter=1000)
|
| 31 |
+
model.fit(X, y)
|
| 32 |
+
|
| 33 |
+
def predict_sentiment(text):
|
| 34 |
+
try:
|
| 35 |
+
cleaned_text = preprocess_text(text)
|
| 36 |
+
text_vector = vectorizer.transform([cleaned_text])
|
| 37 |
+
prediction = model.predict(text_vector)[0]
|
| 38 |
+
probabilities = model.predict_proba(text_vector)[0]
|
| 39 |
+
confidence = probabilities.max()
|
| 40 |
+
sentiment_label = 'Positive' if prediction == 1 else 'Negative' if prediction == -1 else 'Neutral'
|
| 41 |
+
return f"{sentiment_label} (confidence: {confidence*100:.1f}%)"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error processing your input: {str(e)}"
|
| 44 |
+
|
| 45 |
+
description = """
|
| 46 |
+
<div style="display: flex; align-items: center; gap: 24px; background: #fff; padding: 16px 32px; border-radius: 14px; box-shadow: 0 2px 12px rgba(0,0,0,0.08); width: fit-content;">
|
| 47 |
+
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/c/c5/Vellore_Institute_of_Technology_seal_2017.svg/300px-Vellore_Institute_of_Technology_seal_2017.svg.png" alt="VIT Logo" style="height: 48px; width: auto; border-radius: 8px; background: #f5f7fa; padding: 6px; box-shadow: 0 1px 6px rgba(0,0,0,0.05); transition: transform 0.2s;" onmouseover="this.style.transform='scale(1.08)'" onmouseout="this.style.transform='scale(1)'"/>
|
| 48 |
+
<img src="https://www.ibm.com/brand/experience-guides/developer/8f4e3cc2b5d52354a6d43c8edba1e3c9/02_8-bar-reverse.svg" alt="IBM Logo" style="height: 48px; width: auto; border-radius: 8px; background: #f5f7fa; padding: 6px; box-shadow: 0 1px 6px rgba(0,0,0,0.05); transition: transform 0.2s;" onmouseover="this.style.transform='scale(1.08)'" onmouseout="this.style.transform='scale(1)'"/>
|
| 49 |
+
</div>
|
| 50 |
+
<br>
|
| 51 |
+
<b style="font-size: 1.3rem; letter-spacing: 1px; color: #222; font-family: 'Segoe UI', Arial, sans-serif;">
|
| 52 |
+
Car Rental Feedback Analyzer
|
| 53 |
+
</b>
|
| 54 |
+
<p>Enter your car rental review below. The app will predict the sentiment (Positive, Neutral, Negative).</p>
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
iface = gr.Interface(
|
| 58 |
+
fn=predict_sentiment,
|
| 59 |
+
inputs=gr.Textbox(label="Enter car rental review", placeholder="Type your car rental experience here..."),
|
| 60 |
+
outputs=gr.Textbox(label="Sentiment Prediction"),
|
| 61 |
+
title="🚗 Car Rental Feedback Analyzer",
|
| 62 |
+
description=description,
|
| 63 |
+
examples=[
|
| 64 |
+
["The car was clean and the staff was friendly"],
|
| 65 |
+
["Terrible experience with late delivery"],
|
| 66 |
+
["Average service, nothing special"]
|
| 67 |
+
]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Test locally in Colab first
|
| 71 |
+
iface.launch()
|