Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
-
# Load models (
|
| 5 |
logreg_model = joblib.load('best_lr_model.pkl')
|
| 6 |
knn_model = joblib.load('best_knn_model.pkl')
|
| 7 |
svc_model = joblib.load('best_svc_model.pkl')
|
|
@@ -9,6 +9,10 @@ rf_model = joblib.load('best_rf_model.pkl')
|
|
| 9 |
|
| 10 |
# Prediction function
|
| 11 |
def predict_sentiment(review_text, model_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if model_name == "Logistic Regression":
|
| 13 |
model = logreg_model
|
| 14 |
elif model_name == "K-Nearest Neighbors":
|
|
@@ -17,16 +21,22 @@ def predict_sentiment(review_text, model_name):
|
|
| 17 |
model = svc_model
|
| 18 |
elif model_name == "Random Forest":
|
| 19 |
model = rf_model
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Assuming the models support `predict_proba` for probabilities
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Gradio interface setup
|
| 32 |
def create_interface():
|
|
@@ -55,4 +65,4 @@ def create_interface():
|
|
| 55 |
return interface
|
| 56 |
|
| 57 |
# Launch the interface
|
| 58 |
-
create_interface().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
+
# Load models (Ensure the correct paths to where your models are stored)
|
| 5 |
logreg_model = joblib.load('best_lr_model.pkl')
|
| 6 |
knn_model = joblib.load('best_knn_model.pkl')
|
| 7 |
svc_model = joblib.load('best_svc_model.pkl')
|
|
|
|
| 9 |
|
| 10 |
# Prediction function
|
| 11 |
def predict_sentiment(review_text, model_name):
|
| 12 |
+
# Reshape input to match expected 2D array format (single sample, single feature)
|
| 13 |
+
review_text_reshaped = [review_text] # This ensures the input is in a 2D array (1 sample, N features)
|
| 14 |
+
|
| 15 |
+
# Select the appropriate model
|
| 16 |
if model_name == "Logistic Regression":
|
| 17 |
model = logreg_model
|
| 18 |
elif model_name == "K-Nearest Neighbors":
|
|
|
|
| 21 |
model = svc_model
|
| 22 |
elif model_name == "Random Forest":
|
| 23 |
model = rf_model
|
| 24 |
+
else:
|
| 25 |
+
return "Model not found", {}
|
| 26 |
|
| 27 |
# Assuming the models support `predict_proba` for probabilities
|
| 28 |
+
try:
|
| 29 |
+
prob = model.predict_proba(review_text_reshaped) # Adjust according to your preprocessing steps
|
| 30 |
+
positive_prob = prob[0][1]
|
| 31 |
+
negative_prob = prob[0][0]
|
| 32 |
+
|
| 33 |
+
# Predict the sentiment class (based on the highest probability)
|
| 34 |
+
predicted_class = "Positive Feedback" if positive_prob > negative_prob else "Negative Feedback"
|
| 35 |
+
|
| 36 |
+
return predicted_class, {"Positive Comment": f"{positive_prob * 100:.2f}%", "Negative Comment": f"{negative_prob * 100:.2f}%"}
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Error: {e}", {}
|
| 40 |
|
| 41 |
# Gradio interface setup
|
| 42 |
def create_interface():
|
|
|
|
| 65 |
return interface
|
| 66 |
|
| 67 |
# Launch the interface
|
| 68 |
+
create_interface().launch(share=True) # Add share=True to create a public link
|