Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +22 -0
- requirements.txt +5 -0
- sentiment_model.joblib +3 -0
- trianed.py +39 -0
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
|
| 4 |
+
# Load model & vectorizer
|
| 5 |
+
model = joblib.load("model.joblib")
|
| 6 |
+
tfidf = joblib.load("tfidf_vectorizer.joblib")
|
| 7 |
+
|
| 8 |
+
def predict_sentiment(text):
|
| 9 |
+
vector = tfidf.transform([text])
|
| 10 |
+
prediction = model.predict(vector)[0]
|
| 11 |
+
label = "✅ Positive" if prediction == 1 else "❌ Negative"
|
| 12 |
+
return label
|
| 13 |
+
|
| 14 |
+
iface = gr.Interface(
|
| 15 |
+
fn=predict_sentiment,
|
| 16 |
+
inputs=gr.Textbox(label="Enter a Review"),
|
| 17 |
+
outputs=gr.Textbox(label="Sentiment"),
|
| 18 |
+
title="XGBoost Sentiment Classifier",
|
| 19 |
+
description="Real-time sentiment analysis using TF-IDF + XGBoost."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
scikit-learn
|
| 3 |
+
xgboost
|
| 4 |
+
joblib
|
| 5 |
+
pandas
|
sentiment_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd28e82d3707dcc3f543c3a35541dad91132f9e7ff9049fc55781adbfc018088
|
| 3 |
+
size 40
|
trianed.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
+
from xgboost import XGBClassifier
|
| 5 |
+
from sklearn.metrics import accuracy_score
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
# Load dataset
|
| 9 |
+
df = pd.read_csv("dummy_sentiment_dataset.csv")
|
| 10 |
+
|
| 11 |
+
# Split
|
| 12 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 13 |
+
df["text"], df["label"], test_size=0.2, random_state=42
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# TF-IDF
|
| 17 |
+
tfidf = TfidfVectorizer(max_features=5000)
|
| 18 |
+
X_train_tfidf = tfidf.fit_transform(X_train)
|
| 19 |
+
X_test_tfidf = tfidf.transform(X_test)
|
| 20 |
+
|
| 21 |
+
# Model
|
| 22 |
+
model = XGBClassifier(
|
| 23 |
+
n_estimators=300,
|
| 24 |
+
max_depth=6,
|
| 25 |
+
learning_rate=0.1,
|
| 26 |
+
eval_metric='logloss'
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
model.fit(X_train_tfidf, y_train)
|
| 30 |
+
|
| 31 |
+
# Evaluate
|
| 32 |
+
y_pred = model.predict(X_test_tfidf)
|
| 33 |
+
print("Accuracy:", accuracy_score(y_test, y_pred))
|
| 34 |
+
|
| 35 |
+
# Save model + vectorizer
|
| 36 |
+
joblib.dump(model, "model.joblib")
|
| 37 |
+
joblib.dump(tfidf, "tfidf_vectorizer.joblib")
|
| 38 |
+
|
| 39 |
+
print("✅ Model and vectorizer saved!")
|