Nadun102 commited on
Commit
e7f80c6
Β·
verified Β·
1 Parent(s): 676b2bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -1,16 +1,37 @@
1
  import gradio as gr
2
  import joblib
3
 
4
- model = joblib.load("sentiment_model.joblib")
5
- tfidf = joblib.load("tfidf_vectorizer.joblib")
 
6
 
7
  def predict_sentiment(text):
 
 
 
 
8
  vector = tfidf.transform([text])
 
 
9
  prediction = model.predict(vector)[0]
10
- return "Positive βœ…" if prediction == 1 else "Negative ❌"
 
 
 
 
 
 
 
 
 
11
 
12
- gr.Interface(
 
13
  fn=predict_sentiment,
14
- inputs="text",
15
- outputs="text"
16
- ).launch()
 
 
 
 
 
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()