karthik55 commited on
Commit
54a9c76
·
verified ·
1 Parent(s): 0179634
app.py CHANGED
@@ -1,62 +1,71 @@
1
  import gradio as gr
2
  import joblib
3
 
4
- # Load models
 
 
 
 
 
 
 
5
  models = {
6
- "Logistic Regression": joblib.load("models/best_model.joblib"),
7
- "Random Forest": joblib.load("models/random_forest_model.joblib"),
8
- "KNN": joblib.load("models/trained_knn_model.joblib"),
 
9
  }
10
 
11
- # Load vectorizer
12
- vectorizer = joblib.load("models/vectorizer.joblib")
13
-
14
- # Define prediction function
15
  def predict_sentiment(review, model_name):
16
- # Transform the review text using the vectorizer
17
- processed_review = vectorizer.transform([review])
18
-
19
- # Select the model
20
- model = models[model_name]
21
-
22
- # Make predictions
23
- predicted_class = model.predict(processed_review)[0]
24
- probabilities = model.predict_proba(processed_review)[0]
25
-
26
- # Define sentiment labels
27
- sentiment_labels = ["Negative Comment", "Positive Comment"]
28
- predicted_label = sentiment_labels[predicted_class]
29
-
30
- # Return probabilities as percentages
31
- positive_percentage = probabilities[1] * 100
32
- negative_percentage = probabilities[0] * 100
33
-
34
- return predicted_label, positive_percentage, negative_percentage
35
-
36
- # Build Gradio interface
37
- with gr.Blocks() as interface:
38
- gr.Markdown("<h1>Text Classification Models</h1>")
39
- gr.Markdown("Choose a model and provide a review to see the sentiment analysis results with probabilities displayed as scales.")
40
-
41
- with gr.Row():
42
- with gr.Column():
43
- review_input = gr.Textbox(label="Review Comment", placeholder="Type your comment here...")
44
- model_selector = gr.Dropdown(
45
- choices=list(models.keys()), label="Select Model", value="Logistic Regression"
46
- )
47
- submit_button = gr.Button("Submit")
48
-
49
- with gr.Column():
50
- sentiment_output = gr.Textbox(label="Predicted Sentiment Class", interactive=False)
51
- positive_progress = gr.Slider(label="Positive Comment Percentage", minimum=0, maximum=100, interactive=False)
52
- negative_progress = gr.Slider(label="Negative Comment Percentage", minimum=0, maximum=100, interactive=False)
53
-
54
- submit_button.click(
55
- predict_sentiment,
56
- inputs=[review_input, model_selector],
57
- outputs=[sentiment_output, positive_progress, negative_progress],
58
- )
59
-
60
- # Launch the app
61
- if __name__ == "__main__":
62
- interface.launch()
 
 
 
 
 
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