Spaces:
Sleeping
Sleeping
Upload 4 files
Browse filesFiles changed (4)
- app.pyAdded+22
- requirements.txtAdded+5
- sentiment_model.joblibAdded+3
- trianed.pyAdded+39
12345678910111213141516171819202122import gradio as gr
import joblib
# Load model & vectorizer
model = joblib.load("model.joblib")
tfidf = joblib.load("tfidf_vectorizer.joblib")
def predict_sentiment(text):
vector = tfidf.transform([text])
prediction = model.predict(vector)[0]
label = "✅ Positive" if prediction == 1 else "❌ Negative"
return label
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(label="Enter a Review"),
outputs=gr.Textbox(label="Sentiment"),
title="XGBoost Sentiment Classifier",
description="Real-time sentiment analysis using TF-IDF + XGBoost."
)
iface.launch()12345gradio
scikit-learn
xgboost
joblib
pandas123version https://git-lfs.github.com/spec/v1oid sha256:dd28e82d3707dcc3f543c3a35541dad91132f9e7ff9049fc55781adbfc018088size 40123456789101112131415161718192021222324252627282930313233343536373839import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import joblib
# Load dataset
df = pd.read_csv("dummy_sentiment_dataset.csv")
# Split
X_train, X_test, y_train, y_test = train_test_split(
df["text"], df["label"], test_size=0.2, random_state=42
)
# TF-IDF
tfidf = TfidfVectorizer(max_features=5000)
X_train_tfidf = tfidf.fit_transform(X_train)
X_test_tfidf = tfidf.transform(X_test)
# Model
model = XGBClassifier(
n_estimators=300,
max_depth=6,
learning_rate=0.1,
eval_metric='logloss'
)
model.fit(X_train_tfidf, y_train)
# Evaluate
y_pred = model.predict(X_test_tfidf)
print("Accuracy:", accuracy_score(y_test, y_pred))
# Save model + vectorizer
joblib.dump(model, "model.joblib")
joblib.dump(tfidf, "tfidf_vectorizer.joblib")
print("✅ Model and vectorizer saved!")app.pyAdded+22
requirements.txtAdded+5
sentiment_model.joblibAdded+3
trianed.pyAdded+39