Munazz commited on
Commit
d372470
·
verified ·
1 Parent(s): 60ace42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -3,7 +3,7 @@ import joblib
3
  import numpy as np
4
  from sklearn.feature_extraction.text import CountVectorizer
5
 
6
- # Load models (ensure these models are pre-trained and saved in .pkl files)
7
  logreg_model = joblib.load('best_lr_model.pkl')
8
  knn_model = joblib.load('best_knn_model.pkl')
9
  svc_model = joblib.load('best_svc_model.pkl') # Ensure this model has 'probability=True' if needed
@@ -15,7 +15,7 @@ vectorizer = joblib.load('vectorizer.pkl')
15
  # Define the function to predict sentiment
16
  def predict_sentiment(review_text, model_name):
17
  # Preprocess the review text using the same vectorizer used during training
18
- transformed_text = vectorizer.transform([review_text]) # Transform the input review text
19
 
20
  # Convert sparse matrix to dense (necessary for SVM)
21
  transformed_text_dense = transformed_text.toarray() # Convert sparse matrix to dense array
@@ -46,24 +46,19 @@ def predict_sentiment(review_text, model_name):
46
 
47
  # Step 6.4: Put all components together in Gradio's interface
48
  with gr.Blocks() as demo:
49
- with gr.Row():
50
- # Input components on the left
51
- with gr.Column():
52
- review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...", elem_id="review-text")
53
- model_dropdown = gr.Dropdown(choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"], label="Select Model", elem_id="model-dropdown")
54
- submit_button = gr.Button("Submit", elem_id="submit-btn")
55
- clear_button = gr.Button("Clear", elem_id="clear-btn")
56
-
57
- # Output components on the right
58
- with gr.Column():
59
- output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...", elem_id="sentiment-output")
60
- output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...", elem_id="probability-output")
61
-
62
  # Define the action when the button is clicked
63
  submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
64
 
65
- # Reset inputs when clear button is clicked
66
- clear_button.click(fn=lambda: ("", "", ""), inputs=[], outputs=[review_input, output_sentiment, output_probability])
67
-
68
  # Launch the interface
69
  demo.launch()
 
3
  import numpy as np
4
  from sklearn.feature_extraction.text import CountVectorizer
5
 
6
+ # Load models (change paths to where your .pkl files are stored)
7
  logreg_model = joblib.load('best_lr_model.pkl')
8
  knn_model = joblib.load('best_knn_model.pkl')
9
  svc_model = joblib.load('best_svc_model.pkl') # Ensure this model has 'probability=True' if needed
 
15
  # Define the function to predict sentiment
16
  def predict_sentiment(review_text, model_name):
17
  # Preprocess the review text using the same vectorizer used during training
18
+ transformed_text = vectorizer.transform([review_text]) # This is sparse matrix
19
 
20
  # Convert sparse matrix to dense (necessary for SVM)
21
  transformed_text_dense = transformed_text.toarray() # Convert sparse matrix to dense array
 
46
 
47
  # Step 6.4: Put all components together in Gradio's interface
48
  with gr.Blocks() as demo:
49
+ # Input components
50
+ review_input = gr.Textbox(label="Review Comment", lines=2, placeholder="Enter your review comment here...")
51
+ model_dropdown = gr.Dropdown(choices=["Logistic Regression", "K-Nearest Neighbors", "Random Forest", "SVM"], label="Select Model")
52
+
53
+ # Output components
54
+ output_sentiment = gr.Textbox(label="Predicted Sentiment Class", placeholder="Predicted sentiment will appear here...")
55
+ output_probability = gr.Textbox(label="Predicted Probability", placeholder="Probabilities will appear here...")
56
+
57
+ # Submit Button
58
+ submit_button = gr.Button("Submit")
59
+
 
 
60
  # Define the action when the button is clicked
61
  submit_button.click(fn=predict_sentiment, inputs=[review_input, model_dropdown], outputs=[output_sentiment, output_probability])
62
 
 
 
 
63
  # Launch the interface
64
  demo.launch()