karthik55 commited on
Commit
f6d73cd
1 Parent(s): 3cdcc1e
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+
4
+ # Load models and vectorizer from the models folder
5
+ logistic_model = joblib.load("models/best_logistic_model.pkl")
6
+ svm_model = joblib.load("models/best_svc_model.pkl")
7
+ random_forest_model = joblib.load("models/best_rf_model.pkl")
8
+ knn_model = joblib.load("models/best_knn_model.pkl")
9
+ vectorizer = joblib.load("models/vectorizer.pkl")
10
+
11
+ # Model selection mapping
12
+ models = {
13
+ "Logistic Regression": logistic_model,
14
+ "SVM": svm_model,
15
+ "Random Forest": random_forest_model,
16
+ "KNN": knn_model,
17
+ }
18
+
19
+ # Prediction function
20
+ def predict_sentiment(review, model_name):
21
+ try:
22
+ if not review.strip():
23
+ return "Error: Review cannot be empty", None
24
+
25
+ if model_name not in models:
26
+ return "Error: Invalid model selected", None
27
+
28
+ # Preprocess the text
29
+ text_vector = vectorizer.transform([review])
30
+
31
+ # Predict using the selected model
32
+ model = models[model_name]
33
+ prediction = model.predict(text_vector)[0]
34
+ probabilities = model.predict_proba(text_vector)[0] if hasattr(model, "predict_proba") else None
35
+
36
+ # Format the output
37
+ sentiment = "Positive Feedback" if prediction == 1 else "Negative Feedback"
38
+ probabilities_output = (
39
+ {
40
+ "Positive": probabilities[1], # Raw probability (0.0 - 1.0)
41
+ "Negative": probabilities[0], # Raw probability (0.0 - 1.0)
42
+ }
43
+ if probabilities is not None
44
+ else "Probabilities not available"
45
+ )
46
+
47
+ return sentiment, probabilities_output
48
+ except Exception as e:
49
+ # Log the error to the console for debugging
50
+ print(f"Error in prediction: {e}")
51
+ return f"Error: {str(e)}", None
52
+
53
+ # Create Gradio Interface
54
+ inputs = [
55
+ gr.Textbox(label="Review Comment", placeholder="Enter your review here..."),
56
+ gr.Dropdown(choices=["Logistic Regression", "SVM", "Random Forest", "KNN"], label="Model"),
57
+ ]
58
+
59
+ outputs = [
60
+ gr.Textbox(label="Predicted Sentiment Class"),
61
+ gr.Label(label="Predicted Probability"),
62
+ ]
63
+
64
+ # Launch Gradio App
65
+ gr.Interface(
66
+ fn=predict_sentiment,
67
+ inputs=inputs,
68
+ outputs=outputs,
69
+ title="Sentiment Analysis",
70
+ description="Enter a review and select a model to predict sentiment.",
71
+ ).launch()
models/best_knn_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ae5bfda964fad7a1d30fe5cdaaa8e9991670fa81ec5644033b3e3e1d08674e3
3
+ size 105844
models/best_logistic_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:62b2ecaa84982c38c3df38e113605ced250896abf1cb6c762c411a88a6cdeaa1
3
+ size 15215
models/best_rf_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a4d4944dc56c304bda9e45b9b96496e1af61a137ddcae6cc518cc40591514a24
3
+ size 1025289
models/best_svc_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b2acf2da04be8cb990b9d2fc5573564acdddb7e4b6dfba9c4ff9863193b852f
3
+ size 89531
models/vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4df3665f41c3f8e87d1945f3625acb00d8812f7953e10d20f37e6f99d2cf45cd
3
+ size 21760
requirements.txt ADDED
Binary file (62 Bytes). View file