samir1120 commited on
Commit
c28fc08
·
verified ·
1 Parent(s): d92a05d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -1,33 +1,31 @@
1
  import gradio as gr
2
  import pickle
3
- import numpy as np
4
- import pandas as pd
5
 
6
-
7
-
8
-
9
- # Specify the filename for your PKL file
10
  filename = 'sentiment_model.pkl'
11
 
12
- # Open the file in write binary ('wb') mode and save the model using pickle.dump()
13
  with open(filename, 'rb') as file:
14
- nb_classifier=pickle.load(file)
 
 
15
 
 
16
  def predict_sentiment(text_input):
17
- text_vector = vectorizer.transform([text])
18
- prediction = nb_classifier.predict(text_vector)
19
- return "Positive" if prediction == 1 else "Negative"
20
-
21
- # Test sample
22
-
 
 
23
  with gr.Blocks(theme="compact") as demo:
24
- gr.Markdown("predict_sentiment")
25
- #chatbot_window = gr.Chatbot(label="")
26
  with gr.Row():
27
- # Corrected 'lable' to 'label'
28
- text_input = gr.Textbox(label="Write the Review", placeholder="Enter your sentiment", show_label=False)
29
- # Create a Textbox to display the output
30
  output_box = gr.Textbox(label="Sentiment Prediction")
31
- # Submit the input to the function and display the output in the output_box
32
- text_input.submit(fn=predict_sentiment, inputs=text_input, outputs=output_box)
33
- demo.launch(share=True)
 
 
1
  import gradio as gr
2
  import pickle
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
 
4
 
5
+ # Load the model and vectorizer from the pickle file
 
 
 
6
  filename = 'sentiment_model.pkl'
7
 
 
8
  with open(filename, 'rb') as file:
9
+ loaded_objects = pickle.load(file)
10
+ nb_classifier = loaded_objects['model'] # Trained model
11
+ vectorizer = loaded_objects['vectorizer'] # Pre-trained vectorizer
12
 
13
+ # Define the prediction function
14
  def predict_sentiment(text_input):
15
+ try:
16
+ text_vector = vectorizer.transform([text_input]) # Transform input text
17
+ prediction = nb_classifier.predict(text_vector) # Predict sentiment
18
+ return "Positive" if prediction[0] == 1 else "Negative"
19
+ except Exception as e:
20
+ return f"Error: {e}"
21
+
22
+ # Create the Gradio interface
23
  with gr.Blocks(theme="compact") as demo:
24
+ gr.Markdown("## Sentiment Analysis Predictor")
 
25
  with gr.Row():
26
+ text_input = gr.Textbox(label="Write the Review", placeholder="Enter your sentiment")
 
 
27
  output_box = gr.Textbox(label="Sentiment Prediction")
28
+ text_input.submit(fn=predict_sentiment, inputs=text_input, outputs=output_box)
29
+
30
+ # Launch the Gradio app
31
+ demo.launch(share=True)