Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,21 @@
|
|
| 1 |
-
|
| 2 |
-
from joblib import load
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
svm_model = load_svm_model("ankitdotpy/SVM_model_by_Group12")
|
| 10 |
|
| 11 |
-
# Define
|
| 12 |
def predict_sentiment(text):
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# Vectorize the input text (assuming you already have the vectorizer)
|
| 17 |
-
text_vector = vectorizer.transform([text])
|
| 18 |
-
|
| 19 |
-
# Predict sentiment
|
| 20 |
-
sentiment = clf_svm.predict(text_vector)[0]
|
| 21 |
-
|
| 22 |
-
# Get probabilities for each class
|
| 23 |
-
probabilities = clf_svm.predict_proba(text_vector)[0]
|
| 24 |
-
|
| 25 |
-
# Convert probabilities to percentages
|
| 26 |
-
percentages = [round(prob * 100, 2) for prob in probabilities]
|
| 27 |
-
|
| 28 |
-
# Choose the sentiment label based on the predicted class
|
| 29 |
-
if sentiment == "POSITIVE":
|
| 30 |
-
return f"Positive ({percentages[1]}%)"
|
| 31 |
-
elif sentiment == "NEUTRAL":
|
| 32 |
-
return f"Neutral ({percentages[2]}%)"
|
| 33 |
-
else:
|
| 34 |
-
return f"Negative ({percentages[0]}%)"
|
| 35 |
|
| 36 |
# Create Gradio interface
|
| 37 |
iface = gr.Interface(
|
|
|
|
| 1 |
+
from transformers import pipeline
|
|
|
|
| 2 |
|
| 3 |
+
def load_svm_model(model_name):
|
| 4 |
+
# Load the trained SVM model from Hugging Face
|
| 5 |
+
model = pipeline(task="text-classification", model=model_name)
|
| 6 |
+
return model
|
| 7 |
|
| 8 |
+
# Load SVM model from Hugging Face
|
| 9 |
svm_model = load_svm_model("ankitdotpy/SVM_model_by_Group12")
|
| 10 |
|
| 11 |
+
# Define prediction function
|
| 12 |
def predict_sentiment(text):
|
| 13 |
+
# Predict sentiment using the imported model
|
| 14 |
+
result = svm_model(text)
|
| 15 |
+
sentiment_label = result[0]['label']
|
| 16 |
+
confidence = result[0]['score'] * 100
|
| 17 |
+
return f"{sentiment_label} ({confidence:.2f}%)"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Create Gradio interface
|
| 21 |
iface = gr.Interface(
|