Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
-
model
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
def predict_sentiment(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
vector = tfidf.transform([text])
|
|
|
|
|
|
|
| 9 |
prediction = model.predict(vector)[0]
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
fn=predict_sentiment,
|
| 14 |
-
inputs="text",
|
| 15 |
-
outputs="text"
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
+
# Load model and vectorizer
|
| 5 |
+
model = joblib.load("sentiment_model.pkl")
|
| 6 |
+
tfidf = joblib.load("tfidf_vectorizer.pkl")
|
| 7 |
|
| 8 |
def predict_sentiment(text):
|
| 9 |
+
if not text.strip():
|
| 10 |
+
return "Please enter some text."
|
| 11 |
+
|
| 12 |
+
# Transform the text using the same TF-IDF vectorizer
|
| 13 |
vector = tfidf.transform([text])
|
| 14 |
+
|
| 15 |
+
# Predict sentiment
|
| 16 |
prediction = model.predict(vector)[0]
|
| 17 |
+
|
| 18 |
+
# Optional: make prediction readable
|
| 19 |
+
if prediction == 1:
|
| 20 |
+
label = "π Positive"
|
| 21 |
+
elif prediction == 0:
|
| 22 |
+
label = "π Neutral"
|
| 23 |
+
else:
|
| 24 |
+
label = "π Negative"
|
| 25 |
+
|
| 26 |
+
return label
|
| 27 |
|
| 28 |
+
# Gradio Interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
fn=predict_sentiment,
|
| 31 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Sentiment Classifier",
|
| 34 |
+
description="Predicts whether a sentence is positive, neutral, or negative using an XGBoost model."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
iface.launch()
|