Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
| 4 |
import re
|
|
@@ -6,13 +5,15 @@ import nltk
|
|
| 6 |
from nltk.corpus import stopwords
|
| 7 |
from nltk.tokenize import word_tokenize
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model = joblib.load("sentiment_model.pkl")
|
| 11 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
nltk.download('stopwords')
|
| 15 |
-
|
| 16 |
def preprocess(text):
|
| 17 |
text = str(text).lower()
|
| 18 |
text = re.sub(r'[^a-z\s]', '', text)
|
|
@@ -20,12 +21,17 @@ def preprocess(text):
|
|
| 20 |
tokens = [t for t in tokens if t not in stopwords.words('english')]
|
| 21 |
return ' '.join(tokens)
|
| 22 |
|
|
|
|
| 23 |
def predict_sentiment(text):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=predict_sentiment,
|
| 31 |
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
|
@@ -34,4 +40,4 @@ demo = gr.Interface(
|
|
| 34 |
description="Classify text as Positive, Negative, or Neutral"
|
| 35 |
)
|
| 36 |
|
| 37 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
import re
|
|
|
|
| 5 |
from nltk.corpus import stopwords
|
| 6 |
from nltk.tokenize import word_tokenize
|
| 7 |
|
| 8 |
+
# Ensure NLTK resources are downloaded
|
| 9 |
+
nltk.download('punkt', quiet=True)
|
| 10 |
+
nltk.download('stopwords', quiet=True)
|
| 11 |
+
|
| 12 |
+
# Load model and vectorizer
|
| 13 |
model = joblib.load("sentiment_model.pkl")
|
| 14 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 15 |
|
| 16 |
+
# Preprocessing function
|
|
|
|
|
|
|
| 17 |
def preprocess(text):
|
| 18 |
text = str(text).lower()
|
| 19 |
text = re.sub(r'[^a-z\s]', '', text)
|
|
|
|
| 21 |
tokens = [t for t in tokens if t not in stopwords.words('english')]
|
| 22 |
return ' '.join(tokens)
|
| 23 |
|
| 24 |
+
# Prediction function
|
| 25 |
def predict_sentiment(text):
|
| 26 |
+
try:
|
| 27 |
+
processed = preprocess(text)
|
| 28 |
+
vectorized = vectorizer.transform([processed])
|
| 29 |
+
prediction = model.predict(vectorized)[0]
|
| 30 |
+
return prediction
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error: {str(e)}"
|
| 33 |
|
| 34 |
+
# Gradio interface
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=predict_sentiment,
|
| 37 |
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
|
|
|
| 40 |
description="Classify text as Positive, Negative, or Neutral"
|
| 41 |
)
|
| 42 |
|
| 43 |
+
demo.launch()
|